示例#1
0
        public void Start()
        {
            using (_toGateway = new DealerSocket())
            {
                _toGateway.Options.Identity = _id.ToByteArray();
                _toGateway.Connect(_gatewayEndpoint);

                using (_toWorkers = new RouterSocket())
                {
                    _toWorkers.Options.Identity = _id.ToByteArray();
                    _toWorkers.Bind(_clusterEndpoint);

                    using (_workPoller = new NetMQPoller {
                        _toGateway, _toWorkers
                    })
                    {
                        _toGateway.ReceiveReady += (s, e) =>
                        {
                            var transportMessage = e.Socket.ReceiveMultipartMessage()
                                                   .GetMessageFromDealer <Work>();

                            var work = transportMessage.Message;

                            if (work.Status == WorkerStatus.Ask)
                            {
                                _works.Add(work);
                            }
                        };

                        _toWorkers.ReceiveReady += (s, e) =>
                        {
                            var transportMessage = e.Socket.ReceiveMultipartMessage()
                                                   .GetMessageFromRouter <Work>();

                            var work = transportMessage.Message;

                            if (work.Status == WorkerStatus.Ready)
                            {
                                _workers.Enqueue(new Guid(transportMessage.SenderId));
                            }
                            else if (work.Status == WorkerStatus.Finished)
                            {
                                _toGateway.SendFrame(transportMessage.MessageBytes);
                                _workers.Enqueue(new Guid(transportMessage.SenderId));
                            }

                            _resetEvent.Set();
                        };

                        //register to the gateway router...
                        _toGateway.SendFrame(Work.Ready.Serialize());

                        //...then start state publish
                        _statePublishProc = Task.Run(StatePublish).ConfigureAwait(false);

                        _workPoller.Run();
                    }
                }
            }
        }
示例#2
0
        public void ThreadMQRouterSend()
        {
            try
            {
                using (RouterSocket routerSocket = new RouterSocket())
                {
                    routerSocket.Bind(MQConfig.RouterSendServer);

                    while (!_cancellationTokenSource.IsCancellationRequested)
                    {
                        try
                        {
                            IdKeyData item;
                            if (MQRouterSendQueue.TryTake(out item))
                            {
                                routerSocket.RouterSend(item);
                            }
                        }
                        catch (TerminatingException)
                        {
                        }
                        catch (Exception ex)
                        {
                            MQThreadExceptionHandler?.Invoke(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MQThreadExceptionHandler?.Invoke(ex);
            }
        }
示例#3
0
        // We have two workers, here we copy the code, normally these would run on different boxes…
        public static void Main()
        {
            var random  = new Random(DateTime.Now.Millisecond);
            var workers = new List <Thread>(new[] { new Thread(WorkerTaskA), new Thread(WorkerTaskB) });

            using (var client = new RouterSocket())
            {
                client.Bind(string.Format("tcp://localhost:{0}", PortNumber));

                foreach (var thread in workers)
                {
                    thread.Start(PortNumber);
                }

                // Wait for threads to connect, since otherwise the messages we send won't be routable.
                Thread.Sleep(1000);

                for (int taskNumber = 0; taskNumber < 1000; taskNumber++)
                {
                    // Send two message parts, first the address…
                    client.SendMoreFrame(random.Next(3) > 0 ? Encoding.Unicode.GetBytes("A") : Encoding.Unicode.GetBytes("B"));

                    // And then the workload
                    client.SendFrame("This is the workload");
                }

                client.SendMoreFrame(Encoding.Unicode.GetBytes("A"));
                client.SendFrame("END");

                client.SendMoreFrame(Encoding.Unicode.GetBytes("B"));
                client.SendFrame("END");
            }

            Console.ReadKey();
        }
示例#4
0
        public void StartAndStopStateValidation()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                {
                    front.Bind("inproc://frontend");
                    back.Bind("inproc://backend");

                    var proxy = new Proxy(front, back);
                    Task.Factory.StartNew(proxy.Start);

                    // Send a message through to ensure the proxy has started
                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");
                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());
                        }

                    Assert.Throws <InvalidOperationException>(proxy.Start);
                    Assert.Throws <InvalidOperationException>(proxy.Start);
                    Assert.Throws <InvalidOperationException>(proxy.Start);

                    proxy.Stop(); // blocks until stopped

                    Assert.Throws <InvalidOperationException>(proxy.Stop);
                }
        }
示例#5
0
文件: Program.cs 项目: kreca95/zeroMQ
        public static void Main(string[] args)
        {
            Console.WriteLine("Server");
            using (var router = new RouterSocket())
            {
                router.Options.RouterMandatory = true;
                router.Bind("tcp://127.0.0.1:5555");

                bool more = false;
                while (true)
                {
                    //string a = router.ReceiveFrameString(Encoding.ASCII,out more);
                    //var nick = router.Options.Identity;

                    //Console.WriteLine(nick);
                    //Console.WriteLine(a);

                    var clientMessage = router.ReceiveMultipartMessage();
                    PrintFrames("Server receiving", clientMessage);


                    var msgToClient = new NetMQMessage();
                    msgToClient.Append(clientMessage[0]);
                    msgToClient.Append(clientMessage[1]);
                    msgToClient.Append(clientMessage[2]);

                    router.SendMultipartMessage(msgToClient);
                }
            }
        }
示例#6
0
        public void Receive_BrokerDisconnectedWithLogging_ShouldReturnRequest()
        {
            const string hostAddress     = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
                using (var poller = new NetMQPoller())
                    using (var session = new MDPWorker(hostAddress, "test"))
                    {
                        broker.Bind(hostAddress);
                        // we need to pick up any message in order to avoid errors but don't answer
                        broker.ReceiveReady += (s, e) => e.Socket.ReceiveMultipartMessage();

                        poller.Add(broker);
                        poller.RunAsync();

                        // speed up the test
                        session.HeartbeatDelay = TimeSpan.FromMilliseconds(250);
                        session.ReconnectDelay = TimeSpan.FromMilliseconds(250);
                        // set the event handler to receive the logging messages
                        session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                        // initialise the worker - broker protocol
                        session.Receive(null);

                        poller.Stop();

                        Assert.That(loggingMessages.Count(m => m.Contains("retrying")), Is.EqualTo(3));
                        // 3 times retrying and 1 time initial connecting
                        Assert.That(loggingMessages.Count(m => m.Contains("localhost")), Is.EqualTo(4));
                        Assert.That(loggingMessages.Last().Contains("abandoning"));
                    }
        }
示例#7
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}");
                }
            }
        }
示例#8
0
        private static void Server()
        {
            using (var receiver = new RouterSocket())
            {
                receiver.Bind("tcp://*:9045");
                while (true)
                {
                    var message = receiver.ReceiveMultipartMessage();
                    Console.WriteLine("Received request for equity");
                    //var itr = message.GetEnumerator();
                    //Console.WriteLine($"{(itr.MoveNext() ? "found" : "didn't find")} identity frame");
                    //Console.Write($"from {itr.Current.ConvertToString(Encoding.Unicode)}");

                    var response = new NetMQMessage();
                    response.Append(message.First);
                    //response.Append("goodbye");
                    //receiver.SendMultipartMessage(response);
                    //Console.WriteLine("Sent response.");
                    using (var ms = new MemoryStream())
                    {
                        var equity = ProtoBufSerialize(new Equity()
                        {
                            Account = new Account(), AccountID = 1, ID = 1, UpdateTime = DateTime.Now, Value = 500000
                        }, ms);

                        response.Append(equity);
                        response.AppendEmptyFrame();
                        receiver.SendMultipartMessage(response);
                    }
                }
            }
        }
