Search in sources :

Example 16 with ValueReference

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

the class Router method xhasIn.

@Override
protected boolean xhasIn() {
    // definitely more parts available.
    if (moreIn) {
        return true;
    }
    // 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);
    // TODO: handle the situation when the peer changes its identity.
    while (prefetchedMsg != null && prefetchedMsg.isIdentity()) {
        prefetchedMsg = fq.recvPipe(errno, pipe);
    }
    if (prefetchedMsg == null) {
        return false;
    }
    assert (pipe.get() != null);
    Blob identity = pipe.get().getIdentity();
    prefetchedId = new Msg(identity.data());
    prefetchedId.setFlags(Msg.MORE);
    prefetched = true;
    identitySent = false;
    return true;
}
Also used : Msg(zmq.Msg) Blob(zmq.util.Blob) Pipe(zmq.pipe.Pipe) ValueReference(zmq.util.ValueReference)

Example 17 with ValueReference

use of zmq.util.ValueReference 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 18 with ValueReference

use of zmq.util.ValueReference 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 19 with ValueReference

use of zmq.util.ValueReference 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

ValueReference (zmq.util.ValueReference)19 Msg (zmq.Msg)16 ByteBuffer (java.nio.ByteBuffer)11 Test (org.junit.Test)10 Pipe (zmq.pipe.Pipe)6 Blob (zmq.util.Blob)4 Step (zmq.io.coder.IDecoder.Step)3 Metadata (zmq.io.Metadata)2 IDecoder (zmq.io.coder.IDecoder)1 IEncoder (zmq.io.coder.IEncoder)1 V1Decoder (zmq.io.coder.v1.V1Decoder)1 V1Encoder (zmq.io.coder.v1.V1Encoder)1 V2Decoder (zmq.io.coder.v2.V2Decoder)1 V2Encoder (zmq.io.coder.v2.V2Encoder)1 Mechanisms (zmq.io.mechanism.Mechanisms)1 SelectorProviderChooser (zmq.io.net.SelectorProviderChooser)1 TcpAddressMask (zmq.io.net.tcp.TcpAddress.TcpAddressMask)1 MsgAllocator (zmq.msg.MsgAllocator)1 MsgAllocatorThreshold (zmq.msg.MsgAllocatorThreshold)1