Search in sources :

Example 16 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Channel method xrecv.

@Override
protected Msg xrecv() {
    if (pipe == null) {
        // Initialize the output parameter to be a 0-byte message.
        errno.set(ZError.EAGAIN);
        return null;
    }
    // Drop any messages with more flag
    Msg msg = pipe.read();
    while (msg != null && msg.hasMore()) {
        // drop all frames of the current multi-frame message
        msg = pipe.read();
        while (msg != null && msg.hasMore()) {
            msg = pipe.read();
        }
        // get the new message
        if (msg != null) {
            msg = pipe.read();
        }
    }
    if (msg == null) {
        errno.set(ZError.EAGAIN);
        return null;
    }
    lastIn = pipe;
    return msg;
}
Also used : Msg(zmq.Msg)

Example 17 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class XSub method sendSubscription.

private boolean sendSubscription(byte[] data, int size, Pipe pipe) {
    // Create the subscription message.
    Msg msg = new Msg(size + 1);
    msg.put((byte) 1).put(data, 0, size);
    // Send it to the pipe.
    boolean sent = pipe.write(msg);
    return sent;
}
Also used : Msg(zmq.Msg)

Example 18 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Rep method xrecv.

@Override
protected Msg xrecv() {
    // If we are in middle of sending a reply, we cannot receive next request.
    if (sendingReply) {
        errno.set(ZError.EFSM);
    }
    Msg msg;
    // to the reply pipe.
    if (requestBegins) {
        while (true) {
            msg = super.xrecv();
            if (msg == null) {
                return null;
            }
            if (msg.hasMore()) {
                // Empty message part delimits the traceback stack.
                boolean bottom = (msg.size() == 0);
                // Push it to the reply pipe.
                boolean rc = super.xsend(msg);
                assert (rc);
                if (bottom) {
                    break;
                }
            } else {
                // If the traceback stack is malformed, discard anything
                // already sent to pipe (we're at end of invalid message).
                super.rollback();
            }
        }
        requestBegins = false;
    }
    // Get next message part to return to the user.
    msg = super.xrecv();
    if (msg == null) {
        return null;
    }
    // If whole request is read, flip the FSM to reply-sending state.
    if (!msg.hasMore()) {
        sendingReply = true;
        requestBegins = true;
    }
    return msg;
}
Also used : Msg(zmq.Msg)

Example 19 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Pipe method processHiccup.

@Override
protected void processHiccup(YPipeBase<Msg> pipe) {
    // migrated to this thread.
    assert (outpipe != null);
    outpipe.flush();
    Msg msg;
    while ((msg = outpipe.read()) != null) {
        if (!msg.hasMore()) {
            msgsWritten--;
        }
    }
    // Plug in the new outpipe.
    assert (pipe != null);
    outpipe = pipe;
    outActive = true;
    // If appropriate, notify the user about the hiccup.
    if (state == State.ACTIVE) {
        sink.hiccuped(this);
    }
}
Also used : Msg(zmq.Msg)

Example 20 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class FQ method recvPipe.

public Msg recvPipe(Errno errno, ValueReference<Pipe> pipe) {
    // Round-robin over the pipes to get the next message.
    while (active > 0) {
        // Try to fetch new message. If we've already read part of the message
        // subsequent part should be immediately available.
        final Pipe currentPipe = pipes.get(current);
        final Msg msg = currentPipe.read();
        final boolean fetched = msg != null;
        // the 'current' pointer.
        if (fetched) {
            if (pipe != null) {
                pipe.set(currentPipe);
            }
            more = msg.hasMore();
            if (!more) {
                lastIn = currentPipe;
                // happens when multiple threads receive messages
                assert (active > 0);
                current = (current + 1) % active;
            }
            return msg;
        }
        // we should get the remaining parts without blocking.
        assert (!more);
        active--;
        Collections.swap(pipes, current, active);
        if (current == active) {
            current = 0;
        }
    }
    // No message is available. Initialize the output parameter
    // to be a 0-byte message.
    errno.set(ZError.EAGAIN);
    return null;
}
Also used : Msg(zmq.Msg) Pipe(zmq.pipe.Pipe)

Aggregations

Msg (zmq.Msg)124 Test (org.junit.Test)45 SocketBase (zmq.SocketBase)37 Ctx (zmq.Ctx)32 ByteBuffer (java.nio.ByteBuffer)16 ValueReference (zmq.util.ValueReference)16 Pipe (zmq.pipe.Pipe)13 Blob (zmq.util.Blob)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)2 Metadata (zmq.io.Metadata)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Event (zmq.ZMQ.Event)1 ZObject (zmq.ZObject)1 Step (zmq.io.coder.IDecoder.Step)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1