Add() public method

public Add ( ISocketPollable socket ) : void
socket ISocketPollable
return void
        static void Main(string[] args)
        {
            SubscriberSocket socket = new SubscriberSocket(">tcp://localhost:1012");

            socket.Subscribe("aaa");
            socket.ReceiveReady += Socket_ReceiveReady;

            NetMQ.NetMQPoller poller = new NetMQ.NetMQPoller();
            poller.Add(socket);

            poller.RunAsync();


            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Thread.Sleep(10 * 1000);

                    var tmp = d;
                    d       = new List <TimeSpan>();

                    if (tmp.Any())
                    {
                        Console.WriteLine($"{DateTime.Now}\t{tmp.Count / 10}/s\t{tmp.Average(x => x.TotalMilliseconds)}");
                    }
                    else
                    {
                        Console.WriteLine("-");
                    }
                }
            });

            Console.Read();
        }
Esempio n. 2
0
        ///<summary>
        /// necessary indirection for the responsehandler to be used in sync or async fashion
        ///</summary>
        private XtResult SetupResponder <T, TResult>(ResponseHandler <T, TResult> handler, CancellationToken token)
            where T : class, new()
            where TResult : class
        {
            lock (concurrencyToken)
            {
                if (responderIsSetup)
                {
                    throw new ZeroMqXtSocketException("Responder for this instance of Socket exists already. Use a new instance for each server!");
                }
                responderIsSetup   = true;
                respondingIsActive = true;
            }

            poller         = new NetMQ.NetMQPoller();
            responseSocket = new ResponseSocket();
            // handle notifies when the server is set up
            eventHandle = new ManualResetEvent(false);
            // create a new background thread with the response callback

            Exception faultingException = null;

            _ = Task.Run(() =>
            {
                try
                {
                    responseSocket.Bind(_configuration.Address());

                    // add to poller and register handler
                    poller.Add(responseSocket);
                    receiveHandler = async(s, e) => await ResponseHandlerCallback(responseSocket, handler, token);
                    responseSocket.ReceiveReady += receiveHandler;

                    poller.RunAsync();
                }
                catch (Exception exception)
                {
                    faultingException = exception;
                    _configuration.Logger.Log(new ErrorLogMsg(exception.GetType().Name + "-" + exception.Message));
                    Dispose();
                }
                finally
                {
                    // open resetevent after binding to the socket and when the poller is started
                    eventHandle.Set();
                }
            }, token);

            // wait for the Set inside the background thread so we can know at the calling client that the server is set up properly
            eventHandle.WaitOne();
            return(faultingException == null
                   ? XtResult.Success("setup-response")
                   : XtResult.Failed(ZeroMqXtSocketException.FromException(faultingException), "setup-response"));
        }
Esempio n. 3
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"));
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     create worker with standard parameter
        ///     HeartbeatDelay == 2500 milliseconds
        ///     ReconnectDelay == 2500 milliseconds
        ///     ConnectionRetries == 3
        ///     Verbose == false
        /// </summary>
        private MDPWorker()
        {
            HeartbeatDelay = TimeSpan.FromMilliseconds(2500);
            ReconnectDelay = TimeSpan.FromMilliseconds(2500);

            m_timer = new NetMQTimer(HeartbeatDelay);
            m_timer.Enable = false;
            m_timer.Elapsed += (s, e) => OnHeartbeat();

            m_pollerQueue = new NetMQQueue<Action>();
            m_pollerQueue.ReceiveReady += (sender, args) =>
            {
                var action = args.Queue.Dequeue();
                action.Invoke();
            };

            m_poller = new NetMQPoller();
            m_poller.Add(m_pollerQueue);
            m_poller.Add(m_timer);
            m_poller.RunAsync();
        }
