Examples of ZMsg


Examples of org.zeromq.ZMsg

    //  .split set method
    //  Set a new value in the shared hashmap. Sends a [SET][key][value][ttl]
    //  command through to the agent which does the actual work:
    public void set(String key, String value, int ttl)
    {
        ZMsg msg = new ZMsg();
        msg.add("SET");
        msg.add(key);
        msg.add(value);
        msg.add(String.format("%d", ttl));
        msg.send(pipe);
    }
View Full Code Here

Examples of org.zeromq.ZMsg

    //  Look up value in distributed hash table. Sends [GET][key] to the agent and
    //  waits for a value response. If there is no value available, will eventually
    //  return NULL:
    public String get(String key)
    {
        ZMsg msg = new ZMsg();
        msg.add("GET");
        msg.add(key);
        msg.send(pipe);

        ZMsg reply = ZMsg.recvMsg(pipe);
        if (reply != null) {
            String value = reply.popString();
            reply.destroy();
            return value;
        }
        return null;
    }
View Full Code Here

Examples of org.zeromq.ZMsg

        //  .split handling a control message
        //  Here we handle the different control messages from the frontend;
        //  SUBTREE, CONNECT, SET, and GET:
        private boolean controlMessage()
        {
            ZMsg msg = ZMsg.recvMsg(pipe);
            String command = msg.popString();
            if (command == null)
                return false;      //  Interrupted

            if (command.equals("SUBTREE")) {
                subtree = msg.popString();
            }
            else
            if (command.equals("CONNECT")) {
                String address = msg.popString();
                String service = msg.popString();
                if (nbrServers < SERVER_MAX) {
                    server [nbrServers++] = new Server(
                            ctx, address, Integer.parseInt(service), subtree);
                    //  We broadcast updates to all known servers
                    publisher.connect(String.format("%s:%d",
                            address, Integer.parseInt(service) + 2));
                }
                else
                    System.out.printf("E: too many servers (max. %d)\n", SERVER_MAX);
            }
            else
            //  .split set and get commands
            //  When we set a property, we push the new key-value pair onto
            //  all our connected servers:
            if (command.equals("SET")) {
                String key = msg.popString();
                String value = msg.popString();
                String ttl = msg.popString();
                kvmap.put(key, value);

                //  Send key-value pair on to server
                kvmsg kvmsg = new kvmsg(0);
                kvmsg.setKey(key);
                kvmsg.setUUID();
                kvmsg.fmtBody("%s", value);
                kvmsg.setProp("ttl", ttl);
                kvmsg.send(publisher);
                kvmsg.destroy();
            }
            else
            if (command.equals("GET")) {
                String key = msg.popString();
                String value = kvmap.get(key);
                if (value != null)
                    pipe.send(value);
                else
                    pipe.send("");
            }
            msg.destroy();

            return true;
        }
View Full Code Here

Examples of org.zeromq.ZMsg

        Socket server = ctx.createSocket(ZMQ.REP);
        server.bind(args[0]);

        System.out.printf ("I: echo service is ready at %s\n", args[0]);
        while (true) {
            ZMsg msg = ZMsg.recvMsg(server);
            if (msg == null)
                break;          //  Interrupted
            msg.send(server);
        }
        if (Thread.currentThread().isInterrupted())
            System.out.printf ("W: interrupted\n");

        ctx.destroy();
View Full Code Here

Examples of org.zeromq.ZMsg

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

        int count;
        for (count = 0; count < 100000; count++) {
            ZMsg request = new ZMsg();
            request.addString("Hello world");
            ZMsg reply = clientSession.send("echo", request);
            if (reply != null)
                reply.destroy();
            else
                break; // Interrupt or failure
        }

        System.out.printf("%d requests/replies processed\n", count);
View Full Code Here

Examples of org.zeromq.ZMsg

            System.out.println("Synchronous round-trip test");
            start = System.currentTimeMillis();

            for (requests = 0; requests < SAMPLE_SIZE; requests++) {
                ZMsg req = new ZMsg();
                req.addString("hello");
                req.send(client);
                ZMsg.recvMsg(client).destroy();
            }

            System.out.printf(" %d calls/second\n",
                    (1000 * SAMPLE_SIZE) / (System.currentTimeMillis() - start));

            System.out.println("Asynchronous round-trip test");
            start = System.currentTimeMillis();

            for (requests = 0; requests < SAMPLE_SIZE; requests++) {
                ZMsg req = new ZMsg();
                req.addString("hello");
                req.send(client);
            }
            for (requests = 0; requests < SAMPLE_SIZE
                    && !Thread.currentThread().isInterrupted(); requests++) {
                ZMsg.recvMsg(client).destroy();
            }
View Full Code Here

Examples of org.zeromq.ZMsg

        Socket server = ctx.createSocket(ZMQ.REP);
        server.bind(args[0]);

        System.out.printf ("I: echo service is ready at %s\n", args[0]);
        while (true) {
            ZMsg request = ZMsg.recvMsg(server);
            if (request == null)
                break;          //  Interrupted

            //  Fail nastily if run against wrong client
            assert (request.size() == 2);

            ZFrame identity = request.pop();
            request.destroy();

            ZMsg reply = new ZMsg();
            reply.add(identity);
            reply.add("OK");
            reply.send(server);
        }
        if (Thread.currentThread().isInterrupted())
            System.out.printf ("W: interrupted\n");

        ctx.destroy();
