// --------------------------------------------------------------------- // This is our client task // It connects to the server, and then sends a request once per second // It collects responses as they arrive, and it prints them out. We will // run several client tasks in parallel, each with a different random ID. public static void ClientTask() { using (var context = ZmqContext.Create()) { using (ZmqSocket client = context.CreateSocket(SocketType.DEALER)) { // Generate printable identity for the client string identity = ZHelpers.SetID(client, Encoding.Unicode); client.Connect("tcp://localhost:5570"); client.ReceiveReady += (s, e) => { var zmsg = new ZMessage(e.Socket); Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString()); }; int requestNumber = 0; var poller = new Poller(new List <ZmqSocket> { client }); while (true) { // Tick once per second, pulling in arriving messages for (int centitick = 0; centitick < 100; centitick++) { poller.Poll(TimeSpan.FromMilliseconds(10)); } var zmsg = new ZMessage(""); JSONMessage jsonMess = new JSONMessage(); jsonMess.Service = "Klient"; jsonMess.Function = "RegisterService"; jsonMess.Parameters = new string[] { "Klient", "Location" }; string json = JsonConvert.SerializeObject(jsonMess); zmsg.StringToBody(json); zmsg.Send(client); jsonMess = new JSONMessage(); jsonMess.Service = "Klient"; jsonMess.Function = "GetServiceLocation"; jsonMess.Parameters = new string[] { "Klient", "Location" }; json = JsonConvert.SerializeObject(jsonMess); zmsg.StringToBody(json); zmsg.Send(client); } } } }
// --------------------------------------------------------------------- // This is our client task // It connects to the server, and then sends a request once per second // It collects responses as they arrive, and it prints them out. We will // run several client tasks in parallel, each with a different random ID. static void ClientTask() { using (Context ctx = new Context(1)) { using (Socket client = ctx.Socket(SocketType.XREQ)) { // Generate printable identity for the client ZHelpers.SetID(client, Encoding.Unicode); string identity = client.IdentityToString(Encoding.Unicode); client.Connect("tcp://localhost:5570"); client.PollInHandler += (skt, revents) => { ZMessage zmsg = new ZMessage(skt); Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString()); }; int requestNbr = 0; while (true) { // Tick once per second, pulling in arriving messages for (int centitick = 0; centitick < 100; centitick++) { Context.Poller(new List<Socket>(new Socket[] { client }), 10000); } ZMessage zmsg = new ZMessage(""); zmsg.StringToBody(String.Format("request: {0}", ++requestNbr)); zmsg.Send(client); } } } }
private static void WorkerTask() { using (var ctx = new Context(1)) { using (var worker = ctx.Socket(SocketType.REQ)) { ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect(localBeAddress); var msg = new ZMessage("READY"); msg.Send(worker); while (true) { var recvMsg = new ZMessage(worker); Console.WriteLine("Worker: {0}", recvMsg.BodyToString()); Thread.Sleep(1000); recvMsg.StringToBody("OK"); recvMsg.Send(worker); //var okmsg = new ZMessage("OK"); //okmsg.Send(worker); } } } }
// --------------------------------------------------------------------- // This is our client task // It connects to the server, and then sends a request once per second // It collects responses as they arrive, and it prints them out. We will // run several client tasks in parallel, each with a different random ID. static void ClientTask() { using (Context ctx = new Context(1)) { using (Socket client = ctx.Socket(SocketType.XREQ)) { // Generate printable identity for the client ZHelpers.SetID(client, Encoding.Unicode); string identity = client.IdentityToString(Encoding.Unicode); client.Connect("tcp://localhost:5570"); client.PollInHandler += (skt, revents) => { ZMessage zmsg = new ZMessage(skt); Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString()); }; int requestNbr = 0; while (true) { // Tick once per second, pulling in arriving messages for (int centitick = 0; centitick < 100; centitick++) { Context.Poller(new List <Socket>(new Socket[] { client }), 10000); } ZMessage zmsg = new ZMessage(""); zmsg.StringToBody(String.Format("request: {0}", ++requestNbr)); zmsg.Send(client); } } } }
// Worker using REQ socket to do LRU routing // static void WorkerTask() { using (Context ctx = new Context(1)) { using (Socket worker = ctx.Socket(SocketType.REQ)) { ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect("tcp://localhost:5556"); // Tell broker we're ready for work worker.Send("READY", Encoding.Unicode); while (true) { ZMessage zmsg = new ZMessage(worker); Console.WriteLine("Worker: {0}", Encoding.Unicode.GetString(zmsg.Body)); zmsg.StringToBody("OK"); zmsg.Send(worker); } } } }
// --------------------------------------------------------------------- // This is our client task // It connects to the server, and then sends a request once per second // It collects responses as they arrive, and it prints them out. We will // run several client tasks in parallel, each with a different random ID. public static void ClientTask() { using (var context = ZmqContext.Create()) { using (ZmqSocket client = context.CreateSocket(SocketType.DEALER)) { // Generate printable identity for the client string identity = ZHelpers.SetID(client, Encoding.Unicode); client.Connect("tcp://localhost:5570"); client.ReceiveReady += (s, e) => { var zmsg = new ZMessage(e.Socket); Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString()); }; int requestNumber = 0; var poller = new Poller(new List <ZmqSocket> { client }); while (true) { // Tick once per second, pulling in arriving messages for (int centitick = 0; centitick < 100; centitick++) { poller.Poll(TimeSpan.FromMilliseconds(10)); } var zmsg = new ZMessage(""); zmsg.StringToBody(String.Format("request: {0}", ++requestNumber)); zmsg.Send(client); } } } }
// Worker using REQ socket to do LRU routing // static void WorkerTask() { using (Context ctx = new Context(1)) { using (Socket worker = ctx.Socket(SocketType.REQ)) { ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect("tcp://localhost:5556"); // Tell broker we're ready for work worker.Send("READY", Encoding.Unicode); while (true) { ZMessage zmsg = new ZMessage(worker); Console.WriteLine("Worker: {0}", Encoding.Unicode.GetString(zmsg.Body)); zmsg.StringToBody("OK"); zmsg.Send(worker); } } } }
// --------------------------------------------------------------------- // This is our server task // It uses the multithreaded server model to deal requests out to a pool // of workers and route replies back to clients. One worker can handle // one request at a time but one client can talk to multiple workers at // once. private static void ServerTask() { var workers = new List <Thread>(5); using (var context = ZmqContext.Create()) { using (ZmqSocket frontend = context.CreateSocket(SocketType.ROUTER), backend = context.CreateSocket(SocketType.DEALER)) { frontend.Bind("tcp://*:5570"); backend.Bind("inproc://backend"); for (int workerNumber = 0; workerNumber < 5; workerNumber++) { workers.Add(new Thread(ServerWorker)); workers[workerNumber].Start(context); } // Switch messages between frontend and backend frontend.ReceiveReady += (s, e) => { var zmsg = new ZMessage(e.Socket); zmsg.Send(backend); }; backend.ReceiveReady += (s, e) => { var zmsg = new ZMessage(e.Socket); string lol = zmsg.BodyToString(); JSONMessage m = JsonConvert.DeserializeObject <JSONMessage>(lol); if (m.Function != null) { switch (m.Function) { case "RegisterService": Console.WriteLine("RegisterService"); m.ReponseString = "OK"; Repository.RegisterService(m.Parameters[0], m.Parameters[1]); break; case "GetServiceLocation": Console.WriteLine("GetServiceLocation"); string locat = Repository.GetServiceLocation(m.Parameters[0]); m.ReponseString = locat; break; case "Alive": Console.WriteLine("Alive"); Repository.Alive(m.Parameters[0]); m.ReponseString = "OK"; break; case "Unregister": Console.WriteLine("Unregister"); Repository.Unregister(m.Parameters[0]); m.ReponseString = "OK"; break; default: Console.WriteLine("Unknown: " + m.Function); break; } } string json = JsonConvert.SerializeObject(m); zmsg.StringToBody(json); zmsg.Send(frontend); }; var poller = new Poller(new List <ZmqSocket> { frontend, backend }); while (true) { poller.Poll(); } } } }
private static void WorkerTask() { using (var ctx = ZmqContext.Create()) { using (var worker = ctx.CreateSocket(SocketType.REQ)) { ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect(localBeAddress); var msg = new ZMessage("READY"); msg.Send(worker); while (true) { var recvMsg = new ZMessage(worker); Console.WriteLine("Worker: {0}", recvMsg.BodyToString()); Thread.Sleep(1000); recvMsg.StringToBody("OK"); recvMsg.Send(worker); //var okmsg = new ZMessage("OK"); //okmsg.Send(worker); } } } }