Esempio n. 1
0
        private void HandleWork()
        {
            using (var stateUpdate = new SubscriberSocket())
            {
                stateUpdate.SubscribeToAnyTopic();
                stateUpdate.Bind(_configuration.ToPublisherEndpoint);

                using (var stateUpdatePublish = new PublisherSocket())
                {
                    stateUpdatePublish.Bind(_configuration.ToSubscribersEndpoint);

                    stateUpdate.ReceiveReady += async(s, e) =>
                    {
                        var message = e.Socket.ReceiveMultipartMessage();

                        var subject = message[0].ConvertToString();
                        var payload = message[1];

                        var eventId = await _cache.AppendToStream(subject, payload.Buffer);

                        stateUpdatePublish.SendMoreFrame(message[0].Buffer)
                        .SendMoreFrame(_serializer.Serialize(eventId))
                        .SendFrame(payload.Buffer);
                    };

                    using (_poller = new NetMQPoller {
                        stateUpdate, stateUpdatePublish
                    })
                    {
                        _poller.Run();
                    }
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            using (var pubSocket = new PublisherSocket())

            {
                using (var subSocket = new SubscriberSocket())
                {
                    pubSocket.Bind("tcp://127.0.0.1:55555");

                    var msg = new BlockedOrderCreated();

                    //  pubSocket.SendMoreFrame("BlockedOrderTopic").SendFrame(msg.content);

                    Thread.Sleep(500);

                    subSocket.Connect("tcp://127.0.0.1:55555");
                    Thread.Sleep(500);
                    subSocket.Subscribe("BlockedOrderTopic");
                    pubSocket.SendMoreFrame("BlockedOrderTopic").SendFrame(msg.content);
                    while (true)
                    {
                        Thread.Sleep(500);
                        var messageReceived = subSocket.ReceiveFrameString();
                        Thread.Sleep(500);
                        Debug.WriteLine(messageReceived);
                    }
                }
            }
        }
Esempio n. 3
0
        private PublisherSocket GetNewPublisherSocket(string addressZeroMq)
        {
            PublisherSocket publisherSocket;

            {
                _loggerDelegate?.Invoke(string.Format("Publisher socket binding to: {0}\n", addressZeroMq));

                publisherSocket = new PublisherSocket();

                // Corner case: wait until publisher socket is ready (see code below that waits for
                // "_publisherReadySignal").
                NetMQMonitor monitor;
                {
                    // Must ensure that we have a unique monitor name for every instance of this class.
                    string endPoint = string.Format("inproc://#SubjectNetMQ#Publisher#{0}#{1}", addressZeroMq, Guid.NewGuid().ToString());
                    monitor = new NetMQMonitor(publisherSocket,
                                               endPoint,
                                               SocketEvents.Accepted | SocketEvents.Listening
                                               );
                    monitor.Accepted  += Publisher_Event_Accepted;
                    monitor.Listening += Publisher_Event_Listening;
                    monitor.StartAsync();
                }

                publisherSocket.Options.SendHighWatermark = this.HighwaterMark;
                try
                {
                    publisherSocket.Bind(addressZeroMq);
                }
                catch (NetMQException ex)
                {
                    // This is usually because the address is in use.
                    throw new Exception(string.Format("Error E56874. Cannot bind publisher to '{0}'. 95% probability that this is caused by trying to bind a publisher to a port already in use by another process. To fix, choose a unique publisher port for this process. For more on this error, see 'Readme.md' (or the GitHub homepage for NetMQ.ReactiveExtensions).", addressZeroMq), ex);
                }

                // Corner case: wait until publisher socket is ready (see code below that sets "_publisherReadySignal").
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    _publisherReadySignal.WaitOne(TimeSpan.FromMilliseconds(3000));
                    _loggerDelegate?.Invoke(string.Format("Publisher: Waited {0} ms for binding.\n", sw.ElapsedMilliseconds));
                }
                {
                    monitor.Accepted  -= Publisher_Event_Accepted;
                    monitor.Listening -= Publisher_Event_Listening;
                    // Current issue with NegMQ: Cannot stop or dispose monitor, or else it stops the parent socket.
                    //monitor.Stop();
                    //monitor.Dispose();
                }
            }

            // Otherwise, the first item we publish may get missed by the subscriber. 500 milliseconds consistently works
            // locally, but occasionally fails on the AppVeyor build server. 650 milliseconds is optimal.
            using (EventWaitHandle wait = new ManualResetEvent(false))
            {
                // Cannot use Thread.Sleep() here, as this is incompatible with .NET Core 1.0, Windows 8.0, 8.1, and 10.
                wait.WaitOne(TimeSpan.FromMilliseconds(650));
            }

            return(publisherSocket);
        }
Esempio n. 4
0
        public void Run()
        {
            Task.Run(async() =>
            {
                using (var pubSocket = new PublisherSocket())
                {
                    Console.WriteLine("Publisher socket binding...");
                    pubSocket.Options.SendHighWatermark = 1000;
                    pubSocket.Bind("tcp://localhost:12345");

                    while (true)
                    {
                        var stories = await FetchBestStories();

                        if (stories != null)
                        {
                            Console.WriteLine($"Sending {stories.Count} stories");
                            pubSocket.SendMoreFrame("Stories").SendFrame(JsonConvert.SerializeObject(stories));
                        }

                        await Task.Delay(IntervalSecs * 1000);
                    }
                }
            });
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Random rand = new Random(50);


            using (var pubSocket = new PublisherSocket())
            {
                Console.WriteLine("Publisher socket binding...");
                pubSocket.Options.SendHighWatermark = 100;
                pubSocket.Bind("tcp://localhost:12345");
                Thread.Sleep(200);
                pubSocket.SendMoreFrame("").SendFrame("1231231231313123131231");
                //for (var i = 0; i < 300; i++)
                //{
                //    var randomizedTopic = rand.NextDouble();
                //    if (randomizedTopic > 0.5)
                //    {
                //        var msg = "TopicA msg-" + i;
                //        Console.WriteLine("Sending message : {0}", msg);
                //        pubSocket.SendMoreFrame("TopicA").SendFrame(msg);
                //    }
                //    else
                //    {
                //        var msg = "TopicB msg-" + i;
                //        Console.WriteLine("Sending message : {0}", msg);
                //        pubSocket.SendMoreFrame("TopicB").SendFrame(msg);
                //    }

                //    Thread.Sleep(100);
                //}
            }
        }
Esempio n. 6
0
        public void Start()
        {
            if (context == null)
            {
                context = NetMQContext.Create();

                publisher = context.CreatePublisherSocket();
                publisher.Bind("tcp://*:5252");

                receiver = context.CreatePullSocket();
                receiver.Bind("tcp://*:5253");

                // Start listening..
                Task.Run(() =>
                {
                    try
                    {
                        while (!tokenSource.IsCancellationRequested)
                        {
                            var message = receiver.ReceiveString();
                            var command = Serialization.DeserializeEnvelope <ContractMarker>(message);
                            commandReceivedSubject.OnNext(command);
                        }
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex);
                        throw;
                    }
                });
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var weathers = new string[6] {
                "晴朗", "多云", "阴天", "霾", "雨", "雪"
            };

            Console.WriteLine("发布多个地区天气预报:");

            using (var publisher = new PublisherSocket())
            {
                publisher.Bind("tcp://127.0.0.1:8888");

                var rng = new Random();

                while (_terminateEvent.WaitOne(1000) == false)
                {
                    var zipcode     = rng.Next(0, 10);
                    var temperature = rng.Next(-50, 50);
                    var weatherId   = rng.Next(0, 5);

                    var msg = $"{zipcode} {temperature} {weathers[weatherId]}";
                    publisher.SendFrame(msg);

                    Console.WriteLine(msg);
                    Thread.Sleep(1000);
                }
            }
        }
Esempio n. 8
0
        public Kernel(ConnectionInformation connectionInformation)
        {
            this.connectionInformation = connectionInformation;

            // heartbeat
            hbSocket = new RouterSocket();
            hbSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.hb_port}");

            // control
            controlSocket = new RouterSocket();
            controlSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.control_port}");

            // stdin
            stdinSocket = new RouterSocket();
            stdinSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.stdin_port}");

            // iopub
            ioSocket = new PublisherSocket();
            ioSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.iopub_port}");

            // shell
            shellSocket = new RouterSocket();
            shellSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.shell_port}");

            payload        = new List <Payload>();
            nugetManager   = new NuGetManager(new FileInfo(".").FullName);
            executionCount = 0;
            lastMessage    = null;

            headerCode = getHeaderCode();
            hmac       = new HMACSHA256(Encoding.UTF8.GetBytes(connectionInformation.key));
        }
