Package org.zeromq

Examples of org.zeromq.ZMsg


            ZMQ.Poller items = ctx.getContext().poller();
            items.register(socket, ZMQ.Poller.POLLIN);
            if (items.poll(HEARTBEAT_INTERVAL * 1000) == -1)
                break; // Interrupted
            if (items.pollin(0)) {
                ZMsg msg = ZMsg.recvMsg(socket);
                if (msg == null)
                    break; // Interrupted

                if (verbose) {
                    log.format("I: received message:\n");
                    msg.dump(log.out());
                }

                ZFrame sender = msg.pop();
                ZFrame empty = msg.pop();
                ZFrame header = msg.pop();

                if (MDP.C_CLIENT.frameEquals(header)) {
                    processClient(sender, msg);
                } else if (MDP.W_WORKER.frameEquals(header))
                    processWorker(sender, msg);
                else {
                    log.format("E: invalid message:\n");
                    msg.dump(log.out());
                    msg.destroy();
                }

                sender.destroy();
                empty.destroy();
                header.destroy();
View Full Code Here


     * not destroy the message, this is the caller's job.
     */
    public void sendToWorker(Worker worker, MDP command, String option,
            ZMsg msgp) {

        ZMsg msg = msgp == null ? new ZMsg() : msgp.duplicate();

        // Stack protocol envelope to start of message
        if (option != null)
            msg.addFirst(new ZFrame(option));
        msg.addFirst(command.newFrame());
        msg.addFirst(MDP.W_WORKER.newFrame());

        // Stack routing envelope to start of message
        msg.wrap(worker.address.duplicate());
        if (verbose) {
            log.format("I: sending %s to worker\n", command);
            msg.dump(log.out());
        }
        msg.send(socket);
    }
View Full Code Here

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

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

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

        request.push(MDP.C_CLIENT.newFrame());
        if (verbose) {
            log.format("I: send request to '%s' service: \n", service);
            request.dump(log.out());
        }
        ZMsg reply = null;

        int retriesLeft = retries;
        while (retriesLeft > 0 && !Thread.currentThread().isInterrupted()) {

            request.duplicate().send(client);

            // Poll socket for a reply, with timeout
            ZMQ.Poller items = ctx.getContext().poller();
            items.register(client, ZMQ.Poller.POLLIN);
            if (items.poll(timeout * 1000) == -1)
                break; // Interrupted

            if (items.pollin(0)) {
                ZMsg msg = ZMsg.recvMsg(client);
                if (verbose){
                    log.format("I: received reply: \n");
                    msg.dump(log.out());
                }
                // Don't try to handle errors, just assert noisily
                assert (msg.size() >= 3);

                ZFrame header = msg.pop();
                assert (MDP.C_CLIENT.equals(header.toString()));
                header.destroy();

                ZFrame replyService = msg.pop();
                assert (service.equals(replyService.toString()));
                replyService.destroy();

                reply = msg;
                break;
View Full Code Here

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

        ZMsg reply = null;
        while (!Thread.currentThread().isInterrupted()) {
            ZMsg request = workerSession.receive(reply);
            if (request == null)
                break; //Interrupted
            reply = request; //  Echo is complex... :-)
        }
        workerSession.destroy();
View Full Code Here

     * Returns the reply message or NULL if there was no reply. Does not attempt
     * to recover from a broker failure, this is not possible without storing
     * all unanswered requests and resending them all…
     */
    public ZMsg recv() {
        ZMsg reply = null;

        // Poll socket for a reply, with timeout
        ZMQ.Poller items = ctx.getContext().poller();
        items.register(client, ZMQ.Poller.POLLIN);
        if (items.poll(timeout * 1000) == -1)
            return null; // Interrupted

        if (items.pollin(0)) {
            ZMsg msg = ZMsg.recvMsg(client);
            if (verbose) {
                log.format("I: received reply: \n");
                msg.dump(log.out());
            }
            // Don't try to handle errors, just assert noisily
            assert (msg.size() >= 4);

            ZFrame empty = msg.pop();
            assert (empty.getData().length == 0);
            empty.destroy();

            ZFrame header = msg.pop();
            assert (MDP.C_CLIENT.equals(header.toString()));
            header.destroy();

            ZFrame replyService = msg.pop();
            replyService.destroy();

            reply = msg;
        }
        return reply;
View Full Code Here

            System.out.println ("E: protocol error");
        }

        private void controlMessage ()
        {
            ZMsg msg = ZMsg.recvMsg (pipe);
            String method = msg.popString ();
            if (method.equals ("SUBSCRIBE")) {
                String path = msg.popString ();
                //  Store subscription along with any previous ones        
                //  Check we don't already have a subscription for this path
                for (Sub sub: subs) {                                      
                    if (path.equals (sub.path))                            
                        return;                                            
                }                                                          
                //  Subscription path must start with '/'                  
                //  We'll do better error handling later                   
                assert (path.startsWith ("/"));                            
                                                                           
                //  New subscription, store it for later replay            
                String inbox = config.resolve ("client/inbox", ".inbox");  
                sub = new Sub (this, inbox, path);                         
                subs.add (sub);                                            
            }
            else
            if (method.equals ("SET INBOX")) {
                String path = msg.popString ();
                config.setPath ("client/inbox", path);
            }
            else
            if (method.equals ("SET RESYNC")) {
                long enabled = Long.parseLong (msg.popString ());
                //  Request resynchronization from server               
                config.setPath ("client/resync", enabled > 0 ? "1" :"0");
            }
            else
            if (method.equals ("CONFIG")) {
                String config_file = msg.popString ();
                config.destroy ();
                config = FmqConfig.load (config_file);
                if (config != null)
                    applyConfig ();
                else {
                    System.out.printf ("E: cannot load config file '%s'\n", config_file);
                    config = new FmqConfig ("root", null);
                }
            }
            else
            if (method.equals ("SETOPTION")) {
                String path = msg.popString ();
                String value = msg.popString ();
                config.setPath (path, value);
                config ();
            }
            else
            if (method.equals ("STOP")) {
                pipe.send ("OK");
                stopped = true;
            }
            else
            if (method.equals ("CONNECT")) {
                String endpoint = msg.popString ();
                if (nbrServers < MAX_SERVERS) {
                    Server server = new Server (ctx, endpoint);
                    servers [nbrServers++] = server;
                    dirty = true;
                    serverExecute (server, Event.initialize_event);
                } else
                    System.out.printf ("E: too many server connections (max %d)\n", MAX_SERVERS);
            }
            msg.destroy ();

        }
View Full Code Here

            config ();
        }

        private void controlMessage ()
        {
            ZMsg msg = ZMsg.recvMsg (pipe);
            String method = msg.popString ();
            if (method.equals ("BIND")) {
                String endpoint = msg.popString ();
                port = bind (router, endpoint);
                pipe.send (String.format ("%d", port));
            }
            else
            if (method.equals ("PUBLISH")) {
                String location = msg.popString ();
                String alias = msg.popString ();
                Mount mount = new Mount (location, alias);
                mounts.add (mount);                      
            }
            else
            if (method.equals ("SET ANONYMOUS")) {
                long enabled = Long.parseLong (msg.popString ());
                //  Enable anonymous access without a config file            
                config.setPath ("security/anonymous", enabled > 0 ? "1" :"0");
            }
            else
            if (method.equals ("CONFIG")) {
                String config_file = msg.popString ();
                config.destroy ();
                config = FmqConfig.load (config_file);
                if (config != null)
                    applyConfig ();
                else {
                    System.out.printf ("E: cannot load config file '%s'\n", config_file);
                    config = new FmqConfig ("root", null);
                }
            }
            else
            if (method.equals ("SETOPTION")) {
                String path = msg.popString ();
                String value = msg.popString ();
                config.setPath (path, value);
                config ();
            }
            else
            if (method.equals ("STOP")) {
                pipe.send ("OK");
                stopped = true;
            }
            msg.destroy ();
            msg = null;
        }
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.