Search in sources :

Example 6 with Blob

use of zmq.util.Blob in project jeromq by zeromq.

the class Router method xrecv.

@Override
protected Msg xrecv() {
    Msg msg;
    if (prefetched) {
        if (!identitySent) {
            msg = prefetchedId;
            prefetchedId = null;
            identitySent = true;
        } else {
            msg = prefetchedMsg;
            prefetchedMsg = null;
            prefetched = false;
        }
        moreIn = msg.hasMore();
        return msg;
    }
    ValueReference<Pipe> pipe = new ValueReference<>();
    msg = fq.recvPipe(errno, pipe);
    // TODO: handle the situation when the peer changes its identity.
    while (msg != null && msg.isIdentity()) {
        msg = fq.recvPipe(errno, pipe);
    }
    if (msg == null) {
        return null;
    }
    assert (pipe.get() != null);
    // If we are in the middle of reading a message, just return the next part.
    if (moreIn) {
        moreIn = msg.hasMore();
    } else {
        // We are at the beginning of a message.
        // Keep the message part we have in the prefetch buffer
        // and return the ID of the peer instead.
        prefetchedMsg = msg;
        prefetched = true;
        Blob identity = pipe.get().getIdentity();
        msg = new Msg(identity.data());
        msg.setFlags(Msg.MORE);
        identitySent = true;
    }
    return msg;
}
Also used : Msg(zmq.Msg) Blob(zmq.util.Blob) Pipe(zmq.pipe.Pipe) ValueReference(zmq.util.ValueReference)

Example 7 with Blob

use of zmq.util.Blob in project jeromq by zeromq.

the class Router method xsend.

@Override
protected boolean xsend(Msg msg) {
    // peer to send the message to.
    if (!moreOut) {
        assert (currentOut == null);
        // TODO: The connections should be killed instead.
        if (msg.hasMore()) {
            moreOut = true;
            // Find the pipe associated with the identity stored in the prefix.
            // If there's no such pipe just silently ignore the message, unless
            // mandatory is set.
            Blob identity = Blob.createBlob(msg);
            Outpipe op = outpipes.get(identity);
            if (op != null) {
                currentOut = op.pipe;
                if (!currentOut.checkWrite()) {
                    op.active = false;
                    currentOut = null;
                    if (mandatory) {
                        moreOut = false;
                        errno.set(ZError.EAGAIN);
                        return false;
                    }
                }
            } else if (mandatory) {
                moreOut = false;
                errno.set(ZError.EHOSTUNREACH);
                return false;
            }
        }
        return true;
    }
    // Ignore the MORE flag for raw-sock or assert?
    if (options.rawSocket) {
        msg.resetFlags(Msg.MORE);
    }
    // Check whether this is the last part of the message.
    moreOut = msg.hasMore();
    // Push the message into the pipe. If there's no out pipe, just drop it.
    if (currentOut != null) {
        // Pending messages in the pipe will be dropped (on receiving term- ack)
        if (rawSocket && msg.size() == 0) {
            currentOut.terminate(false);
            currentOut = null;
            return true;
        }
        boolean ok = currentOut.write(msg);
        if (!ok) {
            // Message failed to send - we must close it ourselves.
            currentOut = null;
        } else if (!moreOut) {
            currentOut.flush();
            currentOut = null;
        }
    }
    return true;
}
Also used : Blob(zmq.util.Blob)

Example 8 with Blob

use of zmq.util.Blob in project jeromq by zeromq.

the class Stream method xhasIn.

@Override
protected boolean xhasIn() {
    // We may already have a message pre-fetched.
    if (prefetched) {
        return true;
    }
    // Try to read the next message.
    // The message, if read, is kept in the pre-fetch buffer.
    ValueReference<Pipe> pipe = new ValueReference<>();
    prefetchedMsg = fq.recvPipe(errno, pipe);
    if (prefetchedMsg == null) {
        return false;
    }
    assert (pipe.get() != null);
    assert (!prefetchedMsg.hasMore());
    Blob identity = pipe.get().getIdentity();
    prefetchedId = new Msg(identity.data());
    // forward metadata (if any)
    Metadata metadata = prefetchedMsg.getMetadata();
    if (metadata != null) {
        prefetchedId.setMetadata(metadata);
    }
    prefetchedId.setFlags(Msg.MORE);
    prefetched = true;
    identitySent = false;
    return true;
}
Also used : Msg(zmq.Msg) Blob(zmq.util.Blob) Metadata(zmq.io.Metadata) Pipe(zmq.pipe.Pipe) ValueReference(zmq.util.ValueReference)

Example 9 with Blob

use of zmq.util.Blob in project jeromq by zeromq.

the class Stream method identifyPeer.

private void identifyPeer(Pipe pipe, boolean isLocallyInitiated) {
    // Always assign identity for raw-socket
    Blob identity;
    if (connectRid != null && !connectRid.isEmpty() && isLocallyInitiated) {
        identity = Blob.createBlob(connectRid.getBytes(ZMQ.CHARSET));
        connectRid = null;
        Outpipe outpipe = outpipes.get(identity);
        assert (outpipe == null);
    } else {
        ByteBuffer buf = ByteBuffer.allocate(5);
        buf.put((byte) 0);
        Wire.putUInt32(buf, nextRid++);
        identity = Blob.createBlob(buf.array());
    }
    pipe.setIdentity(identity);
    // Add the record into output pipes lookup table
    Outpipe outpipe = new Outpipe(pipe, true);
    outpipes.put(identity, outpipe);
}
Also used : Blob(zmq.util.Blob) ByteBuffer(java.nio.ByteBuffer)

Example 10 with Blob

use of zmq.util.Blob in project jeromq by zeromq.

the class Stream method xrecv.

@Override
public Msg xrecv() {
    Msg msg;
    if (prefetched) {
        if (!identitySent) {
            msg = prefetchedId;
            prefetchedId = null;
            identitySent = true;
        } else {
            msg = prefetchedMsg;
            prefetchedMsg = null;
            prefetched = false;
        }
        return msg;
    }
    ValueReference<Pipe> pipe = new ValueReference<>();
    prefetchedMsg = fq.recvPipe(errno, pipe);
    // TODO DIFF V4 we sometimes need to process the commands to receive data, let's just return and give it another chance
    if (prefetchedMsg == null) {
        errno.set(ZError.EAGAIN);
        return null;
    }
    assert (pipe.get() != null);
    assert (!prefetchedMsg.hasMore());
    // We have received a frame with TCP data.
    // Rather than sending this frame, we keep it in prefetched
    // buffer and send a frame with peer's ID.
    Blob identity = pipe.get().getIdentity();
    msg = new Msg(identity.data());
    // forward metadata (if any)
    Metadata metadata = prefetchedMsg.getMetadata();
    if (metadata != null) {
        msg.setMetadata(metadata);
    }
    msg.setFlags(Msg.MORE);
    prefetched = true;
    identitySent = true;
    return msg;
}
Also used : Msg(zmq.Msg) Blob(zmq.util.Blob) Metadata(zmq.io.Metadata) Pipe(zmq.pipe.Pipe) ValueReference(zmq.util.ValueReference)

Aggregations

Blob (zmq.util.Blob)10 Msg (zmq.Msg)7 Pipe (zmq.pipe.Pipe)4 ValueReference (zmq.util.ValueReference)4 ByteBuffer (java.nio.ByteBuffer)2 Metadata (zmq.io.Metadata)2