Esempio n. 9
0
        private static void Main()
        {
            Console.Title = "NetMQ Weather Update Server";

            bool stopRequested = false;

            // Wire up the CTRL+C handler
            Console.CancelKeyPress += (sender, e) => stopRequested = true;

            Console.WriteLine("Publishing weather updates...");

            using (var publisher = new PublisherSocket())
            {
                publisher.Bind("tcp://127.0.0.1:5556");

                var rng = new Random();

                while (!stopRequested)
                {
                    int zipcode = rng.Next(0, 99999);
                    int temperature = rng.Next(-80, 135);
                    int relhumidity = rng.Next(0, 90);

                    publisher.SendFrame(string.Format("{0} {1} {2}", zipcode, temperature, relhumidity));
                }
            }
        }
Esempio n. 10
0
 public ZeroMqPublisher(ILogFactory logFactory, string address)
 {
     _logger    = logFactory.CreateLog(this);
     _pubSocket = new PublisherSocket();
     _pubSocket.Options.SendHighWatermark = 1000;
     _pubSocket.Bind(address);
 }
            public void RunPipeline(PairSocket shim)
            {
                publisherSocket = context.CreatePublisherSocket();
                publisherSocket.Bind("tcp://*:" + StreamingProtocol.Port);

                snapshotSocket = context.CreateResponseSocket();
                snapshotSocket.Bind("tcp://*:" + SnapshotProtocol.Port);
                snapshotSocket.ReceiveReady += OnSnapshotReady;

                shim.ReceiveReady += OnShimReady;

                heartbeatTimer          = new NetMQTimer(StreamingProtocol.HeartbeatInterval);
                heartbeatTimer.Elapsed += OnHeartbeatTimerElapsed;

                shim.SignalOK();

                poller = new Poller();
                poller.AddSocket(shim);
                poller.AddSocket(snapshotSocket);
                poller.AddTimer(heartbeatTimer);
                poller.Start();

                publisherSocket.Dispose();
                snapshotSocket.Dispose();
            }