View Full Code Here

Examples of org.zeromq.ZMsg

            //  Track if capacity changes during this iteration
            int previous = localCapacity;


            //  Handle reply from local worker
            ZMsg msg = null;
            if (primary[0].isReadable()) {
                msg = ZMsg.recvMsg(localbe);
                if (msg == null)
                    break;          //  Interrupted
                ZFrame address = msg.unwrap();
                workers.add(address);
                localCapacity++;

                //  If it's READY, don't route the message any further
                ZFrame frame = msg.getFirst();
                if (new String(frame.getData()).equals(WORKER_READY)) {
                    msg.destroy();
                    msg = null;
                }
            }
            //  Or handle reply from peer broker
            else if (primary[1].isReadable()) {
                msg = ZMsg.recvMsg(cloudbe);
                if (msg == null)
                    break;          //  Interrupted
                //  We don't use peer broker address for anything
                ZFrame address = msg.unwrap();
                address.destroy();
            }
            //  Route reply to cloud if it's addressed to a broker
            for (argn = 1; msg != null && argn < argv.length; argn++) {
                byte[] data = msg.getFirst().getData();
                if (argv[argn].equals(new String(data))) {
                    msg.send(cloudfe);
                    msg = null;
                }
            }
            //  Route reply to client if we still need to
            if (msg != null)
                msg.send(localfe);

            //  If we have input messages on our statefe or monitor sockets we
            //  can process these immediately:

            if (primary[2].isReadable()) {
                String peer = statefe.recvStr();
                String status = statefe.recvStr();
                cloudCapacity = Integer.parseInt(status);
            }
            if (primary[3].isReadable()) {
                String status = monitor.recvStr();
                System.out.println(status);
            }

            //  Now we route as many client requests as we have worker capacity
            //  for. We may reroute requests from our local frontend, but not from //
            //  the cloud frontend. We reroute randomly now, just to test things
            //  out. In the next version we'll do this properly by calculating
            //  cloud capacity://

            while (localCapacity + cloudCapacity > 0) {
                PollItem secondary[] = {
                        new PollItem(localfe, Poller.POLLIN),
                        new PollItem(cloudfe, Poller.POLLIN)
                };

                if (localCapacity > 0)
                    rc = ZMQ.poll(secondary, 2, 0);
                else
                    rc = ZMQ.poll(secondary, 1, 0);

                assert (rc >= 0);

                if (secondary[0].isReadable()) {
                    msg = ZMsg.recvMsg(localfe);
                } else if (secondary[1].isReadable()) {
                    msg = ZMsg.recvMsg(cloudfe);
                } else
                    break;      //  No work, go back to backends

                if (localCapacity > 0) {
                    ZFrame frame = workers.remove(0);
                    msg.wrap(frame);
                    msg.send(localbe);
                    localCapacity--;

                } else {
                    //  Route to random broker peer
                    int random_peer = rand.nextInt(argv.length - 1) + 1;
                    msg.push(argv[random_peer]);
                    msg.send(cloudbe);
                }
            }

            //  We broadcast capacity messages to other peers; to reduce chatter
            //  we do this only if our capacity changed.
View Full Code Here

Examples of org.zeromq.ZMsg

            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

                //  Workers are busy for 0/1 seconds
                try {
                    Thread.sleep(rand.nextInt(2) * 1000);
                } catch (InterruptedException e) {
                }

                msg.send(worker);

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

Examples of org.zeromq.ZMsg

            if (items [0].isReadable()) {
                //  Get message
                //  - 3-part envelope + content -> request
                //  - 1-part HEARTBEAT -> heartbeat
                ZMsg msg = ZMsg.recvMsg(worker);
                if (msg == null)
                    break;          //  Interrupted

                //  To test the robustness of the queue implementation we //
                //  simulate various typical problems, such as the worker
                //  crashing, or running very slowly. We do this after a few
                //  cycles so that the architecture can get up and running
                //  first:
                if (msg.size() == 3) {
                    cycles++;
                    if (cycles > 3 && rand.nextInt(5) == 0) {
                        System.out.println ("I: simulating a crash\n");
                        msg.destroy();
                        msg = null;
                        break;
                    }
                    else
                    if (cycles > 3 && rand.nextInt (5) == 0) {
                        System.out.println ("I: simulating CPU overload\n");
                        try {
                            Thread.sleep (3000);
                        } catch (InterruptedException e) {
                            break;
                        }
                    }
                    System.out.println ("I: normal reply\n");
                    msg.send( worker);
                    liveness = HEARTBEAT_LIVENESS;
                    try {
                        Thread.sleep (1000);
                    } catch (InterruptedException e) {
                        break;
                    }              //  Do some heavy work
                }
                else
                //  When we get a heartbeat message from the queue, it means the
                //  queue was (recently) alive, so reset our liveness indicator:
                if (msg.size() == 1) {
                    ZFrame frame = msg.getFirst();
                    if (PPP_HEARTBEAT.equals(new String(frame.getData())))
                        liveness = HEARTBEAT_LIVENESS;
                    else {
                        System.out.println ("E: invalid message\n");
                        msg.dump(System.out);
                    }
                    msg.destroy();
                }
                else {
                    System.out.println ("E: invalid message\n");
                    msg.dump(System.out);
                }
                interval = INTERVAL_INIT;
            }
            else
            //  If the queue hasn't sent us heartbeats in a while, destroy the
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.