Package org.zeromq

Examples of org.zeromq.ZMsg


  @Test
  public void testZmqDealer() throws Exception {
    final ZMQ.Context context = ZMQ.context(1);
    final ZMQ.Socket socket = context.socket(ZMQ.DEALER);
    socket.connect("tcp://" + serverAddress.getHostName() + ":" + serverAddress.getPort());
    final ZMsg request = ZMsg.newStringMsg("envelope", "", "hello", "world");
    request.send(socket, false);

    final ZMTPIncomingMessage receivedRequest = incomingMessages.take();
    final ZMTPMessage receivedMessage = receivedRequest.getMessage();
    receivedRequest.getSession().getChannel().write(receivedMessage);

    final ZMsg reply = ZMsg.recvMsg(socket);
    Iterator<ZFrame> reqIter = request.iterator();
    Iterator<ZFrame> replyIter = reply.iterator();
    while (reqIter.hasNext()) {
      assertTrue(replyIter.hasNext());
      assertArrayEquals(reqIter.next().getData(), replyIter.next().getData());
    }
    assertFalse(replyIter.hasNext());
View Full Code Here


        asList(ZMTPFrame.create("hello"), ZMTPFrame.create("world")));

    final Channel channel = channelsConnected.take();
    channel.write(request);

    final ZMsg receivedReply = ZMsg.recvMsg(socket);

    assertEquals(ZMsg.newStringMsg(identity, "envelope", "", "hello", "world"), receivedReply);
  }
View Full Code Here

    final ZMTPTestConnector tester = new ZMTPTestConnector() {
      ZMsg m;

      @Override
      public void preConnect(final ZMQ.Socket socket) {
        m = new ZMsg();

        for (int i = 0; i < 16; i++) {
          m.addString("test-frame-" + i);
        }
View Full Code Here

        String topic = null;
        boolean more = false;
        boolean stop = false;
        ZLog zlog = null;
        Msg msg;
        ZMsg response = null;

        while (!Thread.currentThread().isInterrupted()
                && !stop) {

            msg = worker.base().recv(0);
            if (msg == null)
                break;
            more = msg.hasMore();

            switch (state) {
            case START:
                byte[] id = msg.data();
                if (id == null)
                    break;
                flag = id[1];
                topic = ZPUtils.getTopic(id);
                if (topic == null) {
                    break;
                }
                state = TOPIC;
                zlog = logMgr.get(topic);

                if (flag > 0) {
                    response = new ZMsg();
                    response.add(id);
                }
                break;

            case TOPIC:

                if (msg.size() == 0 && more) { // bottom
                    state = COUNT;

                    if (flag > 0)
                        response.add(msg.data());
                    break;
                }

            case COUNT:

                if (decoder) {
                    count = ByteBuffer.wrap(msg.data()).getInt();
                    state = MESSAGE;
                    break;
                }
                else {
                    state = SINGLE;
                }

            case SINGLE:

                if (store(zlog, msg)) {
                    if (flag > 0 && zlog.flushed()) {
                        response.add(new ZFrame(msg.buf().array()));
                        response.send(worker);
                    }
                } else
                    stop = true;
                if (!more)
                    state = START;
                break;

            case MESSAGE:

                if (store(zlog, count, msg)) {
                    if (flag > 0 && zlog.flushed()) {
                        response.add(getLastFrame(msg.buf().duplicate()));
                        response.send(worker);
                    }
                } else
                    stop = true;
                if (!more)
                    state = START;
View Full Code Here

            ZFrame frame = new ZFrame(WORKER_READY);
            frame.send(worker, 0);

            while (true) {
                //  Send request, get reply
                ZMsg msg = ZMsg.recvMsg(worker, 0);
                if (msg == null)
                    break;              //  Interrupted
                msg.getLast().print("Worker: ");
                msg.getLast().reset("OK");
                msg.send(worker);

            }
            ctx.destroy();
        }
View Full Code Here

    public static void main(String[] args) {
        boolean verbose = (args.length > 0 && "-v".equals(args[0]));
        mdcliapi clientSession = new mdcliapi("tcp://localhost:5555", verbose);

        ZMsg request = new ZMsg();

        // This is the service we want to look up
        request.addString("echo");

        // This is the service we send our request to
        ZMsg reply = clientSession.send("mmi.service", request);

        if (reply != null) {
            String replyCode = reply.getFirst().toString();
            System.out.printf("Lookup echo service: %s\n", replyCode);
        } else {
            System.out
                    .println("E: no response from broker, make sure it's running");
        }
View Full Code Here

    //  the second part is the endpoint. It waits 100msec for the connection to
    //  come up, which isn't pretty, but saves us from sending all requests to a
    //  single server, at startup time:
    public void connect(String endpoint)
    {
        ZMsg msg = new ZMsg();
        msg.add("CONNECT");
        msg.add(endpoint);

        msg.send(pipe);
        try {
            Thread.sleep(100);   //  Allow connection to come up
        } catch (InterruptedException e) {
        }
    }
View Full Code Here

    //  to the backend, specifying a command "REQUEST" and the request message:
    public ZMsg request(ZMsg request)
    {
        request.push("REQUEST");
        request.send(pipe);
        ZMsg reply = ZMsg.recvMsg(pipe);
        if (reply != null) {
            String status = reply.popString();
            if (status.equals("FAILED"))
                reply.destroy();
        }
        return reply;
    }
View Full Code Here

        }

        private void ping(Socket socket)
        {
            if (System.currentTimeMillis() >= pingAt) {
                ZMsg ping = new ZMsg();
                ping.add(endpoint);
                ping.add("PING");
                ping.send(socket);
                pingAt = System.currentTimeMillis() + PING_INTERVAL;
            }
        }
View Full Code Here

        //  (it's going to be CONNECT or REQUEST):

        //  Callback when we remove server from agent 'servers' hash table
        private void controlMessage()
        {
            ZMsg msg = ZMsg.recvMsg(pipe);
            String command = msg.popString();

            if (command.equals("CONNECT")) {
                String endpoint = msg.popString();
                System.out.printf("I: connecting to %s...\n", endpoint);
                router.connect(endpoint);
                Server server = new Server(endpoint);
                servers.put(endpoint, server);
                actives.add(server);
                server.pingAt = System.currentTimeMillis() + PING_INTERVAL;
                server.expires = System.currentTimeMillis() + SERVER_TTL;
            }
            else
            if (command.equals("REQUEST")) {
                assert (request == null);    //  Strict request-reply cycle
                //  Prefix request with sequence number and empty envelope
                String sequenceText = String.format("%d", ++sequence);
                msg.push(sequenceText);
                //  Take ownership of request message
                request = msg;
                msg = null;
                //  Request expires after global timeout
                expires = System.currentTimeMillis() + GLOBAL_TIMEOUT;
            }
            if (msg != null)
                msg.destroy();
        }
View Full Code Here

TOP

Related Classes of org.zeromq.ZMsg

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.