Exemplo n.º 1
0
        public static void Main()
        {
            // using (var responder = new ResponseSocket())
            // {
            //     responder.Bind("tcp://*:5555");

            //     while (true)
            //     {
            //         string str = responder.ReceiveFrameString();
            //         Console.WriteLine(str);
            //         Thread.Sleep(50);  //  Do some 'work'
            //         responder.SendFrame("World");
            //     }
            // }
            Console.WriteLine("Connecting to hello world server…");
            using (var requester = new SubscriberSocket())
            {
                requester.Connect("tcp://localhost:5555");
                requester.Subscribe("PUB");
                int requestNumber = 0;
                while (true)
                {
                    NetMQMessage arr = requester.ReceiveMultipartMessage();
                    string       str = arr[1].ConvertToString();
                    Console.WriteLine(str);
                    Console.WriteLine(requestNumber++);
                }
            }
        }
Exemplo n.º 2
0
        private void ThreadSub()
        {
            List <SubscriberSocket> socklist = new List <SubscriberSocket>();
            var sock = new SubscriberSocket();

            sock.Options.IPv4Only = true;
            sock.SubscribeToAnyTopic();

            foreach (var port in PortList)
            {
                string addr = $"tcp://{IPAddress.Loopback}:{port}";
                sock.Connect(addr);
            }

            Thread.Sleep(2000);

            while (EventStop.Wait(WaitInterval) == false)
            {
                var    buffer = sock.ReceiveFrameBytes();
                string msg    = Encoding.Unicode.GetString(buffer);
                textBox1.AppendLine(msg);
            }

            sock.Close();
        }
Exemplo n.º 3
0
        private static void XSubscriberSocket()
        {
            using var subSocket = new SubscriberSocket();
            string loginMark = "";
            string userName  = "";

            subSocket.Connect(_subscriberAddress);
            subSocket.Subscribe($"{_topic}/{TopicType.EquipAddEvent}");
            subSocket.Subscribe($"{_topic}/{TopicType.EquipChangeEvent}");
            subSocket.Subscribe($"{_topic}/{TopicType.EquipDeleteEvent}");
            subSocket.Subscribe($"{_topic}/{TopicType.YcChangeEvent}/{11151}");
            subSocket.Subscribe($"{_topic}/{TopicType.YxChangeEvent}/{11151}");
            subSocket.Subscribe($"{_topic}/{TopicType.SendControl}/{11151}");
            subSocket.Subscribe($"{_topic}/{TopicType.AddRealTimeSnapshot}");
            subSocket.Subscribe($"{_topic}/{TopicType.EquipStateEvent}");
            subSocket.Subscribe($"{_topic}/{TopicType.DeleteRealTimeSnapshot}");
            subSocket.Subscribe($"{_topic}/{TopicType.OpenPage4InterScreen}");
            subSocket.Subscribe($"{_topic}/{TopicType.ShowOrClosePage}");
            subSocket.Subscribe($"{_topic}/{TopicType.KickClient}/{loginMark}");
            subSocket.Subscribe($"{_topic}/{TopicType.ShowMsg}");
            subSocket.Subscribe($"{_topic}/{TopicType.NotifyOffLine}");
            subSocket.Subscribe($"{_topic}/{TopicType.NotifyRoleOffLine}");
            subSocket.Subscribe($"{_topic}/{TopicType.ShowLockSetParmMsg}");
            subSocket.Subscribe($"{_topic}/{TopicType.VOpenPage}/{userName}");
            subSocket.Subscribe($"{_topic}/{TopicType.ShowInfo}");
            subSocket.SubscribeToAnyTopic();
            Console.WriteLine("Subscriber socket connecting...\n");
            ReceiveFrameString();
        }
