Example #1
0
        public void BindAndConnect()
        {
            using (PairSocket bind = new PairSocket())
            {
                bind.Bind("inproc://test");
                Assert.That(!bind.HasOut);

                using (PairSocket connect = new PairSocket())
                {
                    connect.Connect("inproc://test");

                    Assert.That(connect.HasOut);
                    Assert.That(bind.HasOut);
                    Assert.That(!bind.HasIn);

                    Assert.That(connect.TrySendFrame("Hello"));
                    Assert.That(bind.HasIn);

                    string message;

                    Assert.That(bind.TryReceiveFrameString(out message));
                    Assert.That(message == "Hello");
                }
            }
        }
        public void Run(PairSocket shim)
        {
            shim.SignalOK();

            shim.ReceiveReady += OnShimReady;

            m_messagesPipe = new PairSocket();
            m_messagesPipe.Connect(string.Format("inproc://wsrouter-{0}", m_id));
            m_messagesPipe.ReceiveReady += OnMessagePipeReady;

            m_stream = new StreamSocket();
            m_stream.ReceiveReady += OnStreamReady;

            m_poller = new NetMQPoller();
            m_poller.Add(m_messagesPipe);
            m_poller.Add(shim);
            m_poller.Add(m_stream);

            m_messagesPipe.SignalOK();

            m_poller.Run();

            m_messagesPipe.Dispose();
            m_stream.Dispose();
        }
Example #3
0
        /// <summary>
        /// Create new NetMQQueue.
        /// </summary>
        /// <param name="context">NetMQContext must be provided to the queue</param>
        /// <param name="capacity">The capacity of the queue, use zero for unlimited</param>
        public NetMQQueue(NetMQContext context, int capacity = 0)
        {
            m_context = context;
            m_queue   = new ConcurrentQueue <T>();
            m_writer  = m_context.CreatePairSocket();
            m_reader  = m_context.CreatePairSocket();

            if (capacity != 0)
            {
                m_writer.Options.SendHighWatermark = m_reader.Options.ReceiveHighWatermark = capacity / 2;
            }
            else
            {
                m_writer.Options.SendHighWatermark = m_reader.Options.ReceiveHighWatermark = 0;
            }

            m_eventDelegator = new EventDelegator <NetMQQueueEventArgs <T> >(() =>
            {
                m_reader.ReceiveReady += OnReceiveReady;
            }, () =>
            {
                m_reader.ReceiveReady -= OnReceiveReady;
            });

            string address = string.Format("inproc://NetMQQueue#{0}", Interlocked.Increment(ref s_sequence));

            m_reader.Bind(address);
            m_writer.Connect(address);

            m_dequeueMsg = new Msg();
            m_dequeueMsg.InitEmpty();
        }
Example #4
0
    void SocketThreadLoop()
    {
        while (true)
        {
            threadRunning   = true;
            senderCancelled = false;
            AsyncIO.ForceDotNet.Force();
            using (var sock = new PairSocket())
            {
                sock.Connect("tcp://" + ip + ":" + port);

                while (!senderCancelled)
                {
                    if (!framePending)
                    {
                        continue;
                    }
                    sock.SendFrame(msgBuffer);
                    framesSent++;
                    framePending = false;
                }

                sock.Close();
            }
            NetMQConfig.Cleanup();
            threadRunning = false;
        }
    }
 private void ConnectionWork()
 {
     AsyncIO.ForceDotNet.Force();
     using (var pairSocket = new PairSocket()) {
         pairSocket.Options.ReceiveHighWatermark = 1000;
         if (_createConnection)
         {
             pairSocket.Bind("tcp://*:12345");
         }
         else
         {
             pairSocket.Connect("tcp://localhost:12345");
         }
         //Do one more loop in-case we send out a closing msg and then cancel the connection
         bool flushedBuffer = true;
         while (!_connectionCancelled || !flushedBuffer)
         {
             string frameString;
             while (pairSocket.TryReceiveFrameString(out frameString))
             {
                 _incomingMessageQueue.Enqueue(frameString);
             }
             while (_outgoingMessageQueue.TryDequeue(out frameString))
             {
                 pairSocket.SendFrame(frameString);
             }
             flushedBuffer = _connectionCancelled;
         }
         pairSocket.Close();
     }
     NetMQConfig.Cleanup();
 }
