Search in sources :

Example 6 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class CurveClientMechanism method decode.

@Override
public Msg decode(Msg msg) {
    assert (state == State.CONNECTED);
    if (!compare(msg, "MESSAGE", true)) {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
        errno.set(ZError.EPROTO);
        return null;
    }
    if (msg.size() < 33) {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_MESSAGE);
        errno.set(ZError.EPROTO);
        return null;
    }
    ByteBuffer messageNonce = ByteBuffer.allocate(Curve.Size.NONCE.bytes());
    messageNonce.put("CurveZMQMESSAGES".getBytes(ZMQ.CHARSET));
    msg.transfer(messageNonce, 8, 8);
    long nonce = msg.getLong(8);
    if (nonce <= cnPeerNonce) {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
        errno.set(ZError.EPROTO);
        return null;
    }
    cnPeerNonce = nonce;
    int clen = Curve.Size.BOXZERO.bytes() + msg.size() - 16;
    ByteBuffer messagePlaintext = ByteBuffer.allocate(clen);
    ByteBuffer messageBox = ByteBuffer.allocate(clen);
    messageBox.position(Curve.Size.BOXZERO.bytes());
    msg.transfer(messageBox, 16, msg.size() - 16);
    int rc = cryptoBox.openAfternm(messagePlaintext, messageBox, clen, messageNonce, cnPrecom);
    if (rc == 0) {
        Msg decoded = new Msg(clen - 1 - Curve.Size.ZERO.bytes());
        byte flags = messagePlaintext.get(Curve.Size.ZERO.bytes());
        if ((flags & 0x01) != 0) {
            decoded.setFlags(Msg.MORE);
        }
        if ((flags & 0x02) != 0) {
            decoded.setFlags(Msg.COMMAND);
        }
        messagePlaintext.position(Curve.Size.ZERO.bytes() + 1);
        decoded.put(messagePlaintext);
        return decoded;
    } else {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
        errno.set(ZError.EPROTO);
        return null;
    }
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer)

Example 7 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Router method identifyPeer.

private boolean identifyPeer(Pipe pipe, boolean isLocallyInitiated) {
    Blob identity;
    if (connectRid != null && !connectRid.isEmpty() && isLocallyInitiated) {
        identity = Blob.createBlob(connectRid.getBytes(ZMQ.CHARSET));
        connectRid = null;
        Outpipe outpipe = outpipes.get(identity);
        // Not allowed to duplicate an existing rid
        assert (outpipe == null);
    } else {
        if (options.rawSocket) {
            // Always assign identity for raw-socket
            ByteBuffer buffer = ByteBuffer.allocate(5);
            buffer.put((byte) 0);
            Wire.putUInt32(buffer, nextRid++);
            identity = Blob.createBlob(buffer.array());
        } else {
            // Pick up handshake cases and also case where next identity is set
            Msg msg = pipe.read();
            if (msg == null) {
                return false;
            }
            if (msg.size() == 0) {
                // Fall back on the auto-generation
                ByteBuffer buf = ByteBuffer.allocate(5);
                buf.put((byte) 0);
                Wire.putUInt32(buf, nextRid++);
                identity = Blob.createBlob(buf.array());
            } else {
                identity = Blob.createBlob(msg);
                if (outpipes.containsKey(identity)) {
                    if (!handover) {
                        // Ignore peers with duplicate ID
                        return false;
                    }
                    // We will allow the new connection to take over this
                    // identity. Temporarily assign a new identity to the
                    // existing pipe so we can terminate it asynchronously.
                    ByteBuffer buf = ByteBuffer.allocate(5);
                    buf.put((byte) 0);
                    Wire.putUInt32(buf, nextRid++);
                    Blob newIdentity = Blob.createBlob(buf.array());
                    // Remove the existing identity entry to allow the new
                    // connection to take the identity.
                    Outpipe existingOutpipe = outpipes.remove(identity);
                    existingOutpipe.pipe.setIdentity(newIdentity);
                    outpipes.put(newIdentity, existingOutpipe);
                    existingOutpipe.pipe.terminate(true);
                }
            }
        }
    }
    pipe.setIdentity(identity);
    // Add the record into output pipes lookup table
    Outpipe outpipe = new Outpipe(pipe, true);
    outpipes.put(identity, outpipe);
    return true;
}
Also used : Msg(zmq.Msg) Blob(zmq.util.Blob) ByteBuffer(java.nio.ByteBuffer)

Example 8 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Server method xrecv.

@Override
protected Msg xrecv() {
    Msg msg;
    ValueReference<Pipe> pipe = new ValueReference<>();
    msg = fq.recvPipe(errno, pipe);
    // Drop any messages with more flag
    while (msg != null && msg.hasMore()) {
        // drop all frames of the current multi-frame message
        msg = fq.recvPipe(errno, null);
        while (msg != null && msg.hasMore()) {
            msg = fq.recvPipe(errno, null);
        }
        // get the new message
        if (msg != null) {
            msg = fq.recvPipe(errno, pipe);
        }
    }
    if (msg == null) {
        return msg;
    }
    assert (pipe.get() != null);
    int routingId = pipe.get().getRoutingId();
    msg.setRoutingId(routingId);
    return msg;
}
Also used : Msg(zmq.Msg) Pipe(zmq.pipe.Pipe) ValueReference(zmq.util.ValueReference)

Example 9 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Sub method xsetsockopt.

@Override
public boolean xsetsockopt(int option, Object optval) {
    if (option != ZMQ.ZMQ_SUBSCRIBE && option != ZMQ.ZMQ_UNSUBSCRIBE) {
        errno.set(ZError.EINVAL);
        return false;
    }
    if (optval == null) {
        errno.set(ZError.EINVAL);
        return false;
    }
    byte[] val = Options.parseBytes(option, optval);
    // Create the subscription message.
    Msg msg = new Msg(val.length + 1);
    if (option == ZMQ.ZMQ_SUBSCRIBE) {
        msg.put((byte) 1);
    } else {
        // option = ZMQ.ZMQ_UNSUBSCRIBE
        msg.put((byte) 0);
    }
    msg.put(val);
    // Pass it further on in the stack.
    boolean rc = super.xsend(msg);
    if (!rc) {
        errno.set(ZError.EINVAL);
        throw new IllegalStateException("Failed to send subscribe/unsubscribe message");
    }
    return true;
}
Also used : Msg(zmq.Msg)

Example 10 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Dish method xrecv.

@Override
protected Msg xrecv() {
    // return it straight ahead.
    if (pendingMsg != null) {
        Msg msg = pendingMsg;
        pendingMsg = null;
        return msg;
    }
    return xxrecv();
}
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