Exemplo n.º 4
0
    protected override void Run()
    {
        AsyncIO.ForceDotNet.Force(); // this line is needed to prevent unity freeze after one use, not sure why yet

        using (SUB = new SubscriberSocket())
            using (PUB = new PublisherSocket())
                using (REQ = new RequestSocket()) {
                    SUB.Connect("tcp://localhost:5555");
                    SUB.Subscribe("");

                    PUB.Connect("tcp://localhost:5556");

                    REQ.Connect("tcp://localhost:5557");

                    Debug.Log("Connected: Receiving Messages");

                    while (Running)
                    {
                        /*Receive continuos Slicer data*/
                        string msg = SUB.ReceiveFrameString();

                        /*Send continuos Unity data*/
                        var bytes = new byte[dataPackage.t.Length * 4];
                        System.Buffer.BlockCopy(dataPackage.t, 0, bytes, 0, bytes.Length);
                        PUB.SendFrame(bytes);

                        /*Request volume data once*/
                        if (reqestData)
                        {
                            REQ.SendFrame("volume");
                            string msg2 = REQ.ReceiveFrameString();
                            Debug.Log(msg2);

                            byte[] file = System.IO.File.ReadAllBytes(msg2);
                            Debug.Log(file.Length);

                            int     d1     = System.BitConverter.ToInt32(file, 0);
                            int     d2     = System.BitConverter.ToInt32(file, 4);
                            int     d3     = System.BitConverter.ToInt32(file, 8);
                            float[] volume = new float[d1 * d2 * d3];
                            System.Buffer.BlockCopy(file, 12, volume, 0, volume.Length * sizeof(float));

                            string s = d1 + " " + d2 + " " + d3 + " : ";
                            //for (int i = 0; i < volume.Length; ++i) s += volume[i] + " ";
                            Debug.Log(s);

                            reqestData = false;
                        }

                        /*60fps*/
                        System.Threading.Thread.Sleep(16);
                    }

                    SUB.Close();
                    PUB.Close();
                    REQ.Close();
                }
        NetMQConfig.Cleanup();
    }
Exemplo n.º 5
0
        public ZmqClient()
        {
            context = NetMQContext.Create();

            subSocket = context.CreateSubscriberSocket();
            subSocket.Options.ReceiveHighWatermark = 1000;
            subSocket.Connect(MarketDataSettings.RealTimeUpdateServerAddress);
        }
        public ZmqClient()
        {
            context = NetMQContext.Create();

            subSocket = context.CreateSubscriberSocket();
            subSocket.Options.ReceiveHighWatermark = 1000;
            subSocket.Connect(MarketDataSettings.RealTimeUpdateServerAddress);
        }
Exemplo n.º 7
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("NetMQSubscriber has been disposed");
            }

            socket.Connect(address.ToString());
        }
Exemplo n.º 8
0
        public FrmClient()
        {
            InitializeComponent();

            client.Connect($"tcp://{serverIP}:{serverPort}");
            subSocket.Connect($"tcp://{pubIP}:{pubPort}");
            //1、订阅主题
            subSocket.Subscribe(topic);
        }
