Search in sources :

Example 16 with Pipe

use of zmq.pipe.Pipe 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 Pipe

use of zmq.pipe.Pipe 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 Pipe

use of zmq.pipe.Pipe 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 Pipe

use of zmq.pipe.Pipe 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)

Example 20 with Pipe

use of zmq.pipe.Pipe in project jeromq by zeromq.

the class Radio method xreadActivated.

@Override
public void xreadActivated(Pipe pipe) {
    Msg msg = pipe.read();
    while (msg != null) {
        if (msg.isJoin()) {
            if (!subscriptions.containsKey(msg.getGroup())) {
                subscriptions.put(msg.getGroup(), new ArrayList<>());
            }
            List<Pipe> pipes = subscriptions.get(msg.getGroup());
            pipes.add(pipe);
        } else if (msg.isLeave()) {
            List<Pipe> pipes = subscriptions.get(msg.getGroup());
            if (pipes != null) {
                pipes.remove(pipe);
                if (pipes.isEmpty()) {
                    subscriptions.remove(msg.getGroup());
                }
            }
        }
        msg = pipe.read();
    }
}
Also used : Msg(zmq.Msg) ArrayList(java.util.ArrayList) List(java.util.List) Pipe(zmq.pipe.Pipe)

Aggregations

Pipe (zmq.pipe.Pipe)20 Msg (zmq.Msg)13 Test (org.junit.Test)6 ValueReference (zmq.util.ValueReference)6 Blob (zmq.util.Blob)4 InetSocketAddress (java.net.InetSocketAddress)2 ZObject (zmq.ZObject)2 Metadata (zmq.io.Metadata)2 IZAddress (zmq.io.net.Address.IZAddress)2 NetProtocol (zmq.io.net.NetProtocol)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Ctx (zmq.Ctx)1 IOThread (zmq.io.IOThread)1 SessionBase (zmq.io.SessionBase)1 Address (zmq.io.net.Address)1