Esempio n. 12
0
        public void ThreadMQPublishServer()
        {
            try
            {
                using (PublisherSocket publishSocket = new PublisherSocket())
                {
                    publishSocket.Bind(MQConfig.PublishServer);

                    try
                    {
                        while (!_cancellationTokenSource.IsCancellationRequested)
                        {
                            KeyData item;
                            if (MQPublishQueue.TryTake(out item))
                            {
                                publishSocket.PublisherSend(item);
                            }
                        }
                    }
                    catch (TerminatingException)
                    {
                    }
                    catch (Exception ex)
                    {
                        MQThreadExceptionHandler?.Invoke(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                MQThreadExceptionHandler?.Invoke(ex);
            }
        }
Esempio n. 13
0
 private void PreparePublishing()
 {
     AsyncIO.ForceDotNet.Force();
     pubSocket = new PublisherSocket();
     pubSocket.Options.SendHighWatermark = 100;
     pubSocket.Bind("tcp://localhost:12344");
 }
Esempio n. 14
0
 public Publisher(string address)
 {
     _context = NetMQContext.Create();
     _socket  = _context.CreatePublisherSocket();
     _socket.Bind(address);
     Console.WriteLine("Publisher: bound to {0}", address);
 }
Esempio n. 15
0
        /// <summary>
        /// Subscribe To Start Auction
        /// </summary>
        public void subscribeToStartAuctionCmd()
        {
            // Bind the publisher to the address
            _publisher = _context.CreatePublisherSocket();
            _publisher.Bind(ConfigurationManager.AppSettings["pubAddr"]);

            // Connect to address for StartAuction and subscribe to the topic - StartAuction
            var startAuctionSub   = _context.CreateSubscriberSocket();
            var startAuctionTopic = ConfigurationManager.AppSettings["startAuctionTopic"];

            startAuctionSub.Connect(ConfigurationManager.AppSettings["startAuctionAddr"]);
            startAuctionSub.Subscribe(startAuctionTopic);
            Console.WriteLine("SUB: " + startAuctionTopic);

            while (true)
            {
                string startAuctionCmd = startAuctionSub.ReceiveString();
                Console.WriteLine("REC: " + startAuctionCmd);
                publishAcknowledgement(startAuctionCmd);
                // Extract the ID and get the bidders emails
                string    id       = MessageParser.parseMessage(startAuctionCmd, "<id>", "</id>");
                IDatabase database = DatabaseFacade.GetDatabase();
                string[]  emails   = database.getBidderEmails(id);

                if (emails != null)
                {
                    publishNotifyBiddersCommand(id, emails);
                }
                publishAuctionStartedEvent(id);
            }
        }
        public void PubSub_Should_NotCrash_IfNo_Thread_Sleep()
        {
            using (var pub = new PublisherSocket())
            {
                using (var sub = new SubscriberSocket())
                {
                    int freePort = TcpPortFree();
                    pub.Bind("tcp://127.0.0.1:" + freePort);
                    sub.Connect("tcp://127.0.0.1:" + freePort);

                    sub.Subscribe("*");

                    Stopwatch sw = Stopwatch.StartNew();
                    {
                        for (int i = 0; i < 50; i++)
                        {
                            pub.SendFrame("*");                             // Ping.

                            Console.Write("*");
                            string topic;
                            var    gotTopic = sub.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out topic);
                            string ping;
                            var    gotPing = sub.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out ping);
                            if (gotTopic == true)
                            {
                                Console.Write("\n");
                                break;
                            }
                        }
                    }
                    Console.WriteLine("Connected in {0} ms.", sw.ElapsedMilliseconds);
                }
            }
        }
        private void Produce()
        {
            var eventSerializer = new EventSerializer();

            _publisherSocket = new PublisherSocket();

            _publisherSocket.Options.SendHighWatermark = 1000;

            _publisherSocket.Bind(_producerConfiguration.Endpoint);

            while (!_cancel.IsCancellationRequested)
            {
                var next = Next();

                var topic = eventSerializer.Serialize(next);

                var message = new TransportMessage()
                {
                    MessageType = next.GetType(),
                    MessageId   = Guid.NewGuid(),
                    Message     = Encoding.UTF32.GetBytes(JsonConvert.SerializeObject(next, _settings)),
                };

                var msg = Encoding.UTF32.GetBytes(JsonConvert.SerializeObject(message, _settings));

                _publisherSocket.SendMoreFrame(topic).SendFrame(msg);

                Task.Delay(_rand.Next(250, 500)).Wait();
            }
        }
Esempio n. 18
0
        private static void StartShellLoop(string shellAddress, string ioPubAddress)
        {
            ShellRouterSocket.Bind(shellAddress);
            Log.Info($"Bound shell server to address {shellAddress}");

            ShellPublisherSocket.Bind(ioPubAddress);
            Log.Info($"Bound IO pub to address {ioPubAddress}");

            while (!StopEvent.Wait(0))
            {
                var message = GetNextMessage();

                Log.Info($"Received message {JsonSerializer.Serialize(message)}");

                if (MessageHandlers.TryGetValue(message.Header.MessageType, out IShellMessageHandler handler))
                {
                    Log.Info($"Sending message to {message.Header.MessageType} handler");
                    handler.HandleMessage(message, ShellRouterSocket, ShellPublisherSocket);
                    Log.Info("Message handling complete");
                }
                else
                {
                    Log.Error($"No message handler found for message type {message.Header.MessageType}");
                }
            }
        }
Esempio n. 19
0
        private void Run()
        {
            using var publisher = new PublisherSocket();

            publisher.Bind(@"tcp://127.0.0.1:17232");

            using var subscriber = new SubscriberSocket();
            subscriber.Bind(@"tcp://127.0.0.1:17233");
            subscriber.SubscribeToAnyTopic();

            running.Set();

            using var server = new TelnetServer(IPAddress.Any, port, publisher, subscriber)
                  {
                      OptionReuseAddress = true
                  };

            Log.Information("Server starting ...");

            server.Start();

            while (running.WaitOne(TimeSpan.Zero))
            {
                if (subscriber.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(100), out var frame))
                {
                    server.Multicast(frame);
                }
            }

            server.Stop();

            Log.Information("Server stopped.");
        }