Example #6
0
 private static void Node1(string url)
 {
     using (var s = new PairSocket())
     {
         s.Connect(url);
         SendReceive(s);
     }
 }
Example #7
0
        private NetMQActor(PairSocket self, PairSocket shim, IShimHandler shimHandler)
        {
            m_shimHandler = shimHandler;

            m_self = self;
            m_shim = shim;

            EventHandler <NetMQSocketEventArgs> onReceive = (sender, e) =>
                                                            m_receiveEvent.Fire(this, new NetMQActorEventArgs(this));

            EventHandler <NetMQSocketEventArgs> onSend = (sender, e) =>
                                                         m_sendEvent.Fire(this, new NetMQActorEventArgs(this));

            m_receiveEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.ReceiveReady += onReceive,
                () => m_self.ReceiveReady -= onReceive);

            m_sendEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.SendReady += onSend,
                () => m_self.SendReady -= onSend);

            var random = new Random();

            // Bind and connect pipe ends
            string actorName;
            string endPoint;

            while (true)
            {
                try
                {
                    actorName = string.Format("NetMQActor-{0}-{1}", random.Next(0, 10000), random.Next(0, 10000));
                    endPoint  = string.Format("inproc://{0}", actorName);
                    m_self.Bind(endPoint);
                    break;
                }
                catch (AddressAlreadyInUseException)
                {
                    // Loop around and try another random address
                }
            }

            m_shim.Connect(endPoint);

            m_shimThread = new Thread(RunShim)
            {
                Name = actorName
            };
            m_shimThread.Start();

            // Mandatory handshake for new actor so that constructor returns only
            // when actor has also initialised. This eliminates timing issues at
            // application start up.
            m_self.ReceiveSignal();
        }
Example #8
0
        private NetMQActor([NotNull] NetMQContext context, [NotNull] IShimHandler shimHandler)
        {
            m_shimHandler = shimHandler;

            m_self = context.CreatePairSocket();
            m_shim = context.CreatePairSocket();

            EventHandler <NetMQSocketEventArgs> onReceive = (sender, e) =>
                                                            m_receiveEvent.Fire(this, new NetMQActorEventArgs(this));

            EventHandler <NetMQSocketEventArgs> onSend = (sender, e) =>
                                                         m_sendEvent.Fire(this, new NetMQActorEventArgs(this));

            m_receiveEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.ReceiveReady += onReceive,
                () => m_self.ReceiveReady -= onReceive);

            m_sendEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.SendReady += onSend,
                () => m_self.SendReady -= onSend);

            var random = new Random();

            //now binding and connect pipe ends
            string endPoint;

            while (true)
            {
                try
                {
                    endPoint = string.Format("inproc://NetMQActor-{0}-{1}", random.Next(0, 10000), random.Next(0, 10000));
                    m_self.Bind(endPoint);
                    break;
                }
                catch (AddressAlreadyInUseException)
                {
                    // In case address already in use we continue searching for an address
                }
            }

            m_shim.Connect(endPoint);

            m_shimThread = new Thread(RunShim);
            m_shimThread.Start();

            // Mandatory handshake for new actor so that constructor returns only
            // when actor has also initialised. This eliminates timing issues at
            // application start up.
            m_self.ReceiveSignal();
        }
