Java Code Examples for com.twosigma.beakerx.message.Message#setParentHeader()

The following examples show how to use com.twosigma.beakerx.message.Message#setParentHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommInfoHandler.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private void handleMsg(Message message) {
  logger.debug("Processing CommInfoHandler");
  Message reply = new Message(new Header(COMM_INFO_REPLY, message.getHeader().getSession()));
  HashMap<String, Serializable> content = new HashMap<>();
  content.put(COMMS, new HashMap<String, Serializable>());

  String target = getMessageTarget(message);
  kernel.getCommHashSet().stream()
          .map(hash -> kernel.getComm(hash))
          .filter(comm -> target == null || target.isEmpty() || comm.getTargetName().equals(target))
          .forEach(comm -> {
            HashMap<String, Serializable> commRepDetails = new HashMap<>();
            commRepDetails.put(TARGET_NAME, comm.getTargetName());
            ((HashMap<String, Serializable>) content.get(COMMS)).put(comm.getCommId(), commRepDetails);
          });
  reply.setContent(content);
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  send(reply);
}
 
Example 2
Source File: CommCloseHandler.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private void handleMsg(Message message) {
  logger.debug("Processing CommCloseHandler");
  Map<String, Serializable> commMap = message.getContent();

  String targetName =
          (kernel.getComm(getString(commMap, COMM_ID)) != null)
                  ? kernel.getComm(getString(commMap, COMM_ID)).getTargetName()
                  : "";
  kernel.removeComm(getString(commMap, COMM_ID));

  Message reply = new Message(new Header(COMM_CLOSE, message.getHeader().getSession()));
  HashMap<String, Serializable> map = new HashMap<>();
  map.put(DATA, new HashMap<>());
  reply.setContent(map);
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  send(reply);
  logger.debug("Comm closed, target name = " + targetName);
}
 
Example 3
Source File: BxComm.java    From beakerx with Apache License 2.0 6 votes vote down vote up
public void close() {
  Message parentMessage = getParentMessage();

  if (this.getCloseCallbackList() != null && !this.getCloseCallbackList().isEmpty()) {
    for (Handler<Message> handler : getCloseCallbackList()) {
      handler.handle(parentMessage);
    }
  }
  Message message = new Message(new Header(COMM_CLOSE, parentMessage.getHeader().getSession()));
  if (parentMessage != null) {
    message.setParentHeader(parentMessage.getHeader());
  }
  HashMap<String, Serializable> map = new HashMap<>();
  map.put(COMM_ID, getCommId());
  map.put(DATA, new HashMap<>());
  map.put(METADATA, new HashMap<>());
  message.setContent(map);
  message.setMetadata(buildMetadata());

  kernel.removeComm(getCommId());
  kernel.publish(singletonList(message));
}
 
Example 4
Source File: BxComm.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private void doOpen(Message parentMessage, Buffer buffer) {
  Preconditions.checkNotNull(parentMessage, "parent message can not be null");
  Message message = new Message(new Header(COMM_OPEN, parentMessage.getHeader().getSession()));
  message.setParentHeader(parentMessage.getHeader());
  HashMap<String, Serializable> map = new HashMap<>();
  map.put(COMM_ID, getCommId());
  map.put(TARGET_NAME, getTargetName());

  HashMap<String, Serializable> state = new HashMap<>();
  state.put(STATE, data.getData());
  state.put(METHOD, (Serializable) data.getData().get(METHOD));
  if (!buffer.isEmpty()) {
    state.put(BUFFER_PATHS, buffer.getBufferPaths());
    message.setBuffers(buffer.getBuffers());
  }
  map.put(DATA, state);
  map.put(METADATA, metadata);

  map.put(TARGET_MODULE, getTargetModule());
  message.setContent(map);
  message.setMetadata(buildMetadata());
  kernel.publish(singletonList(message));
  kernel.addComm(getCommId(), this);
}
 
Example 5
Source File: KernelSocketsZMQ.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private void handleControlMsg() {
  Message message = readMessage(controlSocket);
  JupyterMessages type = message.getHeader().getTypeEnum();
  if (type.equals(SHUTDOWN_REQUEST)) {
    Message reply = new Message(new Header(SHUTDOWN_REPLY, message.getHeader().getSession()));
    reply.setParentHeader(message.getHeader());
    reply.setContent(message.getContent());
    sendMsg(controlSocket, Collections.singletonList(reply));
    shutdown();
  }
}
 
Example 6
Source File: JupyterHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private static Message executeRequestMessage() {
  Message message = new Message(initHeader(JupyterMessages.EXECUTE_REQUEST));
  message.setIdentities(Arrays.asList("identities".getBytes()));
  message.setParentHeader(null);
  message.setMetadata(new LinkedHashMap<>());
  return message;
}
 
Example 7
Source File: ExecuteRequestHandlerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private static Message copyMessage(Message origin) {
  String header = toJson(origin.getHeader());
  Message copy = new Message(parse(header, Header.class));
  for (byte[] list : origin.getIdentities()) {
    copy.getIdentities().add(list.clone());
  }
  String parent = toJson(origin.getParentHeader());
  String metadata = toJson(origin.getMetadata());
  String content = toJson(origin.getContent());
  copy.setParentHeader(parse(parent, Header.class));
  copy.setMetadata(parse(metadata, LinkedHashMap.class));
  copy.setContent(parse(content, LinkedHashMap.class));
  return copy;
}
 