示例#9
0
        /// <summary>
        /// Router模式,会自动路由到Controler
        /// </summary>
        public void ThreadMQRouterReceive()
        {
            try
            {
                using (RouterSocket routerSocket = new RouterSocket())
                {
                    routerSocket.Bind(MQConfig.RouterReceiveServer);

                    while (!_cancellationTokenSource.IsCancellationRequested)
                    {
                        try
                        {
                            IdKeyData receiveData = routerSocket.RouterReceive();
                            Task.Factory.StartNew(() =>
                            {
                                RouteConfig.Instance.ExecCmd(receiveData.Id, receiveData.Key, receiveData.Data);
                            });
                        }
                        catch (TerminatingException)
                        {
                        }
                        catch (Exception ex)
                        {
                            MQThreadExceptionHandler?.Invoke(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MQThreadExceptionHandler?.Invoke(ex);
            }
        }
示例#10
0
        public void SendWrongMDPVersionFromBrokerNoLoggingShouldThrowApplicationException()
        {
            const string hostAddress = "tcp://localhost:5555";

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
                using (var poller = new NetMQPoller())
                    using (var session = new MDPClientAsync(hostAddress))
                    {
                        broker.Bind(hostAddress);
                        // we need to pick up any message in order to avoid errors
                        broker.ReceiveReady += (s, e) =>
                        {
                            // return empty reply
                            var msg = e.Socket.ReceiveMultipartMessage();
                            // we expect to receive a 4 Frame message
                            // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                            if (msg.FrameCount != 5)
                            {
                                Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                            }
                            // REQUEST socket will strip the his address + empty frame
                            // ROUTER has to add the address prelude in order to identify the correct socket(!)
                            // [REQ ADR][EMPTY]["MDPC00"]["echo"]["REQUEST"]
                            var clientAddress = msg.Pop();
                            msg.Pop();               // forget empty frame
                            msg.Pop();               // drop the MDP Version Frame
                            msg.Push("MDPC00");      // insert wrong MDP version
                            msg.Push(NetMQFrame.Empty);
                            msg.Push(clientAddress); // reinsert the client's address

                            e.Socket.SendMultipartMessage(msg);
                        };

                        int timeOutInMillis = 10000;
                        var timer           = new NetMQTimer(timeOutInMillis); // Used so it doesn't block if something goes wrong!
                        timer.Elapsed += (s, e) =>
                        {
                            Assert.Fail($"Waited {timeOutInMillis} and had no response from broker");
                            poller.Stop();
                        };

                        poller.Add(broker);
                        poller.Add(timer);

                        session.ReplyReady += (s, e) =>
                        {
                            Assert.True(e.HasError());
                            Assert.That(e.Exception.Message, Is.StringContaining("MDP Version mismatch"));
                            poller.Stop(); // To unlock the Task.Wait()
                        };

                        var task = Task.Factory.StartNew(() => poller.Run());

                        var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                        session.Send("echo", requestMessage);

                        task.Wait();
                    }
        }
示例#11
0
        public void SendAndReceive()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                {
                    front.Bind("inproc://frontend");
                    back.Bind("inproc://backend");

                    var proxy = new Proxy(front, back);
                    Task.Factory.StartNew(proxy.Start);

                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");

                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());
                        }

                    proxy.Stop();
                }
        }
示例#12
0
        static void RunServer(CancellationToken cancellationToken)
        {
            Task.Factory.StartNew(() =>
            {
                var random = new Random();
                using (var server = new RouterSocket())
                {
                    server.Bind("tcp://*:5555");
                    Console.WriteLine("\t\t\tServer: Bound to *:5555");

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        var request = server.ReceiveMultipartMessage();
                        var message = request[2].ConvertToString();

                        Console.WriteLine($"\t\t\tServer: Received {message}");

                        // Simulate work
                        Thread.Sleep(200);

                        var responsestr = $"World {random.Next(100)}";
                        Console.WriteLine($"\t\t\tServer: Sending {responsestr}");

                        var response = new NetMQMessage();
                        response.Append(request[0]);
                        response.AppendEmptyFrame();
                        response.Append(responsestr);
                        response.AppendEmptyFrame();
                        server.SendMultipartMessage(response);
                    }
                }
            });
        }
示例#13
0
        public async Task <Swarm> InitContextAsync(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            _router.Bind(_listenUrl.ToString());
            foreach (Peer peer in _peers.Keys)
            {
                try
                {
                    Peer replacedPeer = await DialPeerAsync(peer, cancellationToken);

                    if (replacedPeer != peer)
                    {
                        _peers[replacedPeer] = _peers[peer];
                        _peers.Remove(peer);
                    }
                }
                catch (IOException e)
                {
                    _logger.Error(e, $"IOException occured in DialPeerAsync({peer}).");
                    continue;
                }
            }

            _contextInitialized = true;
            return(this);
        }
示例#14
0
        private void ListenerWork()
        {
            AsyncIO.ForceDotNet.Force();

            using (var server = new RouterSocket()) {
                server.Bind("tcp://*:5555");

                while (!_listenerCancelled)
                {
                    //server.SkipFrame(); // to skip identity

                    string identity;
                    if (!server.TryReceiveFrameString(out identity))
                    {
                        continue;
                    }
                    //UnityEngine.Debug.LogFormat ("identity {0}", identity);

                    string message;
                    if (!server.TryReceiveFrameString(out message))
                    {
                        continue;
                    }
                    //UnityEngine.Debug.LogFormat ("message {0}", message);

                    //server.SendMoreFrame(identity).SendFrame("message");
                    Request request = new Request(server, identity, message);
                    _requestQueue.Enqueue(request);
                }
            }

            NetMQConfig.Cleanup();
        }
示例#15
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));
        }
示例#16
0
        private void ClusterSate()
        {
            using (_clusterState = new RouterSocket())
            {
                _clusterState.Bind(_clusterStateEndpoint);

                _clusterState.ReceiveReady += (s, e) =>
                {
                    var state = e.Socket.ReceiveMultipartMessage()
                                .GetMessageFromDealer <ClusterState>();

                    _clustersState.AddOrUpdate(new Guid(state.SenderId), state.Message, (key, value) =>
                    {
                        return(value);
                    });

                    _resetEvent.Set();
                };

                using (_clusterStatePoller = new NetMQPoller {
                    _clusterState
                })
                {
                    _clusterStatePoller.Run();
                }
            }
        }
示例#17
0
        private async Task HandleStateOfTheWorldRequest()
        {
            using (_stateRequestSocket = new RouterSocket())
            {
                _stateRequestSocket.Bind(_configuration.StateOftheWorldEndpoint);

                while (!_cancel.IsCancellationRequested)
                {
                    var message = _stateRequestSocket.ReceiveMultipartMessage();
                    var sender  = message[0].Buffer;
                    var request = _serializer.Deserialize <IStateRequest>(message[1].Buffer);

                    var stream = await _cache.GetStreamBySubject(request.Subject);

                    var response = new StateReply()
                    {
                        Subject = request.Subject,
                        Events  = stream.ToList()
                    };

                    _stateRequestSocket.SendMoreFrame(sender)
                    .SendFrame(_serializer.Serialize(response));
                }
            }
        }
示例#18
0
文件: Program.cs 项目: cjkao/netmq
        // We have two workers, here we copy the code, normally these would run on different boxes…
        public static void Main()
        {
            var random = new Random(DateTime.Now.Millisecond);
            var workers = new List<Thread>(new[] { new Thread(WorkerTaskA), new Thread(WorkerTaskB) });

            using (var client = new RouterSocket())
            {
                client.Bind($"tcp://localhost:{PortNumber}");

                foreach (var thread in workers)
                {
                    thread.Start(PortNumber);
                }

                // Wait for threads to connect, since otherwise the messages we send won't be routable.
                Thread.Sleep(1000);

                for (int taskNumber = 0; taskNumber < 1000; taskNumber++)
                {
                    // Send two message parts, first the address…
                    client.SendMoreFrame(random.Next(3) > 0 ? Encoding.Unicode.GetBytes("A") : Encoding.Unicode.GetBytes("B"));

                    // And then the workload
                    client.SendFrame("This is the workload");
                }

                client.SendMoreFrame(Encoding.Unicode.GetBytes("A"));
                client.SendFrame("END");

                client.SendMoreFrame(Encoding.Unicode.GetBytes("B"));
                client.SendFrame("END");
            }

            Console.ReadKey();
        }
