use of zmq.util.Blob 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;
}
use of zmq.util.Blob in project jeromq by zeromq.
the class Stream method xsend.
@Override
protected boolean xsend(Msg msg) {
// peer to send the message to.
if (!moreOut) {
assert (currentOut == null);
// TODO: The connections should be killed instead.
if (msg.hasMore()) {
moreOut = true;
// Find the pipe associated with the identity stored in the prefix.
// If there's no such pipe return an error
Blob identity = Blob.createBlob(msg);
Outpipe op = outpipes.get(identity);
if (op != null) {
currentOut = op.pipe;
if (!currentOut.checkWrite()) {
op.active = false;
currentOut = null;
errno.set(ZError.EAGAIN);
return false;
}
} else {
errno.set(ZError.EHOSTUNREACH);
return false;
}
}
// Expect one more message frame.
moreOut = true;
return true;
}
// Ignore the MORE flag
msg.resetFlags(Msg.MORE);
// This is the last part of the message.
moreOut = false;
// Push the message into the pipe. If there's no out pipe, just drop it.
if (currentOut != null) {
// Pending messages in the pipe will be dropped (on receiving term- ack)
if (msg.size() == 0) {
currentOut.terminate(false);
currentOut = null;
return true;
}
boolean ok = currentOut.write(msg);
if (ok) {
currentOut.flush();
}
currentOut = null;
}
return true;
}
use of zmq.util.Blob 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);
}
use of zmq.util.Blob in project jeromq by zeromq.
the class XPub method xrecv.
@Override
protected Msg xrecv() {
// If there is at least one
if (pendingData.isEmpty()) {
errno.set(ZError.EAGAIN);
return null;
}
// User is reading a message, set lastPipe and remove it from the deque
if (manual && !pendingPipes.isEmpty()) {
lastPipe = pendingPipes.pollFirst();
}
Blob first = pendingData.pollFirst();
Msg msg = new Msg(first.data());
int flags = pendingFlags.pollFirst();
msg.setFlags(flags);
return msg;
}
use of zmq.util.Blob in project jeromq by zeromq.
the class Router method xhasIn.
@Override
protected boolean xhasIn() {
// definitely more parts available.
if (moreIn) {
return true;
}
// 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);
// TODO: handle the situation when the peer changes its identity.
while (prefetchedMsg != null && prefetchedMsg.isIdentity()) {
prefetchedMsg = fq.recvPipe(errno, pipe);
}
if (prefetchedMsg == null) {
return false;
}
assert (pipe.get() != null);
Blob identity = pipe.get().getIdentity();
prefetchedId = new Msg(identity.data());
prefetchedId.setFlags(Msg.MORE);
prefetched = true;
identitySent = false;
return true;
}
Aggregations