本文整理汇总了Java中org.zeromq.ZFrame类的典型用法代码示例。如果您正苦于以下问题:Java ZFrame类的具体用法?Java ZFrame怎么用?Java ZFrame使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ZFrame类属于org.zeromq包,在下文中一共展示了ZFrame类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: gunzipMultifameZMsg
import org.zeromq.ZFrame; //导入依赖的package包/类
public static String[] gunzipMultifameZMsg(ZMsg msg) throws IOException {
Iterator<ZFrame> frames = msg.iterator();
// pop off first frame, which contains "/GOVI/KV8" (the feed name)
// (isn't there a method for this?)
String header = frames.next().toString();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while (frames.hasNext()) {
ZFrame frame = frames.next();
byte[] frameData = frame.getData();
buffer.write(frameData);
}
if (buffer.size() == 0) {
return null;
}
// chain input streams to gunzip contents of byte buffer
InputStream gzippedMessageStream = new ByteArrayInputStream(
buffer.toByteArray());
InputStream messageStream = new GZIPInputStream(gzippedMessageStream);
// copy input stream back to transport stream
buffer.reset();
byte[] b = new byte[4096];
for (int n; (n = messageStream.read(b)) != -1;) {
buffer.write(b, 0, n);
}
return new String[] { header, buffer.toString() };
}
示例2: receive
import org.zeromq.ZFrame; //导入依赖的package包/类
/**
* Block until a message can be read
*
* @return
* @throws IOException
*/
public Envelope receive(long timeout) throws IOException {
ZMQ.poll(items, timeout);
if (items[0].isReadable()) {
// System.out.println("Receiving");
// The DEALER socket gives us the address envelope and message
ZMsg msg = ZMsg.recvMsg(socket);
// System.out.println("Recvd " + msg);
ZFrame content = msg.getLast();
assert (content != null);
// System.out.println("Content " + content);
return Envelope.parseFrom(content.getData());
} else
return null; // timed out
}
示例3: sendToBroker
import org.zeromq.ZFrame; //导入依赖的package包/类
/**
* Send message to broker If no msg is provided, creates one internally
*
* @param command
* @param option
* @param msg
*/
void sendToBroker(MDP command, String option, ZMsg msg) {
msg = msg != null ? msg.duplicate() : new ZMsg();
// Stack protocol envelope to start of message
if (option != null)
msg.addFirst(new ZFrame(option));
msg.addFirst(command.newFrame());
msg.addFirst(MDP.W_WORKER.newFrame());
msg.addFirst(new ZFrame(ZMQ.MESSAGE_SEPARATOR));
if (verbose) {
log.format("I: sending %s to broker\n", command);
msg.dump(log.out());
}
msg.send(worker);
}
示例4: run
import org.zeromq.ZFrame; //导入依赖的package包/类
@Override
public void run (Object ... args)
{
ZContext context = new ZContext();
// Prepare our context and sockets
Socket worker = context.createSocket (ZMQ.REQ);
ZHelper.setId (worker); // Set a printable identity
worker.connect("ipc://backend.ipc");
// Tell backend we're ready for work
ZFrame frame = new ZFrame (WORKER_READY);
frame.send (worker, 0);
while(true)
{
ZMsg msg = ZMsg.recvMsg (worker);
if (msg == null)
break;
msg.getLast ().reset ("OK");
msg.send (worker);
}
context.destroy ();
}
示例5: serviceCall
import org.zeromq.ZFrame; //导入依赖的package包/类
static ZMsg
serviceCall (mdcliapi session, String service, ZMsg request)
{
ZMsg reply = session.send(service, request);
if (reply != null) {
ZFrame status = reply.pop();
if (status.streq("200")) {
status.destroy();
return reply;
}
else if (status.streq("400")) {
System.out.println("E: client fatal error, aborting");
}
else
if (status.streq("500")) {
System.out.println("E: server fatal error, aborting");
}
reply.destroy();
}
return null; // Didn't succeed; don't care why not
}
示例6: handle
import org.zeromq.ZFrame; //导入依赖的package包/类
@Override
public int handle(ZLoop loop, PollItem item, Object arg_) {
LBBroker arg = (LBBroker)arg_;
ZMsg msg = ZMsg.recvMsg(arg.backend);
if (msg != null) {
ZFrame address = msg.unwrap();
// Queue worker address for load-balancing
arg.workers.add(address);
// Enable reader on frontend if we went from 0 to 1 workers
if (arg.workers.size() == 1) {
PollItem newItem = new PollItem (arg.frontend, ZMQ.Poller.POLLIN);
loop.addPoller (newItem, frontendHandler, arg);
}
// Forward message to client if it's not a READY
ZFrame frame = msg.getFirst();
if (Arrays.equals (frame.getData(), WORKER_READY))
msg.destroy();
else
msg.send(arg.frontend);
}
return 0;
}
示例7: run
import org.zeromq.ZFrame; //导入依赖的package包/类
@Override
public void run()
{
ZContext ctx = new ZContext();
Socket worker = ctx.createSocket(ZMQ.REQ);
worker.connect(String.format("ipc://%s-localbe.ipc", self));
// Tell broker we're ready for work
ZFrame frame = new ZFrame(WORKER_READY);
frame.send(worker, 0);
while (true) {
// Send request, get reply
ZMsg msg = ZMsg.recvMsg(worker, 0);
if (msg == null)
break; // Interrupted
msg.getLast().print("Worker: ");
msg.getLast().reset("OK");
msg.send(worker);
}
ctx.destroy();
}
示例8: run
import org.zeromq.ZFrame; //导入依赖的package包/类
public void run()
{
ZContext context = new ZContext();
// Prepare our context and sockets
Socket worker = context.createSocket(ZMQ.REQ);
worker.connect("ipc://backend.ipc");
ZFrame frame = new ZFrame(lruqueue3.LRU_READY);
// Tell backend we're ready for work
frame.send(worker, 0);
while (true) {
ZMsg msg = ZMsg.recvMsg(worker);
if (msg == null)
break;
msg.getLast().reset("OK".getBytes(ZMQ.CHARSET));
msg.send(worker);
System.out.println(Thread.currentThread().getName() + " Worker Sent OK");
}
context.destroy();
}
示例9: handle
import org.zeromq.ZFrame; //导入依赖的package包/类
@Override
public int handle(ZLoop loop, PollItem item, Object arg_)
{
LRUQueueArg arg = (LRUQueueArg) arg_;
ZMsg msg = ZMsg.recvMsg(arg.backend);
if (msg != null) {
ZFrame address = msg.unwrap();
// Queue worker address for LRU routing
arg.workers.add(address);
// Enable reader on frontend if we went from 0 to 1 workers
if (arg.workers.size() == 1) {
PollItem poller = new PollItem(arg.frontend, ZMQ.Poller.POLLIN);
loop.addPoller(poller, lruqueue3.handle_frontend, arg);
}
// Forward message to client if it's not a READY
ZFrame frame = msg.getFirst();
if (new String(frame.getData(), ZMQ.CHARSET).equals(lruqueue3.LRU_READY))
msg.destroy();
else
msg.send(arg.frontend);
}
return 0;
}
示例10: serviceInternal
import org.zeromq.ZFrame; //导入依赖的package包/类
/**
* Handle internal service according to 8/MMI specification
*/
private void serviceInternal(ZFrame serviceFrame, ZMsg msg) {
String returnCode = "501";
if ("mmi.service".equals(serviceFrame.toString())) {
String name = msg.peekLast().toString();
returnCode = services.containsKey(name) ? "200" : "400";
}
msg.peekLast().reset(returnCode.getBytes(ZMQ.CHARSET));
// Remove & save client return envelope and insert the
// protocol header and service name, then rewrap envelope.
ZFrame client = msg.unwrap();
msg.addFirst(serviceFrame.duplicate());
msg.addFirst(MDP.C_CLIENT.newFrame());
msg.wrap(client);
msg.send(socket);
}
示例11: sendToWorker
import org.zeromq.ZFrame; //导入依赖的package包/类
/**
* Send message to worker. If message is provided, sends that message. Does
* not destroy the message, this is the caller's job.
*/
public void sendToWorker(Worker worker, MDP command, String option,
ZMsg msgp) {
ZMsg msg = msgp == null ? new ZMsg() : msgp.duplicate();
// Stack protocol envelope to start of message
if (option != null)
msg.addFirst(new ZFrame(option));
msg.addFirst(command.newFrame());
msg.addFirst(MDP.W_WORKER.newFrame());
// Stack routing envelope to start of message
msg.wrap(worker.address.duplicate());
if (verbose) {
log.format("I: sending %s to worker\n", command);
msg.dump(log.out());
}
msg.send(socket);
}
示例12: readMessage
import org.zeromq.ZFrame; //导入依赖的package包/类
private Message readMessage(ZMQ.Socket socket) {
ZMsg zmsg = null;
Message message = new Message();
try {
zmsg = ZMsg.recvMsg(socket);
ZFrame[] parts = new ZFrame[zmsg.size()];
zmsg.toArray(parts);
byte[] uuid = parts[MessageParts.UUID].getData();
byte[] header = parts[MessageParts.HEADER].getData();
byte[] parent = parts[MessageParts.PARENT].getData();
byte[] metadata = parts[MessageParts.METADATA].getData();
byte[] content = parts[MessageParts.CONTENT].getData();
byte[] expectedSig = parts[MessageParts.HMAC].getData();
verifyDelim(parts[MessageParts.DELIM]);
verifySignatures(expectedSig, header, parent, metadata, content);
if (uuid != null) {
message.getIdentities().add(uuid);
}
message.setHeader(parse(header, Header.class));
message.setParentHeader(parse(parent, Header.class));
message.setMetadata(parse(metadata, LinkedHashMap.class));
message.setContent(parse(content, LinkedHashMap.class));
} finally {
if (zmsg != null) {
zmsg.destroy();
}
}
return message;
}
示例13: verifyDelim
import org.zeromq.ZFrame; //导入依赖的package包/类
private String verifyDelim(ZFrame zframe) {
String delim = new String(zframe.getData(), StandardCharsets.UTF_8);
if (!DELIM.equals(delim)) {
throw new RuntimeException("Delimiter <IDS|MSG> not found");
}
return delim;
}
示例14: TransferWrapperRaw
import org.zeromq.ZFrame; //导入依赖的package包/类
/**
* Expect multipart message at least three parts:
*
* 0 envelope frame -> envelope[N-4]
* ...
* N-4 envelope frame -> envelope[0]
* N-3 Empty Delimiter Frame
* N-2 Serialized callback ID -> callbackId
* N-1 Serialized payload -> payload
*
* @param message input
* @throws TransferHelper.ProtocolException if protocol is violated
*/
public TransferWrapperRaw(ZMsg message) throws TransferHelper.ProtocolException {
if (message == null) { throw new IllegalArgumentException(); }
int N = message.size();
if (N <= 2) {
throw new TransferHelper.ProtocolException("Wrong number of Frames. Expected lower than 3: " + message.size());
}
ZFrame payloadFrame = message.pollLast();
ZFrame callbackFrame = message.pollLast();
ZFrame delimiterFrame = message.pollLast();
if (delimiterFrame.size() != 0) {
throw new TransferHelper.ProtocolException("Delimiter frame not empty.");
}
callbackId = TransferHelper.bytes2int(callbackFrame.getData());
payload = payloadFrame.getData();
envelope = new ZFrame[N-3];
for(int i = 0; i < N-3; i++) {
envelope[i] = message.pollLast();
}
if (message.size() != 0) throw new IllegalStateException("Message not empty.");
}
示例15: toMessage
import org.zeromq.ZFrame; //导入依赖的package包/类
/**
* Inverse to ZMsg constructor.
*
* @return message
*/
public ZMsg toMessage() {
ZMsg out = new ZMsg();
out.addFirst(payload);
out.addFirst(TransferHelper.int2bytes(callbackId));
out.addFirst(new byte[0]);
for (ZFrame f : envelope){
out.addFirst(f);
}
return out;
}