示例#19
0
        public void Send_WrongServiceNameWithLogging_ShouldLogPermanentError()
        {
            const string hostAddress     = "tcp://localhost:5555";
            var          loggingMessages = new List <string>();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
                using (var poller = new NetMQPoller())
                    using (var session = new MDPClient(hostAddress))
                    {
                        broker.Bind(hostAddress);
                        // we need to pick up any message in order to avoid errors
                        broker.ReceiveReady += (s, e) =>
                        {
                            // just swallow message -> wrong service name
                            e.Socket.ReceiveMultipartMessage();
                        };

                        poller.Add(broker);
                        poller.RunAsync();

                        // set the event handler to receive the logging messages
                        session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                        // well formed message
                        var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                        // wrong service name
                        session.Send("xyz", requestMessage);

                        poller.Stop();

                        Assert.That(loggingMessages.Count, Is.EqualTo(7));
                        Assert.That(loggingMessages[6], Is.EqualTo("[CLIENT ERROR] permanent error, abandoning!"));
                    }
        }
示例#20
0
        /// <summary>
        ///     Starts the server.
        /// </summary>
        public void StartServer()
        {
            using (routerSocket)
            {
                Thread.Sleep(10000);
                logger.Log(LogLevel.Info, $"Trying to bind to {routerConnectionString}");
                routerSocket.Bind(routerConnectionString);
                running = true;
                logger.Log(LogLevel.Info, $"Router socket successfully binded on {routerConnectionString}");
                Console.WriteLine($"Server is bind to {routerConnectionString}");
                while (running)
                {
                    logger.Log(LogLevel.Info, $"Router socket is waiting on message on {routerConnectionString}");
                    var message   = routerSocket.ReceiveMultipartMessage();
                    var accountID = message[2].ConvertToInt32();
                    logger.Log(LogLevel.Info, $"Received request for equity on account ID:{accountID}");

                    var response = new NetMQMessage();

                    using (var ms = new MemoryStream())
                    {
                        var input  = new MyDBContext().Equity.FromSql($"SELECT value FROM dbo.Equity WHERE AccountID={accountID}").Last();
                        var equity = MyUtils.ProtoBufSerialize(input, ms);
                        response.Append(message[0]);
                        response.AppendEmptyFrame();
                        response.Append(equity);
                        routerSocket.SendMultipartMessage(response);
                    }
                }
            }
        }
示例#21
0
        public void SendEmptyReplyFromBrokerWithLoggingShouldThrowApplicationException()
        {
            const string hostAddress     = "tcp://localhost:5555";
            var          loggingMessages = new List <string>();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
                using (var poller = new NetMQPoller())
                    using (var session = new MDPClientAsync(hostAddress))
                    {
                        broker.Bind(hostAddress);
                        // we need to pick up any message in order to avoid errors
                        broker.ReceiveReady += (s, e) =>
                        {
                            // return empty reply
                            var msg = e.Socket.ReceiveMultipartMessage();
                            // we expect to receive a 4 Frame message
                            // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"][requestId]
                            if (msg.FrameCount != 6)
                            {
                                Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                            }
                            // REQUEST socket will strip the his address + empty frame
                            // ROUTER has to add the address prelude in order to identify the correct socket(!)
                            // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"][requestId]

                            e.Socket.SendMultipartMessage(msg);
                        };

                        session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                        session.ReplyReady   += (s, e) =>
                        {
                            Assert.That(loggingMessages.Count, Is.EqualTo(3));
                            Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                            Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                            Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);

                            poller.Stop(); // To unlock the Task.Wait()
                        };

                        poller.Add(broker);
                        var task = Task.Factory.StartNew(() => poller.Run());
                        // well formed message
                        var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                        // correct call
                        session.Send("echo", requestMessage);

                        int timeOutInMillis = 10000;
                        var timer           = new NetMQTimer(timeOutInMillis); // Used so it doesn't block if something goes wrong!
                        timer.Elapsed += (s, e) =>
                        {
                            Assert.Fail($"Waited {timeOutInMillis} and had no response from broker");
                            poller.Stop();
                        };

                        poller.Add(timer);
                        task.Wait();
                    }
        }
示例#22
0
        public void WhisperTest()
        {
            Action <ZreMsg> setMessage = m =>
            {
                m.Id = ZreMsg.MessageId.Whisper;

                m.Whisper.Sequence = 123;

                m.Whisper.Content = new NetMQMessage();
                m.Whisper.Content.Append("Hello");
                m.Whisper.Content.Append("World");
            };

            Action <ZreMsg> checkMessage = m =>
            {
                Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Whisper));
                Assert.That(m.Whisper.Sequence, Is.EqualTo(123));
                Assert.That(m.Whisper.Content.FrameCount, Is.EqualTo(2));
                var str1 = m.Whisper.Content[0].ConvertToString();
                Assert.That(str1, Is.EqualTo("Hello"));
                var str2 = m.Whisper.Content[1].ConvertToString();
                Assert.That(str2, Is.EqualTo("World"));
            };

            using (var client = new DealerSocket())
                using (var server = new RouterSocket())
                {
                    server.Bind("inproc://zprototestWhisper");
                    client.Connect("inproc://zprototestWhisper");

                    ZreMsg clientMessage = new ZreMsg();
                    ZreMsg serverMessage = new ZreMsg();

                    for (int i = 0; i < 2; i++)
                    {
                        // client send message to server
                        setMessage(clientMessage);
                        clientMessage.Send(client);

                        // server receive the message
                        serverMessage.Receive(server);

                        // check that message received ok
                        Assert.That(serverMessage.RoutingId, Is.Not.Null);
                        checkMessage(serverMessage);

                        // reply to client, no need to set the message, using client data
                        serverMessage.Send(server);

                        // client receive the message
                        clientMessage.Receive(client);

                        // check that message received ok
                        Assert.That(clientMessage.RoutingId, Is.Null);
                        checkMessage(clientMessage);
                    }
                }
        }
示例#23
0
        public static void Main(string[] args)
        {
            var workers = new List <Thread>(WORKERS_COUNT);

            using (NetMQContext context = NetMQContext.Create())
            {
                using (RouterSocket client = context.CreateRouterSocket())
                {
                    string cnn = string.Format("tcp://localhost:{0}", PORT_NUMBER);
                    client.Bind(cnn);
                    Console.WriteLine("[B] Connect to {0}", cnn);

                    for (int workerNumber = 0; workerNumber < WORKERS_COUNT; workerNumber++)
                    {
                        workers.Add(new Thread(WorkerTask));
                        workers[workerNumber].Start(PORT_NUMBER);
                    }

                    for (int taskNumber = 0; taskNumber < WORKERS_COUNT * 10; taskNumber++)
                    {
                        //  LRU worker is next waiting in queue
                        string address = client.ReceiveString();
                        //Console.WriteLine("[B] Message received: {0}", address);
                        string empty = client.ReceiveString();
                        //Console.WriteLine("[B] Message received: {0}", empty);
                        string ready = client.ReceiveString();
                        //Console.WriteLine("[B] Message received: {0}", ready);

                        client.SendMore(address);
                        //Console.WriteLine("[B] Message sent: {0}", address);
                        client.SendMore("");
                        //Console.WriteLine("[B] Message sent: {0}", "");
                        client.Send("This is the workload");
                        //Console.WriteLine("[B] Message sent: {0}", "This is the workload");
                    }

                    //  Now ask mamas to shut down and report their results
                    for (int taskNbr = 0; taskNbr < WORKERS_COUNT; taskNbr++)
                    {
                        string address = client.ReceiveString();
                        //Console.WriteLine("[B] Message received: {0}", address);
                        string empty = client.ReceiveString();
                        //Console.WriteLine("[B] Message received: {0}", empty);
                        string ready = client.ReceiveString();
                        //Console.WriteLine("[B] Message received: {0}", ready);

                        client.SendMore(address);
                        //Console.WriteLine("[B] Message sent: {0}", address);
                        client.SendMore("");
                        //Console.WriteLine("[B] Message sent: {0}", "");
                        client.Send("END");
                        //Console.WriteLine("[B] Message sent: {0}", "END");
                    }
                }
            }

            Console.ReadLine();
        }