Esempio n. 20
0
        public bool SendMessage()
        {
            var netMqTimer = new NetMQTimer(100);
            var timer      = new Timer(180000);

            timer.Start();
            timer.Elapsed += (sender, args) =>
            {
                this.Cancel = true;

                timer.Stop();
            };

            using (var publisher = new PublisherSocket())
            {
                publisher.Bind("tcp://*:3245");
                Thread.Sleep(3000);
                var rng = new Random();

                while (!Cancel)
                {
                    var zipcode     = 10001;//rng.Next(0, 99999);
                    var temperature = rng.Next(-80, 135);
                    var humidity    = rng.Next(0, 90);

                    publisher.SendFrame($"{zipcode} {temperature} {humidity}");
                }
            }

            return(true);
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            Random rand = new Random(50);


            using (var pubSocket = new PublisherSocket())
            {
                Console.WriteLine("Publisher socket binding...");
                pubSocket.Options.SendHighWatermark = 100;
                pubSocket.Bind("tcp://localhost:12345");
                Thread.Sleep(200);
                pubSocket.SendMoreFrame("").SendFrame("1231231231313123131231");
                //for (var i = 0; i < 300; i++)
                //{
                //    var randomizedTopic = rand.NextDouble();
                //    if (randomizedTopic > 0.5)
                //    {
                //        var msg = "TopicA msg-" + i;
                //        Console.WriteLine("Sending message : {0}", msg);
                //        pubSocket.SendMoreFrame("TopicA").SendFrame(msg);
                //    }
                //    else
                //    {
                //        var msg = "TopicB msg-" + i;
                //        Console.WriteLine("Sending message : {0}", msg);
                //        pubSocket.SendMoreFrame("TopicB").SendFrame(msg);
                //    }

                //    Thread.Sleep(100);
                //}
            }
        }
Esempio n. 22
0
 public void PubSub()
 {
     ThreadPool.QueueUserWorkItem(state => {
         var server = new PublisherSocket();
         server.Bind("tcp://*:5557");
         while (!_stopLoops)
         {
             try {
                 var dataFacadeEvent = new DataFacadeEvent {
                     State = new List <User> {
                         new User {
                             Id = 666
                         }
                     }
                 };
                 using (var responseStream = new MemoryStream()) {
                     Serializer.Serialize(responseStream, dataFacadeEvent);
                     server.SendFrame(responseStream.ToArray());
                 }
                 Thread.Sleep(5000);
             } catch (Exception e) {
                 Console.WriteLine(e);
             }
         }
     });
 }
Esempio n. 23
0
        public void Start(int feederPort)
        {
            Random rand = new Random(50);

            _feederSocket = new PublisherSocket();

            _feederSocket.Options.SendHighWatermark = 1000;
            _feederSocket.Bind($"tcp://*:{feederPort}");

            /*
             * for (var i = 0; i < 100; i++)
             * {
             *  var randomizedTopic = rand.NextDouble();
             *  if (randomizedTopic > 0.5)
             *  {
             *      var msg = "TopicA msg-" + i;
             *      Console.WriteLine("Sending message : {0}", msg);
             *      _feederSocket.SendMoreFrame("TopicA").SendFrame(msg);
             *  }
             *  else
             *  {
             *      var msg = "TopicB msg-" + i;
             *      Console.WriteLine("Sending message : {0}", msg);
             *      _feederSocket.SendMoreFrame("TopicB").SendFrame(msg);
             *  }
             *  Thread.Sleep(500);
             *
             * }
             */
        }
Esempio n. 24
0
        public async Task ShouldSubscribeToSpecificSubject()
        {
            using (var publisherSocket = new PublisherSocket())
            {
                publisherSocket.Bind(ToSubscribersEndpoint);

                var createEvent = new Func <string, string, Task>(async(streamId, market) =>
                {
                    var @event  = new ChangeCcyPairPrice(streamId, market, 0.0, 0.0, 0.0, 0.0);
                    var message = _eventSerializer.ToProducerMessage(@event);

                    var eventId = new EventId(streamId, 0, string.IsNullOrEmpty(market) ? streamId : $"{streamId}.{market}", DateTime.Now.Ticks);

                    publisherSocket.SendMoreFrame(message.Subject)
                    .SendMoreFrame(_serializer.Serialize(eventId))
                    .SendFrame(_serializer.Serialize(message));

                    await Task.Delay(500);
                });

                var subscribedToStreamId = "EUR/USD";
                var subscribedToMarket   = "Harmony";

                var NOTsubscribedToStreamId = "EUR/GBP";
                var NOTsubscribedToMarket   = "FxConnect";

                var cacheConfiguration = new DynamicCacheConfiguration(ToSubscribersEndpoint, StateOfTheWorldEndpoint, HeartbeatEndpoint)
                {
                    Subject          = $"{subscribedToStreamId}.{subscribedToMarket}",
                    HeartbeatDelay   = TimeSpan.FromSeconds(1),
                    HeartbeatTimeout = TimeSpan.FromSeconds(1),
                };

                var cache = new DynamicCache <string, CurrencyPair>(cacheConfiguration, LoggerForTests <DynamicCache <string, CurrencyPair> > .Default(), _eventSerializer);

                await cache.Run();

                await Task.Delay(1000);

                await createEvent(NOTsubscribedToStreamId, NOTsubscribedToMarket);

                Assert.AreEqual(0, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count());

                await createEvent(NOTsubscribedToStreamId, subscribedToMarket);

                Assert.AreEqual(0, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count());

                await createEvent(subscribedToStreamId, NOTsubscribedToMarket);

                Assert.AreEqual(0, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count());

                await createEvent(subscribedToStreamId, subscribedToMarket);

                Assert.AreEqual(1, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count());

                await createEvent(subscribedToStreamId, string.Empty);

                Assert.AreEqual(1, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count());
            }
        }