Exemplo n.º 9
0
        private IObservable <PooledArraySegment <byte>[]> ZmqSubscribeEndpoint(DaemonEndpointConfig endPoint, string url, string topic, int numMsgSegments = 2)
        {
            return(Observable.Defer(() => Observable.Create <PooledArraySegment <byte>[]>(obs =>
            {
                var tcs = new CancellationTokenSource();

                Task.Factory.StartNew(() =>
                {
                    using (tcs)
                    {
                        while (!tcs.IsCancellationRequested)
                        {
                            try
                            {
                                using (var subSocket = new SubscriberSocket())
                                {
                                    //subSocket.Options.ReceiveHighWatermark = 1000;
                                    subSocket.Connect(url);
                                    subSocket.Subscribe(topic);

                                    logger.Debug($"Subscribed to {url}/{BitcoinConstants.ZmqPublisherTopicBlockHash}");

                                    while (!tcs.IsCancellationRequested)
                                    {
                                        var msg = subSocket.ReceiveMultipartMessage(numMsgSegments);

                                        // Export all frame data as array of PooledArraySegments
                                        var result = msg.Select(x =>
                                        {
                                            var buf = ArrayPool <byte> .Shared.Rent(x.BufferSize);
                                            Array.Copy(x.ToByteArray(), buf, x.BufferSize);
                                            return new PooledArraySegment <byte>(buf, 0, x.BufferSize);
                                        }).ToArray();

                                        obs.OnNext(result);
                                    }
                                }
                            }

                            catch (Exception ex)
                            {
                                logger.Error(ex);
                            }

                            // do not consume all CPU cycles in case of a long lasting error condition
                            Thread.Sleep(1000);
                        }
                    }
                }, tcs.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

                return Disposable.Create(() =>
                {
                    tcs.Cancel();
                });
            })));
        }
        public void Test_Two_Subscribers()
        {
            NUnitUtils.PrintTestName();
            var sw = Stopwatch.StartNew();

            using (var pub = new PublisherSocket())
            {
                using (var sub1 = new SubscriberSocket())
                {
                    using (var sub2 = new SubscriberSocket())
                    {
                        var freePort = NUnitUtils.TcpPortFree();
                        pub.Bind("tcp://127.0.0.1:" + freePort);
                        sub1.Connect("tcp://127.0.0.1:" + freePort);
                        sub1.Subscribe("A");
                        sub2.Connect("tcp://127.0.0.1:" + freePort);
                        sub2.Subscribe("B");

                        Thread.Sleep(500);

                        var swInner = Stopwatch.StartNew();
                        {
                            pub.SendFrame("A\n"); // Ping.
                            {
                                string topic;
                                var    pass1 = sub1.TryReceiveFrameString(TimeSpan.FromMilliseconds(250), out topic);
                                if (pass1)
                                {
                                    Console.Write(topic);
                                }
                                else
                                {
                                    Assert.Fail();
                                }
                            }
                            pub.SendFrame("B\n"); // Ping.
                            {
                                string topic;
                                var    pass2 = sub2.TryReceiveFrameString(TimeSpan.FromMilliseconds(250), out topic);
                                if (pass2)
                                {
                                    Console.Write(topic);
                                }
                                else
                                {
                                    Assert.Fail();
                                }
                            }
                        }
                        Console.WriteLine("Connected in {0} ms.", swInner.ElapsedMilliseconds);
                    }
                }
            }

            NUnitUtils.PrintElapsedTime(sw.Elapsed);
        }
Exemplo n.º 11
0
    // Start is called before the first frame update
    private void Start()
    {
        _hip = GetComponent <Rigidbody>();

        _pub.Bind(pubAddress);

        _sub.Connect(subAddress);
        _sub.SubscribeToAnyTopic();
        _sub.Options.ReceiveHighWatermark = 1;
    }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            //TestSerilog();

            using (var subSocket = new SubscriberSocket())
            {
                var tcpLocalhost = "tcp://localhost:12345";
                subSocket.Connect(tcpLocalhost);
                subSocket.SubscribeToAnyTopic();
                Console.WriteLine("Waiting to Connect");

                var connectMessage = subSocket.ReceiveMultipartMessage();
                if (connectMessage[0].ConvertToString(Encoding.UTF8) == "CONNECT")
                {
                    Console.WriteLine("Connected");

                    var port = connectMessage[1].ConvertToString(Encoding.UTF8);
                    using (var reqSocket = new RequestSocket(port))
                    {
                        var syncMessage = new NetMQMessage();
                        syncMessage.Append(new NetMQFrame("SYNC", Encoding.UTF8));
                        syncMessage.Append(new NetMQFrame(tcpLocalhost, Encoding.UTF8));
                        reqSocket.SendMultipartMessage(syncMessage);
                        Console.WriteLine("Syncing");
                        var syncOk = reqSocket.ReceiveFrameString();
                        Console.WriteLine($"Synced: {syncOk}");
                    }

                    using (var poller = new NetMQPoller())
                    {
                        poller.Add(subSocket);

                        subSocket.ReceiveReady += (sender, eventArgs) =>
                        {
                            var subMessage = eventArgs.Socket.ReceiveMultipartMessage();
                            var topic      = subMessage[0].ConvertToString(Encoding.UTF8);
                            if (topic == "TopicA")
                            {
                                var decodedMessage = MessagePackSerializer.Deserialize <string>(subMessage[1].ToByteArray());
                                Console.WriteLine(
                                    $"{subMessage[0].ConvertToString(Encoding.UTF8)}: {decodedMessage}");
                            }
                        };

                        poller.RunAsync();

                        Console.WriteLine("Press Enter to Exit");
                        Console.ReadLine();

                        poller.StopAsync();
                    }
                }
            }
        }
 /// <summary>
 /// Intent: See interface.
 /// </summary>
 public SubscriberSocket GetSharedSubscriberSocket(string addressZeroMq)
 {
     lock (_initializeSubscriberLock)
     {
         // Must return a unique subscriber for every new ISubject of T.
         var subscriberSocket = new SubscriberSocket();
         subscriberSocket.Options.ReceiveHighWatermark = this.HighwaterMark;
         subscriberSocket.Connect(addressZeroMq);
         return(subscriberSocket);
     }
 }