Example #9
0
        public void Run(PairSocket shim)
        {
            string shimAdd = $"inproc://wsrouter-{Id}";

            Monitor.Enter(this);
            try
            {
                isRuning = true;
                shim.SignalOK();

                shim.ReceiveReady += OnShimReady;

                MessagesPipe = new PairSocket();
                MessagesPipe.Connect(shimAdd);
                MessagesPipe.ReceiveReady += OnMessagePipeReady;

                Stream = new StreamSocket();
                Stream.Bind(Address);
                Stream.ReceiveReady += OnStreamReady;

                Poller = new NetMQPoller
                {
                    MessagesPipe,
                    shim,
                    Stream
                };
                MessagesPipe.SignalOK();
                Poller.Run();
                shim.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Monitor.Exit(this);
            }
        }
        public void RunPipeline(PairSocket shim)
        {
            shim.SignalOK();

            shim.ReceiveReady += OnShimReady;

            m_messagesPipe = m_context.CreatePairSocket();
            m_messagesPipe.Connect(string.Format("inproc://wsrouter-{0}", m_id));
            m_messagesPipe.ReceiveReady += OnMessagePipeReady;

            m_stream = m_context.CreateStreamSocket();
            m_stream.ReceiveReady += OnStreamReady;

            m_poller = new Poller(m_messagesPipe, shim, m_stream);

            m_messagesPipe.SignalOK();

            m_poller.Start();

            m_messagesPipe.Dispose();
            m_stream.Dispose();
        }
Example #11
0
        /// <summary>
        ///     <para>is the main thread of the broker</para>
        ///     <para>it spawns threads handling titanic operations</para>
        ///     <para>it receives GUID from Titanic Request Service and dispatches the requests
        ///     to available workers via MDPBroker</para>
        ///     <para>it also manages the appropriate changes in the file system as well as in queue</para>
        /// </summary>
        /// <param name="requestWorker">mdp worker processing the incoming requests for services</param>
        /// <param name="replyWorker">mdp worker processing incoming reply requests</param>
        /// <param name="closeWorker">mdp worker processing incoming close requests</param>
        /// <param name="serviceCallClient">mdp client forwarding requests to service providing mdp worker
        ///                                 via mdp broker and collecting replies</param>
        /// <exception cref="TerminatingException">The socket has been stopped.</exception>
        /// <exception cref="AddressAlreadyInUseException">The specified address is already in use.</exception>
        /// <exception cref="NetMQException">No IO thread was found, or the protocol's listener encountered an
        ///                                  error during initialization.</exception>
        /// <exception cref="ObjectDisposedException">thrown if the socket was already disposed</exception>
        public void Run( IMDPWorker requestWorker = null,
                          IMDPWorker replyWorker = null,
                          IMDPWorker closeWorker = null,
                          IMDPClient serviceCallClient = null)
        {
            using (var pipeStart = new PairSocket ())
            using (var pipeEnd = new PairSocket ())
            using (var cts = new CancellationTokenSource ())
            {
                // set up the inter thread communication pipe
                pipeStart.Bind (_titanic_internal_communication);
                pipeEnd.Connect (_titanic_internal_communication);

                // start the three child tasks
                var requestTask = Task.Run (() => ProcessTitanicRequest (pipeEnd, requestWorker), cts.Token);
                var replyTask = Task.Run (() => ProcessTitanicReply (replyWorker), cts.Token);
                var closeTask = Task.Run (() => ProcessTitanicClose (closeWorker), cts.Token);

                var tasks = new[] { requestTask, replyTask, closeTask };

                while (true)
                {
                    // wait for 1s for a new request from 'Request' to process
                    var input = pipeStart.Poll (PollEvents.PollIn, TimeSpan.FromMilliseconds (1000));

                    // any message available? -> process it
                    if ((input & PollEvents.PollIn) == PollEvents.PollIn)
                    {
                        // only one frame will be send [Guid]
                        var msg = pipeStart.ReceiveFrameString ();

                        Guid guid;
                        if (!Guid.TryParse (msg, out guid))
                            Log ("[TITANIC BROKER] Received a malformed GUID via pipe - throw it away");
                        else
                        {
                            Log (string.Format ("[TITANIC BROKER] Received request GUID {0} via pipe", msg));
                            // now we have a valid GUID - save it to disk for further use
                            m_io.SaveNewRequestEntry (guid);
                        }
                    }
                    //! now dispatch (brute force) the requests -> SHOULD BE MORE INTELLIGENT (!)
                    // dispatching will also worry about the handling of a potential reply
                    // dispatch only requests which have not been closed
                    foreach (var entry in m_io.GetNotClosedRequestEntries ().Where (entry => entry != default (RequestEntry)))
                    {
                        if (DispatchRequests (entry.RequestId, serviceCallClient))
                            m_io.SaveProcessedRequestEntry (entry);
                    }

                    //! should implement some sort of restart
                    // beware of the silently dieing threads - must be detected!
                    if (DidAnyTaskStopp (tasks))
                    {
                        // stopp all threads
                        cts.Cancel ();
                        // stop processing!
                        break;
                    }
                }
            }
        }
