/// <summary> /// 三经典模式:推拉模式(管道模式) /// 一个消息只能被消费一次 /// </summary> static void PushPull() { using (var pullSocket = new PullSocket()) using (var pushSocket = new PushSocket()) { var port = pullSocket.BindRandomPort($"{address}"); pushSocket.Connect($"{address}:" + port); pushSocket.SendMoreFrame("hello").SendFrame("hello world"); var s = pullSocket.ReceiveFrameString(); s += pullSocket.ReceiveFrameString(); Console.WriteLine(s);//hellohello world } }
public static void Main(string[] args) { using (var client = new PullSocket()) { client.Bind("tcp://127.0.0.1:5488"); while (true) { string a = client.ReceiveFrameString(); // To receive a flag from external programs to start the service if (bool.Parse(a)) { Console.WriteLine("START"); // Added Begin CancellationTokenSource source = new CancellationTokenSource(); CancellationToken token = source.Token; // Added End Thread t = new Thread(new ParameterizedThreadStart(ThreadProc)); //t.Start(args); t.Start(source); //CreateHostBuilder(args).Build().RunAsync(token).Wait(); // This works at the first start // ATTENTION! CreateHostBuilder(args).Build().Run() will return an error about args.Length < 0 CreateHostBuilder(args).Build().RunAsync(token); Console.WriteLine("DONE Start"); } } } }
public 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(">tcp://127.0.0.1:5557")) using (var sender = new PushSocket(">tcp://127.0.0.1:5558")) { //process tasks forever while (true) { //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("Sending to Sink"); sender.SendFrame(string.Empty); } } }
static void Main(string[] args) { Console.WriteLine("====== WORKER ======"); var sinkPort = ConfigurationManager.AppSettings.Get("sinkPort") ?? "8088"; var ventilatorPort = ConfigurationManager.AppSettings.Get("ventilatorPort") ?? "400"; using (var receiver = new PullSocket($">tcp://localhost:{sinkPort}")) using (var sender = new PushSocket($">tcp://localhost:{ventilatorPort}")) { while (true) { string workload = receiver.ReceiveFrameString(); // the protocoll is as follows: [0] -> lower, [1] -> upper, [2] -> height string[] workLoadArray = workload.Split(','); var calculator = new MandelbrotCalculator(); var result = calculator.Calculate(Convert.ToInt32(workLoadArray[2]), 400, Convert.ToInt32(workLoadArray[0]), Convert.ToInt32(workLoadArray[1])); byte[] data; BinaryFormatter binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, result); data = memoryStream.ToArray(); } Console.WriteLine("Sending"); sender.SendFrame(data); } } }
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("====== Back Queue STARTED ======"); using var fromWorkerForPosion = new PullSocket($"@{ThreadSafeFactory.BackPosionQueue}"); using var fromWorkerForUrl = new PullSocket($"@{ThreadSafeFactory.BackUrlQueue}"); using var forFrontForUrl = new PushSocket($"@{ThreadSafeFactory.BackQueue}"); Console.WriteLine("Socket initialized"); var fromWorkerProcessorForFrontQueue = Task.Run(() => { do { try { Console.WriteLine($"Waiting for worker link"); var queueItem = fromWorkerForUrl.ReceiveFrameString(); Console.WriteLine($"Received [{queueItem}] from worker"); Console.WriteLine($"Sending to front queue"); forFrontForUrl.SendFrame(queueItem); Console.WriteLine($"Sent to front queue"); } catch (Exception ex) { Console.WriteLine($"Error in Front Queue Processor {ex.Message}"); } } while (true); }); var fromWorkerProcessorForPoison = Task.Run(() => { do { Console.WriteLine($"Waiting for poison items"); var workload = fromWorkerForPosion.ReceiveFrameString(); UrlPosionQueueItem queueItem = workload.FromJson <UrlPosionQueueItem>(); _posionQueue.Enqueue(queueItem); Console.WriteLine($"Received poison [{queueItem.ToJson()}] from worker"); } while (true); }); var poisonQueueProcessor = Task.Run(() => { do { //while (_posionQueue.IsEmpty) //{ // Task.Delay(5000).Wait(); //} if (_posionQueue.TryDequeue(out UrlPosionQueueItem queueItem)) { Console.WriteLine($"Processing poison {queueItem.ToJson()}"); } // TODO: Persist all posion queue items with failure reasons in db } while (true); }); Task.WaitAll(fromWorkerProcessorForFrontQueue, /* fromWorkerProcessorForPoison,*/ poisonQueueProcessor); Console.WriteLine("====== FRONT QUEUE ======"); }
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(); } }
public void SimplePushPull() { using (var pullSocket = new PullSocket()) using (var pushSocket = new PushSocket()) { var port = pullSocket.BindRandomPort("tcp://127.0.0.1"); pushSocket.Connect("tcp://127.0.0.1:" + port); pushSocket.SendFrame("hello"); Assert.AreEqual("hello", pullSocket.ReceiveFrameString()); } }
public void EmptyMessage() { using (var pullSocket = new PullSocket()) using (var pushSocket = new PushSocket()) { var port = pullSocket.BindRandomPort("tcp://127.0.0.1"); pushSocket.Connect("tcp://127.0.0.1:" + port); pushSocket.SendFrame(new byte[300]); Assert.AreEqual(300, pullSocket.ReceiveFrameString().Length); } }
public void It_Should_Send_Message_To_Socket() { const string address = "tcp://127.0.0.1:12345"; using (var socket = new PullSocket()) using (AutoMock mock = AutoMock.GetLoose()) { mock.Provide <IOptions <MessageClientOptions> >(new OptionsWrapper <MessageClientOptions>(new MessageClientOptions { Address = address })); socket.Connect(address); var service = mock.Create <MessageClientImpl>(); service.ScheduleDelivery("A some message 1"); service.ScheduleDelivery("A some message 2"); service.ScheduleDelivery("A some message 3"); Assert.Equal("A some message 1", socket.ReceiveFrameString()); Assert.Equal("A some message 2", socket.ReceiveFrameString()); Assert.Equal("A some message 3", socket.ReceiveFrameString()); } }
static void Main(string[] args) { // Task Sink // Bindd PULL socket to tcp://localhost:5558 // Collects results from workers via that socket Console.WriteLine("====== SINK ======"); //socket to receive messages on using (var receiver = new PullSocket("@tcp://localhost:5558")) { //wait for start of batch (see Ventilator.csproj Program.cs) var startOfBatchTrigger = receiver.ReceiveFrameString(); Console.WriteLine("Seen start of batch"); //Start our clock now var watch = Stopwatch.StartNew(); for (int taskNumber = 0; taskNumber < 100; taskNumber++) { var workerDoneTrigger = receiver.ReceiveFrameString(); if (taskNumber % 10 == 0) { Console.Write(":"); } else { Console.Write("."); } } watch.Stop(); //Calculate and report duration of batch Console.WriteLine(); Console.WriteLine("Total elapsed time {0} msec", watch.ElapsedMilliseconds); Console.ReadLine(); } }
public override IObservable <string> Generate() { return(Observable.Create <string>((observer, cancellationToken) => { return Task.Factory.StartNew(() => { using (var receiver = new PullSocket(string.Format("@tcp://{0}:{1}", Address, Port))) { observer.OnNext(receiver.ReceiveFrameString()); while (!cancellationToken.IsCancellationRequested) { string msg = receiver.ReceiveFrameString(); if (msg != null) { observer.OnNext(msg); Console.WriteLine("From Client: {0}", msg); } } } }); }) .PublishReconnectable() .RefCount()); }
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 ======"); var mainServer = ConfigurationManager.AppSettings.Get("mainServer"); using (var receiver = new PullSocket(">tcp://localhost:80")) using (var sender = new PushSocket(">tcp://localhost:400")) { //process tasks forever while (true) { //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(); //0 ... lower, 1... upper... 2..height string[] workLoadArray = workload.Split(','); var calculator = new Calculator(); var result = calculator.Calculate(Convert.ToInt32(workLoadArray[0]), Convert.ToInt32(workLoadArray[1]), Convert.ToInt32(workLoadArray[2]), 400); byte[] dataGame; BinaryFormatter binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, result); dataGame = memoryStream.ToArray(); } Console.WriteLine("Sending to Sink"); sender.SendFrame(dataGame); } } }
void StartServer() { Debug.Log("Start Server!"); using (PullSocket pullSocket = new PullSocket()) using (PublisherSocket publisherSocket = new PublisherSocket()) { pullSocket.Bind("tcp://*:20002"); publisherSocket.Bind("tcp://*:20001"); while (true) { string str = pullSocket.ReceiveFrameString(); Debug.Log("[Server] Get! :: " + str); publisherSocket.SendFrame("Hello Client!"); Debug.Log("[Server] Send Hello Client!"); } } }
static void Main(string[] args) { using (var priceValidator = new PullSocket("@tcp://localhost:11000")) { while (true) { var message = priceValidator.ReceiveFrameString(); var productMessage = Newtonsoft.Json.JsonConvert.DeserializeObject <ValidatedProductMessage>(message); Console.WriteLine($"Product validated: {productMessage.Product.Name}"); Console.WriteLine($"Worker id: {productMessage.ProcessedBy}"); Console.WriteLine("Erros:"); foreach (var error in productMessage.Erros) { Console.WriteLine(error); } } } }
public void ContinousReceiveMessages() { var pullSocket = new PullSocket(); pullSocket.Bind("tcp://*:" + RemoteServGr.NodePort); while (RemoteServGr.Running) { var msgJson = pullSocket.ReceiveFrameString(); ReceiveMessage(msgJson); } var msg = new Message { MsgType = MessageType.EndingProgram, SendingServer = RemoteServGr.NodeAddress }; var messageWrapper = new RemoteDispatch(msg, null); RemoteServGr.SendQueue.Add(messageWrapper); pullSocket.Close(); }
static void Main(string[] args) { Console.WriteLine("====== WORKER ======"); using (var pull = new PullSocket()) using (var sink = new DealerSocket()) { pull.Connect("tcp://127.0.0.1:8888"); sink.Connect("tcp://127.0.0.1:7777"); while (true) { var workload = pull.ReceiveFrameString(); Thread.Sleep(int.Parse(workload)); Console.WriteLine("Sending to Sink"); sink.SendFrame(string.Empty); } } }
public static void ThreadProc(object obj) { //string[] args = ToStringArray(obj); var source = obj as CancellationTokenSource; using (var recv = new PullSocket()) { recv.Bind("tcp://127.0.0.1:5489"); while (true) { string temp = recv.ReceiveFrameString(); // To receive a flag from external programs to stop the service if (!bool.Parse(temp)) { Console.WriteLine("STOP"); source.Cancel(); source.Dispose(); //CreateHostBuilder(args).Build().StopAsync().Wait(); Console.WriteLine("DONE Stop"); break; // Jump out of the loop as soon as the service is stopped. } } } }
static void Main(string[] args) { var identity = Guid.NewGuid().ToString(); using (var receiver = new PullSocket(">tcp://localhost:10000")) using (var validationPush = new PushSocket(">tcp://localhost:11000")) { while (true) { var productTohavePriceValidated = receiver.ReceiveFrameString(); Console.WriteLine(productTohavePriceValidated); var product = Newtonsoft.Json.JsonConvert.DeserializeObject <Product>(productTohavePriceValidated); var validationMessages = ValidateProduct(product); validationMessages.AddRange(ValidatePrice(product)); var message = new ValidatedProductMessage(product, identity, validationMessages.ToArray()); validationPush.SendFrame(Newtonsoft.Json.JsonConvert.SerializeObject(message)); } } }
public static void Main(string[] args) { Console.WriteLine("====== WORKER ======"); using (var receiver = new PullSocket(">tcp://localhost:80")) using (var sender = new PushSocket(">tcp://localhost:400")) { //process tasks forever while (true) { string workload = receiver.ReceiveFrameString(); Console.WriteLine("got"); string[] values = workload.Split(','); double xReminder = -2 + ((double)Int32.Parse(values[0]) * 0.013); MandlBrotCalcer calcer = new MandlBrotCalcer(); List <(int, int, int)> list = calcer.CalcPart(Int32.Parse(values[0]), Int32.Parse(values[1]), 0, Int32.Parse(values[2]), 18, 4, xReminder, 1.2, 0.013); BinaryFormatter binaryFormatter = new BinaryFormatter(); byte[] listBytes; using (var memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, list); listBytes = memoryStream.ToArray(); } sender.SendFrame(listBytes); Console.WriteLine("Sending to Sink"); sender.SendFrame(string.Empty); } } }
static void Main(string[] seed) { Console.WriteLine("====== FRONT QUEUE ======"); using var toWorkers = new PushSocket($"@{ThreadSafeFactory.FrontQueue}"); using var fromBackQueue = new PullSocket($">{ThreadSafeFactory.BackQueue}"); Console.WriteLine("Socket initialized"); foreach (var s in seed) { _urlQueue.Enqueue(new UrlQueueItem { Url = s }); } var backQueueProcessor = Task.Run(() => { // Receive from backqueue while (true) { Console.WriteLine($"Waiting for backqueue"); var workload = fromBackQueue.ReceiveFrameString(); Console.WriteLine($"Got message from backqueue"); UrlQueueItem queueItem = new UrlQueueItem { Url = workload }; if (!_urlVisiting.ContainsKey(queueItem.Url)) { _urlQueue.Enqueue(queueItem); } else { Console.WriteLine($"Url {queueItem.Url} already sent to processing"); } } }); var frontQueueProcessor = Task.Run(() => { // send to worker while (true) { do { Parallel.For(0, maxThread, (threadIndex) => { //while (_urlQueue.IsEmpty) //{ // Task.Delay(5000).Wait(); //} if (_urlQueue.TryDequeue(out UrlQueueItem url)) { if (_politenessPolicy.CanIGoThere(url, out long crawlDelay)) { Task.Delay((int)crawlDelay).Wait(); url.QueuedOn = DateTime.UtcNow; Console.WriteLine($"Sending to worker [{url.Url}]"); toWorkers.SendFrame(url.ToJson()); Console.WriteLine($"Sent to worker"); _urlVisiting.AddOrUpdate(url.Url.ToLower(), false, (key, val) => false); } } }); } while (!_urlQueue.IsEmpty); } });
static void Main(string[] args) { using (var receiver = new PullSocket(">tcp://localhost:5557")) using (var sender = new PushSocket(">tcp://localhost:5558")) using (Model.ticketClassesDataContext = new TicketClassesDataContext("Server=(localdb)\\ProjectsV13;Database=TicketsDatabase;")) { Model.ticketClassesDataContext.Connection.Open(); var result = Model.ticketClassesDataContext.ExecuteQuery <Station>( @"SELECT * FROM dbo.Station" ); foreach (var r in result) { ; } while (true) { Console.WriteLine("waiting"); var message = receiver.ReceiveFrameString(); string[] vs = message.Split(';'); Console.WriteLine("Received {0}", message); byte[] answer = { }; switch (vs[0]) { case "GetDistance": answer = Controller.GetDistance(vs); break; case "GetAllStationNames": answer = Controller.GetAllStationNames(vs); break; case "CheckIfLoginAvailable": answer = Controller.GetUserByLogin(vs); break; case "AddUser": answer = Controller.AddUser(vs); break; case "Authenticate": answer = Controller.Authenticate(vs); break; case "BuyTicket": answer = Controller.BuyTicket(vs); break; case "GetTicketsByUser": answer = Controller.GetTicketsByUser(vs); break; } Thread.Sleep(1000); //Send an answer //Console.WriteLine("Sending: " + Encoding.ASCII.GetString(answer)); sender.SendFrame(answer); } } }
static void Main(string[] args) { const int POISON_QUEUE_MAX_TRY = 5; Console.WriteLine("====== WORKER ======"); using var fromFrontQueue = new PullSocket($">{ThreadSafeFactory.FrontQueue}"); using var toBackQueue = new PushSocket($">{ThreadSafeFactory.BackUrlQueue}"); using var toPoisonQueue = new PushSocket($">{ThreadSafeFactory.BackPosionQueue}"); Console.WriteLine("Initialized Socket"); //process tasks forever var frontQueueProcessor = Task.Run(() => { while (true) { try { Console.WriteLine($"Waiting for front queue"); var workload = fromFrontQueue.ReceiveFrameString(); UrlQueueItem queueItem = workload.FromJson <UrlQueueItem>(); Console.WriteLine($"Request from frontqueue {queueItem.Url}"); _urlQueue.Enqueue(queueItem); } catch (Exception ex) { Console.WriteLine($"Error in Front Queue Processor {ex.Message}"); } } }); var processUrlTask = Task.Run(() => { while (true) { Parallel.For(0, 2 * maxThread / 3, (threadIndex) => { //while (_urlQueue.IsEmpty) //{ // Task.Delay(5000).Wait(); //} if (_urlQueue.TryDequeue(out UrlQueueItem url)) { Console.WriteLine($"Processing {url.Url}"); url.CrawlStart = DateTime.UtcNow; try { foreach (var childUrl in _htmlRenderer.FindChildLinks(url)) { if (!_visitedUrls.ContainsKey(childUrl)) { _visitedUrls.TryAdd(childUrl, false); Console.WriteLine($"Sending to backqueue"); toBackQueue.SendFrame(childUrl); Console.WriteLine($"Sending to backqueue"); } } } catch (Exception ex) { var u = url.Clone() as UrlPosionQueueItem; u.TryCount = 0; u.CrawlFinished = DateTime.UtcNow; u.Errors = new List <string> { ex.Message + "|" + ex?.InnerException?.Message }; _urlPoisonQueue.Enqueue(u); Console.WriteLine($"Error in Url Processor {ex.Message}"); } url.CrawlFinished = DateTime.UtcNow; } }); }