示例#24
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);
        }
        public void Messages_From_Router_To_Dealer_With_Subscription()
        {
            Console.Write("Test sending message from publisher(router) to subscribers (dealer).\n");

            var maxMessage = 5;
            var cd         = new CountdownEvent(maxMessage);

            string endpoint;

            using (var publisher = new RouterSocket())
                using (var subscriber = new DealerSocket())
                    using (var poller = new NetMQPoller {
                        subscriber
                    })
                    {
                        publisher.Bind("tcp://127.0.0.1:0");
                        endpoint = publisher.Options.LastEndpoint;

                        subscriber.Connect(endpoint);
                        subscriber.ReceiveReady += (sender, e) =>
                        {
                            var strs = e.Socket.ReceiveMultipartStrings();
                            foreach (var str in strs)
                            {
                                Console.WriteLine("Subscribe: " + str);
                            }
                            cd.Signal();
                        };
                        var clientId = Encoding.Unicode.GetBytes("ClientIdTheIsLongerThen32BytesForSureAbsolutelySure");
                        subscriber.Options.Identity = clientId;

                        const string request = "Ping";

                        // Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it
                        // sends at least one message which makes it necessary to open the connection. I believe this is a
                        // low-level feature of the TCP/IP transport.
                        subscriber.SendFrame(request); // Ping.

                        var serverId = publisher.ReceiveFrameBytes();
                        //Assert.AreEqual(request, publisher.ReceiveFrameString());

                        for (var i = 0; i < maxMessage; i++)
                        {
                            var msg = string.Format("[message: {0}]", i);
                            Console.Write("Publish: {0}\n", msg);
                            publisher.SendMoreFrame(serverId).SendFrame(msg);
                            //publisher.SendMoreFrame("").SendFrame(msg);
                        }

                        poller.RunAsync();

                        if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
                        {
                            Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
                        }
                    }
        }
示例#26
0
 /// <summary>
 /// 绑定本地代理
 /// </summary>
 /// <param name="frontAddress"></param>
 /// <param name="backAddress"></param>
 public void Bind(string frontAddress, string backAddress)
 {
     frontEnd = new RouterSocket();
     backEnd  = new DealerSocket();
     frontEnd.Bind(frontAddress);
     backEnd.Bind(backAddress);
     proxy = new Proxy(frontEnd, backEnd);
     Task.Factory.StartNew(proxy.Start);
 }
示例#27
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("NetMQReceiver has been disposed");
            }

            socket.Bind(address.ToString());
        }
示例#28
0
        public NetMQPeer(int bindPort)
        {
            routerSocket = new RouterSocket();
            routerSocket.Bind($"tcp://*:{bindPort}");

            longRunThread = new Thread(new ThreadStart(ServerLoop));
            longRunThread.IsBackground = true;
            longRunThread.Start();
        }
示例#29
0
        public void LeaveTest()
        {
            Action <ZreMsg> setMessage = m =>
            {
                m.Id = ZreMsg.MessageId.Leave;

                m.Leave.Sequence = 123;
                m.Leave.Group    = "Life is short but Now lasts for ever";
                m.Leave.Status   = 123;
            };

            Action <ZreMsg> checkMessage = m =>
            {
                Assert.That(m.Id, Is.EqualTo(ZreMsg.MessageId.Leave));
                Assert.That(m.Leave.Sequence, Is.EqualTo(123));
                Assert.That(m.Leave.Group, Is.EqualTo("Life is short but Now lasts for ever"));
                Assert.That(m.Leave.Status, Is.EqualTo(123));
            };

            using (var client = new DealerSocket())
                using (var server = new RouterSocket())
                {
                    // TODO: If I use inproc://zprototest ReSharper TestRunner fails with NetMQ.AddressAlreadyInUseException :
                    //  Cannot bind address ( inproc://zprototest ) - already in use.
                    //  But only when I run all these tests at the same time.
                    server.Bind("inproc://zprototestLeave");
                    client.Connect("inproc://zprototestLeave");

                    ZreMsg clientMessage = new ZreMsg();
                    ZreMsg serverMessage = new ZreMsg();

                    for (int i = 0; i < 2; i++)
                    {
                        // client send message to server
                        setMessage(clientMessage);
                        clientMessage.Send(client);

                        // server receive the message
                        serverMessage.Receive(server);

                        // check that message received ok
                        Assert.That(serverMessage.RoutingId, Is.Not.Null);
                        checkMessage(serverMessage);

                        // reply to client, no need to set the message, using client data
                        serverMessage.Send(server);

                        // client receive the message
                        clientMessage.Receive(client);

                        // check that message received ok
                        Assert.That(clientMessage.RoutingId, Is.Null);
                        checkMessage(clientMessage);
                    }
                }
        }
示例#30
0
        public void ReceiveImplicitConnect_ValidScenario_ShouldReturnRequest()
        {
            const string hostAddress     = "tcp://localhost:5557";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
                using (var poller = new NetMQPoller())
                    using (var session = new MDPWorker(hostAddress, "test", new[] { (byte)'1' }))
                    {
                        broker.Bind(hostAddress);
                        // we need to pick up any message in order to avoid errors
                        broker.ReceiveReady += (s, e) =>
                        {
                            var msg = e.Socket.ReceiveMultipartMessage();
                            // we expect to receive a 5 Frame message
                            // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                            if (msg.FrameCount != 5)
                            {
                                Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                            }
                            // make sure the frames are as expected
                            Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                            Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                            Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                            Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Ready));
                            Assert.That(msg[4].ConvertToString(), Is.EqualTo("test"));

                            // tell worker to stop gracefully
                            var reply = new NetMQMessage();
                            reply.Push(new[] { (byte)MDPCommand.Kill });
                            // push MDP Version
                            reply.Push(msg[2]);
                            // push separator
                            reply.Push(NetMQFrame.Empty);
                            // push worker address
                            reply.Push(msg[0]);
                            // send reply which is a request for the worker
                            e.Socket.SendMultipartMessage(reply);
                        };

                        poller.Add(broker);
                        poller.RunAsync();

                        // set the event handler to receive the logging messages
                        session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                        // initialise the worker - broker protocol
                        session.Receive(null);

                        Assert.That(loggingMessages.Count, Is.EqualTo(5));
                        Assert.That(loggingMessages[0], Is.EqualTo("[WORKER] connected to broker at tcp://localhost:5557"));
                        Assert.That(loggingMessages[1].Contains("[WORKER] sending"), Is.True);
                        Assert.That(loggingMessages[2].Contains("[WORKER] received"));
                        Assert.That(loggingMessages[4].Contains("abandoning"));
                    }
        }
示例#31
0
 protected override void OnShimCommand(string command)
 {
     switch (command)
     {
     case BindCommand:
         string addresss = Shim.ReceiveFrameString();
         m_serverSocket.Bind(addresss);
         break;
     }
 }
示例#32
0
文件: Program.cs 项目: GrabCAD/netmq
        public static void Main()
        {
            var workers = new List<Thread>(WorkersCount);

            using (var client = new RouterSocket())
            {
                string cnn = $"tcp://localhost:{PortNumber}";
                client.Bind(cnn);
                Console.WriteLine("[B] Connect to {0}", cnn);

                for (int workerNumber = 0; workerNumber < WorkersCount; workerNumber++)
                {
                    workers.Add(new Thread(WorkerTask));
                    workers[workerNumber].Start(PortNumber);
                }

                for (int taskNumber = 0; taskNumber < WorkersCount*10; taskNumber++)
                {
                    // LRU worker is next waiting in queue
                    string address = client.ReceiveFrameString();
                    //Console.WriteLine("[B] Message received: {0}", address);
                    string empty = client.ReceiveFrameString();
                    //Console.WriteLine("[B] Message received: {0}", empty);
                    string ready = client.ReceiveFrameString();
                    //Console.WriteLine("[B] Message received: {0}", ready);

                    client.SendMoreFrame(address);
                    //Console.WriteLine("[B] Message sent: {0}", address);
                    client.SendMoreFrame("");
                    //Console.WriteLine("[B] Message sent: {0}", "");
                    client.SendFrame("This is the workload");
                    //Console.WriteLine("[B] Message sent: {0}", "This is the workload");
                }

                // Now ask mamas to shut down and report their results
                for (int taskNbr = 0; taskNbr < WorkersCount; taskNbr++)
                {
                    string address = client.ReceiveFrameString();
                    //Console.WriteLine("[B] Message received: {0}", address);
                    string empty = client.ReceiveFrameString();
                    //Console.WriteLine("[B] Message received: {0}", empty);
                    string ready = client.ReceiveFrameString();
                    //Console.WriteLine("[B] Message received: {0}", ready);

                    client.SendMoreFrame(address);
                    //Console.WriteLine("[B] Message sent: {0}", address);
                    client.SendMoreFrame("");
                    //Console.WriteLine("[B] Message sent: {0}", "");
                    client.SendFrame("END");
                    //Console.WriteLine("[B] Message sent: {0}", "END");
                }
            }

            Console.ReadLine();
        }
