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); } } } }
// --------------------------------------------------------------------- // 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); } } } }
// --------------------------------------------------------------------- // 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"); string serviceRepoAddress = ConfigurationSettings.AppSettings["serviceRepoAddressZMQ"]; client.Connect(serviceRepoAddress); 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 }); var zmsg2 = new ZMessage(""); JSONMessage jsonMess = new JSONMessage(); jsonMess.Service = "Klient"; jsonMess.Function = "RegisterService"; jsonMess.Parameters = new string[] { "Klient", "Location", "binding" }; string json = JsonConvert.SerializeObject(jsonMess); zmsg2.StringToBody(json); zmsg2.Send(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(""); jsonMess = new JSONMessage(); jsonMess.Service = "Klient"; jsonMess.Function = "GetServiceLocation"; jsonMess.Parameters = new string[] { "Klient", "binding" }; 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. 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); } } } }
// --------------------------------------------------------------------- // 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(); } } } }
public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: peering2 <myself> <peer_1> … <peer_N>"); return; } var myself = args[0]; Console.WriteLine("Hello, I am " + myself); using (var context = new Context(1)) { using (Socket cloudfe = context.Socket(SocketType.ROUTER), cloudbe = context.Socket(SocketType.ROUTER), localfe = context.Socket(SocketType.ROUTER), localbe = context.Socket(SocketType.ROUTER)) { cloudFeAddress = "tcp://127.0.0.1:" + myself; cloudfe.Identity = Encoding.Unicode.GetBytes(myself); cloudfe.Bind(cloudFeAddress); cloudbe.Identity = Encoding.Unicode.GetBytes(myself); for (int arg = 1; arg < args.Length; arg++) { var endpoint = "tcp://127.0.0.1:" + args[arg]; peers.Add(endpoint); Console.WriteLine("I: connecting to cloud frontend at " + endpoint); cloudbe.Connect(endpoint); } localFeAddress = cloudFeAddress + "1"; localfe.Bind(localFeAddress); localBeAddress = cloudFeAddress + "2"; localbe.Bind(localBeAddress); Console.WriteLine("Press Enter when all brokers are started: "); Console.ReadKey(); var workers = new List <Thread>(); for (int workerNumber = 0; workerNumber < numberOfWorkers; workerNumber++) { workers.Add(new Thread(WorkerTask)); workers[workerNumber].Start(); } var clients = new List <Thread>(); for (int clientNumber = 0; clientNumber < numberOfClients; clientNumber++) { clients.Add(new Thread(ClientTask)); clients[clientNumber].Start(); } var workerQueue = new Queue <byte[]>(); var localfeReady = false; var cloudfeReady = false; var backends = new PollItem[2]; backends[0] = localbe.CreatePollItem(IOMultiPlex.POLLIN); backends[0].PollInHandler += (socket, revents) => { var zmsg = new ZMessage(socket); // Use worker address for LRU routing workerQueue.Enqueue(zmsg.Unwrap()); if (zmsg.BodyToString() != "READY") { SendReply(zmsg, cloudfe, localfe); } }; backends[1] = cloudbe.CreatePollItem(IOMultiPlex.POLLIN); backends[1].PollInHandler += (socket, revents) => { var zmsg = new ZMessage(socket); // We don't use peer broker address for anything zmsg.Unwrap(); SendReply(zmsg, cloudfe, localfe); }; var frontends = new PollItem[2]; frontends[0] = cloudfe.CreatePollItem(IOMultiPlex.POLLIN); frontends[0].PollInHandler += (socket, revents) => { cloudfeReady = true; }; frontends[1] = localfe.CreatePollItem(IOMultiPlex.POLLIN); frontends[1].PollInHandler += (socket, revents) => { localfeReady = true; }; while (true) { var timeout = (workerQueue.Count > 0 ? 1000000 : -1); var rc = Context.Poller(backends, timeout); if (rc == -1) { break; // Interrupted } while (workerQueue.Count > 0) { Context.Poller(frontends, 0); bool reRoutable; ZMessage msg; if (cloudfeReady) { cloudfeReady = false; msg = new ZMessage(cloudfe); reRoutable = false; } else if (localfeReady) { localfeReady = false; msg = new ZMessage(localfe); reRoutable = true; } else { break; } //if (reRoutable && workerQueue.Count > 0 && randomizer.Next(3) == 0) if (reRoutable && peers.Count > 0 && randomizer.Next(4) == 0) { var randomPeer = randomizer.Next(1, args.Length - 1); var endpoint = "tcp://127.0.0.1:" + args[randomPeer]; msg.Wrap(Encoding.Unicode.GetBytes(endpoint), new byte[0]); msg.Send(cloudbe); } else { msg.Wrap(workerQueue.Dequeue(), new byte[0]); msg.Send(localbe); } } } } } }
private void StartZmqServer() { _runServer = true; var svr = new Action( delegate { using ( var context = new Context( 1 ) ) { using ( var router = context.Socket( SocketType.ROUTER ) ) { router.Bind( "tcp://*:5556" ); router.PollInHandler += ( socket, revents ) => { var zmsg = new ZMessage( socket ); WriteLog( zmsg.BodyToString() ); }; while ( _runServer ) { Context.Poller( router ); } } } } ); WriteLog( "Starting ZeroMQ server" ); _zmqServer = new Task( svr ); _zmqServer.Start(); }
public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: peering2 <myself> <peer_1> ... <peer_N>"); return; } var myself = args[0]; Console.WriteLine("Hello, I am " + myself); using (var context = ZmqContext.Create()) { using (ZmqSocket cloudfe = context.CreateSocket(SocketType.ROUTER), cloudbe = context.CreateSocket(SocketType.ROUTER), localfe = context.CreateSocket(SocketType.ROUTER), localbe = context.CreateSocket(SocketType.ROUTER)) { cloudFeAddress = "tcp://127.0.0.1:" + myself; cloudfe.Identity = Encoding.Unicode.GetBytes(myself); cloudfe.Bind(cloudFeAddress); cloudbe.Identity = Encoding.Unicode.GetBytes(myself); for (int arg = 1; arg < args.Length; arg++) { var endpoint = "tcp://127.0.0.1:" + args[arg]; peers.Add(endpoint); Console.WriteLine("I: connecting to cloud frontend at " + endpoint); cloudbe.Connect(endpoint); } localFeAddress = cloudFeAddress + "1"; localfe.Bind(localFeAddress); localBeAddress = cloudFeAddress + "2"; localbe.Bind(localBeAddress); Console.WriteLine("Press Enter when all brokers are started: "); Console.ReadKey(); var workers = new List<Thread>(); for (int workerNumber = 0; workerNumber < numberOfWorkers; workerNumber++) { workers.Add(new Thread(WorkerTask)); workers[workerNumber].Start(); } var clients = new List<Thread>(); for (int clientNumber = 0; clientNumber < numberOfClients; clientNumber++) { clients.Add(new Thread(ClientTask)); clients[clientNumber].Start(); } var workerQueue = new Queue<byte[]>(); var localfeReady = false; var cloudfeReady = false; var backends = new Poller(); localbe.ReceiveReady += (socket, revents) => { var zmsg = new ZMessage(revents.Socket); // Use worker address for LRU routing workerQueue.Enqueue(zmsg.Unwrap()); if (zmsg.BodyToString() != "READY") { SendReply(zmsg, cloudfe, localfe); } }; cloudbe.ReceiveReady += (socket, revents) => { var zmsg = new ZMessage(revents.Socket); // We don't use peer broker address for anything zmsg.Unwrap(); SendReply(zmsg, cloudfe, localfe); }; var frontends = new Poller(); cloudfe.ReceiveReady += (socket, revents) => { cloudfeReady = true; }; localfe.ReceiveReady += (socket, revents) => { localfeReady = true; }; while (true) { var timeout = (workerQueue.Count > 0 ? 1000000 : -1); var rc = backends.Poll(TimeSpan.FromMilliseconds(timeout)); if (rc == -1) break; // Interrupted while (workerQueue.Count > 0) { frontends.Poll(TimeSpan.Zero); bool reRoutable; ZMessage msg; if (cloudfeReady) { cloudfeReady = false; msg = new ZMessage(cloudfe); reRoutable = false; } else if (localfeReady) { localfeReady = false; msg = new ZMessage(localfe); reRoutable = true; } else { break; } //if (reRoutable && workerQueue.Count > 0 && randomizer.Next(3) == 0) if (reRoutable && peers.Count > 0 && randomizer.Next(4) == 0) { var randomPeer = randomizer.Next(1, args.Length - 1); var endpoint = "tcp://127.0.0.1:" + args[randomPeer]; msg.Wrap(Encoding.Unicode.GetBytes(endpoint), new byte[0]); msg.Send(cloudbe); } else { msg.Wrap(workerQueue.Dequeue(), new byte[0]); msg.Send(localbe); } } } } } }
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); } } } }