static void Main(string[] args) { // Console.WriteLine("Connecting to hello world server…"); // using (var requester = new RequestSocket()) // { // requester.Connect("tcp://localhost:5555"); // int requestNumber; // for (requestNumber = 0; requestNumber != 100; requestNumber++) // { // Console.WriteLine("Sending Hello {0}...", requestNumber); // requester.SendFrame("Hello"); // string str = requester.ReceiveFrameString(); // Console.WriteLine("Received World {0}", requestNumber); // } // } using (var responder = new PushSocket()) { responder.Bind("tcp://*:5555"); while (true) { //string str = responder.ReceiveFrameString(); string str = "hellllllo"; Console.WriteLine(str); Thread.Sleep(50); // Do some 'work' responder.SendFrame(str); } } }
private bool _disposedValue = false; // Для определения избыточных вызовов #endregion Fields #region Constructors public FanBrocker(NetMQContext context, string ventAddress, string sinkAddress, int workersCnt) { _logger.Trace("Brocker created"); _ventAddress = ventAddress; _sinkAddress = sinkAddress; _sinkSocket = context.CreatePullSocket(); _sinkSocket.Options.ReceiveBuffer = 1; _sinkSocket.Bind(sinkAddress); _ventSocket = context.CreatePushSocket(); _ventSocket.Options.SendBuffer = 1; _ventSocket.Bind(ventAddress); Task.Run(() => { try { while (true) { var ba = _sinkSocket.ReceiveFrameString(); _logger.Trace("Brocker received data {0}", ba); var data = JsonConvert.DeserializeObject<ProcessedEventArgs>(ba); OnFramesProcessed(data); } } catch (Exception) { _logger.Error("EXCEPTION"); } }); }
static void Main(string[] args) { Console.WriteLine("====== VENTILATOR ======"); using (var push = new PushSocket()) using (var sink = new DealerSocket()) { push.Bind("tcp://127.0.0.1:8888"); sink.Connect("tcp://127.0.0.1:7777"); Console.WriteLine("Press enter when worker are ready"); Console.ReadLine(); Console.WriteLine("Sending start of batch to Sink"); sink.SendFrame("0"); Console.WriteLine("Sending tasks to workers"); var rnd = new Random(); var totalMs = 0; for (int taskNumber = 0; taskNumber < 100; taskNumber++) { int workload = rnd.Next(0, 100); totalMs += workload; Console.WriteLine("Workload : {0}", workload); push.SendFrame(workload.ToString()); } Console.WriteLine($"Total expected cost : {totalMs} msec"); Console.WriteLine("Press Enter to quit"); Console.ReadLine(); } }
protected override Task ExecuteAsync(CancellationToken stoppingToken) { xpubSocket.Bind("tcp://127.0.0.1:5556"); xsubSocket.Bind("tcp://127.0.0.1:5557"); _logger.LogInformation("MQBusService started"); var proxy = new Proxy(xsubSocket, xpubSocket); return(Task.Run(proxy.Start)); }
static void Main(string[] args) { // Task Ventilator // Binds PUSH socket to tcp://localhost:5557 // Sends batch of tasks to workers via that socket Console.WriteLine("====== VENTILATOR ======"); try { using (var sink = new PushSocket()) using (var sender = new PushSocket()) { sender.Bind("tcp://*:5558"); Console.WriteLine("Press enter when worker are ready"); Console.ReadLine(); sink.Bind("tcp://*:5557"); //the first message it "0" and signals start of batch //see the Sink.csproj Program.cs file for where this is used Console.WriteLine("Sending start of batch to Sink"); sink.SendFrame("0"); Console.WriteLine("Sending tasks to workers"); //initialise random number generator Random rand = new Random(0); //expected costs in Ms int totalMs = 0; //send 100 tasks (workload for tasks, is just some random sleep time that //the workers can perform, in real life each work would do more than sleep for (int taskNumber = 0; taskNumber < 100; taskNumber++) { //Random workload from 1 to 100 msec int workload = rand.Next(0, 100); totalMs += workload; Console.WriteLine("Workload : {0}", workload); sender.SendFrame(workload.ToString()); Console.WriteLine("Total counts : {0}", taskNumber); } Console.WriteLine("Total expected cost : {0} msec", totalMs); Console.WriteLine("Press Enter to quit"); Console.ReadLine(); } } catch (Exception e) { Console.Write(e.Message); Console.ReadKey(); } }
private void FanOutEntry(object n) { FanoutRun f; string name = (string)n; if (FanoutRM.ContainsKey(name)) { f = FanoutRM[name]; } else { Logging.logger.Error("FanOutEntry get name failed " + name); return; } PushSocket p = new PushSocket(); string e = "tcp://" + f.point.ip + ":" + f.point.port; try { p.Bind(e); f.pushsock = p; } catch (Exception err) { Logging.logger.Error(ModName + " bind funin socket failed " + e + " " + err.Message); throw (err); } string str; f.Working = true; while (f.Running) { str = Entry4GetFanoutData(f.name); if (str != null) { p.SendFrame(str); } DelayTime(); } f.Working = false; p.Close(); return; }
public void SeparateControlSocketsObservedMessages() { using (var front = new RouterSocket()) using (var back = new DealerSocket()) using (var controlInPush = new PushSocket()) using (var controlInPull = new PullSocket()) using (var controlOutPush = new PushSocket()) using (var controlOutPull = new PullSocket()) { front.Bind("inproc://frontend"); back.Bind("inproc://backend"); controlInPush.Bind("inproc://controlIn"); controlInPull.Connect("inproc://controlIn"); controlOutPush.Bind("inproc://controlOut"); controlOutPull.Connect("inproc://controlOut"); var proxy = new Proxy(front, back, controlInPush, controlOutPush); Task.Factory.StartNew(proxy.Start); using (var client = new RequestSocket()) using (var server = new ResponseSocket()) { client.Connect("inproc://frontend"); server.Connect("inproc://backend"); client.SendFrame("hello"); Assert.AreEqual("hello", server.ReceiveFrameString()); server.SendFrame("reply"); Assert.AreEqual("reply", client.ReceiveFrameString()); } Assert.IsNotNull(controlInPull.ReceiveFrameBytes()); // receive identity Assert.IsEmpty(controlInPull.ReceiveFrameString()); // pull terminator Assert.AreEqual("hello", controlInPull.ReceiveFrameString()); Assert.IsNotNull(controlOutPull.ReceiveFrameBytes()); // receive identity Assert.IsEmpty(controlOutPull.ReceiveFrameString()); // pull terminator Assert.AreEqual("reply", controlOutPull.ReceiveFrameString()); proxy.Stop(); } }
static void Main(string[] args) { // Task Worker // Connects PULL socket to tcp://localhost:5557 // collects workload for socket from Ventilator via that socket // Connects PUSH socket to tcp://localhost:5558 // Sends results to Sink via that socket Console.WriteLine("====== WORKER ======"); using (var receiver = new PullSocket()) using (var sender = new PushSocket()) { receiver.Connect("tcp://localhost:5558"); sender.Bind("tcp://localhost:5559"); //process tasks forever int count = 0; while (true) { count++; //workload from the vetilator is a simple delay //to simulate some work being done, see //Ventilator.csproj Proram.cs for the workload sent //In real life some more meaningful work would be done string workload = receiver.ReceiveFrameString(); //simulate some work being done // Thread.Sleep(int.Parse(workload)); //send results to sink, sink just needs to know worker //is done, message content is not important, just the presence of //a message means worker is done. //See Sink.csproj Proram.cs Console.WriteLine("receive frame {1} Counts {0}", count, workload); sender.SendFrame(string.Empty); } } }
MyClass() { _pushSocket = new PushSocket(); _pushSocket.Bind("someaddress"); Task.Factory.StartNew(WorkerThread, TaskCreationOptions.LongRunning); }