Example 8
Source File: ExecuteRequestHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private void announceTheCode(Message message, String code) {
  Message reply = new Message(new Header(EXECUTE_INPUT, message.getHeader().getSession()));
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  Map<String, Serializable> map1 = new HashMap<>(2);
  map1.put("execution_count", executionCount);
  map1.put("code", code);
  reply.setContent(map1);
  kernel.publish(singletonList(reply));
}
 
Example 9
Source File: BxComm.java    From beakerx with Apache License 2.0 5 votes vote down vote up
public static Message messageMessage(JupyterMessages type, Buffer buffer, Map<String, Serializable> content, Message parentMessage) {
  Message message = new Message(new Header(type, parentMessage.getHeader().getSession()));
  checkNotNull(parentMessage);
  message.setParentHeader(parentMessage.getHeader());
  message.setContent(content);
  message.setMetadata(buildMetadata());
  if (!buffer.isEmpty()) {
    message.setBuffers(buffer.getBuffers());
  }
  return message;
}
 
Example 10
Source File: InspectHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private Message createMsg(Message message, InspectResult inspectResult) {
    Message reply = new Message(new Header(INSPECT_REPLY, message.getHeader().getSession()));
    reply.setIdentities(message.getIdentities());
    reply.setParentHeader(message.getHeader());
    Map<String, Serializable> content = new HashMap<>();
    content.put(STATUS, "ok");
    content.put(DATA, inspectResult.getData());
    //content.put(METADATA, {});
    content.put(FOUND, inspectResult.getFound());

    reply.setContent(content);
    return reply;
}
 
Example 11
Source File: CompleteHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private Message createMsg(Message message, int cursorPos, AutocompleteResult autocomplete) {
  Message reply = new Message(new Header(COMPLETE_REPLY, message.getHeader().getSession()));
  reply.setIdentities(message.getIdentities());
  reply.setParentHeader(message.getHeader());
  Map<String, Serializable> content = new HashMap<>();
  content.put(STATUS, "ok");
  content.put(MATCHES, autocomplete.getMatches().toArray());
  content.put(CURSOR_END, cursorPos);
  content.put(CURSOR_START, autocomplete.getStartIndex());

  reply.setContent(content);
  return reply;
}
 
Example 12
Source File: IsCompleteRequestHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private void handleMsg(Message message) {
  logger.debug("Processing is complete request");
  Message reply = new Message(new Header(IS_COMPLETE_REPLY, message.getHeader().getSession()));
  HashMap<String, Serializable> map = new HashMap<>();
  map.put("status", "complete");
  reply.setContent(map);
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  send(reply);
}
 
Example 13
Source File: KernelInfoHandler.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private void handleMsg(Message message) {
  logger.debug("Processing kernel info request");
  Message reply = new Message(new Header(KERNEL_INFO_REPLY, message.getHeader().getSession()));
  reply.setContent(content());
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  send(reply);
}
 
Example 14
Source File: InputRequestMessageFactoryImpl.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Override
public Message create(Message parent) {
  Message message = new Message(new Header(INPUT_REQUEST, parent.getHeader().getSession()), parent.getIdentities());
  message.setContent(content());
  message.setParentHeader(parent.getHeader());
  return message;
}
 
Example 15
Source File: ScijavaKernelInfoHandler.java    From scijava-jupyter-kernel with Apache License 2.0 5 votes vote down vote up
private void handleMsg(Message message) {

        Message reply = new Message();

        HashMap<String, Serializable> map = new HashMap<>(6);
        map.put("protocol_version", "5.0");
        map.put("implementation", "scijava");
        map.put("implementation_version", "1.0");

        HashMap<String, Serializable> map1 = new HashMap<>(7);
        map1.put("name", "scijava");
        map1.put("version", "1.0");
        map1.put("mimetype", "");
        map1.put("file_extension", "");
        map1.put("pygments_lexer", "groovy");
        map1.put("codemirror_mode", "groovy");
        map1.put("nbconverter_exporter", "");

        map.put("language_info", map1);
        String banner = "SciJava Jupyter Kernel v" + getClass().getPackage().getSpecificationVersion() + " | ";
        map.put("banner", banner);
        map.put("beakerx", true);

        List<String> helpLinks = new ArrayList<>();
        helpLinks.add("https://imagej.net/Jupyter");
        helpLinks.add("https://github.com/scijava/scijava-jupyter-kernel");
        map.put("help_links", (Serializable) helpLinks);

        reply.setContent(map);
        reply.setHeader(new Header(KERNEL_INFO_REPLY, message.getHeader().getSession()));
        reply.setParentHeader(message.getHeader());
        reply.setIdentities(message.getIdentities());
        send(reply);
    }
 
Example 16
Source File: MessageCreator.java    From beakerx with Apache License 2.0 4 votes vote down vote up
private static Message initMessage(JupyterMessages type, Message message) {
  Message reply = new Message(new Header(type, message.getHeader().getSession()));
  reply.setParentHeader(message.getHeader());
  reply.setIdentities(message.getIdentities());
  return reply;
}
 
Example 17
Source File: MagicKernelResponse.java    From beakerx with Apache License 2.0 4 votes vote down vote up
private void publishMessage(KernelFunctionality kernel, Message message, int executionCount) {
    for (Message msg : responses) {
        msg.setParentHeader(message.getHeader());
    }
    kernel.publish(responses);
}
 
Example 18
Source File: JupyterHandlerTest.java    From beakerx with Apache License 2.0 4 votes vote down vote up
public static void initMessage(Message message) {
  message.getIdentities().add("identityStr".getBytes());
  message.setParentHeader(message.getHeader());
  message.setMetadata(new LinkedHashMap<>());
  message.setContent(new LinkedHashMap<>());
}