public static void Main(string[] args) { var workers = new List <Thread>(WORKERS_COUNT); using (NetMQContext context = NetMQContext.Create()) { using (RouterSocket client = context.CreateRouterSocket()) { string cnn = string.Format("tcp://localhost:{0}", PORT_NUMBER); client.Bind(cnn); Console.WriteLine("[B] Connect to {0}", cnn); for (int workerNumber = 0; workerNumber < WORKERS_COUNT; workerNumber++) { workers.Add(new Thread(WorkerTask)); workers[workerNumber].Start(PORT_NUMBER); } for (int taskNumber = 0; taskNumber < WORKERS_COUNT * 10; taskNumber++) { // LRU worker is next waiting in queue string address = client.ReceiveString(); //Console.WriteLine("[B] Message received: {0}", address); string empty = client.ReceiveString(); //Console.WriteLine("[B] Message received: {0}", empty); string ready = client.ReceiveString(); //Console.WriteLine("[B] Message received: {0}", ready); client.SendMore(address); //Console.WriteLine("[B] Message sent: {0}", address); client.SendMore(""); //Console.WriteLine("[B] Message sent: {0}", ""); client.Send("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 < WORKERS_COUNT; taskNbr++) { string address = client.ReceiveString(); //Console.WriteLine("[B] Message received: {0}", address); string empty = client.ReceiveString(); //Console.WriteLine("[B] Message received: {0}", empty); string ready = client.ReceiveString(); //Console.WriteLine("[B] Message received: {0}", ready); client.SendMore(address); //Console.WriteLine("[B] Message sent: {0}", address); client.SendMore(""); //Console.WriteLine("[B] Message sent: {0}", ""); client.Send("END"); //Console.WriteLine("[B] Message sent: {0}", "END"); } } } Console.ReadLine(); }
private static void BackendOnReceiveReady(object sender, NetMQSocketEventArgs socket) { // Queue worker address for LRU routing byte[] workerAddress = socket.Socket.Receive(); // Use worker address for LRU routing workerQueue.Enqueue(workerAddress); // Second frame is empty byte[] empty = socket.Socket.Receive(); // Third frame is READY or else a client reply address byte[] clientAddress = socket.Socket.Receive(); // If client reply, send rest back to frontend // Forward message to client if it's not a READY if (Encoding.Unicode.GetString(clientAddress) != LRU_READY) { empty = socket.Socket.Receive(); byte[] reply = socket.Socket.Receive(); frontend.SendMore(clientAddress); frontend.SendMore(""); frontend.Send(reply); } }
private static void FrontendOnReceiveReady(object sender, NetMQSocketEventArgs socket) { // 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 = socket.Socket.Receive(); byte[] empty = socket.Socket.Receive(); byte[] request = socket.Socket.Receive(); byte[] deq; try { deq = workerQueue.Dequeue(); backend.SendMore(deq); backend.SendMore(Encoding.Unicode.GetBytes("")); backend.SendMore(clientAddr); backend.SendMore(Encoding.Unicode.GetBytes("")); backend.Send(request); } catch (Exception exc) { Console.WriteLine("Q: [FrontendOnReceiveReady] Dequeue exc: {0}", exc.ToString()); } }
// We have two workers, here we copy the code, normally these would // run on different boxes… public static void Main(string[] args) { var randomizer = new Random(DateTime.Now.Millisecond); var workers = new List <Thread>(new[] { new Thread(WorkerTaskA), new Thread(WorkerTaskB) }); using (NetMQContext context = NetMQContext.Create()) { using (RouterSocket client = context.CreateRouterSocket()) { client.Bind(string.Format("tcp://localhost:{0}", PORT_NUMBER)); foreach (Thread thread in workers) { thread.Start(PORT_NUMBER); } // 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.SendMore(randomizer.Next(3) > 0 ? Encoding.Unicode.GetBytes("A") : Encoding.Unicode.GetBytes("B")); // And then the workload client.Send("This is the workload"); } client.SendMore(Encoding.Unicode.GetBytes("A")); client.Send("END"); client.SendMore(Encoding.Unicode.GetBytes("B")); client.Send("END"); } } Console.ReadKey(); }
private static void Main() { const int messageCount = 1000000; const int dealerCount = 100; Console.WriteLine("Sending {0} messages to {1} dealers", messageCount, dealerCount); //BufferPool.SetBufferManagerBufferPool(1024 * 1024 * 10, 1024); using (var router = new RouterSocket()) { router.Options.SendHighWatermark = 0; router.Bind("tcp://*:5555"); var dealers = new List <DealerSocket>(); var identities = new List <Msg>(); var random = new Random(); var identity = new byte[50]; for (var i = 0; i < dealerCount; i++) { random.NextBytes(identity); var dealer = new DealerSocket { Options = { Identity = identity.Skip(10).ToArray(), ReceiveHighWatermark = 0 } }; dealer.Connect("tcp://localhost:5555"); dealers.Add(dealer); var msg = new Msg(); msg.InitGC(identity, 10, identity.Length); // test offsets identities.Add(msg); } Thread.Sleep(500); while (!Console.KeyAvailable) { Thread.Sleep(500); var stopwatch = Stopwatch.StartNew(); for (var i = 0; i < messageCount; i++) { var msg = identities[i % identities.Count]; router.Send(ref msg, true); var msg2 = new Msg(); msg2.InitPool(1); msg2.Put((byte)'E'); router.Send(ref msg2, false); } stopwatch.Stop(); Console.WriteLine("{0:N1} messages sent per second", messageCount / stopwatch.Elapsed.TotalSeconds); } foreach (var dealerSocket in dealers) { dealerSocket.Dispose(); } } }
private static void Main() { const int messageCount = 1000000; const int dealerCount = 100; Console.WriteLine("Sending {0} messages to {1} dealers", messageCount, dealerCount); //BufferPool.SetBufferManagerBufferPool(1024 * 1024 * 10, 1024); using (var router = new RouterSocket()) { router.Options.SendHighWatermark = 0; router.Bind("tcp://*:5555"); var dealers = new List<DealerSocket>(); var identities = new List<Msg>(); var random = new Random(); var identity = new byte[50]; for (var i = 0; i < dealerCount; i++) { random.NextBytes(identity); var dealer = new DealerSocket { Options = { Identity = identity.Skip(10).ToArray(), ReceiveHighWatermark = 0 } }; dealer.Connect("tcp://localhost:5555"); dealers.Add(dealer); var msg = new Msg(); msg.InitGC(identity, 10, identity.Length); // test offsets identities.Add(msg); } Thread.Sleep(500); while (!Console.KeyAvailable) { Thread.Sleep(500); var stopwatch = Stopwatch.StartNew(); for (var i = 0; i < messageCount; i++) { var msg = identities[i%identities.Count]; router.Send(ref msg, true); var msg2 = new Msg(); msg2.InitPool(1); msg2.Put((byte) 'E'); router.Send(ref msg2, false); } stopwatch.Stop(); Console.WriteLine("{0:N1} messages sent per second", messageCount/stopwatch.Elapsed.TotalSeconds); } foreach (var dealerSocket in dealers) dealerSocket.Dispose(); } }