Esempio n. 25
0
        private static void PublisherSocket()
        {
            Random rand = new Random(50);

            using var pubSocket = new PublisherSocket();
            Console.WriteLine("Publisher socket binding...");
            pubSocket.Options.SendHighWatermark = 1000;
            pubSocket.Bind("tcp://*:12345");
            for (var i = 0; i < 100; i++)
            {
                var randomizedTopic = rand.NextDouble();
                if (randomizedTopic > 0.5)
                {
                    var msg = "TopicA msg-" + i;
                    Console.WriteLine("Sending message : {0}", msg);
                    pubSocket.SendMoreFrame("TopicA").SendFrame(msg);
                }
                else
                {
                    var msg = "TopicB msg-" + i;
                    Console.WriteLine("Sending message : {0}", msg);
                    pubSocket.SendMoreFrame("TopicB").SendFrame(msg);
                }
                Thread.Sleep(500);
            }
        }
Esempio n. 26
0
        public static void Publish()
        {
            using (var pub = new PublisherSocket())
            {
                bool usePlain = true;

                if (usePlain)
                {
                    pub.Options.PlainUsername = "******";
                    pub.Options.PlainPassword = "******";
                    pub.Options.PlainServer   = true;
                }
                pub.Bind("tcp://192.168.0.193:6021");
                int i = 0;
                while (true)
                {
                    Thread.Sleep(500);
                    pub.SendMoreFrame("A").SendFrame("Hello - " + i++);
                    Console.WriteLine("SENDING MESSAGE on port 6021");
                }
                Console.WriteLine();
                Console.Write("Press any key to exit...");
                Console.ReadKey();
            }
        }
Esempio n. 27
0
        private static void Main()
        {
            Console.Title = "NetMQ Weather Update Server";

            bool stopRequested = false;

            // Wire up the CTRL+C handler
            Console.CancelKeyPress += (sender, e) => stopRequested = true;

            Console.WriteLine("Publishing weather updates...");

            using (var publisher = new PublisherSocket())
            {
                publisher.Bind("tcp://127.0.0.1:5556");

                var rng = new Random();

                while (!stopRequested)
                {
                    int zipcode     = rng.Next(0, 99999);
                    int temperature = rng.Next(-80, 135);
                    int relhumidity = rng.Next(0, 90);

                    publisher.SendFrame($"{zipcode} {temperature} {relhumidity}");
                }
            }
        }
Esempio n. 28
0
        public static PublisherSocket CreatePublisherSocket(ushort port)
        {
            var publisher = new PublisherSocket();

            publisher.Bind($"tcp://*:{port}");

            return(publisher);
        }
Esempio n. 29
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            SetupDefaultMimeTypes();

            _shell.Bind(_shellAddress);
            _ioPubSocket.Bind(_ioPubAddress);
            _stdIn.Bind(_stdInAddress);
            _control.Bind(_controlAddress);
            var kernelIdentity = Guid.NewGuid().ToString();

            Task.Run(async() =>
            {
                using var activity = Log.OnEnterAndExit();
                while (!cancellationToken.IsCancellationRequested)
                {
                    var request = _shell.GetMessage();

                    activity.Info("Received: {message}", request.ToJson());

                    SetBusy(request);

                    switch (request.Header.MessageType)
                    {
                    case JupyterMessageContentTypes.KernelInfoRequest:
                        kernelIdentity = Encoding.Unicode.GetString(request.Identifiers[0].ToArray());
                        HandleKernelInfoRequest(request);
                        SetIdle(request);
                        break;

                    case JupyterMessageContentTypes.KernelShutdownRequest:
                        SetIdle(request);
                        break;

                    default:
                        var context = new JupyterRequestContext(
                            _shellChannel,
                            _ioPubChannel,
                            _stdInChannel,
                            request,
                            kernelIdentity);

                        await _scheduler.Schedule(context);

                        await context.Done();

                        SetIdle(request);

                        break;
                    }
                }
            }, cancellationToken);

            void SetBusy(ZeroMQMessage request) => _ioPubChannel.Publish(new Status(StatusValues.Busy), request, kernelIdentity);
            void SetIdle(ZeroMQMessage request) => _ioPubChannel.Publish(new Status(StatusValues.Idle), request, kernelIdentity);

            return(Task.CompletedTask);
        }
Esempio n. 30
0
        internal NetMQSink(string bindingAddress, IFormatProvider formatProvider, bool callNetMqCleanup)
        {
            publisherSocket = new PublisherSocket();
            publisherSocket.Bind(bindingAddress);
            fmtProvider  = formatProvider;
            cleanUpNetMq = callNetMqCleanup;

            publisherSocket.Options.Linger = new TimeSpan(0, 0, 5);
        }
