Search in sources :

Example 86 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method inEvent.

@Override
public void inEvent() {
    assert (!ioError);
    // If still handshaking, receive and process the greeting message.
    if (handshaking) {
        if (!handshake()) {
            return;
        }
    }
    assert (decoder != null);
    // If there has been an I/O error, stop polling.
    if (inputStopped) {
        ioObject.removeHandle(handle);
        handle = null;
        ioError = true;
        return;
    }
    // If there's no data to process in the buffer...
    if (insize == 0) {
        // Retrieve the buffer and read as much data as possible.
        // Note that buffer can be arbitrarily large. However, we assume
        // the underlying TCP layer has fixed buffer size and thus the
        // number of bytes read will be always limited.
        inpos = decoder.getBuffer();
        int rc = read(inpos);
        if (rc == 0) {
            error(ErrorReason.CONNECTION);
        }
        if (rc == -1) {
            if (!errno.is(ZError.EAGAIN)) {
                error(ErrorReason.CONNECTION);
            }
            return;
        }
        // Adjust input size
        inpos.flip();
        insize = rc;
    }
    boolean rc = false;
    ValueReference<Integer> processed = new ValueReference<>(0);
    while (insize > 0) {
        // Push the data to the decoder.
        Step.Result result = decoder.decode(inpos, insize, processed);
        assert (processed.get() <= insize);
        insize -= processed.get();
        if (result == Step.Result.MORE_DATA) {
            rc = true;
            break;
        }
        if (result == Step.Result.ERROR) {
            rc = false;
            break;
        }
        Msg msg = decoder.msg();
        rc = processMsg.apply(msg);
        if (!rc) {
            break;
        }
    }
    // or the session has rejected the message.
    if (!rc) {
        if (!errno.is(ZError.EAGAIN)) {
            error(ErrorReason.PROTOCOL);
            return;
        }
        inputStopped = true;
        ioObject.resetPollIn(handle);
    }
    // Flush all messages the decoder may have produced.
    session.flush();
}
Also used : Msg(zmq.Msg) Step(zmq.io.coder.IDecoder.Step) ValueReference(zmq.util.ValueReference)

Example 87 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method identityMsg.

private Msg identityMsg() {
    Msg msg = new Msg(options.identitySize);
    if (options.identitySize > 0) {
        msg.put(options.identity, 0, options.identitySize);
    }
    nextMsg = pullMsgFromSession;
    return msg;
}
Also used : Msg(zmq.Msg)

Example 88 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method producePongMessage.

private Msg producePongMessage(byte[] pingContext) {
    assert (mechanism != null);
    assert (pingContext != null);
    Msg msg = new Msg(5 + pingContext.length);
    msg.setFlags(Msg.COMMAND);
    msg.putShortString("PONG");
    msg.put(pingContext);
    msg = mechanism.encode(msg);
    nextMsg = pullAndEncode;
    return msg;
}
Also used : Msg(zmq.Msg)

Example 89 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method plug.

@Override
public void plug(IOThread ioThread, SessionBase session) {
    assert (!plugged);
    plugged = true;
    // Connect to session object.
    assert (this.session == null);
    assert (session != null);
    this.session = session;
    socket = session.getSocket();
    // Connect to I/O threads poller object.
    ioObject = new IOObject(ioThread, this);
    ioObject.plug();
    handle = ioObject.addFd(fd);
    ioError = false;
    // Make sure batch sizes match large buffer sizes
    final int inBatchSize = Math.max(options.rcvbuf, Config.IN_BATCH_SIZE.getValue());
    final int outBatchSize = Math.max(options.sndbuf, Config.OUT_BATCH_SIZE.getValue());
    if (options.rawSocket) {
        decoder = instantiate(options.decoder, inBatchSize, options.maxMsgSize);
        if (decoder == null) {
            decoder = new RawDecoder(inBatchSize);
        }
        encoder = instantiate(options.encoder, outBatchSize, options.maxMsgSize);
        if (encoder == null) {
            encoder = new RawEncoder(errno, outBatchSize);
        }
        // disable handshaking for raw socket
        handshaking = false;
        nextMsg = pullMsgFromSession;
        processMsg = pushRawMsgToSession;
        if (peerAddress != null && !peerAddress.address().isEmpty()) {
            assert (metadata == null);
            // Compile metadata
            metadata = new Metadata();
            metadata.set(Metadata.PEER_ADDRESS, peerAddress.address());
        }
        // For raw sockets, send an initial 0-length message to the
        // application so that it knows a peer has connected.
        Msg connector = new Msg();
        pushRawMsgToSession(connector);
        session.flush();
    } else {
        // start optional timer, to prevent handshake hanging on no input
        setHandshakeTimer();
        // Send the 'length' and 'flags' fields of the identity message.
        // The 'length' field is encoded in the long format.
        greetingSend.put((byte) 0xff);
        Wire.putUInt64(greetingSend, options.identitySize + 1);
        greetingSend.put((byte) 0x7f);
        outpos.set(greetingSend);
        outsize = greetingSend.position();
        greetingSend.flip();
    }
    ioObject.setPollIn(handle);
    ioObject.setPollOut(handle);
    // Flush all the data that may have been already received downstream.
    inEvent();
}
Also used : Msg(zmq.Msg) RawDecoder(zmq.io.coder.raw.RawDecoder) RawEncoder(zmq.io.coder.raw.RawEncoder)

Example 90 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngine method processIdentityMsg.

private boolean processIdentityMsg(Msg msg) {
    if (options.recvIdentity) {
        msg.setFlags(Msg.IDENTITY);
        boolean rc = session.pushMsg(msg);
        assert (rc);
    }
    if (subscriptionRequired) {
        // Inject the subscription message, so that also
        // ZMQ 2.x peers receive published messages.
        Msg subscription = new Msg(1);
        subscription.put((byte) 1);
        boolean rc = session.pushMsg(subscription);
        assert (rc);
    }
    processMsg = pushMsgToSession;
    return true;
}
Also used : Msg(zmq.Msg)

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