Example #1
0
        public void TestIfItCanSum()
        {
            // Arrange
            Context context = null;
            Socket  slave   = null;

            ZMQ.Socket master = null;
            using (context = new Context())
            {
                using (master = context.Socket(SocketType.REQ))
                {
                    master.Connect(Transport.TCP, CONNECT_HOSTNAME, TEST_PORT);

                    slave = context.Socket(SocketType.REP);
                    slave.Bind(Transport.TCP, BIND_HOSTNAME, TEST_PORT);

                    using (SumadorSendRecvEsclavo target = new SumadorSendRecvEsclavo(slave, slave))
                    {
                        int[] numeros = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 };

                        // Act
                        var task = Task.Factory.StartNew(() => target.EjecutarOperacion());
                        master.Send(numeros);
                        double resultado = master.Recv <double>();

                        // Assert
                        Assert.That(resultado, Is.EqualTo(numeros.Sum()));
                        Assert.That(target.Resultado, Is.EqualTo(resultado));

                        // Reset
                    }
                }
            }
        }
Example #2
0
        public MessageConsumer(Session session, AcknowledgementMode acknowledgementMode, IDestination destination, string selector)
        {
            if (null == Connection.Context)
            {
                throw new NMSConnectionException();
            }

            this.session             = session;
            this.acknowledgementMode = acknowledgementMode;
            this.messageSubscriber   = Connection.Context.Socket(ZSocketType.SUB);
            if (null == this.messageSubscriber)
            {
                throw new ResourceAllocationException();
            }

            string clientId = session.Connection.ClientId;

            this.contextBinding = session.Connection.BrokerUri.LocalPath;
            this.destination    = new Queue(this.contextBinding);
            if (!string.IsNullOrEmpty(clientId))
            {
                this.messageSubscriber.StringToIdentity(clientId, Encoding.Unicode);
            }

            this.messageSubscriber.Connect(contextBinding);
            this.messageSubscriber.Subscribe(selector ?? string.Empty, Encoding.ASCII);
        }
 static void Main(string[] args)
 {
     // allocate a buffer
     byte[] zmq_buffer = new byte[1024];
     //  Prepare our context and socket
     ZMQ.Context context = new ZMQ.Context(1);
     ZMQ.Socket  socket  = context.Socket(ZMQ.SocketType.REP);
     socket.Bind("tcp://*:5555");
     while (true)
     {
         try {
             //  Wait for next request from client
             zmq_buffer = socket.Recv();
             string request = Encoding.ASCII.GetString(zmq_buffer);
             // log that we got one
             Console.WriteLine("Received request: [%s]", request);
             //  Do some 'work'
             Thread.Sleep(1);
             //  Send reply back to client
             socket.Send(Encoding.ASCII.GetBytes("World".ToCharArray()));
         } catch (ZMQ.Exception z) {
             // report the exception
             Console.WriteLine("ZMQ Exception occurred : {0}", z.Message);
         }
     }
 }
Example #4
0
        private void recvProc()
        {
            ZMQ.Socket reqs = CTX.Socket(ZMQ.SocketType.PULL);
            reqs.Connect(sub_addr);

            var items = new[] { reqs.CreatePollItem(ZMQ.IOMultiPlex.POLLIN | ZMQ.IOMultiPlex.POLLERR) };

            while (isRunning)
            {
                int res = CTX.Poll(items, 1000 * 1000);
                if (res == 0)
                {
                    continue;
                }
                foreach (byte[] data in reqs.RecvAll(ZMQ.SendRecvOpt.NOBLOCK))
                {
                    if (data == null)
                    {
                        continue;
                    }
                    recvQ.Enqueue(data);
                    itemsReadyToRecv.Set();
                }
            }

            reqs.Dispose();
            itemsReadyToRecv.Close();
            Interlocked.Decrement(ref threadStillRunning);
        }
        public bool StartNidsEvent()
        {
            if (string.IsNullOrEmpty(ServerAddr) || ServrtPort == 0)
            {
                Log(LOGLEVEL.ERROR, "incorrect server parameters!");
                return(false);
            }

            _context    = new ZMQ.Context(1);
            _subscriber = _context.Socket(ZMQ.SocketType.SUB);
            Connect();

            if (EventTags.Count == 0)
            {
                Log(LOGLEVEL.ERROR, "event node can not be 0!");
                return(false);
            }

            try
            {
                _event  = new ManualResetEvent(false);
                _thread = new Thread(EventRecvThreadMain)
                {
                    Name         = string.Format("nids_event_thread_{0}_{1}", ServerAddr, ServrtPort),
                    IsBackground = true
                };
                _thread.Start();
            }
            catch (Exception ex)
            {
                Log(LOGLEVEL.ERROR, ex.ToString());
                return(false);
            }
            return(true);
        }
Example #6
0
 public void Close()
 {
     if (null != messageProducer)
     {
         messageProducer.Dispose();
         messageProducer = null;
     }
 }