示例#33
0
        public void ReceiveImplicitConnect_ValidScenario_ShouldReturnRequest ()
        {
            const string hostAddress = "tcp://localhost:5557";
            var loggingMessages = new List<string> ();

            // setup the counter socket for communication
            using (var broker = new RouterSocket ())
            using (var poller = new NetMQPoller ())
            using (var session = new MDPWorker (hostAddress, "test", new[] { (byte) '1' }))
            {
                broker.Bind (hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage ();
                    // we expect to receive a 5 Frame message
                    // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                    if (msg.FrameCount != 5)
                        Assert.Fail ("Message with wrong count of frames {0}", msg.FrameCount);
                    // make sure the frames are as expected
                    Assert.That (msg[1], Is.EqualTo (NetMQFrame.Empty));
                    Assert.That (msg[2].ConvertToString (), Is.EqualTo ("MDPW01"));
                    Assert.That (msg[3].BufferSize, Is.EqualTo (1));
                    Assert.That (msg[3].Buffer[0], Is.EqualTo ((byte) MDPCommand.Ready));
                    Assert.That (msg[4].ConvertToString (), Is.EqualTo ("test"));

                    // tell worker to stop gracefully
                    var reply = new NetMQMessage ();
                    reply.Push (new[] { (byte) MDPCommand.Kill });
                    // push MDP Version
                    reply.Push (msg[2]);
                    // push separator
                    reply.Push (NetMQFrame.Empty);
                    // push worker address
                    reply.Push (msg[0]);
                    // send reply which is a request for the worker
                    e.Socket.SendMultipartMessage (reply);
                };

                poller.Add (broker);
                poller.RunAsync ();

                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add (e.Info);
                // initialise the worker - broker protocol
                session.Receive (null);

                Assert.That (loggingMessages.Count, Is.EqualTo (5));
                Assert.That (loggingMessages[0], Is.EqualTo ("[WORKER] connected to broker at tcp://localhost:5557"));
                Assert.That (loggingMessages[1].Contains ("[WORKER] sending"), Is.True);
                Assert.That (loggingMessages[2].Contains ("[WORKER] received"));
                Assert.That (loggingMessages[4].Contains ("abandoning"));
            }
        }
示例#34
0
        public void Send_CorrectInputWithLogging_ShouldReturnCorrectReply()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClient(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>

                {
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // we expect to receive a 4 Frame message
                    // [client adrR][e][mdp header][service][request]
                    if (msg.FrameCount != 5)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [client adr][e][mdp header][service][reply]
                    var request = msg.Last.ConvertToString(); // get the request string
                    msg.RemoveFrame(msg.Last); // remove the request frame
                    msg.Append(new NetMQFrame(request + " OK")); // append the reply frame
                    e.Socket.SendMultipartMessage(msg);
                };

                poller.Add(broker);
                poller.RunAsync();
                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                // well formed message
                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                // correct call
                var reply = session.Send("echo", requestMessage);

                poller.Stop();

                Assert.That(reply.FrameCount, Is.EqualTo(1));
                Assert.That(reply.First.ConvertToString(), Is.EqualTo("REQUEST OK"));
                Assert.That(loggingMessages.Count, Is.EqualTo(3));
                Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);
            }
        }
示例#35
0
        public void ReceiveMessageFailedIfWasNotProcessed()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();
            var serviceName = "echo";
            Guid requestId = Guid.Empty;

            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClientAsync(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    // doesn't reply to client
                    var msg = e.Socket.ReceiveMultipartMessage();
                };

                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                session.ReplyReady += (s, e) =>
                {
                    Assert.IsTrue(false, "I'm not supposed to receive replies since broker is not sending them");
                    poller.Stop();
                };
                session.FailedRequest += (s, e) =>
                {
                    Assert.That(requestId, Is.Not.EqualTo(Guid.Empty));
                    Assert.That(e.RequestId, Is.EqualTo(requestId));
                    poller.Stop();
                };

                poller.Add(broker);
                var task = Task.Factory.StartNew(() => poller.Run());

                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });

                requestId = session.Send(serviceName, requestMessage);
                var result = task.Wait(session.Timeout + session.Timeout);
                Assert.IsTrue(result, $"During {session.Timeout}ms was not received a FailedReply");
            }
        }
示例#36
0
文件: Program.cs 项目: GrabCAD/netmq
        private static void Main()
        {
            const int messageCount = 1000000;
            const int dealerCount = 100;

            Console.WriteLine("Sending {0} messages to {1} dealers", messageCount, dealerCount);

            //BufferPool.SetBufferManagerBufferPool(1024 * 1024 * 10, 1024);

            using (var router = new RouterSocket())
            {
                router.Options.SendHighWatermark = 0;
                router.Bind("tcp://*:5555");

                var dealers = new List<DealerSocket>();
                var identities = new List<Msg>();
                var random = new Random();
                var identity = new byte[50];

                for (var i = 0; i < dealerCount; i++)
                {
                    random.NextBytes(identity);
                    var dealer = new DealerSocket
                    {
                        Options =
                        {
                            Identity = identity.Skip(10).ToArray(),
                            ReceiveHighWatermark = 0
                        }
                    };

                    dealer.Connect("tcp://localhost:5555");

                    dealers.Add(dealer);
                    var msg = new Msg();
                    msg.InitGC(identity, 10, identity.Length); // test offsets
                    identities.Add(msg);
                }

                Thread.Sleep(500);

                while (!Console.KeyAvailable)
                {
                    Thread.Sleep(500);

                    var stopwatch = Stopwatch.StartNew();

                    for (var i = 0; i < messageCount; i++)
                    {
                        var msg = identities[i%identities.Count];
                        router.Send(ref msg, true);
                        var msg2 = new Msg();
                        msg2.InitPool(1);
                        msg2.Put((byte) 'E');
                        router.Send(ref msg2, false);
                    }

                    stopwatch.Stop();

                    Console.WriteLine("{0:N1} messages sent per second", messageCount/stopwatch.Elapsed.TotalSeconds);
                }

                foreach (var dealerSocket in dealers)
                    dealerSocket.Dispose();
            }
        }
示例#37
0
        public void ReconnectToBrokerIfIsNotReplying()
        {
            const string hostAddress = "tcp://localhost:5555";
            const int timeOutToReconnectInMillis = 7500;
            var loggingMessages = new List<string>();
            var serviceName = "echo";
            var messagesReceivedOnBroker = 0;

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClientAsync(hostAddress))
            {
                session.Timeout = TimeSpan.FromMilliseconds(timeOutToReconnectInMillis);
                broker.Bind(hostAddress);
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage();
                    if (messagesReceivedOnBroker != 0) // doesn't respond if is the first message received!
                    {
                        // we expect to receive a 4 Frame message
                        // [client adrR][e][mdp header][service][request]
                        if (msg.FrameCount != 6)
                            Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                        // REQUEST socket will strip the his address + empty frame
                        // ROUTER has to add the address prelude in order to identify the correct socket(!)

                        var requestId = msg.Last.ConvertToString(); // get the requestId string
                        msg.RemoveFrame(msg.Last); // remove the request frame

                        var request = msg.Last.ConvertToString(); // get the request string
                        msg.RemoveFrame(msg.Last); // remove the request frame

                        msg.Append(new NetMQFrame(request + " OK")); // append the reply frame
                        msg.Append(requestId);
                        e.Socket.SendMultipartMessage(msg);
                    }
                    messagesReceivedOnBroker++;
                };

                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                session.ReplyReady += (s, e) =>
                {
                    var reply = e.Reply;

                    Assert.That(reply.FrameCount, Is.EqualTo(1));
                    Assert.That(reply.First.ConvertToString(), Is.EqualTo("REQUEST OK"));

                    poller.Stop();
                };

                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });

                int timeOutInMillis = timeOutToReconnectInMillis + 3000; // Waits for the timeOut on the client
                var timer = new NetMQTimer(timeOutInMillis);
                timer.Elapsed += (s, e) =>
                {
                    session.Send(serviceName, requestMessage); // resends the request after timeout
                };

                poller.Add(timer);
                poller.Add(broker);
                var task = Task.Factory.StartNew(() => poller.Run());

                session.Send(serviceName, requestMessage);

                var result = task.Wait(timeOutToReconnectInMillis * 2);

                var numberOfConnects = loggingMessages.FindAll(x => x.Contains("[CLIENT] connecting to broker")).Count;
                Assert.IsTrue(numberOfConnects > 1);
            }
        }
