org.zeromq.ZFrame类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(210)

本文整理了Java中org.zeromq.ZFrame类的一些代码示例,展示了ZFrame类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZFrame类的具体详情如下:
包路径:org.zeromq.ZFrame
类名称:ZFrame

ZFrame介绍

[英]ZFrame The ZFrame class provides methods to send and receive single message frames across 0MQ sockets. A 'frame' corresponds to one underlying zmq_msg_t in the libzmq code. When you read a frame from a socket, the more() method indicates if the frame is part of an unfinished multipart message. The send() method normally destroys the frame, but with the ZFRAME_REUSE flag, you can send the same frame many times. Frames are binary, and this class has no special support for text data.
[中]ZFrame类提供了跨0MQ套接字发送和接收单个消息帧的方法。“frame”对应于libzmq代码中的一个底层zmq_msg_t。从套接字读取帧时,more()方法指示该帧是否是未完成的多部分消息的一部分。send()方法通常会破坏帧,但使用ZFRAME_REUSE标志,可以多次发送同一帧。帧是二进制的,这个类不支持文本数据。

代码示例

代码示例来源:origin: twosigma/beakerx

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;
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Creates a new frame that duplicates an existing frame
 * @return
 *          Duplicate of frame; message contents copied into new byte array
 */
public ZFrame duplicate()
{
  return new ZFrame(this.data);
}

代码示例来源:origin: org.zeromq/jeromq

public void print(String prefix)
  {
    ZData.print(System.out, prefix, getData(), size());
  }
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Creates copy of this ZMsg.
 * Also duplicates all frame content.
 * @return
 *          The duplicated ZMsg object, else null if this ZMsg contains an empty frame set
 */
public ZMsg duplicate()
{
  if (frames.isEmpty()) {
    return null;
  }
  else {
    ZMsg msg = new ZMsg();
    for (ZFrame f : frames) {
      msg.add(f.duplicate());
    }
    return msg;
  }
}

代码示例来源:origin: org.opendaylight.netide/shim