Esempio n. 31
0
        public void Start(int publishPort)
        {
            ctx           = NetMQContext.Create();
            publishSocket = ctx.CreatePublisherSocket();

            publishSocket.Bind("tcp://*:" + publishPort);

            logger.Info("Message publisher started on port " + publishPort);
        }
        public void Start(int publishPort)
        {
            ctx = NetMQContext.Create();
            publishSocket = ctx.CreatePublisherSocket();

            publishSocket.Bind("tcp://*:" + publishPort);

            logger.Info("Message publisher started on port " + publishPort);
        }
Esempio n. 33
0
        /// <summary>
        /// Add <see cref="IAddress"/> to the socket
        /// </summary>
        /// <param name="address"><see cref="IAddress"/> to be added</param>
        public override void SocketAdd(IAddress address)
        {
            if (disposedValue)
            {
                throw new InvalidOperationException("NetMQPublisher has been disposed");
            }

            socket.Bind(address.ToString());
        }
Esempio n. 34
0
 public void InsertNetMQ(string didiStr)
 {
         using (var pubSocket = new PublisherSocket())
     {
         pubSocket.Options.SendHighWatermark = 10000;
         pubSocket.Bind("tcp://localhost:12345");
         Thread.Sleep(500);
         pubSocket.SendMoreFrame("").SendFrame(didiStr);
     }
 }
        public ReactiveServer()
        {
            var context = NetMQContext.Create();
            repSocket = context.CreateResponseSocket();
            repSocket.Bind("tcp://127.0.0.1:3334");
            repSocket.ReceiveReady += RepSocketOnReceiveReady;
            var repThread = new Thread(RepThread);
            repThread.Start();

            pubSocket = context.CreatePublisherSocket();
            pubSocket.Bind("tcp://127.0.0.1:3333");
        }
Esempio n. 36
0
 static void Main(string[] args)
 {
     Console.Title = "NetMQ LazyPirate Client 2";
     const string serverAddress = "tcp://127.0.0.1:5555";
     const string requestString = "Hi";
     var requestTimeout = TimeSpan.FromMilliseconds(2500);
     var requestRetries = 10;
     var requestMessage = new NetMQMessage(1);
     requestMessage.Append("Hi");
     using (var progressPublisher = new PublisherSocket())
     {
         const string pubSubAddress = "tcp://127.0.0.1:5556";
         progressPublisher.Bind(pubSubAddress);
         SubscriberContinuousLoop(pubSubAddress, requestString);
         while (true)
         {
             var responseString = RequestSocket.RequestResponseMultipartMessageWithRetry(serverAddress, requestMessage, 
                 requestRetries, requestTimeout, progressPublisher);
         }
     }
 }
Esempio n. 37
0
        private void SenderWorker()
        {
            _mqContext = NetMQContext.Create();
            _publisherSocket = _mqContext.CreatePublisherSocket();

            if (Protocol == "tcp")
                _publisherSocket.Connect(_address);

            if (Protocol == "pgm") //Multicast
            {
                _publisherSocket.Options.MulticastHops = 4;
                _publisherSocket.Options.MulticastRate = 40 * 1024; // 40 megabit
                _publisherSocket.Options.MulticastRecoveryInterval = TimeSpan.FromMinutes(10);
                _publisherSocket.Options.SendBuffer = 1024 * 10; // 10 megabyte
                _publisherSocket.Bind(_address);
            }

            _publisherSocket.SendReady += (s, a) => { };
        }