Esempio n. 5
0
        /// <summary>
        ///     setup the client with standard values
        ///     verbose == false
        ///     Connect the client to broker
        /// </summary>
        private MDPClientAsync()
        {
            m_client = null;
            m_connected = false;

            m_poller = new NetMQPoller();
            Timeout = m_defaultTimeOut;

            m_timer = new NetMQTimer(Timeout);
            m_timer.Enable = false;
            m_poller.Add(m_timer);
            m_timer.Elapsed += (s, e) => OnProcessTimeOut();
            m_poller.RunAsync();
        }
Esempio n. 6
0
        public Task Listen()
        {
            _routerSocket = new RouterSocket();
            _routerSocket.Bind($"tcp://{_options.Host}:{_options.Port}");

            _routerSocket.ReceiveReady += _routerSocket_ReceiveReady;

            _pooler = new NetMQ.NetMQPoller();
            _pooler.Add(_routerSocket);

            _pooler.RunAsync();

            return(Task.FromResult(0));
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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");
            }
        }
Esempio n. 9
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));
            }
        }
Esempio n. 10
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"));
                }
            }
        }
Esempio n. 11
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);
            }
        }
Esempio n. 12
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"));
            }
        }
Esempio n. 13
0
            private void Configure([NotNull] string interfaceName, int port)
            {
                // In case the beacon was configured twice
                if (m_udpSocket != null)
                {
                    m_poller.Remove(m_udpSocket);

#if NET35
                    m_udpSocket.Close();
#else
                    m_udpSocket.Dispose();
#endif
                }

                m_udpPort   = port;
                m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                m_poller.Add(m_udpSocket, OnUdpReady);

                // Ask operating system for broadcast permissions on socket
                m_udpSocket.EnableBroadcast = true;

                // Allow multiple owners to bind to socket; incoming
                // messages will replicate to each owner
                m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                IPAddress bindTo = null;
                IPAddress sendTo = null;

                if (interfaceName == "*")
                {
                    bindTo = IPAddress.Any;
                    sendTo = IPAddress.Broadcast;
                }
                else if (interfaceName == "loopback")
                {
                    bindTo = IPAddress.Loopback;
                    sendTo = IPAddress.Broadcast;
                }
                else
                {
                    var interfaceCollection = new InterfaceCollection();

                    var interfaceAddress = !string.IsNullOrEmpty(interfaceName)
                        ? IPAddress.Parse(interfaceName)
                        : null;

                    foreach (var @interface in interfaceCollection)
                    {
                        if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress))
                        {
                            sendTo = @interface.BroadcastAddress;
                            bindTo = @interface.Address;
                            break;
                        }
                    }
                }

                if (bindTo != null)
                {
                    m_broadcastAddress = new IPEndPoint(sendTo, m_udpPort);
                    m_udpSocket.Bind(new IPEndPoint(bindTo, m_udpPort));
                }

                m_pipe.SendFrame(bindTo?.ToString() ?? "");
            }
Esempio n. 14
0
        /// <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);
            }
        }
Esempio n. 15
0
 public void Add(NetMQSocket socket)
 {
     m_poller.Add(socket);
     m_sockets.Add(socket);
 }
Esempio n. 16
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();
            }
        }
Esempio n. 17
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();
            }
        }
Esempio n. 18
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);
            }
        }
Esempio n. 19
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!"));
                }
            }
        }
