// We have two workers, here we copy the code, normally these would run on different boxes… public static void Main() { var random = new Random(DateTime.Now.Millisecond); var workers = new List <Thread>(new[] { new Thread(WorkerTaskA), new Thread(WorkerTaskB) }); using (var client = new RouterSocket()) { client.Bind(string.Format("tcp://localhost:{0}", PortNumber)); foreach (var thread in workers) { thread.Start(PortNumber); } // Wait for threads to connect, since otherwise the messages we send won't be routable. Thread.Sleep(1000); for (int taskNumber = 0; taskNumber < 1000; taskNumber++) { // Send two message parts, first the address… client.SendMoreFrame(random.Next(3) > 0 ? Encoding.Unicode.GetBytes("A") : Encoding.Unicode.GetBytes("B")); // And then the workload client.SendFrame("This is the workload"); } client.SendMoreFrame(Encoding.Unicode.GetBytes("A")); client.SendFrame("END"); client.SendMoreFrame(Encoding.Unicode.GetBytes("B")); client.SendFrame("END"); } Console.ReadKey(); }
// We have two workers, here we copy the code, normally these would run on different boxes… public static void Main() { var random = new Random(DateTime.Now.Millisecond); var workers = new List<Thread>(new[] { new Thread(WorkerTaskA), new Thread(WorkerTaskB) }); using (var client = new RouterSocket()) { client.Bind($"tcp://localhost:{PortNumber}"); foreach (var thread in workers) { thread.Start(PortNumber); } // Wait for threads to connect, since otherwise the messages we send won't be routable. Thread.Sleep(1000); for (int taskNumber = 0; taskNumber < 1000; taskNumber++) { // Send two message parts, first the address… client.SendMoreFrame(random.Next(3) > 0 ? Encoding.Unicode.GetBytes("A") : Encoding.Unicode.GetBytes("B")); // And then the workload client.SendFrame("This is the workload"); } client.SendMoreFrame(Encoding.Unicode.GetBytes("A")); client.SendFrame("END"); client.SendMoreFrame(Encoding.Unicode.GetBytes("B")); client.SendFrame("END"); } Console.ReadKey(); }
public static void Main() { var workers = new List <Thread>(WorkersCount); using (var client = new RouterSocket()) { string cnn = $"tcp://localhost:{PortNumber}"; client.Bind(cnn); Console.WriteLine("[B] Connect to {0}", cnn); for (int workerNumber = 0; workerNumber < WorkersCount; workerNumber++) { workers.Add(new Thread(WorkerTask)); workers[workerNumber].Start(PortNumber); } for (int taskNumber = 0; taskNumber < WorkersCount * 10; taskNumber++) { // LRU worker is next waiting in queue string address = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", address); string empty = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", empty); string ready = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", ready); client.SendMoreFrame(address); //Console.WriteLine("[B] Message sent: {0}", address); client.SendMoreFrame(""); //Console.WriteLine("[B] Message sent: {0}", ""); client.SendFrame("This is the workload"); //Console.WriteLine("[B] Message sent: {0}", "This is the workload"); } // Now ask mamas to shut down and report their results for (int taskNbr = 0; taskNbr < WorkersCount; taskNbr++) { string address = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", address); string empty = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", empty); string ready = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", ready); client.SendMoreFrame(address); //Console.WriteLine("[B] Message sent: {0}", address); client.SendMoreFrame(""); //Console.WriteLine("[B] Message sent: {0}", ""); client.SendFrame("END"); //Console.WriteLine("[B] Message sent: {0}", "END"); } } Console.ReadLine(); }
public static void Main() { var workers = new List<Thread>(WorkersCount); using (var client = new RouterSocket()) { string cnn = $"tcp://localhost:{PortNumber}"; client.Bind(cnn); Console.WriteLine("[B] Connect to {0}", cnn); for (int workerNumber = 0; workerNumber < WorkersCount; workerNumber++) { workers.Add(new Thread(WorkerTask)); workers[workerNumber].Start(PortNumber); } for (int taskNumber = 0; taskNumber < WorkersCount*10; taskNumber++) { // LRU worker is next waiting in queue string address = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", address); string empty = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", empty); string ready = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", ready); client.SendMoreFrame(address); //Console.WriteLine("[B] Message sent: {0}", address); client.SendMoreFrame(""); //Console.WriteLine("[B] Message sent: {0}", ""); client.SendFrame("This is the workload"); //Console.WriteLine("[B] Message sent: {0}", "This is the workload"); } // Now ask mamas to shut down and report their results for (int taskNbr = 0; taskNbr < WorkersCount; taskNbr++) { string address = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", address); string empty = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", empty); string ready = client.ReceiveFrameString(); //Console.WriteLine("[B] Message received: {0}", ready); client.SendMoreFrame(address); //Console.WriteLine("[B] Message sent: {0}", address); client.SendMoreFrame(""); //Console.WriteLine("[B] Message sent: {0}", ""); client.SendFrame("END"); //Console.WriteLine("[B] Message sent: {0}", "END"); } } Console.ReadLine(); }
private void BackendReceiveReady(object sender, NetMQSocketEventArgs e) { // 将worker的地址入队 var address = e.Socket.ReceiveFrameBytes(); workerQueue.Enqueue(address); // 跳过空帧 e.Socket.ReceiveFrameBytes(); // 第三帧是“READY”或是一个client的地址 var clientAddress = e.Socket.ReceiveFrameBytes(); // 如果是一个应答消息,则转发给client if (clientAddress.Length > 1) { //空帧 e.Socket.ReceiveFrameBytes(); var replay = e.Socket.ReceiveFrameBytes(); _frontend.SendMoreFrame(clientAddress); _frontend.SendMoreFrame(""); _frontend.SendFrame(replay); } else { Console.WriteLine($"backend[{Encoding.UTF8.GetString(address)}] receive ready"); } }
public void Receive() { async Task ReceiveAsync() { using (var server = new RouterSocket("inproc://async")) using (var client = new DealerSocket("inproc://async")) { client.SendFrame("Hello"); var(routingKey, _) = await server.ReceiveRoutingKeyAsync(); var(message, _) = await server.ReceiveFrameStringAsync(); Assert.Equal(message, "Hello"); server.SendMoreFrame(routingKey); server.SendFrame(new[] { (byte)0 }); var(bytes, _) = await client.ReceiveFrameBytesAsync(); Assert.Equal(bytes[0], 0); } } using (var runtime = new NetMQRuntime()) { runtime.Run(ReceiveAsync()); } }
public void SendReceive() { async Task SendReceiveAsync() { using (var server = new RouterSocket("inproc://async")) using (var client = new DealerSocket("inproc://async")) { await client.SendFrameAsync("Hello"); var(routingKey, _) = await server.ReceiveRoutingKeyAsync(); var(message, _) = await server.ReceiveFrameStringAsync(); Assert.Equal("Hello", message); server.SendMoreFrame(routingKey); server.SendFrame(new[] { (byte)0 }); var(bytes, _) = await client.ReceiveFrameBytesAsync(); Assert.Equal(new[] { (byte)0 }, bytes); } } using (var runtime = new NetMQRuntime()) { var t = SendReceiveAsync(); runtime.Run(t); if (t.IsFaulted && t.Exception is AggregateException exc) { throw exc.GetBaseException(); } } }
private void FrontendReceiveReady(object sender, NetMQSocketEventArgs e) { //front 收到数据 //client地址 var clientAddress = e.Socket.ReceiveFrameBytes(); //空帧 e.Socket.ReceiveFrameBytes(); //其他数据 var msg = e.Socket.ReceiveFrameBytes(); //从backend中获取一个可用的 byte[] backendAddress = null; if (workerQueue.TryDequeue(out backendAddress)) { _backend.SendMoreFrame(backendAddress); _backend.SendMoreFrame(""); _backend.SendMoreFrame(clientAddress); _backend.SendMoreFrame(""); _backend.SendFrame(msg); } }
private void BackendReceiveReady(object sender, NetMQSocketEventArgs e) { // 将worker的地址入队 var address = e.Socket.ReceiveFrameBytes(); workerQueue.Enqueue(address); // 跳过空帧 e.Socket.ReceiveFrameBytes(); // 第三帧是“READY”或是一个client的地址 var clientAddress = e.Socket.ReceiveFrameBytes(); // 如果是一个应答消息,则转发给client if (clientAddress.Length > 1) { //空帧 e.Socket.ReceiveFrameBytes(); var replay = e.Socket.ReceiveFrameBytes(); _frontend.SendMoreFrame(clientAddress); _frontend.SendMoreFrame(""); _frontend.SendFrame(replay); } }
private void FrontendReceiveReady(object sender, NetMQSocketEventArgs e) { Logger.Trace($"------------FrontendReceiveReady----------------"); //front 收到数据 //client地址 var clientAddress = e.Socket.ReceiveFrameBytes(); //空帧 e.Socket.ReceiveFrameBytes(); //其他数据 var msg = e.Socket.ReceiveFrameBytes(); //从backend中获取一个可用的 byte[] backendAddress = null; if (Logger.IsTraceEnabled) { Logger.Trace($"LRUBroker.WorkerQueue = {workerQueue.Count}"); } if (workerQueue.TryDequeue(out backendAddress)) { _backend.SendMoreFrame(backendAddress); _backend.SendMoreFrame(""); _backend.SendMoreFrame(clientAddress); _backend.SendMoreFrame(""); _backend.SendFrame(msg); } else { if (Logger.IsWarnEnabled) { Logger.Warn("No BackendNetService available"); } } }
private static void Main() { using (var frontend = new RouterSocket()) using (var backend = new RouterSocket()) { // For Clients Console.WriteLine("Q: Binding frontend {0}", FrontendEndpoint); frontend.Bind(FrontendEndpoint); // For Workers Console.WriteLine("Q: Binding backend {0}", BackendEndpoint); backend.Bind(BackendEndpoint); // Logic of LRU loop // - Poll backend always, frontend only if 1+ worker ready // - If worker replies, queue worker as ready and forward reply // to client if necessary // - If client requests, pop next worker and send request to it // Queue of available workers var workerQueue = new Queue<byte[]>(); // Handle worker activity on backend backend.ReceiveReady += (s, e) => { // Queue worker address for LRU routing byte[] workerAddress = e.Socket.ReceiveFrameBytes(); // Use worker address for LRU routing workerQueue.Enqueue(workerAddress); // Second frame is empty e.Socket.SkipFrame(); // Third frame is READY or else a client reply address byte[] clientAddress = e.Socket.ReceiveFrameBytes(); // If client reply, send rest back to frontend // Forward message to client if it's not a READY if (Encoding.Unicode.GetString(clientAddress) != LRUReady) { e.Socket.SkipFrame(); // empty byte[] reply = e.Socket.ReceiveFrameBytes(); frontend.SendMoreFrame(clientAddress); frontend.SendMoreFrame(""); frontend.SendFrame(reply); } }; frontend.ReceiveReady += (s, e) => { // Now get next client request, route to next worker // Dequeue and drop the next worker address // Now get next client request, route to LRU worker // Client request is [address][empty][request] byte[] clientAddr = e.Socket.ReceiveFrameBytes(); e.Socket.SkipFrame(); // empty byte[] request = e.Socket.ReceiveFrameBytes(); try { byte[] deq = workerQueue.Dequeue(); backend.SendMoreFrame(deq); backend.SendMoreFrame(Encoding.Unicode.GetBytes("")); backend.SendMoreFrame(clientAddr); backend.SendMoreFrame(Encoding.Unicode.GetBytes("")); backend.SendFrame(request); } catch (Exception ex) { Console.WriteLine("Q: [FrontendOnReceiveReady] Dequeue exception: {0}", ex.ToString()); } }; while (true) { backend.Poll(TimeSpan.FromMilliseconds(500)); if (workerQueue.Count > 0) frontend.Poll(TimeSpan.FromMilliseconds(500)); } } }
private static void Main() { using (var frontend = new RouterSocket()) using (var backend = new RouterSocket()) { // For Clients Console.WriteLine("Q: Binding frontend {0}", FrontendEndpoint); frontend.Bind(FrontendEndpoint); // For Workers Console.WriteLine("Q: Binding backend {0}", BackendEndpoint); backend.Bind(BackendEndpoint); // Logic of LRU loop // - Poll backend always, frontend only if 1+ worker ready // - If worker replies, queue worker as ready and forward reply // to client if necessary // - If client requests, pop next worker and send request to it // Queue of available workers var workerQueue = new Queue <byte[]>(); // Handle worker activity on backend backend.ReceiveReady += (s, e) => { // Queue worker address for LRU routing byte[] workerAddress = e.Socket.ReceiveFrameBytes(); // Use worker address for LRU routing workerQueue.Enqueue(workerAddress); // Second frame is empty e.Socket.SkipFrame(); // Third frame is READY or else a client reply address byte[] clientAddress = e.Socket.ReceiveFrameBytes(); // If client reply, send rest back to frontend // Forward message to client if it's not a READY if (Encoding.Unicode.GetString(clientAddress) != LRUReady) { e.Socket.SkipFrame(); // empty byte[] reply = e.Socket.ReceiveFrameBytes(); frontend.SendMoreFrame(clientAddress); frontend.SendMoreFrame(""); frontend.SendFrame(reply); } }; frontend.ReceiveReady += (s, e) => { // Now get next client request, route to next worker // Dequeue and drop the next worker address // Now get next client request, route to LRU worker // Client request is [address][empty][request] byte[] clientAddr = e.Socket.ReceiveFrameBytes(); e.Socket.SkipFrame(); // empty byte[] request = e.Socket.ReceiveFrameBytes(); try { byte[] deq = workerQueue.Dequeue(); backend.SendMoreFrame(deq); backend.SendMoreFrame(Encoding.Unicode.GetBytes("")); backend.SendMoreFrame(clientAddr); backend.SendMoreFrame(Encoding.Unicode.GetBytes("")); backend.SendFrame(request); } catch (Exception ex) { Console.WriteLine("Q: [FrontendOnReceiveReady] Dequeue exception: {0}", ex.ToString()); } }; while (true) { backend.Poll(TimeSpan.FromMilliseconds(500)); if (workerQueue.Count > 0) { frontend.Poll(TimeSpan.FromMilliseconds(500)); } } } }