Example #7
0
 /// <summary>
 /// Clean up
 /// </summary>
 public void Close()
 {
     StopAsyncDelivery();
     if (null != messageSubscriber)
     {
         messageSubscriber.Dispose();
         messageSubscriber = null;
     }
 }
Example #8
0
 private void MonitorResponder(Socket monitor, BlockingCollection <byte[]> queue, Encoding encoding)
 {
     while (_isRunning)
     {
         monitor.Recv();
         var queueLength = queue.Count();
         Console.WriteLine("MONITOR - Received queue length monitor request - currently at {0}", queueLength);
         monitor.Send(string.Format("{0}", queueLength), encoding);
     }
 }
Example #9
0
 private void RequestPusher(Socket destination, IEnumerable <byte[]> messages, Encoding encoding)
 {
     foreach (var message in messages)
     {
         var stringMessage = encoding.GetString(message);
         Console.WriteLine("PUSHER - Pulled message {0} from queue", stringMessage);
         destination.Send(message);
         destination.Recv();
     }
     _isRunning = false;
 }
Example #10
0
        public void Run(Socket frontend, Socket backend, Socket monitor)
        {
            _isRunning = true;
            var tasks = new[]
            {
                Task.Run(() => Heartbeat(_encoding)),
                Task.Run(() => RequestReceiver(frontend, _queue, _encoding)),
                Task.Run(() => RequestPusher(backend, _queue.GetConsumingEnumerable(), _encoding)),
                Task.Run(() => MonitorResponder(monitor, _queue, _encoding)),
            };

            Task.WaitAll(tasks);
            Console.WriteLine("Broker done!");
        }
Example #11
0
 static void Main(string[] args)
 {
     InitErrMessages();
     rep = cntx.Socket(ZMQ.SocketType.REP);
     rep.Bind("tcp://*:1000");
     pub = cntx.Socket(ZMQ.SocketType.PUB);
     pub.Bind("tcp://*:1001");
     while (true)
     {
         byte[] buf = rep.Recv(0);
         if (buf != null)
         {
             Process(buf);
         }
     }
 }
Example #12
0
 static void Main(string[] args)
 {
     InitErrMessages();
     rep = cntx.Socket(ZMQ.SocketType.REP);
     rep.Bind("tcp://*:1000");
     pub = cntx.Socket(ZMQ.SocketType.PUB);
     pub.Bind("tcp://*:1001");
     while (true)
     {
         byte[] buf = rep.Recv(0);
         if (buf != null)
         {
             Process(buf);
         }
     }
 }
Example #13
0
        private void sendProc()
        {
            ZMQ.Socket resp = CTX.Socket(ZMQ.SocketType.PUB);
            if (!string.IsNullOrEmpty(SenderId))
            {
                resp.Identity = Encoding.ASCII.GetBytes(SenderId);
            }
            resp.Connect(pub_addr);

            while (isRunning)
            {
                itemsReadyToSend.WaitOne();
                lock (sendQ)
                {
                    while (sendQ.Count != 0)
                    {
                        byte[] stuffToSend = sendQ.Dequeue();

                        bool sentOk = false;
                        while (!sentOk)
                        {
                            try
                            {
                                resp.Send(stuffToSend);
                                sentOk = true;
                            }
                            catch (ZMQ.Exception ex)
                            {
                                if (ex.Errno == (int)ZMQ.ERRNOS.EAGAIN)
                                {
                                    sentOk = false;
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                }
            }

            resp.Dispose();
            itemsReadyToSend.Close();
            Interlocked.Decrement(ref threadStillRunning);
        }
Example #14
0
        private void RequestReceiver(Socket source, BlockingCollection <byte[]> queue, Encoding encoding)
        {
            while (_isRunning)
            {
                var message       = source.Recv();
                var stringMessage = encoding.GetString(message);
                Console.WriteLine("RECEIVER - Received message {0} from source", stringMessage);
                queue.Add(message);
                source.Send();

                if (stringMessage == "Done")
                {
                    break;
                }
            }
            _queue.CompleteAdding();
        }
Example #15
0
        public MessageProducer(Connection connection, Session session, IDestination destination)
        {
            if (null == Connection.Context)
            {
                throw new NMSConnectionException();
            }

            this.session         = session;
            this.destination     = destination;
            this.messageProducer = Connection.Context.Socket(ZSocketType.SUB);

            string clientId = connection.ClientId;

            if (!string.IsNullOrEmpty(clientId))
            {
                this.messageProducer.StringToIdentity(clientId, Encoding.Unicode);
            }

            this.messageProducer.Connect(connection.BrokerUri.LocalPath);
        }
Example #16
0
        private void ZMQConnect()
        {
            //  Prepare our context and socket
            m_Context= new ZMQ.Context(1);

            m_Socket = m_Context.Socket(ZMQ.XREQ);
            m_Socket.Connect("tcp://localhost:5555");
        }
Example #17
0
 public Socket(ZMQ.Socket zmqSocket)
 {
     _zmqSocket = zmqSocket;
 }
Example #18
0
 public Socket(ZMQ.Socket zmqSocket)
 {
     _zmqSocket = zmqSocket;
 }