Esempio n. 20
0
        public DealerRouter()
        {
            const int delay = 3000;

            //Keep dealersocket reference within each three threads
            var clientSocketPerThread = new ThreadLocal <DealerSocket>();


            using (var server = new RouterSocket("tcp://127.0.0.1:5556"))
                using (var poller = new NetMQPoller())
                {
                    for (int i = 0; i < 3; i++)
                    {
                        Task.Factory.StartNew(state =>
                        {
                            DealerSocket client = null;

                            if (!clientSocketPerThread.IsValueCreated)
                            {
                                client = new DealerSocket();
                                client.Options.Identity = Encoding.Unicode.GetBytes(state.ToString());
                                client.Connect("tcp://127.0.0.1:5556");
                                client.ReceiveReady        += Client_ReceivedReady;
                                clientSocketPerThread.Value = client;
                                poller.Add(client);
                            }
                            else
                            {
                                client = clientSocketPerThread.Value;
                            }

                            //the dealersocket will provide an identity for message when it is created
                            while (true)
                            {
                                var messageToServer = new NetMQMessage();
                                messageToServer.AppendEmptyFrame();
                                messageToServer.Append(state.ToString());
                                Console.WriteLine("======================================");
                                Console.WriteLine(" OUTGOING MESSAGE TO SERVER ");
                                Console.WriteLine("======================================");
                                PrintFrames("Client Sending", messageToServer);
                                client.SendMultipartMessage(messageToServer);
                                Thread.Sleep(delay);
                            }
                        }, string.Format("client {0}", i), TaskCreationOptions.LongRunning);
                    }
                    poller.RunAsync();

                    while (true)
                    {
                        var clientMessage = server.ReceiveMultipartMessage();
                        Console.WriteLine("======================================");
                        Console.WriteLine(" INCOMING CLIENT MESSAGE FROM CLIENT ");
                        Console.WriteLine("======================================");
                        PrintFrames("Server receiving", clientMessage);

                        if (clientMessage.FrameCount == 3)
                        {
                            var    clientAddress         = clientMessage[0];
                            var    clientOriginalMessage = clientMessage[2].ConvertToString();
                            string response = string.Format("{0} back from server {1} address {2}",
                                                            clientOriginalMessage, DateTime.Now.ToLongTimeString(), clientAddress.ConvertToString());
                            var messageToClient = new NetMQMessage();
                            messageToClient.Append(clientAddress);
                            messageToClient.AppendEmptyFrame();
                            messageToClient.Append(response);
                            server.SendMultipartMessage(messageToClient);
                        }
                    }
                }
        }
Esempio n. 21
0
        /// <summary>
        ///     setup the client with standard values
        ///     verbose == false
        ///     Connect the client to broker
        /// </summary>
        private MDPClientAsync()
        {
            m_client = null;
            m_connected = false;
            m_currentBrokerIndex = -1; // TODO use random!?

            m_pollerQueue = new NetMQQueue<Action>();
            m_pollerQueue.ReceiveReady += (sender, args) => OnAction(args);

            m_requestQueue = new RequestsQueue();
            m_requestQueue.FailedRequest += (s, e) => OnFailedRequest(e);

            m_poller = new NetMQPoller();
            Timeout = m_defaultTimeOut;

            m_timer = new NetMQTimer(m_purgeCheckTime);
            m_timer.Enable = false;
            m_timer.Elapsed += (s, e) => OnPurgeRequest();
            m_poller.Add(m_timer);
            m_poller.Add(m_pollerQueue);

            m_poller.Add(m_timer);
            m_poller.RunAsync();
        }
Esempio n. 22
0
 internal void Add(NetMQSocket socket)
 {
     m_poller.Add(socket);
     m_sockets.Add(socket);
 }