Example #12
0
        /// <summary>
        ///     <para>is the main thread of the broker</para>
        ///     <para>it spawns threads handling titanic operations</para>
        ///     <para>it receives GUID from Titanic Request Service and dispatches the requests
        ///     to available workers via MDPBroker</para>
        ///     <para>it also manages the appropriate changes in the file system as well as in queue</para>
        /// </summary>
        /// <param name="requestWorker">mdp worker processing the incoming requests for services</param>
        /// <param name="replyWorker">mdp worker processing incoming reply requests</param>
        /// <param name="closeWorker">mdp worker processing incoming close requests</param>
        /// <param name="serviceCallClient">mdp client forwarding requests to service providing mdp worker
        ///                                 via mdp broker and collecting replies</param>
        /// <exception cref="TerminatingException">The socket has been stopped.</exception>
        /// <exception cref="AddressAlreadyInUseException">The specified address is already in use.</exception>
        /// <exception cref="NetMQException">No IO thread was found, or the protocol's listener encountered an
        ///                                  error during initialization.</exception>
        /// <exception cref="ObjectDisposedException">thrown if the socket was already disposed</exception>
        public void Run([CanBeNull] IMDPWorker requestWorker     = null,
                        [CanBeNull] IMDPWorker replyWorker       = null,
                        [CanBeNull] IMDPWorker closeWorker       = null,
                        [CanBeNull] IMDPClient serviceCallClient = null)
        {
            using (var pipeStart = new PairSocket())
                using (var pipeEnd = new PairSocket())
                    using (var cts = new CancellationTokenSource())
                    {
                        // set up the inter thread communication pipe
                        pipeStart.Bind(_titanic_internal_communication);
                        pipeEnd.Connect(_titanic_internal_communication);

                        // start the three child tasks
                        var requestTask = Task.Run(() => ProcessTitanicRequest(pipeEnd, requestWorker), cts.Token);
                        var replyTask   = Task.Run(() => ProcessTitanicReply(replyWorker), cts.Token);
                        var closeTask   = Task.Run(() => ProcessTitanicClose(closeWorker), cts.Token);

                        var tasks = new[] { requestTask, replyTask, closeTask };

                        while (true)
                        {
                            // wait for 1s for a new request from 'Request' to process
                            var input = pipeStart.Poll(PollEvents.PollIn, TimeSpan.FromMilliseconds(1000));

                            // any message available? -> process it
                            if ((input & PollEvents.PollIn) == PollEvents.PollIn)
                            {
                                // only one frame will be send [Guid]
                                var msg = pipeStart.ReceiveFrameString();

                                Guid guid;
                                if (!Guid.TryParse(msg, out guid))
                                {
                                    Log("[TITANIC BROKER] Received a malformed GUID via pipe - throw it away");
                                }
                                else
                                {
                                    Log($"[TITANIC BROKER] Received request GUID {msg} via pipe");
                                    // now we have a valid GUID - save it to disk for further use
                                    m_io.SaveNewRequestEntry(guid);
                                }
                            }
                            //! now dispatch (brute force) the requests -> SHOULD BE MORE INTELLIGENT (!)
                            // dispatching will also worry about the handling of a potential reply
                            // dispatch only requests which have not been closed
                            foreach (var entry in m_io.GetNotClosedRequestEntries().Where(entry => entry != default(RequestEntry)))
                            {
                                if (DispatchRequests(entry.RequestId, serviceCallClient))
                                {
                                    m_io.SaveProcessedRequestEntry(entry);
                                }
                            }

                            //! should implement some sort of restart
                            // beware of the silently dieing threads - must be detected!
                            if (DidAnyTaskStopp(tasks))
                            {
                                // stop all threads
                                cts.Cancel();
                                // stop processing!
                                break;
                            }
                        }
                    }
        }
