public void Send_NoServiceNameWithLogging_ShouldThrowApplicationException() { const string hostAddress = "tcp://localhost:5555"; var loggingMessages = new List <string>(); // setup the counter socket for communication using (var session = new MDPClient(hostAddress)) { // set the event handler to receive the logging messages session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info); // well formed message var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") }); // correct call try { session.Send(string.Empty, requestMessage); } catch (ApplicationException ex) { Assert.That(ex.Message, Is.EqualTo("serviceName must not be empty or null.")); } Assert.That(loggingMessages.Count, Is.EqualTo(0)); } }
public void Send_WrongServiceNameWithLogging_ShouldLogPermanentError() { const string hostAddress = "tcp://localhost:5555"; var loggingMessages = new List <string>(); // setup the counter socket for communication using (var broker = new RouterSocket()) using (var poller = new NetMQPoller()) using (var session = new MDPClient(hostAddress)) { broker.Bind(hostAddress); // we need to pick up any message in order to avoid errors broker.ReceiveReady += (s, e) => { // just swallow message -> wrong service name e.Socket.ReceiveMultipartMessage(); }; poller.Add(broker); poller.RunAsync(); // set the event handler to receive the logging messages session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info); // well formed message var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") }); // wrong service name session.Send("xyz", requestMessage); poller.Stop(); Assert.That(loggingMessages.Count, Is.EqualTo(7)); Assert.That(loggingMessages[6], Is.EqualTo("[CLIENT ERROR] permanent error, abandoning!")); } }
public void Send_WrongHeaderFromBrokerNoLogging_ShouldThrowApplicationException() { const string hostAddress = "tcp://localhost:5555"; // setup the counter socket for communication using (var context = NetMQContext.Create()) using (var broker = context.CreateRouterSocket()) using (var poller = new Poller()) using (var session = new MDPClient(hostAddress)) { broker.Bind(hostAddress); // we need to pick up any message in order to avoid errors broker.ReceiveReady += (s, e) => { // return empty reply var msg = e.Socket.ReceiveMultipartMessage(); // we expect to receive a 4 Frame message // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"] if (msg.FrameCount != 5) { Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount); } // REQUEST socket will strip the his address + empty frame // ROUTER has to add the address prelude in order to identify the correct socket(!) // [REQ ADR][EMPTY]["MDPC00"]["echo"]["REQUEST"] var clientAddress = msg.Pop(); msg.Pop(); // forget empty frame var mdpVersion = msg.Pop(); msg.Pop(); // drop service name version msg.Push("NoService"); msg.Push(mdpVersion); msg.Push(NetMQFrame.Empty); msg.Push(clientAddress); // reinsert the client's address e.Socket.SendMessage(msg); }; poller.AddSocket(broker); Task.Factory.StartNew(poller.PollTillCancelled); // well formed message var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") }); try { session.Send("echo", requestMessage); } catch (ApplicationException ex) { Assert.That(ex.Message, Is.EqualTo("[CLIENT INFO] answered by wrong service: NoService")); } poller.CancelAndJoin(); poller.RemoveSocket(broker); } }
public void Send_CorrectInputWithLogging_ShouldReturnCorrectReply() { const string hostAddress = "tcp://localhost:5555"; var loggingMessages = new List <string>(); // setup the counter socket for communication using (var context = NetMQContext.Create()) using (var broker = context.CreateRouterSocket()) using (var poller = new Poller()) using (var session = new MDPClient(hostAddress)) { broker.Bind(hostAddress); // we need to pick up any message in order to avoid errors broker.ReceiveReady += (s, e) => { var msg = e.Socket.ReceiveMultipartMessage(); // we expect to receive a 4 Frame message // [client adrR][e][mdp header][service][request] if (msg.FrameCount != 5) { Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount); } // REQUEST socket will strip the his address + empty frame // ROUTER has to add the address prelude in order to identify the correct socket(!) // [client adr][e][mdp header][service][reply] var request = msg.Last.ConvertToString(); // get the request string msg.RemoveFrame(msg.Last); // remove the request frame msg.Append(new NetMQFrame(request + " OK")); // append the reply frame e.Socket.SendMessage(msg); }; poller.AddSocket(broker); Task.Factory.StartNew(poller.PollTillCancelled); // set the event handler to receive the logging messages session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info); // well formed message var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") }); // correct call var reply = session.Send("echo", requestMessage); poller.CancelAndJoin(); poller.RemoveSocket(broker); Assert.That(reply.FrameCount, Is.EqualTo(1)); Assert.That(reply.First.ConvertToString(), Is.EqualTo("REQUEST OK")); Assert.That(loggingMessages.Count, Is.EqualTo(3)); Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555")); Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True); Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True); } }
public void Send_WrongMDPVersionFromBrokerNoLogging_ShouldThrowApplicationException() { const string hostAddress = "tcp://localhost:5555"; // setup the counter socket for communication using (var broker = new RouterSocket()) using (var poller = new NetMQPoller()) using (var session = new MDPClient(hostAddress)) { broker.Bind(hostAddress); // we need to pick up any message in order to avoid errors broker.ReceiveReady += (s, e) => { // return empty reply var msg = e.Socket.ReceiveMultipartMessage(); // we expect to receive a 4 Frame message // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"] if (msg.FrameCount != 5) { Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount); } // REQUEST socket will strip the his address + empty frame // ROUTER has to add the address prelude in order to identify the correct socket(!) // [REQ ADR][EMPTY]["MDPC00"]["echo"]["REQUEST"] var clientAddress = msg.Pop(); msg.Pop(); // forget empty frame msg.Pop(); // drop the MDP Version Frame msg.Push("MDPC00"); // insert wrong MDP version msg.Push(NetMQFrame.Empty); msg.Push(clientAddress); // reinsert the client's address e.Socket.SendMultipartMessage(msg); }; poller.Add(broker); poller.RunAsync(); // well formed message var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") }); try { session.Send("echo", requestMessage); } catch (ApplicationException ex) { Assert.That(ex.Message, Is.StringContaining("MDP Version mismatch")); } } }
public void Send_EmptyReplyFromBrokerWithLogging_ShouldThrowApplicationException() { const string hostAddress = "tcp://localhost:5555"; var loggingMessages = new List <string>(); // setup the counter socket for communication using (var context = NetMQContext.Create()) using (var broker = context.CreateRouterSocket()) using (var poller = new Poller()) using (var session = new MDPClient(hostAddress)) { broker.Bind(hostAddress); // we need to pick up any message in order to avoid errors broker.ReceiveReady += (s, e) => { // return empty reply var msg = e.Socket.ReceiveMultipartMessage(); // we expect to receive a 4 Frame message // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"] if (msg.FrameCount != 5) { Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount); } // REQUEST socket will strip the his address + empty frame // ROUTER has to add the address prelude in order to identify the correct socket(!) // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"] e.Socket.SendMessage(msg); }; poller.AddSocket(broker); Task.Factory.StartNew(poller.PollTillCancelled); // set the event handler to receive the logging messages session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info); // well formed message var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") }); // correct call session.Send("echo", requestMessage); poller.CancelAndJoin(); poller.RemoveSocket(broker); Assert.That(loggingMessages.Count, Is.EqualTo(3)); Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555")); Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True); Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True); } }
/// <summary> /// usage: MDPServiceDiscoveryClientExample [-v] /// /// implements a MDPClient API usage with Service Discovery /// </summary> static void Main(string[] args) { const string service_to_lookup = "echo"; const string service_discovery = "mmi.service"; var verbose = args.Length == 1 && args[0] == "-v"; var id = Encoding.ASCII.GetBytes("SDC01"); // give WORKER & BROKER time to settle Thread.Sleep(250); using (var session = new MDPClient("tcp://localhost:5555", id)) { if (verbose) { session.LogInfoReady += (s, e) => Console.WriteLine("{0}", e.Info); } var request = new NetMQMessage(); // set the service name request.Push(service_to_lookup); // send the request to service discovery var reply = session.Send(service_discovery, request); if (reply != null && !reply.IsEmpty) { var answer = reply.First.ConvertToString(); Console.WriteLine("Lookup {0} service returned: {1}/{2}", service_to_lookup, answer, reply); } else { Console.WriteLine("ERROR: no response from broker, seems like broker is NOT running!"); } } Console.Write("Exit with any key."); Console.ReadKey(); }
/// <summary> /// usage: MDPClientExample [-v] [-rn] (1 ;lt n ;lt 100000 / Default == 10) /// /// implements a MDPClient API usage /// </summary> private static void Main(string[] args) { if (args.Length == 1 || args.Length > 2) { if (!(args[0] == "-v" || args[0].Contains("-r"))) { Console.WriteLine("MDPClientExample [-v(erbose) OR -h(elp)]"); Console.WriteLine("\t-v => verbose"); Console.WriteLine("\tto stop processing use CTRL+C"); return; } } const string service_name = "echo"; const int max_runs = 100000; bool verbose = false; int runs = 10; if (args.Length >= 1) { if (args.Length == 1 && args[0] == "-v") { verbose = true; } else if (args.Length == 2) { verbose = args[0] == "-v" || args[1] == "-v"; } if (args[0].Contains("-r") || args.Length > 1) { if (args[0].Contains("-r")) { runs = GetInt(args[0]); } else if (args[1].Contains("-r")) { runs = GetInt(args[1]); } } } runs = runs == -1 ? 10 : runs > max_runs ? max_runs : runs; var id = new[] { (byte)'C', (byte)'1' }; var watch = new Stopwatch(); Console.WriteLine("Starting MDPClient and will send {0} requests to service <{1}>.", runs, service_name); Console.WriteLine("(writes '.' for every 100 and '|' for every 1000 requests)\n"); try { // create MDP client and set verboseness && use automatic disposal using (var session = new MDPClient("tcp://localhost:5555", id)) { if (verbose) { session.LogInfoReady += (s, e) => Console.WriteLine("{0}", e.Info); } // just give everything time to settle in Thread.Sleep(500); watch.Start(); for (int count = 0; count < runs; count++) { var request = new NetMQMessage(); // set the request data request.Push("Helo World!"); // send the request to the service var reply = session.Send(service_name, request); if (ReferenceEquals(reply, null)) { break; } if (count % 1000 == 0) { Console.Write("|"); } else if (count % 100 == 0) { Console.Write("."); } } watch.Stop(); } } catch (Exception ex) { Console.WriteLine("ERROR:"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); return; } var time = watch.Elapsed; Console.WriteLine("{0} request/replies in {1} ms processed! Took {2:N3} ms per REQ/REP", runs, time.TotalMilliseconds, time.TotalMilliseconds / runs); Console.Write("\nExit with any key!"); Console.ReadKey(); }