Exemplo n.º 14
0
 private void createRSocket()
 {
     clientR = context.CreateSubscriberSocket();
     string[] addrs = cfg.ServerS.Split(',');
     foreach (string addr in addrs)
     {
         clientR.Connect(addr);
     }
     clientR.Subscribe(Global.DC_CTRL_HB);                                                                                       //接收心跳数据包
     clientR.Subscribe(Global.Encoding.GetBytes(string.Concat(Global.DC_HEAD_AS_RPLY, Global.ENVELOPE_SPLITTER, this.guidStr))); //接收Act操作的回复数据包
 }
Exemplo n.º 15
0
        public byte[] NetMQReceiveBytes(string connectionString)
        {
            using (SubscriberSocket subSocket = new SubscriberSocket())
            {
                subSocket.Connect(connectionString);

                subSocket.Subscribe(String.Empty);
                byte[] result = subSocket.ReceiveFrameBytes();

                return(result);
            }
        }
Exemplo n.º 16
0
        public void ConnectSubscribe()
        {
            subscriber = new SubscriberSocket();
            subscriber.Connect(SubscribeAddress);
            subscriber.ReceiveReady += _Subscire_ReceiveReady;
            subscriber.SubscribeToAnyTopic();

            subscriberPoller = new NetMQPoller {
                subscriber
            };
            subscriberPoller.RunAsync();
        }