示例#38
0
        public void SendCorrectInputWithLoggingShouldReturnCorrectReply()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();
            var serviceName = "echo";

            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClientAsync(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // we expect to receive a 4 Frame message
                    // [client adrR][e][mdp header][service][request]
                    if (msg.FrameCount != 6)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [client adr][e][mdp header][service][reply][requestId]

                    var requestId = msg.Last.ConvertToString(); // get the requestId string
                    msg.RemoveFrame(msg.Last); // remove the request frame

                    var request = msg.Last.ConvertToString(); // get the request string
                    msg.RemoveFrame(msg.Last); // remove the request frame

                    msg.Append(new NetMQFrame(request + " OK")); // append the reply frame
                    msg.Append(requestId);
                    e.Socket.SendMultipartMessage(msg);
                };

                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                session.ReplyReady += (s, e) =>
                {
                    var reply = e.Reply;

                    Assert.That(reply.FrameCount, Is.EqualTo(1));
                    Assert.That(reply.First.ConvertToString(), Is.EqualTo("REQUEST OK"));

                    poller.Stop();
                };
                int timeOutInMillis = 10000;
                var timer = new NetMQTimer(timeOutInMillis); // Used so it doesn't block if something goes wrong!
                timer.Elapsed += (s, e) =>
                {
                    Assert.Fail($"Waited {timeOutInMillis} and had no response from broker");
                    poller.Stop();
                };

                poller.Add(broker);
                poller.Add(timer);
                var task = Task.Factory.StartNew(() => poller.Run());

                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });

                session.Send(serviceName, requestMessage);

                task.Wait();

                Assert.That(loggingMessages.Count, Is.EqualTo(3));
                Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);
            }
        }
示例#39
0
文件: Program.cs 项目: sharpe5/netmq
        /// <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();
            }
        }
示例#40
0
文件: Program.cs 项目: cjkao/netmq
        private static void Main()
        {
            using (var frontend = new RouterSocket())
            using (var backend = new RouterSocket())
            {
                // For Clients
                Console.WriteLine("Q: Binding frontend {0}", FrontendEndpoint);
                frontend.Bind(FrontendEndpoint);

                // For Workers
                Console.WriteLine("Q: Binding backend {0}", BackendEndpoint);
                backend.Bind(BackendEndpoint);

                // Logic of LRU loop
                // - Poll backend always, frontend only if 1+ worker ready
                // - If worker replies, queue worker as ready and forward reply
                //   to client if necessary
                // - If client requests, pop next worker and send request to it

                // Queue of available workers
                var workerQueue = new Queue<byte[]>();

                // Handle worker activity on backend
                backend.ReceiveReady += (s, e) =>
                {
                    // Queue worker address for LRU routing
                    byte[] workerAddress = e.Socket.ReceiveFrameBytes();

                    // Use worker address for LRU routing
                    workerQueue.Enqueue(workerAddress);

                    // Second frame is empty
                    e.Socket.SkipFrame();

                    // Third frame is READY or else a client reply address
                    byte[] clientAddress = e.Socket.ReceiveFrameBytes();

                    // If client reply, send rest back to frontend
                    // Forward message to client if it's not a READY
                    if (Encoding.Unicode.GetString(clientAddress) != LRUReady)
                    {
                        e.Socket.SkipFrame(); // empty

                        byte[] reply = e.Socket.ReceiveFrameBytes();

                        frontend.SendMoreFrame(clientAddress);
                        frontend.SendMoreFrame("");
                        frontend.SendFrame(reply);
                    }
                };

                frontend.ReceiveReady += (s, e) =>
                {
                    // Now get next client request, route to next worker
                    // Dequeue and drop the next worker address

                    // Now get next client request, route to LRU worker
                    // Client request is [address][empty][request]
                    byte[] clientAddr = e.Socket.ReceiveFrameBytes();
                    e.Socket.SkipFrame(); // empty
                    byte[] request = e.Socket.ReceiveFrameBytes();

                    try
                    {
                        byte[] deq = workerQueue.Dequeue();
                        backend.SendMoreFrame(deq);
                        backend.SendMoreFrame(Encoding.Unicode.GetBytes(""));
                        backend.SendMoreFrame(clientAddr);
                        backend.SendMoreFrame(Encoding.Unicode.GetBytes(""));
                        backend.SendFrame(request);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Q: [FrontendOnReceiveReady] Dequeue exception: {0}", ex.ToString());
                    }
                };

                while (true)
                {
                    backend.Poll(TimeSpan.FromMilliseconds(500));

                    if (workerQueue.Count > 0)
                        frontend.Poll(TimeSpan.FromMilliseconds(500));
                }
            }
        }
示例#41
0
文件: Program.cs 项目: cjkao/netmq
        /// <summary>
        /// ParanoidPirate.Queue [-v]
        ///
        /// Does load-balancing with heartbeating on worker tasks to detect
        /// crashed, blocked or slow running worker tasks    .
        /// </summary>
        private static void Main(string[] args)
        {
            Console.Title = "NetMQ ParanoidPirate Queue";

            // serves as flag for exiting the program
            var exit = false;
            // catch CTRL+C as exit command
            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                exit = true;
            };

            var verbose = args.Length > 0 && args[0] == "-v";

            using (var frontend = new RouterSocket())
            using (var backend = new RouterSocket())
            using (var poller = new NetMQPoller())
            {
                frontend.Bind(Commons.QueueFrontend);
                backend.Bind(Commons.QueueBackend);

                var workers = new Workers();

                // client sends to this socket
                frontend.ReceiveReady += (s, e) =>
                {
                    // only process incoming client requests
                    // if we have workers available handle client requests as long as we have workers
                    // storage capability of the socket otherwise and pick up later
                    if (workers.Available)
                    {
                        // get all message frames!
                        var request = frontend.ReceiveMultipartMessage();

                        if (verbose)
                            Console.WriteLine("[QUEUE] received {0}", request);

                        // get next available worker
                        var worker = workers.Next();
                        // wrap message with worker's address
                        var msg = Wrap(worker, request);

                        if (verbose)
                            Console.WriteLine("[QUEUE -> WORKER] sending {0}", msg);

                        backend.SendMultipartMessage(msg);
                    }
                };

                // worker sends to this socket
                backend.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage();

                    if (verbose)
                        Console.WriteLine("[QUEUE <- WORKER] received {0}", msg);

                    // use workers identity for load-balancing
                    var workerIdentity = Unwrap(msg);
                    var worker = new Worker(workerIdentity);
                    workers.Ready(worker);
                    // just convenience
                    var readableWorkerId = workerIdentity.ConvertToString();

                    if (msg.FrameCount == 1)
                    {
                        var data = msg[0].ConvertToString();
                        // the message is either READY or HEARTBEAT or corrupted
                        switch (data)
                        {
                            case Commons.PPPHeartbeat:
                                Console.WriteLine("[QUEUE <- WORKER] Received a Heartbeat from {0}",
                                    readableWorkerId);
                                break;
                            case Commons.PPPReady:
                                Console.WriteLine("[QUEUE <- WORKER] Received a READY form {0}",
                                    readableWorkerId);
                                break;
                            default:
                                Console.WriteLine("[QUEUE <- WORKER] ERROR received an invalid message!");
                                break;
                        }
                    }
                    else
                    {
                        if (verbose)
                            Console.WriteLine("[QUEUE -> CLIENT] sending {0}", msg);

                        frontend.SendMultipartMessage(msg);
                    }
                };

                var timer = new NetMQTimer(Commons.HeartbeatInterval);
                // every specified ms QUEUE shall send a heartbeat to all connected workers
                timer.Elapsed += (s, e) =>
                {
                    // send heartbeat to every worker
                    foreach (var worker in workers)
                    {
                        var heartbeat = new NetMQMessage();

                        heartbeat.Push(new NetMQFrame(Commons.PPPHeartbeat));
                        heartbeat.Push(worker.Identity);

                        Console.WriteLine("[QUEUE -> WORKER] sending heartbeat!");

                        backend.SendMultipartMessage(heartbeat);
                    }
                    // restart timer
                    e.Timer.Enable = true;
                    // remove all dead or expired workers
                    workers.Purge();
                };

                if (verbose)
                    Console.WriteLine("[QUEUE] Start listening!");

                poller.Add(frontend);
                poller.Add(backend);

                poller.RunAsync();

                // hit CRTL+C to stop the while loop
                while (!exit)
                    Thread.Sleep(100);
            }
        }
