예제 #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
                    }
                }
            }
        }
 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);
         }
     }
 }
예제 #3
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);
     }
 }
예제 #4
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;
 }
예제 #5
0
        private void RecvEventsLoop()
        {
            while (true)
            {
                byte[] msg = _subscriber.Recv(RecvExpire);
                if (msg != null)
                {
                    OnEventReceived(msg);
                }

                if (_event.WaitOne(0))
                {
                    break;
                }
            }
        }
예제 #6
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);
         }
     }
 }
예제 #7
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);
         }
     }
 }
예제 #8
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();
        }
예제 #9
0
파일: Socket.cs 프로젝트: paralect/brod
        public Queue <byte[]> RecvAll(Int32 timeout)
        {
            byte[] bytes = _zmqSocket.Recv(timeout);

            if (bytes == null)
            {
                return(null);
            }

            var queue = new Queue <byte[]>();

            queue.Enqueue(bytes);

            while (_zmqSocket.RcvMore)
            {
                queue.Enqueue(_zmqSocket.Recv());
            }

            return(queue);
        }
예제 #10
0
 /// <summary>
 /// Receive message from subscriber
 /// </summary>
 /// <returns>
 /// message interface
 /// </returns>
 public IMessage Receive()
 {
     // TODO: Support decoding of all message types + all meta data (e.g., headers and properties)
     return(ToNmsMessage(messageSubscriber.Recv(Encoding.ASCII, ZSendRecvOpt.NONE)));
 }