Exemplo n.º 17
0
        private void Subscribe()
        {
            subscribing = true;

            subscriberSocket.Connect(@"tcp://127.0.0.1:17232");
            subscriberSocket.SubscribeToAnyTopic();

            while (subscribing)
            {
                if (subscriberSocket.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(100), out var frame))
                {
                    teleprinter.Read(frame[^ 1]);
Exemplo n.º 18
0
        public TcpConsumer(string address)
        {
            Address = address;
            sub     = new SubscriberSocket();
            sub.Connect(Address);
            var type = typeof(T).AssemblyQualifiedName;

            sub.Subscribe(type);
            subThread = new Thread(Consume);
            subThread.Start();
            pipe = new List <Action <T> >();
        }
Exemplo n.º 19
0
        private static Task StartProcessor(string nodeAddr, string[] subscriptions, CancellationTokenSource cancellationTokenSource)
        {
            telemetryClient.TrackTrace($"Starting processor for {nodeAddr}", SeverityLevel.Information, new Dictionary <string, string>
            {
                { nameof(nodeAddr), nodeAddr },
            });
            return(Task.Factory.StartNew(async() =>
            {
                string index = "kv6";
                using (var socket = new SubscriberSocket())
                {
                    socket.Connect(nodeAddr);
                    try
                    {
                        subscriptions.All((s) =>
                        {
                            socket.Subscribe(s);
                            return true;
                        });

                        while (!cancellationTokenSource.IsCancellationRequested)
                        {
                            var messageList = socket.ReceiveMultipartBytes(2);
                            var subscription = Encoding.UTF8.GetString(messageList[0]);

                            string payload = null;
                            using (GZipStream stream = new GZipStream(new MemoryStream(messageList[1]), CompressionMode.Decompress))
                                using (var sr = new StreamReader(stream))
                                    payload = await sr.ReadToEndAsync();

#if DEBUG
                            Console.Write($"{subscription} - {payload.Length} chars...");
#endif

                            var xml = DeserializeMessage(payload);
                            telemetryClient.TrackTrace($"Received payload for {subscription}, size: {payload.Length} characters", SeverityLevel.Verbose, new Dictionary <string, string>
                            {
                                { nameof(subscription), subscription },
                                { nameof(payload), payload },
                            });
                            var jsonObjects = GetJsonRepresentation(xml.Timestamp, xml.KV6posinfo);
                            await StoreJsonObjects(index, jsonObjects);
                        }
                    }
                    finally
                    {
                        socket.Disconnect(nodeAddr);
                        socket.Close();
                        telemetryClient.TrackTrace("Stopped processor", SeverityLevel.Information);
                    }
                }
            }, cancellationTokenSource.Token));
        }
Exemplo n.º 20
0
        private void InitSub()
        {
            subscriber = new SubscriberSocket();
            subscriber.Connect(Address);
            subscriber.ReceiveReady += _Subscire_ReceiveReady;
            subscriber.SubscribeToAnyTopic();

            subscriberPoller = new NetMQPoller {
                subscriber
            };
            subscriberPoller.RunAsync();
        }
Exemplo n.º 21
0
        public string NetMQReceiveString(string connectionString)
        {
            using (SubscriberSocket subSocket = new SubscriberSocket())
            {
                subSocket.Connect(connectionString);

                subSocket.Subscribe(String.Empty);
                string result = subSocket.ReceiveFrameString();

                return(result);
            }
        }
Exemplo n.º 22
0
        private void SubscribeEntry(object n)
        {
            string name = (string)n;

            using (SubscriberSocket subscriber = new SubscriberSocket())
            {
                if (!SubRM.ContainsKey(name))
                {
                    Logging.logger.Error("do not have the sub name, return");
                    return;
                }

                string endpoint = "tcp://" + SubRM[name].point.ip + ":" + SubRM[name].point.port;


                try
                {
                    subscriber.Connect(endpoint);
                }
                catch (Exception err)
                {
                    Logging.logger.Error("connect to DataSubscribe faild " + endpoint + " " + err.Message);
                    throw (err);
                }

                subscriber.Subscribe("");
                SubRM[name].subsock = subscriber;
                SubRM[name].Working = true;
                while (SubRM[name].Running)
                {
                    //string received = serverSocket.ReceiveFrameString();
                    string received = string.Empty;
                    bool   result   = subscriber.TryReceiveFrameString(out received);

                    if (result == true)
                    {
                        try
                        {
                            this.SubscriptEntry(name, received);
                        }
                        catch (Exception err)
                        {
                            Logging.logger.Error(err.Message);
                            throw (err);
                        }
                    }
                    DelayTime();
                }
                SubRM[name].Working = false;
                subscriber.Close();
            }
        }
Exemplo n.º 23
0
        public void Connect(string givenAddress)
        {
            if (IsConnected)
            {
                return;
            }

            subSocket = context.CreateSubscriberSocket();
            subSocket.Connect(givenAddress);
            subSocket.Subscribe("EasyErgsocket");

            IsConnected = true;
        }
Exemplo n.º 24
0
        void establish()
        {
            subSocket = Core.ZeroMQ.CreateSubscriberSocket();
            subSocket.Options.ReceiveHighWatermark = EngCfg.HighWatermark;
            ConnAddres = ConnectAddr(typeof(SubscriberSocket));
            subSocket.Connect(ConnAddres);
            ConsoleEx.DebugLog("Sub socket has connected to " + ConnAddres, ConsoleEx.YELLOW);
            subSocket.Subscribe(topic);

            subSocket.ReceiveReady += Client_ReceiveReady;

            AddPoller();
        }
Exemplo n.º 25
0
        private void Subscribe(object state)
        {
            _running = true;
            foreach (string subscriberAddress in _configuration.SubscriberAddresses)
            {
                SubscriberSocket subscriberSocket = _context.CreateSubscriberSocket();
                subscriberSocket.Connect(subscriberAddress);
                subscriberSocket.Subscribe("");

                _subscriberSockets.Add(subscriberSocket);
                Task.Factory.StartNew(() => WaitForMessages(subscriberSocket));
            }
        }
Exemplo n.º 26
0
        public PriceTickerScreenViewModel()
        {
            // get UI context so can ensure updates are on correct threada
            var uiContext = SynchronizationContext.Current;

            // add the list of Assets to display
            AssetViewModels.Add(new AssetViewModel {
                Name = "Stock 1", Price = 0
            });
            AssetViewModels.Add(new AssetViewModel {
                Name = "Stock 2", Price = 0
            });

            // set up subscriber to asset stream subject
            _assetStream.Subscribe(asset =>
            {
                var avm = AssetViewModels.Single(a => a.Name == asset.Name);

                // update on UI context
                uiContext.Send(_ =>
                {
                    avm.Price = asset.Price;
                    avm.History.Add(new HistoryItem
                    {
                        Timestamp = asset.Timestamp,
                        Price     = asset.Price
                    });
                }, null);
            });

            // connect to the price server, receive updates and push to the subject stream
            Task.Run(() =>
            {
                using (var client = new SubscriberSocket())
                {
                    client.Options.ReceiveHighWatermark = 1000;
                    client.Connect("tcp://localhost:5555");
                    client.Subscribe("Stock 1");
                    client.Subscribe("Stock 2");

                    while (true)
                    {
                        var topicMessage = client.ReceiveFrameString();
                        var message      = client.ReceiveFrameString();

                        // push onto subject
                        _assetStream.OnNext(message.Deserialise <Asset>());
                    }
                }
            });
        }
Exemplo n.º 27
0
            public void Run(PairSocket shim)
            {
                _poller = new NetMQPoller();

                _ = Observable.FromEventPattern <NetMQSocketEventArgs>(e => shim.ReceiveReady += e, e => shim.ReceiveReady -= e)
                    .Select(e => e.EventArgs)
                    .Subscribe(OnShimReady);

                _poller.Add(shim);

                // Timer
                var timeoutTimer = new NetMQTimer(TimeSpan.FromSeconds(5));

                _ = Observable.FromEventPattern <NetMQTimerEventArgs>(e => timeoutTimer.Elapsed += e, e => timeoutTimer.Elapsed -= e)
                    .Select(e => e.EventArgs)
                    .ObserveOn(ThreadPoolScheduler.Instance)
                    .Subscribe(OnTimeoutElapsed);
                _poller.Add(timeoutTimer);

                _subscriberSocket = new SubscriberSocket();
                _subscriberSocket.Options.Linger = TimeSpan.Zero;
                _subscriberSocket.Subscribe(CoreHearbeatTopic);
                _subscriberSocket.Connect($"tcp://{_address}:{_port + 1}");
                _subject.OnNext(ConnectionStatus.Connecting);

                _ = Observable.FromEventPattern <NetMQSocketEventArgs>(e => _subscriberSocket.ReceiveReady += e, e => _subscriberSocket.ReceiveReady -= e)
                    .Select(e => e.EventArgs)
                    .Where(e => e.Socket.ReceiveFrameString() == CoreHearbeatTopic)
                    .ObserveOn(ThreadPoolScheduler.Instance)
                    .Subscribe(e =>
                {
                    timeoutTimer.Reset();
                    Thread.MemoryBarrier();
                    var status = _borgConnected
                            ? (ConnectionStatus.Online | ConnectionStatus.Connected)
                            : (ConnectionStatus.Online | ConnectionStatus.Disconnected);
                    _subject.OnNext(status);
                });

                _poller.Add(_subscriberSocket);
                timeoutTimer.Reset();
                shim.SignalOK();

                _poller.Run();

                // Cleanup stuff after stopping
                _poller.Remove(_subscriberSocket);
                _poller.Remove(timeoutTimer);
                _poller.Remove(shim);
                _poller.Dispose();
            }
Exemplo n.º 28
0
        public void Start(string serverAddress, int serverPublishPort, int clientId, Action <ServerToClientMessage> messageHandler)
        {
            this.messageHandler = messageHandler;

            subscribeSocket = new SubscriberSocket();
            subscribeSocket.Connect(string.Format("tcp://{0}:{1}", serverAddress, serverPublishPort));
            subscribeSocket.Subscribe(clientId.ToString());
            subscribeSocket.Subscribe("a");
            isRunning          = true;
            receiveThread      = new Thread(Run);
            receiveThread.Name = "ClientMessageListenThread";
            receiveThread.Start();
            logger.Info("Server message subscriber started");
        }
Exemplo n.º 29
0
        static int Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Arguments: <nodeAddr> <subscr...>");
                Console.WriteLine("Eg: tcp://node03.kv7.openov.nl:7817 /GOVI/KV8");
                return(-1);
            }

            var nodeAddr      = args[0];
            var subscriptions = args.Skip(1).ToArray();

            using (var socket = new SubscriberSocket())
            {
                int msgCount = 0;
                socket.Connect(nodeAddr);
                try
                {
                    subscriptions.All((s) =>
                    {
                        socket.Subscribe(s);
                        return(true);
                    });

                    // Download just 20 messages, it's just a test tool...
                    while (msgCount < 20)
                    {
                        var messageList = socket.ReceiveMultipartBytes(2);
                        msgCount++;
                        var msg1 = Encoding.UTF8.GetString(messageList[0]);
                        using (GZipStream stream = new GZipStream(new MemoryStream(messageList[1]), CompressionMode.Decompress))
                            using (var sr = new StreamReader(stream))
                            {
                                var msg2 = sr.ReadToEnd();
                                Console.Write($"{msg1} - {msg2.Length} chars...");
                                var filename = msg1.Substring(1).Replace('/', '-') + $"{msgCount}.xml";
                                File.WriteAllText(filename, msg2, Encoding.UTF8);
                                Console.WriteLine();
                            }
                    }
                }
                finally
                {
                    socket.Disconnect(nodeAddr);
                    socket.Close();
                }
            }

            return(0);
        }
 public void Start(string serverAddress, int serverPublishPort, int clientId, Action<ServerToClientMessage> messageHandler)
 {
     this.messageHandler = messageHandler;
     ctx = NetMQContext.Create();
     subscribeSocket = ctx.CreateSubscriberSocket();
     subscribeSocket.Connect(string.Format("tcp://{0}:{1}", serverAddress, serverPublishPort));
     subscribeSocket.Subscribe(clientId.ToString());
     subscribeSocket.Subscribe("a");
     isRunning = true;
     receiveThread = new Thread(Run);
     receiveThread.Name = "ClientMessageListenThread";
     receiveThread.Start();
     logger.Info("Server message subscriber started");
 }