Esempio n. 23
0
        public void Run()
        {
            Console.WriteLine("[CLIENT {0}] Starting", m_id);

            var rnd = new Random(m_id);
            // each request shall have an unique id in order to recognize an reply for a request
            var messageId = new byte[5];
            // create clientId for messages
            var clientId = new[] { m_id };
            // a flag to signal that an answer has arrived
            bool messageAnswered = false;
            // we use a poller because we have a socket and a timer to monitor
            using (var clientPoller = new NetMQPoller())
            using (var client = new RequestSocket())
            using (var monitor = new PushSocket())
            {
                client.Connect(m_localFrontendAddress);
                monitor.Connect(m_monitorAddress);

                client.Options.Identity = new[] { m_id };
                var timer = new NetMQTimer((int)TimeSpan.FromSeconds(10).TotalMilliseconds);

                // use as flag to indicate exit
                var exit = false;

                // every 10 s check if message has been received, if not then send error message and ext
                // and restart timer otherwise
                timer.Elapsed += (s, e) =>
                {
                    if (messageAnswered)
                    {
                        e.Timer.Enable = true;
                    }
                    else
                    {
                        var msg = string.Format("[CLIENT {0}] ERR - EXIT - lost message {1}", m_id, messageId);
                        // send an error message
                        monitor.SendFrame(msg);

                        // if poller is started than stop it
                        if (clientPoller.IsRunning)
                            clientPoller.Stop();
                        // mark the required exit
                        exit = true;
                    }
                };

                // process arriving answers
                client.ReceiveReady += (s, e) =>
                {
                    // mark the arrival of an answer
                    messageAnswered = true;
                    // worker is supposed to answer with our request id
                    var reply = e.Socket.ReceiveMultipartMessage();

                    if (reply.FrameCount == 0)
                    {
                        // something went wrong
                        monitor.SendFrame(string.Format("[CLIENT {0}] Received an empty message!", m_id));
                        // mark the exit flag to ensure the exit
                        exit = true;
                    }
                    else
                    {
                        var sb = new StringBuilder();

                        // create success message
                        foreach (var frame in reply)
                            sb.Append("[" + frame.ConvertToString() + "]");

                        // send the success message
                        monitor.SendFrame(string.Format("[CLIENT {0}] Received answer {1}",
                            m_id,
                            sb.ToString()));
                    }
                };

                // add socket & timer to poller
                clientPoller.Add(client);
                clientPoller.Add(timer);

                // start poller in another thread to allow the continued processing
                clientPoller.RunAsync();

                // if true the message has been answered
                // the 0th message is always answered
                messageAnswered = true;

                while (!exit)
                {
                    // simulate sporadic activity by randomly delaying
                    Thread.Sleep((int)TimeSpan.FromSeconds(rnd.Next(5)).TotalMilliseconds);

                    // only send next message if the previous one has been replied to
                    if (messageAnswered)
                    {
                        // generate random 5 byte as identity for for the message
                        rnd.NextBytes(messageId);

                        messageAnswered = false;

                        // create message [client adr][empty][message id] and send it
                        var msg = new NetMQMessage();

                        msg.Push(messageId);
                        msg.Push(NetMQFrame.Empty);
                        msg.Push(clientId);

                        client.SendMultipartMessage(msg);
                    }
                }

                // stop poller if needed
                if (clientPoller.IsRunning)
                    clientPoller.Stop();
            }
        }
Esempio n. 24
0
        /// <summary>
        ///     run the broker - if not bound to endpoint automatically binds to known endpoint
        /// </summary>
        /// <param name="token">CancellationToken to cancel the method</param>
        /// <exception cref="InvalidOperationException">Can't start same broker more than once!</exception>
        public void RunSynchronous (CancellationToken token)
        {
            if (m_isRunning)
                throw new InvalidOperationException ("Can't start same broker more than once!");

            if (!m_isBound)
                Bind ();

            m_isRunning = true;

            using (var poller = new NetMQPoller ())
            {
                Socket.ReceiveReady += ProcessReceivedMessage;
                // get timer for scheduling heartbeat
                var timer = new NetMQTimer (HeartbeatInterval);
                // send every 'HeartbeatInterval' a heartbeat to all not expired workers
                timer.Elapsed += (s, e) => SendHeartbeat ();

                poller.Add (Socket);
                poller.Add (timer);

                Log ("Starting to listen for incoming messages ...");

                // start the poller and wait for the return, which will happen once token is
                // signalling Cancel(!)
                Task.Factory.StartNew (poller.Run, token).Wait ();

                Log ("... Stopped!");

                // clean up
                poller.Remove (timer);
                poller.Remove (Socket);
                // unregister event handler
                Socket.ReceiveReady -= ProcessReceivedMessage;
            }

            m_isRunning = false;
        }
Esempio n. 25
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!"));
            }
        }
Esempio n. 26
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);
            }
        }
Esempio n. 27
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"));
            }
        }