示例#42
0
        static void Main(string[] args)
        {
            using (var context = NetMQContext.Create())
            {
                frontend = context.CreateRouterSocket();
                backend = context.CreateRouterSocket();

                // For Clients
                Console.WriteLine("Q: Binding frontend {0}", FRONTEND_ENDPOINT);
                frontend.Bind(FRONTEND_ENDPOINT);

                // For Workers
                Console.WriteLine("Q: Binding backend {0}", BACKEND_ENDPOINT);
                backend.Bind(BACKEND_ENDPOINT);

                //  Logic of LRU loop
                //  - Poll backend always, frontend only if 1+ worker ready
                //  - If worker replies, queue worker as ready and forward reply
                //    to client if necessary
                //  - If client requests, pop next worker and send request to it

                //  Queue of available workers
                workerQueue = new Queue<byte[]>();

                //  Handle worker activity on backend
                backend.ReceiveReady += BackendOnReceiveReady;
                frontend.ReceiveReady += FrontendOnReceiveReady;

                while (true)
                {
                    backend.Poll(TimeSpan.FromMilliseconds(500));
                    if (workerQueue.Count > 0)
                        frontend.Poll(TimeSpan.FromMilliseconds(500));
                }
            }
        }
示例#43
0
        public void Send_WrongServiceNameWithLogging_ShouldLogPermanentError()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClient(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    // just swallow message -> wrong service name
                    e.Socket.ReceiveMultipartMessage();
                };

                poller.Add(broker);
                poller.RunAsync();

                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                // well formed message
                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                // wrong service name
                session.Send("xyz", requestMessage);

                poller.Stop();

                Assert.That(loggingMessages.Count, Is.EqualTo(7));
                Assert.That(loggingMessages[6], Is.EqualTo("[CLIENT ERROR] permanent error, abandoning!"));
            }
        }
示例#44
0
        public void Send_EmptyReplyFromBrokerWithLogging_ShouldThrowApplicationException()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClient(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    // return empty reply
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // we expect to receive a 4 Frame message
                    // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                    if (msg.FrameCount != 5)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                    e.Socket.SendMultipartMessage(msg);
                };

                poller.Add(broker);
                poller.RunAsync();

                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                // well formed message
                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                // correct call
                session.Send("echo", requestMessage);

                poller.Stop();

                Assert.That(loggingMessages.Count, Is.EqualTo(3));
                Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);
            }
        }
示例#45
0
        public void Send_WrongHeaderFromBrokerNoLogging_ShouldThrowApplicationException()
        {
            const string hostAddress = "tcp://localhost:5555";

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClient(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    // return empty reply
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // we expect to receive a 4 Frame message
                    // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                    if (msg.FrameCount != 5)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [REQ ADR][EMPTY]["MDPC00"]["echo"]["REQUEST"]
                    var clientAddress = msg.Pop();
                    msg.Pop(); // forget empty frame
                    var mdpVersion = msg.Pop();
                    msg.Pop(); // drop service name version
                    msg.Push("NoService");
                    msg.Push(mdpVersion);
                    msg.Push(NetMQFrame.Empty);
                    msg.Push(clientAddress); // reinsert the client's address

                    e.Socket.SendMultipartMessage(msg);
                };

                poller.Add(broker);
                poller.RunAsync();

                // well formed message
                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });

                try
                {
                    session.Send("echo", requestMessage);
                }
                catch (ApplicationException ex)
                {
                    Assert.That(ex.Message, Is.EqualTo("[CLIENT INFO] answered by wrong service: NoService"));
                }
            }
        }
示例#46
0
        public void Receive_BrokerDisconnectedWithLogging_ShouldReturnRequest ()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string> ();

            // setup the counter socket for communication
            using (var broker = new RouterSocket ())
            using (var poller = new NetMQPoller ())
            using (var session = new MDPWorker (hostAddress, "test"))
            {
                broker.Bind (hostAddress);
                // we need to pick up any message in order to avoid errors but don't answer
                broker.ReceiveReady += (s, e) => e.Socket.ReceiveMultipartMessage ();

                poller.Add (broker);
                poller.RunAsync ();

                // speed up the test
                session.HeartbeatDelay = TimeSpan.FromMilliseconds (250);
                session.ReconnectDelay = TimeSpan.FromMilliseconds (250);
                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add (e.Info);
                // initialise the worker - broker protocol
                session.Receive (null);

                poller.Stop ();

                Assert.That (loggingMessages.Count (m => m.Contains ("retrying")), Is.EqualTo (3));
                // 3 times retrying and 1 time initial connecting
                Assert.That (loggingMessages.Count (m => m.Contains ("localhost")), Is.EqualTo (4));
                Assert.That (loggingMessages.Last ().Contains ("abandoning"));
            }
        }
示例#47
0
        public void SendWrongMDPVersionFromBrokerNoLoggingShouldThrowApplicationException()
        {
            const string hostAddress = "tcp://localhost:5555";

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClientAsync(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    // return empty reply
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // we expect to receive a 4 Frame message
                    // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"][requestId]
                    if (msg.FrameCount != 6)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [REQ ADR][EMPTY]["MDPC00"]["echo"]["REQUEST"][requestId]
                    var clientAddress = msg.Pop();
                    msg.Pop(); // forget empty frame
                    msg.Pop(); // drop the MDP Version Frame
                    msg.Push("MDPC00"); // insert wrong MDP version
                    msg.Push(NetMQFrame.Empty);
                    msg.Push(clientAddress); // reinsert the client's address

                    e.Socket.SendMultipartMessage(msg);
                };

                int timeOutInMillis = 10000;
                var timer = new NetMQTimer(timeOutInMillis); // Used so it doesn't block if something goes wrong!
                timer.Elapsed += (s, e) =>
                {
                    Assert.Fail($"Waited {timeOutInMillis} and had no response from broker");
                    poller.Stop();
                };

                poller.Add(broker);
                poller.Add(timer);

                session.ReplyReady += (s, e) =>
                {
                    Assert.True(e.HasError());
                    Assert.That(e.Exception.Message, Is.StringContaining("MDP Version mismatch"));
                    poller.Stop(); // To unlock the Task.Wait()
                };

                session.FailedRequest += (s, e) =>
                {
                    Assert.True(e.HasError());
                    Assert.That(e.Exception.Message, Is.StringContaining("MDP Version mismatch"));
                    poller.Stop(); // To unlock the Task.Wait()
                };

                var task = Task.Factory.StartNew(() => poller.Run());

                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                session.Send("echo", requestMessage);

                task.Wait();
            }
        }
