// --------------------------------------------------------------------- // 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 ClientTask() { using (var ctx = new Context(1)) { using (var client = ctx.Socket(SocketType.REQ)) { ZHelpers.SetID(client, Encoding.Unicode); client.Connect("tcp://localhost:5555"); while (true) { // Send request, get repl client.Send("HELLO", Encoding.Unicode); string reply = client.Recv(Encoding.Unicode); if (string.IsNullOrEmpty(reply)) { break; } Console.WriteLine("Client: {0}", reply); } } } }
static void Main(string[] args) { Console.WriteLine(ZHelpers.Version()); Console.WriteLine("Press enter to continue."); Console.ReadLine(); }
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); } } } }
private static void ClientTask() { using (var ctx = new Context(1)) { using (var client = ctx.Socket(SocketType.REQ)) { ZHelpers.SetID(client, Encoding.Unicode); client.Connect(localFeAddress); while (true) { client.Send("HELLO", Encoding.Unicode); string reply = client.Recv(Encoding.Unicode); if (string.IsNullOrEmpty(reply)) { break; } Console.WriteLine("Client: {0}", reply); Thread.Sleep(1000); } } } }
static void WorkerTask() { Random rand = new Random(DateTime.Now.Millisecond); using (Context ctx = new Context(1)) { using (Socket worker = ctx.Socket(SocketType.REQ)) { // We use a string identity for ease here ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect("tcp://localhost:5555"); int total = 0; while (true) { // Tell the router we're ready for work worker.Send("Ready", Encoding.Unicode); // Get workload from router, until finished string workload = worker.Recv(Encoding.Unicode); if (workload.Equals("END")) { Console.WriteLine("Processed: {0} tasks", total); break; } total++; // Do some random work Thread.Sleep(rand.Next(1, 1000)); } } } }
// 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) { // Read and save all frames until we get an empty frame // In this example there is only 1 but it could be more string address = worker.Recv(Encoding.Unicode); string empty = worker.Recv(Encoding.Unicode); // Get request, send reply string request = worker.Recv(Encoding.Unicode); Console.WriteLine("Worker: {0}", request); worker.SendMore(address, Encoding.Unicode); worker.SendMore(); worker.Send("OK", Encoding.Unicode); } } } }
// We will do this all in one thread to emphasize the sequence // of events... static void Main(string[] args) { using (Context ctx = new Context(1)) { using (Socket client = ctx.Socket(SocketType.XREP), worker = ctx.Socket(SocketType.REP)) { client.Bind("inproc://routing"); worker.StringToIdentity("A", Encoding.Unicode); worker.Connect("inproc://routing"); // Wait for the worker to connect so that when we send a message // with routing envelope, it will actually match the worker... Thread.Sleep(1000); // Send papa address, address stack, empty part, and request client.SendMore("A", Encoding.Unicode); client.SendMore("address 3", Encoding.Unicode); client.SendMore("address 2", Encoding.Unicode); client.SendMore("address 1", Encoding.Unicode); client.SendMore("", Encoding.Unicode); client.Send("This is the workload", Encoding.Unicode); // Worker should get just the workload ZHelpers.Dump(worker, Encoding.Unicode); // We don't play with envelopes in the worker worker.Send("This is the reply", Encoding.Unicode); // Now dump what we got off the XREP socket... ZHelpers.Dump(client, Encoding.Unicode); } } }
static void AsyncReturn(byte[] identity, Queue <byte[]> msgParts) { using (Socket skt = new Socket(SocketType.PUSH)) { skt.Connect("inproc://asyncReturn"); skt.SendMore(identity); skt.SendMore(msgParts.Dequeue()); skt.Send("Replying to \"" + ZHelpers.DecodeUUID(identity) + "\" with \"" + Encoding.Unicode.GetString(msgParts.Dequeue()) + "\"", Encoding.Unicode); } }
// --------------------------------------------------------------------- // 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); } } } }
// Basic request-reply client using REQ socket // static void ClientTask() { using (Context ctx = new Context(1)) { using (Socket client = ctx.Socket(SocketType.REQ)) { ZHelpers.SetID(client, Encoding.Unicode); client.Connect("tcp://localhost:5555"); // Send request, get reply client.Send("HELLO", Encoding.Unicode); string reply = client.Recv(Encoding.Unicode); Console.WriteLine("Client: {0}", reply); } } }
static Socket ConnectWorker(Context context) { Socket worker = context.Socket(SocketType.DEALER); //Set random identity to make tracing easier ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect("tcp://localhost:5556"); //Tell the queue we're ready for work Console.WriteLine("I: worker ready"); worker.Send(PPP_READY, Encoding.Unicode); return(worker); }
private static void ClientTask() { using (var context = ZmqContext.Create()) { using (ZmqSocket client = context.CreateSocket(SocketType.REQ)) { ZHelpers.SetID(client, Encoding.Unicode); client.Connect("tcp://localhost:5555"); // Send request, get reply client.Send("HELLO", Encoding.Unicode); string reply = client.Receive(Encoding.Unicode); Console.WriteLine("Client: {0}", reply); } } }
// This main thread simply starts several clients, and a server, and then // waits for the server to finish. // static void Main(string[] args) { ZHelpers.VersionAssert(2, 1); List <Thread> clients = new List <Thread>(3); for (int clientNbr = 0; clientNbr < 3; clientNbr++) { clients.Add(new Thread(ClientTask)); clients[clientNbr].Start(); } Thread serverThread = new Thread(ServerTask); serverThread.Start(); Console.ReadLine(); }
// 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); } } } }
static void Main(string[] args) { ZHelpers.VersionAssert(2, 1); List <Thread> workers = new List <Thread>(NBR_WORKERS); using (Context ctx = new Context(1)) { using (Socket client = ctx.Socket(SocketType.ROUTER)) { client.Bind("tcp://*:5555"); for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) { workers.Add(new Thread(WorkerTask)); workers[workerNbr].Start(); } for (int taskNbr = 0; taskNbr < NBR_WORKERS * 10; taskNbr++) { // LRU worker is next waiting in queue string address = client.Recv(Encoding.Unicode); string empty = client.Recv(Encoding.Unicode); string ready = client.Recv(Encoding.Unicode); client.SendMore(address, Encoding.Unicode); client.SendMore(); client.Send("This is the workload", Encoding.Unicode); } // Now ask mamas to shut down and report their results for (int taskNbr = 0; taskNbr < NBR_WORKERS; taskNbr++) { string address = client.Recv(Encoding.Unicode); string empty = client.Recv(Encoding.Unicode); string ready = client.Recv(Encoding.Unicode); client.SendMore(address, Encoding.Unicode); client.SendMore(); client.Send("END", Encoding.Unicode); } Console.ReadLine(); } } }
public static void Main(string[] args) { using (var context = new Context()) { using (Socket sink = context.Socket(SocketType.ROUTER), anonymous = context.Socket(SocketType.REQ), identified = context.Socket(SocketType.REQ)) { sink.Bind("inproc://example"); // First allow 0MQ to set the identity anonymous.Connect("inproc://example"); anonymous.Send("ROUTER uses a generated UUID", Encoding.Unicode); ZHelpers.Dump(sink, Encoding.Unicode); // Then set the identity ourselves identified.StringToIdentity("PEER2", Encoding.Unicode); identified.Connect("inproc://example"); identified.Send("ROUTER socket uses REQ's socket identity", Encoding.Unicode); ZHelpers.Dump(sink, Encoding.Unicode); } } }
static void Main(string[] args) { using (Context ctx = new Context()) { using (Socket sink = ctx.Socket(SocketType.XREP), anonymous = ctx.Socket(SocketType.REQ), identified = ctx.Socket(SocketType.REQ)) { sink.Bind("inproc://example"); // First allow 0MQ to set the identity anonymous.Connect("inproc://example"); anonymous.Send("XREP uses a generated UUID", Encoding.Unicode); ZHelpers.Dump(sink, Encoding.Unicode); // Then set the identity ourself identified.StringToIdentity("Hello", Encoding.Unicode); identified.Connect("inproc://example"); identified.Send("XREP socket uses REQ's socket identity", Encoding.Unicode); ZHelpers.Dump(sink, Encoding.Unicode); } } }
public static void WorkerTask() { var randomizer = new Random(DateTime.Now.Millisecond); using (var context = ZmqContext.Create()) { using (ZmqSocket worker = context.CreateSocket(SocketType.REQ)) { // We use a string identity for ease here ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect("tcp://localhost:5555"); int total = 0; bool end = false; while (!end) { // Tell the router we're ready for work worker.Send("Ready", Encoding.Unicode); // Get workload from router, until finished string workload = worker.Receive(Encoding.Unicode); if (workload.Equals("END")) { end = true; } else { total++; Thread.Sleep(randomizer.Next(1, 1000)); // Simulate 'work' } } Console.WriteLine("ID ({0}) processed: {1} tasks", worker.Identity, total); } } }
static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (var worker = context.CreateSocket(SocketType.REQ)) { var randomizer = new Random(DateTime.Now.Millisecond); var identity = ZHelpers.SetID(worker, Encoding.Unicode); worker.Connect("tcp://localhost:5556"); Console.WriteLine("I: {0} worker ready", identity); worker.Send("READY", Encoding.Unicode); var cycles = 0; while (true) { var zmsg = new ZMessage(worker); cycles += 1; if (cycles > 3 && randomizer.Next(0, 5) == 0) { Console.WriteLine("I: {0} simulating a crash", identity); break; } else if (cycles > 3 && randomizer.Next(0, 5) == 0) { Console.WriteLine("I: {0} simulating CPU overload", identity); System.Threading.Thread.Sleep(3000); } Console.WriteLine("I: {0} normal reply", identity); System.Threading.Thread.Sleep(1000); 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); } } } }
public static void Main(string[] args) { Console.WriteLine(ZHelpers.Version()); }
public static string GetZmqVersion() { return(ZHelpers.Version()); }