Example #13
0
        public static void Execute()
        {
            Console.WriteLine("Executing Pair test");

            _clientData = new byte[DataSize];
            _serverData = new byte[DataSize];
            var r = new Random();

            r.NextBytes(_clientData);
            r.NextBytes(_serverData);

            var clientThread = new Thread(
                () =>
            {
                var req = new PairSocket();
                req.Connect(InprocAddress);

                byte[] streamOutput = new byte[BufferSize];
                while (true)
                {
                    var sw = Stopwatch.StartNew();
                    for (int i = 0; i < Iter; i++)
                    {
                        var result = req.SendImmediate(_clientData);
                        Trace.Assert(result);
                        int read = 0;
                        using (var stream = req.ReceiveStream())
                            while (stream.Length != stream.Position)
                            {
                                read += stream.Read(streamOutput, 0, streamOutput.Length);
                            }
                        Trace.Assert(read == _serverData.Length);
                    }
                    sw.Stop();
                    var secondsPerSend = sw.Elapsed.TotalSeconds / (double)Iter;
                    Console.WriteLine("Pair Time {0} us, {1} per second, {2} mb/s ",
                                      (int)(secondsPerSend * 1000d * 1000d),
                                      (int)(1d / secondsPerSend),
                                      (int)(DataSize * 2d / (1024d * 1024d * secondsPerSend)));
                }
            });

            clientThread.Start();

            {
                var rep = new PairSocket();
                rep.Bind(InprocAddress);

                byte[] streamOutput = new byte[BufferSize];

                var sw = Stopwatch.StartNew();
                while (sw.Elapsed.TotalSeconds < 10)
                {
                    int read = 0;
                    using (var stream = rep.ReceiveStream())
                        while (stream.Length != stream.Position)
                        {
                            read += stream.Read(streamOutput, 0, streamOutput.Length);
                        }
                    rep.SendImmediate(_serverData);
                }

                clientThread.Abort();
            }
        }
Example #14
0
        static void Main()
        {
            using (var client = new PairSocket())
            {
                client.Connect("tcp://127.0.0.1:5556");
                client.SendFrame("hello");

                bool firstRun = true;

                // talk
                while (true)
                {
                    // receive reward
                    string reward = client.ReceiveFrameString();
                    if (reward == "-1")
                    {
                        reward = "-";
                    }
                    else if (reward == "1")
                    {
                        reward = "+";
                    }
                    else
                    {
                        reward = " ";
                    }

                    if (firstRun)
                    {
                        reward   = string.Empty;
                        firstRun = false;
                    }
                    else
                    {
                        if (!Interactive)
                        {
                            Learner.RegisterReward(reward);
                        }
                    }

                    // receive teacher/env char
                    string teacherChar = client.ReceiveFrameString();

                    Display.ShowStep(reward, teacherChar);

                    // reply
                    string reply;
                    if (Interactive)
                    {
                        var key = Console.ReadKey();
                        reply = key.KeyChar.ToString();
                    }
                    else
                    {
                        reply = Learner.Answer(teacherChar);
                    }

                    Display.ShowReply(reply);
                    client.SendFrame(reply);
                }
            }
        }