Esempio n. 38
0
        /// <summary>
        ///     the broker setting up the cluster
        ///
        ///
        ///          State 2 ---+         +--- State n
        ///                     |         |
        ///                     +----+----+
        ///     client 1 ---|        |         |--- worker 1
        ///     client 2 ---+---- BROKER 1 ----+--- worker 2
        ///     :           |        |         |    :
        ///     client n ---+   +----+----+    +--- worker n
        ///                     |         |
        ///                  BROKER 2   BROKER n
        ///
        ///     BROKER 2 and n are not included and must be setup separately
        ///
        ///     A minimum of two address must be supplied
        /// </summary>
        /// <param name="args">[0] = this broker's address
        ///                    [1] = 1st peer's address
        ///                     :
        ///                    [n] = nth peer address</param>
        /// <remarks>
        ///     since "inproc://" is not working in NetMQ we use "tcp://"
        ///     for each broker we need 5 ports which for this example are
        ///     assigned as follows (in true life it should be configurable whether
        ///     they are ports or tcp/ip addresses)
        ///
        ///     this brokers address => local frontend binds to     tcp://127.0.0.1:5555
        ///                             cloud frontend binds to                    :5556
        ///                             local backend binds to                     :5557
        ///                             state backend binds to                     :5558
        ///                             monitor PULL binds to                      :5559
        ///
        ///     the sockets are connected as follows
        ///
        ///               this broker's monitor PUSH connects to    tcp://127.0.0.1:5559
        ///
        ///                         (if peer's address and port is  tcp://127.0.0.1:5575)
        ///
        ///               this broker's cloud backend connects to                  :5576
        ///               this broker's state frontend connects to                 :5578
        ///
        ///     this scheme is fix in this example
        /// </remarks>
        public static void Main(string[] args)
        {
            Console.Title = "NetMQ Inter-Broker Router";

            const string baseAddress = "tcp://127.0.0.1:";

            if (args.Length < 2)
            {
                Console.WriteLine("usage: program me peer1 [peer]*");
                Console.WriteLine("each broker needs 5 port for his sockets!");
                Console.WriteLine("place enough distance between multiple broker addresses!");
                Environment.Exit(-1);
            }

            // trapping Ctrl+C as exit signal!
            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                s_keepRunning = false;
            };

            // get random generator for later use
            var rnd = new Random();
            // get list for registering the clients
            var clients = new List<byte[]>(NbrClients);
            // get a list of peer addresses
            var peers = new List<byte[]>();
            // get all peer addresses - first is this broker!
            for (var i = 1; i < args.Length; i++)
                peers.Add(Encoding.UTF8.GetBytes(args[i]));

            // build this broker's address
            var me = baseAddress + args[0];
            // get the port as integer for later use
            var myPort = int.Parse(args[0]);

            Console.WriteLine("[BROKER] The broker can be stopped by using CTRL+C!");
            Console.WriteLine("[BROKER] setting up sockets ...");

            // set up all the addresses needed in the due course
            var localFrontendAddress = me;
            var cloudFrontendAddress = baseAddress + (myPort + 1);
            var localBackendAddress = baseAddress + (myPort + 2);
            var stateBackendAddress = baseAddress + (myPort + 3);
            var monitorAddress = baseAddress + (myPort + 4);

            // create the context and all the sockets
            using (var localFrontend = new RouterSocket())
            using (var localBackend = new RouterSocket())
            using (var cloudFrontend = new RouterSocket())
            using (var cloudBackend = new RouterSocket())
            using (var stateBackend = new PublisherSocket())
            using (var stateFrontend = new SubscriberSocket())
            using (var monitor = new PullSocket())
            {
                // give every socket an unique identity, e.g. LocalFrontend[Port]
                SetIdentities(myPort,
                    localFrontend,
                    cloudFrontend,
                    localBackend,
                    stateBackend,
                    monitor,
                    cloudBackend, stateFrontend);

                // subscribe to any message on the stateFrontend socket!
                stateFrontend.Subscribe("");

                // bind the serving sockets
                localFrontend.Bind(localFrontendAddress);
                cloudFrontend.Bind(cloudFrontendAddress);
                localBackend.Bind(localBackendAddress);
                stateBackend.Bind(stateBackendAddress);
                monitor.Bind(monitorAddress);

                // connect sockets to peers
                for (var i = 1; i < args.Length; i++)
                {
                    // build the cloud back end address
                    var peerPort = int.Parse(args[i]);
                    var address = baseAddress + (peerPort + 1);
                    Console.WriteLine("[BROKER] connect to cloud peer {0}", address);

                    // this cloudBackend connects to all peer cloudFrontends
                    cloudBackend.Connect(address);

                    // build the state front end address
                    address = baseAddress + (peerPort + 3);
                    Console.WriteLine("[BROKER] subscribe to state peer {0}", address);

                    // this stateFrontend to all peer stateBackends
                    stateFrontend.Connect(address);
                }

                // setup the local worker queue for LRU and monitor cloud capacity
                var workerQueue = new Queue<byte[]>();
                int previousLocalCapacity = 0;

                // receive the capacity available from other peer(s)
                stateFrontend.ReceiveReady += (s, e) =>
                {
                    // the message should contain the available cloud capacity
                    var capacity = e.Socket.ReceiveFrameString();

                    Debug.Assert(string.IsNullOrWhiteSpace(capacity), "StateFrontend: message was empty!");

                    int couldCapacity;
                    Debug.Assert(int.TryParse(capacity, out couldCapacity), "StateFrontend: message did not contain a number!");
                };

                // get the status message and print it
                monitor.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveFrameString();

                    Console.WriteLine("[MONITOR] {0}", msg);
                };

                // all local clients are connecting to this socket
                // they send a REQ and get a REPLY
                localFrontend.ReceiveReady += (s, e) =>
                {
                    // [client adr][empty][message id]
                    var request = e.Socket.ReceiveMultipartMessage();
                    // register the local client for later identification if not known
                    if (!clients.Any(n => AreSame(n, request[0])))
                        clients.Add(request[0].Buffer);
                    // if we have local capacity send worker else send to cloud
                    if (workerQueue.Count > 0)
                    {
                        // get the LRU worker adr
                        var worker = workerQueue.Dequeue();
                        // wrap message with workers address
                        var msg = Wrap(worker, request);
                        // send message to the worker
                        // [worker adr][empty][client adr][empty][data]
                        localBackend.SendMultipartMessage(msg);
                    }
                    else
                    {
                        // get an random index for peers
                        var peerIdx = rnd.Next(peers.Count - 2) + 2;
                        // get peers address
                        var peer = peers[peerIdx];
                        // wrap message with peer's address
                        var msg = Wrap(peer, request);
                        // [peer adr][empty][client adr][empty][data]
                        cloudBackend.SendMultipartMessage(msg);
                    }
                };

                // the workers are connected to this socket
                // we get a REPLY either for a cloud client [worker adr][empty][peer adr][empty][peer client adr][empty][data]
                // or local client [worker adr][empty][client adr][empty][data]
                // or a READY message [worker adr][empty][WORKER_READY]
                localBackend.ReceiveReady += (s, e) =>
                {
                    // a worker can send "READY" or a request
                    // or an REPLAY
                    var msg = e.Socket.ReceiveMultipartMessage();

                    // just to make sure we received a proper message
                    Debug.Assert(msg != null && msg.FrameCount > 0, "[LocalBackend] message was empty or frame count == 0!");

                    // get the workers identity
                    var id = Unwrap(msg);
                    // this worker done in either way so add it to available workers
                    workerQueue.Enqueue(id);
                    // if it is NOT a ready message we need to route the message
                    // it could be a reply to a peer or a local client
                    // [WORKER_READY] or [client adr][empty][data] or [peer adr][empty][peer client adr][empty][data]
                    if (msg[0].Buffer[0] != WorkerReady)
                    {
                        Debug.Assert(msg.FrameCount > 2, "[LocalBackend] None READY message malformed");

                        // if the adr (first frame) is any of the clients send the REPLY there
                        // and send it to the peer otherwise
                        if (clients.Any(n => AreSame(n, msg.First)))
                            localFrontend.SendMultipartMessage(msg);
                        else
                            cloudFrontend.SendMultipartMessage(msg);
                    }
                };

                // this socket is connected to all peers
                // we receive either a REQ or a REPLY form a peer
                // REQ [peer adr][empty][peer client adr][empty][message id] -> send to peer for processing
                // REP [peer adr][empty][client adr][empty][message id] -> send to local client
                cloudBackend.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage();

                    // just to make sure we received a message
                    Debug.Assert(msg != null && msg.FrameCount > 0, "[CloudBackend] message was empty or frame count == 0!");

                    // we need the peers address for proper addressing
                    var peerAdr = Unwrap(msg);

                    // the remaining message must be at least 3 frames!
                    Debug.Assert(msg.FrameCount > 2, "[CloudBackend] message malformed");

                    // if the id is any of the local clients it is a REPLY
                    // and a REQ otherwise
                    if (clients.Any(n => AreSame(n, msg.First)))
                    {
                        // [client adr][empty][message id]
                        localFrontend.SendMultipartMessage(msg);
                    }
                    else
                    {
                        // add the peers address to the request
                        var request = Wrap(peerAdr, msg);
                        // [peer adr][empty][peer client adr][empty][message id]
                        cloudFrontend.SendMultipartMessage(request);
                    }
                };

                // all peers are binding to this socket
                // we receive REPLY or REQ from peers
                // REQ [peer adr][empty][peer client adr][empty][data] -> send to local worker for processing
                // REP [peer adr][empty][client adr][empty][data] -> send to local client
                cloudFrontend.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage();

                    // just to make sure we received a message
                    Debug.Assert(msg != null && msg.FrameCount > 0, "[CloudFrontend] message was empty or frame count == 0!");

                    // we may need the peers address for proper addressing
                    var peerAdr = Unwrap(msg);

                    // the remaining message must be at least 3 frames!
                    Debug.Assert(msg.FrameCount > 2, "[CloudFrontend] message malformed");

                    // if the address is any of the local clients it is a REPLY
                    // and a REQ otherwise
                    if (clients.Any(n => AreSame(n, msg.First)))
                        localFrontend.SendMultipartMessage(msg);
                    else
                    {
                        // in order to know which per to send back the peers adr must be added again
                        var original = Wrap(peerAdr, msg);

                        // reduce the capacity to reflect the use of a worker by a cloud request
                        previousLocalCapacity = workerQueue.Count;
                        // get the LRU worker
                        var workerAdr = workerQueue.Dequeue();
                        // wrap the message with the worker address and send
                        var request = Wrap(workerAdr, original);
                        localBackend.SendMultipartMessage(request);
                    }
                };

                // in order to reduce chatter we only check to see if we have local capacity to provide to cloud
                // periodically every 2 seconds with a timer
                var timer = new NetMQTimer((int)TimeSpan.FromSeconds(2).TotalMilliseconds);

                timer.Elapsed += (t, e) =>
                {
                    // send message only if the previous send information changed
                    if (previousLocalCapacity != workerQueue.Count)
                    {
                        // set the information
                        previousLocalCapacity = workerQueue.Count;
                        // generate the message
                        var msg = new NetMQMessage();
                        var data = new NetMQFrame(previousLocalCapacity.ToString());
                        msg.Append(data);
                        var stateMessage = Wrap(Encoding.UTF8.GetBytes(me), msg);
                        // publish info
                        stateBackend.SendMultipartMessage(stateMessage);
                    }

                    // restart the timer
                    e.Timer.Enable = true;
                };

                // start all clients and workers as threads
                var clientTasks = new Thread[NbrClients];
                var workerTasks = new Thread[NbrWorker];

                for (var i = 0; i < NbrClients; i++)
                {
                    var client = new Client(localFrontendAddress, monitorAddress, (byte)i);
                    clientTasks[i] = new Thread(client.Run) { Name = string.Format("Client_{0}", i) };
                    clientTasks[i].Start();
                }

                for (var i = 0; i < NbrWorker; i++)
                {
                    var worker = new Worker(localBackendAddress, (byte)i);
                    workerTasks[i] = new Thread(worker.Run) { Name = string.Format("Worker_{0}", i) };
                    workerTasks[i].Start();
                }

                // create poller and add sockets & timer
                var poller = new NetMQPoller
                {
                    localFrontend,
                    localBackend,
                    cloudFrontend,
                    cloudBackend,
                    stateFrontend,
                    stateBackend,
                    monitor,
                    timer
                };

                // start monitoring the sockets
                poller.RunAsync();

                // we wait for a CTRL+C to exit
                while (s_keepRunning)
                    Thread.Sleep(100);

                Console.WriteLine("Ctrl-C encountered! Exiting the program!");

                if (poller.IsRunning)
                    poller.Stop();

                poller.Dispose();
            }
        }