Exemplo n.º 31
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _subscriberSocket.Connect(_options.Address);
            _subscriberSocket.SubscribeToAnyTopic();
            while (true)
            {
                var bytesReceived = _subscriberSocket.ReceiveFrameBytes();
                var uncompressed  = ZlibStream.UncompressBuffer(bytesReceived);
                var entity        = _encoding.GetString(uncompressed);
                await _importTargetFactory.ImportAsync(entity);

                _logger.LogInformation(entity);
            }
        }
Exemplo n.º 32
0
        public void Subscribe()
        {
            using (SubscriberSocket subscriber = new SubscriberSocket())
            {
                subscriber.Options.ReceiveHighWatermark = 1000;

                subscriber.Connect("tcp://127.0.0.1:12345");
                subscriber.Subscribe("TopicA");


                var data = subscriber.ReceiveFrameString();
                Console.WriteLine("Topic: TopicA received data: {0}", data);
            }
        }
Exemplo n.º 33
0
 static void Main(string[] args)
 {
     using (var subSocket = new SubscriberSocket())
     {
         subSocket.Options.ReceiveHighWatermark = 100;
         subSocket.Connect("tcp://localhost:12345");
         subSocket.Subscribe("");
         Console.WriteLine("Subscriber socket connecting...");
         while (true)
         {
             string messageTopicReceived = subSocket.ReceiveFrameString();
             string messageReceived = subSocket.ReceiveFrameString();
             Console.WriteLine(messageReceived);
             Thread.Sleep(300);
         }
     }
 }
