Package org.zeromq

Examples of org.zeromq.ZFrame


                //  Check if we have the credit to send chunk                              
                if (chunk.size () <= client.credit) {                                      
                    client.reply.setSequence (client.sequence++);                          
                    client.reply.setOperation (FmqMsg.FMQ_MSG_FILE_CREATE);                
                    client.reply.setOffset (client.offset);                                
                    client.reply.setChunk (new ZFrame (chunk.data ()));                    
                                                                                           
                    client.offset += chunk.size ();                                        
                    client.credit -= chunk.size ();                                        
                    client.next_event = Event.send_chunk_event;                            
                                                                                           
View Full Code Here


    public static FmqMsg recv (Socket input)
    {
        assert (input != null);
        FmqMsg self = new FmqMsg (0);
        ZFrame frame = null;

        try {
            //  Read valid message frame from socket; we loop over any
            //  garbage data we might receive from badly-connected peers
            while (true) {
                //  If we're reading from a ROUTER socket, get address
                if (input.getType () == ZMQ.ROUTER) {
                    self.address = ZFrame.recvFrame (input);
                    if (self.address == null)
                        return null;         //  Interrupted
                    if (!input.hasReceiveMore ())
                        throw new IllegalArgumentException ();
                }
                //  Read and parse command in frame
                frame = ZFrame.recvFrame (input);
                if (frame == null)
                    return null;             //  Interrupted

                //  Get and check protocol signature
                self.needle = ByteBuffer.wrap (frame.getData ());
                int signature = self.getNumber2 ();
                if (signature == (0xAAA0 | 3))
                    break;                  //  Valid signature

                //  Protocol assertion, drop message
                while (input.hasReceiveMore ()) {
                    frame.destroy ();
                    frame = ZFrame.recvFrame (input);
                }
                frame.destroy ();
            }

            //  Get message id, which is first byte in frame
            self.id = self.getNumber1 ();
            int listSize;
            int hashSize;

            switch (self.id) {
            case OHAI:
                self.protocol = self.getString ();
                if (!self.protocol.equals( "FILEMQ"))
                    throw new IllegalArgumentException ();
                self.version = self.getNumber1 ();
                if (self.version != FMQ_MSG_VERSION)
                    throw new IllegalArgumentException ();
                break;

            case ORLY:
                listSize = self.getNumber1 ();
                self.mechanisms = new ArrayList<String> ();
                while (listSize-- > 0) {
                    String string = self.getString ();
                    self.mechanisms.add (string);
                }
                //  Get next frame, leave current untouched
                if (!input.hasReceiveMore ())
                    throw new IllegalArgumentException ();
                self.challenge = ZFrame.recvFrame (input);
                break;

            case YARLY:
                self.mechanism = self.getString ();
                //  Get next frame, leave current untouched
                if (!input.hasReceiveMore ())
                    throw new IllegalArgumentException ();
                self.response = ZFrame.recvFrame (input);
                break;

            case OHAI_OK:
                break;

            case ICANHAZ:
                self.path = self.getString ();
                hashSize = self.getNumber1 ();
                self.options = new HashMap <String, String> ();
                while (hashSize-- > 0) {
                    String string = self.getString ();
                    String [] kv = string.split("=");
                    self.options.put(kv[0], kv[1]);
                }

                hashSize = self.getNumber1 ();
                self.cache = new HashMap <String, String> ();
                while (hashSize-- > 0) {
                    String string = self.getString ();
                    String [] kv = string.split("=");
                    self.cache.put(kv[0], kv[1]);
                }

                break;

            case ICANHAZ_OK:
                break;

            case NOM:
                self.credit = self.getNumber8 ();
                self.sequence = self.getNumber8 ();
                break;

            case CHEEZBURGER:
                self.sequence = self.getNumber8 ();
                self.operation = self.getNumber1 ();
                self.filename = self.getString ();
                self.offset = self.getNumber8 ();
                self.eof = self.getNumber1 ();
                hashSize = self.getNumber1 ();
                self.headers = new HashMap <String, String> ();
                while (hashSize-- > 0) {
                    String string = self.getString ();
                    String [] kv = string.split("=");
                    self.headers.put(kv[0], kv[1]);
                }

                //  Get next frame, leave current untouched
                if (!input.hasReceiveMore ())
                    throw new IllegalArgumentException ();
                self.chunk = ZFrame.recvFrame (input);
                break;

            case HUGZ:
                break;

            case HUGZ_OK:
                break;

            case KTHXBAI:
                break;

            case SRSLY:
                self.reason = self.getString ();
                break;

            case RTFM:
                self.reason = self.getString ();
                break;

            default:
                throw new IllegalArgumentException ();
            }

            return self;

        } catch (Exception e) {
            //  Error returns
            System.out.printf ("E: malformed message '%d'\n", self.id);
            self.destroy ();
            return null;
        } finally {
            if (frame != null)
                frame.destroy ();
        }
    }
View Full Code Here

        default:
            System.out.printf ("E: bad message type '%d', not sent\n", id);
            assert (false);
        }
        //  Now serialize message into the frame
        ZFrame frame = new ZFrame (new byte [frameSize]);
        needle = ByteBuffer.wrap (frame.getData ());
        int frameFlags = 0;
        putNumber2 (0xAAA0 | 3);
        putNumber1 ((byte) id);

        switch (id) {
        case OHAI:
            putString ("FILEMQ");
            putNumber1 (FMQ_MSG_VERSION);
            break;
           
        case ORLY:
            if (mechanisms != null) {
                putNumber1 ((byte) mechanisms.size ());
                for (String value : mechanisms) {
                    putString (value);
                }
            }
            else
                putNumber1 ((byte) 0);      //  Empty string array
            frameFlags = ZMQ.SNDMORE;
            break;
           
        case YARLY:
            if (mechanism != null)
                putString (mechanism);
            else
                putNumber1 ((byte) 0);      //  Empty string
            frameFlags = ZMQ.SNDMORE;
            break;
           
        case OHAI_OK:
            break;
           
        case ICANHAZ:
            if (path != null)
                putString (path);
            else
                putNumber1 ((byte) 0);      //  Empty string
            if (options != null) {
                putNumber1 ((byte) options.size ());
                for (Map.Entry <String, String> entry: options.entrySet ()) {
                    optionsWrite (entry, this);
                }
            }
            else
                putNumber1 ((byte) 0);      //  Empty dictionary
            if (cache != null) {
                putNumber1 ((byte) cache.size ());
                for (Map.Entry <String, String> entry: cache.entrySet ()) {
                    cacheWrite (entry, this);
                }
            }
            else
                putNumber1 ((byte) 0);      //  Empty dictionary
            break;
           
        case ICANHAZ_OK:
            break;
           
        case NOM:
            putNumber8 (credit);
            putNumber8 (sequence);
            break;
           
        case CHEEZBURGER:
            putNumber8 (sequence);
            putNumber1 (operation);
            if (filename != null)
                putString (filename);
            else
                putNumber1 ((byte) 0);      //  Empty string
            putNumber8 (offset);
            putNumber1 (eof);
            if (headers != null) {
                putNumber1 ((byte) headers.size ());
                for (Map.Entry <String, String> entry: headers.entrySet ()) {
                    headersWrite (entry, this);
                }
            }
            else
                putNumber1 ((byte) 0);      //  Empty dictionary
            frameFlags = ZMQ.SNDMORE;
            break;
           
        case HUGZ:
            break;
           
        case HUGZ_OK:
            break;
           
        case KTHXBAI:
            break;
           
        case SRSLY:
            if (reason != null)
                putString (reason);
            else
                putNumber1 ((byte) 0);      //  Empty string
            break;
           
        case RTFM:
            if (reason != null)
                putString (reason);
            else
                putNumber1 ((byte) 0);      //  Empty string
            break;
           
        }
        //  If we're sending to a ROUTER, we send the address first
        if (socket.getType () == ZMQ.ROUTER) {
            assert (address != null);
            if (!address.sendAndDestroy (socket, ZMQ.SNDMORE)) {
                destroy ();
                return false;
            }
        }
        //  Now send the data frame
        if (!frame.sendAndDestroy (socket, frameFlags)) {
            frame.destroy ();
            destroy ();
            return false;
        }
       
        //  Now send any frame fields, in order
        switch (id) {
        case ORLY:
            //  If challenge isn't set, send an empty frame
            if (challenge == null)
                challenge = new ZFrame ("".getBytes ());
            if (!challenge.sendAndDestroy (socket, 0)) {
                frame.destroy ();
                destroy ();
                return false;
            }
            break;
        case YARLY:
            //  If response isn't set, send an empty frame
            if (response == null)
                response = new ZFrame ("".getBytes ());
            if (!response.sendAndDestroy (socket, 0)) {
                frame.destroy ();
                destroy ();
                return false;
            }
            break;
        case CHEEZBURGER:
            //  If chunk isn't set, send an empty frame
            if (chunk == null)
                chunk = new ZFrame ("".getBytes ());
            if (!chunk.sendAndDestroy (socket, 0)) {
                frame.destroy ();
                destroy ();
                return false;
            }
            break;
        }
View Full Code Here

TOP

Related Classes of org.zeromq.ZFrame

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.