Search in sources :

Example 1 with Metadata

use of zmq.io.Metadata in project jeromq by zeromq.

the class ZMetadata method read.

public static ZMetadata read(String meta) {
    if (meta == null || meta.length() == 0) {
        return null;
    }
    try {
        ByteBuffer buffer = ZMQ.CHARSET.newEncoder().encode(CharBuffer.wrap(meta));
        Metadata data = new Metadata();
        data.read(buffer, 0, null);
        return new ZMetadata(data);
    } catch (CharacterCodingException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Metadata(zmq.io.Metadata) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Metadata

use of zmq.io.Metadata in project jeromq by zeromq.

the class ZMQ method getMessageMetadata.

// Get message metadata string
public static String getMessageMetadata(Msg msg, String property) {
    String data = null;
    Metadata metadata = msg.getMetadata();
    if (metadata != null) {
        data = metadata.get(property);
    }
    return data;
}
Also used : Metadata(zmq.io.Metadata)

Example 3 with Metadata

use of zmq.io.Metadata 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 4 with Metadata

use of zmq.io.Metadata 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)

Aggregations

Metadata (zmq.io.Metadata)4 Msg (zmq.Msg)2 Pipe (zmq.pipe.Pipe)2 Blob (zmq.util.Blob)2 ValueReference (zmq.util.ValueReference)2 ByteBuffer (java.nio.ByteBuffer)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1