Exemplo n.º 34
0
        private static void Main()
        {
            Console.Title = "NetMQ Weather Update Client";

            const int zipToSubscribeTo = 10001;
            const int iterations = 100;

            int totalTemp = 0;
            int totalHumidity = 0;

            Console.WriteLine("Collecting updates for weather service for zipcode {0}...", zipToSubscribeTo);

            using (var subscriber = new SubscriberSocket())
            {
                subscriber.Connect("tcp://127.0.0.1:5556");
                subscriber.Subscribe(zipToSubscribeTo.ToString(CultureInfo.InvariantCulture));

                for (int i = 0; i < iterations; i++)
                {
                    string results = subscriber.ReceiveFrameString();
                    Console.Write(".");

                    // "zip temp relh" ... "10001 84 23" -> ["10001", "84", "23"]
                    string[] split = results.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    int zip = int.Parse(split[0]);
                    if (zip != zipToSubscribeTo)
                    {
                        throw new Exception(string.Format("Received message for unexpected zipcode: {0} (expected {1})", zip, zipToSubscribeTo));
                    }

                    totalTemp += int.Parse(split[1]);
                    totalHumidity += int.Parse(split[2]);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Average temperature was: {0}", totalTemp/iterations);
            Console.WriteLine("Average relative humidity was: {0}", totalHumidity/iterations);
        }
Exemplo n.º 35
0
        private static void SubscriberContinuousLoop(string pubSubAddress, string requestString)
        {
            Task.Factory.StartNew(() =>
            {
                using (var progressSubscriber = new SubscriberSocket())
                {
                    progressSubscriber.Connect(pubSubAddress);
                    progressSubscriber.SubscribeToAnyTopic();
                    while (true)
                    {
                        var topic = progressSubscriber.ReceiveFrameString();
                        RequestSocket.ProgressTopic progressTopic;
                        Enum.TryParse(topic, out progressTopic);
                        switch (progressTopic)
                        {
                            case RequestSocket.ProgressTopic.Send:
                                Console.WriteLine("C: Sending {0}", requestString);
                                break;
                            case RequestSocket.ProgressTopic.Retry:
                                Console.WriteLine("C: No response from server, retrying...");
                                break;
                            case RequestSocket.ProgressTopic.Failure:
                                Console.WriteLine("C: Server seems to be offline, abandoning");
                                break;
                            case RequestSocket.ProgressTopic.Success:
                                Console.WriteLine("C: Server replied OK");
                                break;
                            default:
                                throw new ArgumentOutOfRangeException();
                        }


                    }
                }
            });
        }
Exemplo n.º 36
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();
            }
        }