if (poller.pollin(0)) {
  ZMsg message = ZMsg.recvMsg(socket);
  byte[] data = message.getLast().getData();
  if (coreListener != null) {
    Message msg = NetIPConverter.parseConcreteMessage(data);
  if (message.getFirst().toString().equals(STOP_COMMAND)) {
    break;
  } else {

代码示例来源:origin: org.zeromq/jeromq

/**
 * Receive a new frame off the socket, Returns newly-allocated frame, or
 * null if there was no input waiting, or if the read was interrupted.
 * @param   socket
 *              Socket to read from
 * @param   flags
 *              Pass flags to 0MQ socket.recv call
 * @return
 *              received frame, else null
 */
public static ZFrame recvFrame(Socket socket, int flags)
{
  ZFrame f = new ZFrame();
  byte[] data = f.recv(socket, flags);
  if (data == null) {
    return null;
  }
  return f;
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Dump the message in human readable format. This should only be used
 * for debugging and tracing, inefficient in handling large messages.
 **/
public void dump(Appendable out)
{
  try {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.printf("--------------------------------------\n");
    for (ZFrame frame : frames) {
      pw.printf("[%03d] %s\n", frame.size(), frame.toString());
    }
    out.append(sw.getBuffer());
    sw.close();
  }
  catch (IOException e) {
    throw new RuntimeException("Message dump exception " + super.toString(), e);
  }
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Pop frame off front of message, caller now owns frame.
 * If next frame is empty, pops and destroys that empty frame
 * (e.g. useful when unwrapping ROUTER socket envelopes)
 * @return
 *          Unwrapped frame
 */
public ZFrame unwrap()
{
  if (size() == 0) {
    return null;
  }
  ZFrame f = pop();
  ZFrame empty = getFirst();
  if (empty.hasData() && empty.size() == 0) {
    empty = pop();
    empty.destroy();
  }
  return f;
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Sends frame to socket if it contains data.
 * Use this method to send a frame and destroy the data after.
 * @param socket
 *          0MQ socket to send frame
 * @param flags
 *          Valid send() method flags, defined in org.zeromq.ZMQ class
 * @return
 *          True if success, else False
 */
public boolean sendAndDestroy(Socket socket, int flags)
{
  boolean ret = send(socket, flags);
  if (ret) {
    destroy();
  }
  return ret;
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Pop a ZFrame and return the toString() representation of it.
 *
 * @return toString version of pop'ed frame, or null if no frame exists.
 */
public String popString()
{
  ZFrame frame = pop();
  if (frame == null) {
    return null;
  }
  return frame.toString();
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Destructor.
 * Explicitly destroys all ZFrames contains in the ZMsg
 */
public void destroy()
{
  for (ZFrame f : frames) {
    f.destroy();
  }
  frames.clear();
}

代码示例来源:origin: org.zeromq/jeromq

@Override
public boolean equals(Object o)
{
  if (this == o) {
    return true;
  }
  if (o == null || getClass() != o.getClass()) {
    return false;
  }
  ZMsg zMsg = (ZMsg) o;
  //based on AbstractList
  Iterator<ZFrame> e1 = frames.iterator();
  Iterator<ZFrame> e2 = zMsg.frames.iterator();
  while (e1.hasNext() && e2.hasNext()) {
    ZFrame o1 = e1.next();
    ZFrame o2 = e2.next();
    if (!(o1 == null ? o2 == null : o1.equals(o2))) {
      return false;
    }
  }
  return !(e1.hasNext() || e2.hasNext());
}

代码示例来源:origin: org.zeromq/jeromq

/**
 * Add a String as a new ZFrame to the end of list
 * @param str
 *              String to add to list
 */
public void addString(String str)
{
  frames.add(new ZFrame(str));
}

代码示例来源:origin: org.zeromq/jeromq

for (ZFrame f : msg) {
  file.writeInt(f.size());
  file.write(f.getData());

代码示例来源:origin: net.unit8.enkan/enkan-repl-pseudo

@Override
public void run() {
  while(!Thread.currentThread().isInterrupted()) {
    ZMsg msg = ZMsg.recvMsg(socket);
    ZFrame clientAddress = msg.pop();
    String input = msg.popString();
    int cursor = Integer.parseInt(msg.popString());
    int[] anchor = {-1};
    ZMsg reply = new ZMsg();
    reply.add(clientAddress.duplicate());
    String trimmedCommand = input.trim();
    if (trimmedCommand.startsWith("/")) {
      if (!trimmedCommand.contains(" ")) {
        Predicate<String> filter = trimmedCommand.equals("/") ?
            n -> true : n -> n.startsWith(trimmedCommand.substring(1));
        commandNames.stream()
            .filter(filter)
            .forEach(s -> reply.add("/" + s));
        anchor[0] = 0;
      }
    }
    reply.send(socket, true);
  }
}

代码示例来源:origin: twosigma/beakerx

private Message readMessage(ZMQ.Socket socket) {
 ZMsg zmsg = null;
 Message message = null;
 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);
  message = new Message(parse(header, Header.class));
  if (uuid != null) {
   message.getIdentities().add(uuid);
  }
  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;
}

代码示例来源:origin: org.zeromq/jeromq

public void addFirst(byte[] data)
{
  addFirst(new ZFrame(data));
}

代码示例来源:origin: kawasima/enkan

@Override
public void run() {
  while(!Thread.currentThread().isInterrupted()) {
    ZMsg msg = ZMsg.recvMsg(socket);
    ZFrame clientAddress = msg.pop();
    String input = msg.popString();
    int cursor = Integer.parseInt(msg.popString());
    int[] anchor = {-1};
    ZMsg reply = new ZMsg();
    reply.add(clientAddress.duplicate());
    String trimmedCommand = input.trim();
    if (trimmedCommand.startsWith("/")) {
      if (!trimmedCommand.contains(" ")) {
        Predicate<String> filter = trimmedCommand.equals("/") ?
            n -> true : n -> n.startsWith(trimmedCommand.substring(1));
        commandNames.stream()
            .filter(filter)
            .forEach(s -> reply.add("/" + s));
        anchor[0] = 0;
      }
    }
    reply.send(socket, true);
  }
}

代码示例来源:origin: fiber-space/jupyter-kernel-jsr223

throw new RuntimeException("[jupyter-kernel.jar] Message incomplete. Didn't receive required message parts");
uuid = zframes[MessageParts.UUID].getData();
String delim = new String(zframes[MessageParts.DELIM].getData(), 
             StandardCharsets.UTF_8);
if (!delim.equals(delimiter)) {
  throw new RuntimeException("[jupyter-kernel.jar] Incorrectly formatted message. Delimiter <IDS|MSG> not found");
byte[] header = zframes[MessageParts.HEADER].getData();
byte[] parent = zframes[MessageParts.PARENT].getData();
byte[] meta = zframes[MessageParts.METADATA].getData();
byte[] content = zframes[MessageParts.CONTENT].getData();
byte[] hmac = zframes[MessageParts.HMAC].getData();

代码示例来源:origin: org.zeromq/jeromq

public void push(byte[] data)
{
  push(new ZFrame(data));
}

相关文章