Search in sources :

Example 31 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class ZFrame method recvFrame.

/**
 * Receive a new frame off the socket, Returns newly-allocated frame, or
 * null if there was no input waiting, or if the read was interrupted.
 * @param   socket
 *              Socket to read from
 * @param   flags
 *              Pass flags to 0MQ socket.recv call
 * @return
 *              received frame, else null
 */
public static ZFrame recvFrame(Socket socket, int flags) {
    final SocketBase base = socket.base();
    zmq.Msg msg = base.recv(flags);
    if (msg == null) {
        // Check to see if there was an error in recv
        socket.mayRaise();
        return null;
    }
    ZFrame frame = new ZFrame(msg);
    frame.setGroup(msg.getGroup());
    return frame;
}
Also used : SocketBase(zmq.SocketBase) Msg(zmq.Msg)

Example 32 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method mechanismReady.

private void mechanismReady() {
    if (options.heartbeatInterval > 0) {
        ioObject.addTimer(options.heartbeatInterval, HEARTBEAT_IVL_TIMER_ID);
        hasHeartbeatTimer = true;
    }
    if (options.recvIdentity) {
        Msg identity = mechanism.peerIdentity();
        boolean rc = session.pushMsg(identity);
        if (!rc && errno.is(ZError.EAGAIN)) {
            // so we can just bail out of the identity set.
            return;
        }
        assert (rc);
        session.flush();
    }
    nextMsg = pullAndEncode;
    processMsg = writeCredential;
    // Compile metadata.
    assert (metadata == null);
    metadata = new Metadata();
    // If we have a peer_address, add it to metadata
    if (peerAddress != null && !peerAddress.address().isEmpty()) {
        metadata.set(Metadata.PEER_ADDRESS, peerAddress.address());
    }
    // Add ZAP properties.
    metadata.set(mechanism.zapProperties);
    // Add ZMTP properties.
    metadata.set(mechanism.zmtpProperties);
    if (metadata.isEmpty()) {
        metadata = null;
    }
}
Also used : Msg(zmq.Msg)

Example 33 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method writeCredential.

private boolean writeCredential(Msg msg) {
    assert (mechanism != null);
    assert (session != null);
    Blob credential = mechanism.getUserId();
    if (credential != null && credential.size() > 0) {
        Msg cred = new Msg(credential.size());
        cred.put(credential.data(), 0, credential.size());
        cred.setFlags(Msg.CREDENTIAL);
        boolean rc = session.pushMsg(cred);
        if (!rc) {
            return false;
        }
    }
    processMsg = decodeAndPush;
    return decodeAndPush.apply(msg);
}
Also used : Msg(zmq.Msg) Blob(zmq.util.Blob)

Example 34 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class V2Decoder method allocate.

@Override
protected Msg allocate(int size) {
    Msg msg = super.allocate(size);
    msg.setFlags(msgFlags);
    return msg;
}
Also used : Msg(zmq.Msg)

Example 35 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class ReqSpecTest method messageFormat.

private void messageFormat(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
    // Server socket will accept connections
    SocketBase req = ZMQ.socket(ctx, bindType);
    assertThat(req, notNullValue());
    boolean rc = ZMQ.bind(req, address);
    assertThat(rc, is(true));
    SocketBase router = ZMQ.socket(ctx, connectType);
    assertThat(router, notNullValue());
    String host = (String) ZMQ.getSocketOptionExt(req, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    rc = ZMQ.connect(router, host);
    assertThat(rc, is(true));
    // Send a multi-part request.
    sendSeq(req, "ABC", "DEF");
    // Receive peer identity
    Msg msg = ZMQ.recv(router, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size() > 0, is(true));
    Msg peerId = msg;
    int more = ZMQ.getSocketOption(router, ZMQ.ZMQ_RCVMORE);
    assertThat(more, is(1));
    // Receive the rest.
    recvSeq(router, null, "ABC", "DEF");
    // Send back a single-part reply.
    int ret = ZMQ.send(router, peerId, ZMQ.ZMQ_SNDMORE);
    assertThat(ret, is(peerId.size()));
    sendSeq(router, null, "GHI");
    // Receive reply.
    recvSeq(req, "GHI");
    ZMQ.closeZeroLinger(req);
    ZMQ.closeZeroLinger(router);
    // Wait for disconnects.
    ZMQ.msleep(100);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase)

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