# Class JsonNioService

java.lang.Object
naga.NIOService
org.openscience.jmol.app.jsonkiosk.JsonNioService

All Implemented Interfaces:
  `JsonNioServer`

public class JsonNioService extends naga.NIOService implements JsonNioServer
A class for interacting with Jmol over local sockets. See also org.molecularplayground.MPJmolApp.java for how this works. Note that this service does not require MPJmolApp -- it is a package in the standard Jmol app. Listens over a port on the local host for instructions on what to display. Instructions come in over the port as JSON strings. This class uses the Naga asynchronous socket network I/O package (NIO), the JSON.org JSON package and Jmol. https://code.google.com/p/naga/ Initial versions of this code, including the JSON-base protocol were created by Adam Williams, U-Mass Amherst see https://MolecularPlayground.org and org.openscience.jmol.molecularplayground.MPJmolApp.java `Sequence of events: 1) Jmol initiates server listening on a port using the JmolScript command with an arbitrary negative port number. (-30000 used here just for an example): sync -30000 This can be done also through the command line using jmol -P -30000 or jmol --port -30000 Jmol will respond to System.out: JsonNioServerThread-JmolNioServer JsonNioServerSocket on 30000 2) Client sends handshake to port 30000. As with all communications to this service, there must be no new-line characters (\n) ANYWHERE in the JSON being sent EXCEPT for a single message terminator: {"magic": "JmolApp", "role": "out"}\n where "out" here indicates that this socket is for Jmol (reply) output. Jmol will reply with the 30-byte response: {"type":"reply","reply":"OK"}\n (The client may see only 29 bytes, as it may or may not strip the final \n.) Optionally, the client may also indicate a specified port for Jmol input. But typically this is just the currently active port. {"magic": "JmolApp", "role": "in"}\n Jmol will reply with {"type": "reply", "reply": "OK"}\n; 3) Client sequentially sends Jmol script commands over the "in" socket: {"type": "command", "command": command}\n where required command is some JSON-escaped string such as "rotate x 30" or "load $caffeine". For example: {"type": "command", "command": "var atoms = {_C or _H};select atoms"}\n For the rest of this discussion, we will use the Jmol command that communicates with another Jmol instance rather than this JSON context: SYNC 30000 "var atoms = {_C or _H};select atoms" in this case. 4) Jmol throughout this process is sending replies that come from the Jmol Statuslistener class. For example: {"type":"reply","reply":"SCRIPT:script 8 started"} {"type":"reply","reply":"SCRIPT:Script completed"} {"type":"reply","reply":"SCRIPT:Jmol script terminated"} Note that your client will be subscribed to many of the Jmol status callbacks (see org.openscience.jmol.app.jmolpanel.StatusListener), including: LOADSTRUCT ANIMFRAME SCRIPT ECHO PICK CLICK RESIZE ERROR MINIMIZATION STRUCTUREMODIFIED All scripts and callback messages run in order but asynchronously in Jmol. You do not need to wait for one script to be finished before issuing another; there is a queue that handles that. If you want to be sure that a particular script has been run, simply add a MESSAGE command as its last part: sync 30000 "background blue;message The background is blue now" and it will appear as a SCRIPT callback: {"type":"reply","reply":"SCRIPT:The background is blue now"} after which you can handle that event appropriately. The SCRIPT callback can be particularly useful to monitor: sync 30000 "backgrund blue" {"type":"reply","reply":"SCRIPT:script compiler ERROR: command expected\n----\n >>>> backgrund blue invalid input: '<'invalid input: '<'invalid input: '<'invalid input: '<'"} Note that the ERROR callback does not fire for compile errors, only for errors found while running a parsed script: {"type":"reply","reply":"ERROR:ScriptException"} Note that all of these messages are "thumbnails" in the sense that they are just a message string. You can subscribe to a full report for any of these callbacks using 'SYNC:ON' for the callback function: set XxxxxCallback SYNC:ON For example, issuing sync 30000 "load $caffeine" gives the simple reply: {"type":"reply","reply":"LOADSTRUCT:https://cactus.nci.nih.gov/chemical/structure/caffeine/file?format=sdfinvalid input: '&get3d'=true"} but after sync 30000 "set LoadStructCallback 'SYNC:ON' we get additional details, and array of data with nine elements: {"type":"reply","reply":["LOADSTRUCT", "https://cactus.nci.nih.gov/chemical/structure/caffeine/file?format=sdfinvalid input: '&get3d'=true", "file?format=sdfinvalid input: '&get3d'=true", "C8H10N4O2", null, 3, "1.1", "1.1", null]} Exact specifications for these callbacks are not well documented. See org.jmol.viewer.StatusManager code for details. Remove the callback listener using set XxxxxCallback SYNC:OFF Note that unlike Java, you get only one SYNC callback; this is not an array of listeners. 5) Shutdown can be requested by sending {"type": "quit"}\n or by issuing the command sync 30000 "exitjmol" Note that the Molecular Playgournd implemented an extensive set of gesture-handling methods that are also available via this interface. Many of these methods utilize the JmolViewer.syncScript() method, which directly manipulates the display as though someone were using a mouse. {"type" : "move", "style" : "rotate", "x" : deltaX, "y", deltaY } {"type" : "move", "style" : "translate", "x" : deltaX, "y", deltaY } {"type" : "move", "style" : "zoom", "scale" : scale } (1.0 = 100%) {"type" : "sync", "sync" : syncText } {"type" : "touch", "eventType" : eventType, "touchID" : touchID, "iData" : idata, "time" : time, "x" : x, "y" : y, "z" : z } For details on the "touch" type, see org.jmol.viewer.ActionManagerMT::processEvent Note that all of the move and sync commands utilize the Jmol sync functionality originally intended for applets. So any valid sync command may be used with the "sync" style. These include essentially all the actions that a user can make with a mouse, including the following, where the notation invalid input: '<'....> represents a number of a given type. These events interrupt any currently running script, just as with typical mouse actions. "centerAt invalid input: '<'int:x> invalid input: '<'int:y> invalid input: '<'float:ptx> invalid input: '<'float:pty> invalid input: '<'float:ptz>" -- set {ptx,pty,ptz} at screen (x,y) "rotateMolecule invalid input: '<'float:deltaX> invalid input: '<'float:deltaY>" "rotateXYBy invalid input: '<'float:deltaX> invalid input: '<'float:deltaY>" "rotateZBy invalid input: '<'int:degrees>" "rotateZBy invalid input: '<'int:degrees> invalid input: '<'int:x> invalid input: '<'int:y>" (with center reset) "rotateArcBall invalid input: '<'int:x> invalid input: '<'int:y> invalid input: '<'float:factor>" "spinXYBy invalid input: '<'int:x> invalid input: '<'int:y> invalid input: '<'float:speed>" -- a "flick" gesture "translateXYBy invalid input: '<'float:deltaX, float:deltaY>" "zoomBy invalid input: '<'int:pixels>" "zoomByFactor invalid input: '<'float:factor>" "zoomByFactor invalid input: '<'float:factor> invalid input: '<'int:x> invalid input: '<'int:y>" (with center reset) In addition, a Jmol client send "raw" JSON strings over the socket via the SYNC command: sync 30000 '{"type": "command", "command": "var atoms = {_C or _H};select atoms"}' and since JmolScript's associative array is equivalent to JSON, this message does not have to be a string; it can be an associative array: sync 30000 {"type":"command","command":"background orange"} Even simpler, Jmol's native associative array uses [...] instead of {...} and does not require quoting keys (unless they contain spaces): sync 30000 [type:"command", command:"background orange"] And, finally, the message can be in the form of a JmolScript variable: x = [type:"command", command:"background orange"] sync 30000 x`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`protected class`
`JsonNioService.JsonNioClientThread`
`protected class`
`JsonNioService.JsonNioServerThread`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected JsonNioClient`
`client`
`protected boolean`
`halt`
`protected String`
`myName`
`protected naga.NIOSocket`
`outSocket`
`protected int`
`port`
`protected int`
`version`
`static final int`
`VERSION`

### Fields inherited from class naga.NIOService

`DEFAULT_IO_BUFFER_SIZE`

### Fields inherited from interface JsonNioServer

`INSOCKET, OUTSOCKET`

## Constructor Summary

Constructors
Constructor
Description
`JsonNioService()`

## Method Summary

Modifier and Type
Method
Description
`void`
`close()`
`static double`
`getDouble(Map<String,Object> map, String key)`
`static int`
`getInt(Map<String,Object> map, String key)`
`static long`
`getLong(Map<String,Object> map, String key)`
`int`
`getPort()`
`static String`
`getString(Map<String,Object> map, String key)`
`boolean`
`hasOuputSocket()`
`protected void`
`initialize(String role, naga.NIOSocket nioSocket)`
`protected void`
`processMessage(byte[] packet, naga.NIOSocket socket)`
`void`
`reply(int port, Object data)`
`protected void`
`sendMessage(Map<String,Object> map, String msg, naga.NIOSocket socket)`
`void`
`sendToJmol(int port, String msg)`
send the message - not for replies.
`void`
`startService(int port, JsonNioClient client, Viewer jmolViewer, String name, int version)`
`static byte[]`
`toJSONBytes(Map<String,Object> map)`
Guaranteed to create a clean no-whitespace JSON stream terminated by a single \n.
`static Map<String,Object>`
`toMap(byte[] packet)`

### Methods inherited from class naga.NIOService

`getBufferSize, getQueue, isOpen, notifyException, openServerSocket, openServerSocket, openServerSocket, openSocket, openSocket, openSSLServerSocket, openSSLServerSocket, openSSLServerSocket, openSSLSocket, openSSLSocket, queue, selectBlocking, selectBlocking, selectNonBlocking, setBufferSize, setExceptionObserver, wakeup`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### VERSION

public static final int VERSION

See Also:
  - Constant Field Values

### myName

protected String myName

### halt

protected boolean halt

### port

protected int port

### outSocket

protected naga.NIOSocket outSocket

### client

protected JsonNioClient client

### version

protected int version

## Constructor Details

### JsonNioService

public JsonNioService() throws IOException

Throws:
  `IOException`

## Method Details

### getPort

public int getPort()

Specified by:
  `getPort` in interface `JsonNioServer`

### startService

public void startService(int port, JsonNioClient client, Viewer jmolViewer, String name, int version) throws IOException

Specified by:
  `startService` in interface `JsonNioServer`
Throws:
  `IOException`

### hasOuputSocket

public boolean hasOuputSocket()

Specified by:
  `hasOuputSocket` in interface `JsonNioServer`

### sendToJmol

public void sendToJmol(int port, String msg)
send the message - not for replies.

Specified by:
  `sendToJmol` in interface `JsonNioServer`
Parameters:
  `port` -
  `msg` -

### processMessage

protected void processMessage(byte[] packet, naga.NIOSocket socket)

### sendMessage

protected void sendMessage(Map<String,Object> map, String msg, naga.NIOSocket socket)

### reply

public void reply(int port, Object data)

Specified by:
  `reply` in interface `JsonNioServer`

### close

public void close()

Specified by:
  `close` in interface `JsonNioServer`
Overrides:
  `close` in class `naga.NIOService`

### initialize

protected void initialize(String role, naga.NIOSocket nioSocket)

### toJSONBytes

public static byte[] toJSONBytes(Map<String,Object> map)
Guaranteed to create a clean no-whitespace JSON stream terminated by a single \n.

Parameters:
  `map` -
Returns:
  clean bytes

### toMap

public static Map<String,Object> toMap(byte[] packet)

### getString

public static String getString(Map<String,Object> map, String key)

### getLong

public static long getLong(Map<String,Object> map, String key) throws Exception

Throws:
  `Exception`

### getInt

public static int getInt(Map<String,Object> map, String key) throws Exception

Throws:
  `Exception`

### getDouble

public static double getDouble(Map<String,Object> map, String key) throws Exception

Throws:
  `Exception`