示例#48
0
        public void Receive_RequestWithTooLittleFrames_ShouldThrowApplicationException ()
        {
            const string hostAddress = "tcp://localhost:5555";

            // setup the counter socket for communication
            using (var broker = new RouterSocket ())
            using (var poller = new NetMQPoller ())
            using (var session = new MDPWorker (hostAddress, "test"))
            {
                broker.Bind (hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage ();
                    // we expect to receive a 5 Frame message
                    // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                    if (msg.FrameCount != 5)
                        Assert.Fail ("Message with wrong count of frames {0}", msg.FrameCount);
                    // make sure the frames are as expected
                    Assert.That (msg[1], Is.EqualTo (NetMQFrame.Empty));
                    Assert.That (msg[2].ConvertToString (), Is.EqualTo ("MDPW01"));
                    Assert.That (msg[3].BufferSize, Is.EqualTo (1));
                    Assert.That (msg[3].Buffer[0], Is.EqualTo ((byte) MDPCommand.Ready));
                    Assert.That (msg[4].ConvertToString (), Is.EqualTo ("test"));

                    // tell worker to stop gracefully
                    var reply = new NetMQMessage ();
                    reply.Push (new[] { (byte) MDPCommand.Kill });
                    // push separator
                    reply.Push (NetMQFrame.Empty);
                    // push worker address
                    reply.Push (msg[0]);
                    // send reply which is a request for the worker
                    e.Socket.SendMultipartMessage (reply);
                };

                poller.Add (broker);
                poller.RunAsync ();

                try
                {
                    session.Receive (null);
                }
                catch (ApplicationException ex)
                {
                    Assert.That (ex.Message, Is.EqualTo ("Malformed request received!"));
                }
            }
        }
示例#49
0
        public void SendEmptyReplyFromBrokerWithLoggingShouldThrowApplicationException()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();

            // setup the counter socket for communication
            using (var broker = new RouterSocket())
            using (var poller = new NetMQPoller())
            using (var session = new MDPClientAsync(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    // return empty reply
                    var msg = e.Socket.ReceiveMultipartMessage();
                    // we expect to receive a 4 Frame message
                    // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"][requestId]
                    if (msg.FrameCount != 6)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"][requestId]

                    e.Socket.SendMultipartMessage(msg);
                };

                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                session.ReplyReady += (s, e) =>
                {
                    Assert.That(loggingMessages.Count, Is.EqualTo(3));
                    Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                    Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                    Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);

                    poller.Stop(); // To unlock the Task.Wait()
                };

                poller.Add(broker);
                var task = Task.Factory.StartNew(() => poller.Run());
                // well formed message
                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                // correct call
                session.Send("echo", requestMessage);

                int timeOutInMillis = 10000;
                var timer = new NetMQTimer(timeOutInMillis); // Used so it doesn't block if something goes wrong!
                timer.Elapsed += (s, e) =>
                {
                    Assert.Fail($"Waited {timeOutInMillis} and had no response from broker");
                    poller.Stop();
                };

                poller.Add(timer);
                task.Wait();
            }
        }
示例#50
0
        public void Receive_RequestWithWrongMDPComand_ShouldLogCorrectMessage ()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string> ();
            var first = true;

            // setup the counter socket for communication
            using (var broker = new RouterSocket ())
            using (var poller = new NetMQPoller ())
            using (var session = new MDPWorker (hostAddress, "test", Encoding.ASCII.GetBytes ("Worker"), 2))
            {
                broker.Bind (hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage ();
                    // we expect to receive a 5 Frame message
                    // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                    if (msg.FrameCount != 5)
                        return; // it is a HEARTBEAT
                    // make sure the frames are as expected
                    Assert.That (msg[1], Is.EqualTo (NetMQFrame.Empty));
                    Assert.That (msg[2].ConvertToString (), Is.EqualTo ("MDPW01"));
                    Assert.That (msg[3].BufferSize, Is.EqualTo (1));
                    Assert.That (msg[3].Buffer[0], Is.EqualTo ((byte) MDPCommand.Ready));
                    Assert.That (msg[4].ConvertToString (), Is.EqualTo ("test"));

                    // tell worker to stop gracefully
                    var reply = new NetMQMessage ();
                    if (first)
                    {
                        reply.Push (new[] { (byte) 0xff });
                        first = false;
                    }
                    else
                        reply.Push (new[] { (byte) MDPCommand.Kill });
                    // push MDP Version
                    reply.Push ("MDPW01");
                    // push separator
                    reply.Push (NetMQFrame.Empty);
                    // push worker address
                    reply.Push (msg[0]);
                    // send reply which is a request for the worker
                    e.Socket.SendMultipartMessage (reply);
                };
                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add (e.Info);

                poller.Add (broker);
                poller.RunAsync ();

                session.HeartbeatDelay = TimeSpan.FromMilliseconds (250);
                session.ReconnectDelay = TimeSpan.FromMilliseconds (250);
                // initialise the worker - broker protocol
                session.Receive (null);

                Assert.That (loggingMessages.Count (m => m.Contains ("[WORKER ERROR] invalid command received")), Is.EqualTo (1));
                Assert.That (loggingMessages.Count (m => m.Contains ("abandoning")), Is.EqualTo (1));
            }
        }
示例#51
0
        public void Receive_REPLYtoREQUEST_ShouldSendCorrectReply ()
        {
            const string hostAddress = "tcp://localhost:5557";
            var loggingMessages = new List<string> ();

            // setup the counter socket for communication
            using (var broker = new RouterSocket ())
            using (var poller = new NetMQPoller ())
            using (var session = new MDPWorker (hostAddress, "test", new[] { (byte) 'W', (byte) '1' }))
            {
                broker.Bind (hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage ();
                    if (msg[3].Buffer[0] == (byte) MDPCommand.Ready)
                    {
                        // this is a READY message and we
                        // send REQUEST message
                        var request = new NetMQMessage ();
                        request.Push ("echo test"); // [request]
                        request.Push (NetMQFrame.Empty); // [e][request]
                        request.Push ("C1"); // [client adr][e][request]
                        request.Push (new[] { (byte) MDPCommand.Request }); // [command][client adr][e][request]
                        request.Push (msg[2]); // [header][command][client adr][e][request]
                        request.Push (NetMQFrame.Empty); // [e][header][command][client adr][e][request]
                        request.Push (msg[0]); // [worker adr][e][header][command][client adr][e][request]
                        // send reply which is a request for the worker
                        e.Socket.SendMultipartMessage (request);
                    }

                    if (msg[3].Buffer[0] == (byte) MDPCommand.Reply)
                    {
                        // we expect to receive
                        // [WORKER ADR][e]["MDPW01"][REPLY][CLIENT ADR][e][request == "echo test"]
                        // make sure the frames are as expected
                        Assert.That (msg[0].ConvertToString (), Is.EqualTo ("W1"));
                        Assert.That (msg[1], Is.EqualTo (NetMQFrame.Empty));
                        Assert.That (msg[2].ConvertToString (), Is.EqualTo ("MDPW01"));
                        Assert.That (msg[3].BufferSize, Is.EqualTo (1));
                        Assert.That (msg[3].Buffer[0], Is.EqualTo ((byte) MDPCommand.Reply));
                        Assert.That (msg[4].ConvertToString (), Is.EqualTo ("C1"));
                        Assert.That (msg[5], Is.EqualTo (NetMQFrame.Empty));
                        Assert.That (msg[6].ConvertToString (), Is.EqualTo ("echo test"));

                        // tell worker to stop gracefully
                        var reply = new NetMQMessage ();
                        reply.Push (new[] { (byte) MDPCommand.Kill });
                        // push MDP Version
                        reply.Push (msg[2]);
                        // push separator
                        reply.Push (NetMQFrame.Empty);
                        // push worker address
                        reply.Push (msg[0]);
                        // send reply which is a request for the worker
                        e.Socket.SendMultipartMessage (reply);
                    }
                };

                poller.Add (broker);
                poller.RunAsync ();

                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add (e.Info);
                // initialise the worker - broker protocol
                // and get initial request
                var workerRequest = session.Receive (null);
                // just echo the request
                session.Receive (workerRequest);

                poller.Stop ();

                Assert.That (loggingMessages.Count, Is.EqualTo (8));
                Assert.That (loggingMessages[0], Is.EqualTo ("[WORKER] connected to broker at tcp://localhost:5557"));
                Assert.That (loggingMessages[1].Contains ("Ready"));
                Assert.That (loggingMessages[2].Contains ("[WORKER] received"));
                Assert.That (loggingMessages[3].Contains ("Request"));
                Assert.That (loggingMessages[4].Contains ("Reply"));
                Assert.That (loggingMessages[6].Contains ("Kill"));
                Assert.That (loggingMessages[7].Contains ("abandoning"));
            }
        }