Esempio n. 28
0
            private void Configure([NotNull] string interfaceName, int port)
            {
                // In case the beacon was configured twice
                if (m_udpSocket != null)
                {
                    m_poller.Remove(m_udpSocket);

#if NET35
                    m_udpSocket.Close();
#else
                    m_udpSocket.Dispose();
#endif
                }

                m_udpPort   = port;
                m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                m_poller.Add(m_udpSocket, OnUdpReady);

                // Ask operating system for broadcast permissions on socket
                m_udpSocket.EnableBroadcast = true;

                // Allow multiple owners to bind to socket; incoming
                // messages will replicate to each owner
                m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                IPAddress bindTo = null;
                IPAddress sendTo = null;

                if (interfaceName == "*")
                {
                    bindTo = IPAddress.Any;
                    sendTo = IPAddress.Broadcast;
                }
                else if (interfaceName == "loopback")
                {
                    bindTo = IPAddress.Loopback;
                    sendTo = IPAddress.Broadcast;
                }
                else
                {
                    var interfaceCollection = new InterfaceCollection();

                    var interfaceAddress = !string.IsNullOrEmpty(interfaceName)
                        ? IPAddress.Parse(interfaceName)
                        : null;

                    foreach (var @interface in interfaceCollection)
                    {
                        if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress))
                        {
                            // because windows and unix differ in how they handle broadcast addressing this needs to be platform specific
                            // on windows any interface can recieve broadcast by requesting to enable broadcast on the socket
                            // on linux to recieve broadcast you must bind to the broadcast address specifically
                            //bindTo = @interface.Address;
                            sendTo = @interface.BroadcastAddress;
#if NET35 || NET40
                            if (Environment.OSVersion.Platform == PlatformID.Unix)
#else
                            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
#endif
                            {
                                bindTo = @interface.BroadcastAddress;
                            }
                            else
                            {
                                bindTo = @interface.Address;
                            }
                            sendTo = @interface.BroadcastAddress;

                            break;
                        }
                    }
                }

                if (bindTo != null)
                {
                    m_broadcastAddress = new IPEndPoint(sendTo, m_udpPort);
                    m_udpSocket.Bind(new IPEndPoint(bindTo, m_udpPort));
                }

                m_pipe.SendFrame(bindTo?.ToString() ?? "");
            }
Esempio n. 29
0
            private void Configure(string interfaceName, int port)
            {
                // In case the beacon was configured twice
                if (m_udpSocket != null)
                {
                    m_poller.Remove(m_udpSocket);
                    m_udpSocket.Close();
                }

                m_udpPort   = port;
                m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                m_poller.Add(m_udpSocket, OnUdpReady);

                // Ask operating system for broadcast permissions on socket
                m_udpSocket.EnableBroadcast = true;

                // Allow multiple owners to bind to socket; incoming
                // messages will replicate to each owner
                m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                IPAddress bindTo = null;
                IPAddress sendTo = null;

                if (interfaceName == "*")
                {
                    bindTo = IPAddress.Any;
                    sendTo = IPAddress.Broadcast;
                }
                else
                {
                    var interfaceCollection = new InterfaceCollection();

                    var interfaceAddress = !string.IsNullOrEmpty(interfaceName)
                        ? IPAddress.Parse(interfaceName)
                        : null;

                    foreach (var @interface in interfaceCollection)
                    {
                        if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress))
                        {
                            sendTo = @interface.BroadcastAddress;
                            bindTo = @interface.Address;
                            break;
                        }
                    }
                }

                if (bindTo != null)
                {
                    m_broadcastAddress = new IPEndPoint(sendTo, m_udpPort);
                    m_udpSocket.Bind(new IPEndPoint(bindTo, m_udpPort));

                    string hostname = "";

                    try
                    {
                        if (!IPAddress.Any.Equals(bindTo) && !IPAddress.IPv6Any.Equals(bindTo))
                        {
                            var host = Dns.GetHostEntry(bindTo);
                            hostname = host != null ? host.HostName : "";
                        }
                    }
                    catch (Exception)
                    {}

                    m_pipe.SendFrame(hostname);
                }
            }