@WebSocket public class WebClientProtocol extends java.lang.Object implements WebClientMessageTypes
IMPORTANT: Do not use logging anywhere inside this class. When an authentication plugin is running during session establishment with the P2J server using logging no messages are written into the log and the application is blocked.
The payloads are in 2 formats binary and JSON text.
Input messages: Keyboard input. offset value content ------------------------------------ 0 0x01 message id 1 uu keystroke int value 2 ll keystroke = uu << 8 | ll
Keyboard pair input. offset value content ------------------------------------ 0 0x02 message id 1 uu keyCode int value 2 ll keyCode = uu << 8 | ll 3 uu charCode int value 4 ll charCode = uu << 8 | ll
Paste. offset value content ------------------------------------ 0 0x03 message id 1 uu charCode int value 2 ll charCode = uu << 8 | ll ... n-1 uu charCode int value n ll charCode = uu << 8 | ll
Output messages: Clear screen. offset value content ------------------------------------ 0 0x80 message id
Draw text on screen. offset value content ------------------------------------ 0 0x81 message id 1..n data data packets structure of data packets: offset value content ------------------------------------ 0 size packet size high byte 1 size packet size low byte 2 width rectangle with 3 height rectangle height 4 row row number 5 col column number 6 attr text attributes 7 color FG/BG color pairs 8..n text text characters, each character on 2 bytes (UTF-16)
Set cursor position. offset value content ------------------------------------ 0 0x82 message id 1 row row 2 col column
Set cursor status. offset value content ------------------------------------ 0 0x83 message id 1 stat status 1 = visible 0 = hidden
Play a beep message. offset value content ------------------------------------ 0 0x84 message id
Quit message. offset value content ------------------------------------ 0 0x85 message id
Palette message is in JSON format as follow. {"c":0, "p":[color1, color2, ... color3]} Example: {"c":0, "p":["#000000", "#FF0000", "#00FF00", "#0000FF", "#FFFFFF"]} offset value content ------------------------------------ 0 0x86 message id 1 mode mode 1 = VT100 0 = P2J
Switch mode.
Modifier and Type | Class and Description |
---|---|
(package private) class |
WebClientProtocol.MessagesCollector
MessagesColector is responsible to collect the original message from the messages of
this type MSG_PARTIAL.
|
Modifier and Type | Field and Description |
---|---|
protected ClientProtocolHooks |
callbacks
Callbacks for delegated processing.
|
private WebClientProtocol.MessagesCollector |
collector
The partial messages collector
|
private int |
connections
Number of simultaneous connections.
|
protected boolean |
isVT100
Keyboard input is being processed in virtual VT100 terminal mode.
|
private java.util.Deque<java.lang.Integer> |
keyBuffer
Keyboard input queue.
|
protected java.lang.Object |
lock
Instance lock for synchronization of operations.
|
private static java.util.Set<java.nio.file.attribute.PosixFilePermission> |
NEW_TMP_FILE_PERMISSIONS
The file permissions to create a new temporary file
|
private PushMessagesWorker |
pushWorker
Push messages worker thread.
|
private java.util.Map<java.lang.Integer,java.lang.Object> |
receivedMessages
A map with the received messages from java-script side.
|
private long |
timeout
Web socket timeout.
|
private static java.lang.String |
TMP_FILE_EXT
The new created temporary file extension
|
private static java.nio.file.StandardOpenOption[] |
TMP_FILE_OPTIONS
The options used to open file channels for created temporary files
|
private static java.lang.String |
TMP_FILE_PREFIX
The new created temporary file name should start from this string
|
private WatchdogTimer |
watchdog
Watchdog thread instance.
|
private long |
wdtimeout
Watchdog thread time out value.
|
private WebTaskWorker |
webWorker
Dedicated worker to process the tasks, to release the web-socket.
|
CAPTURE_MOUSE, ENABLE_OS_EVENTS, MSG_ADD_WINDOW_MESSAGE_TEXT, MSG_BEEP, MSG_CLEAR, MSG_CLEAR_WINDOW_MESSAGES, MSG_CLIENT_READY, MSG_CLIPBOARD_CONTENTS, MSG_CLIPBOARD_PREPARE, MSG_CREATE_CHILD_WINDOW, MSG_CREATE_FONT, MSG_CREATE_WINDOW, MSG_CURRENT_SELECTION, MSG_CURSOR_POS, MSG_CURSOR_STATUS, MSG_DEREGISTER_WIDGET, MSG_DERIVE_FONT, MSG_DESKTOP_RESIZED, MSG_DESTROY_WINDOW, MSG_DONE_CREATE_FONT, MSG_DONE_DERIVE_FONT, MSG_DRAW, MSG_DRAW_HASH_REMOVED, MSG_DROP_FILE_NOTIFY, MSG_FILE_UPLOADING, MSG_GET_DESKTOP_DIMENSION, MSG_GET_FONT_HEIGHT, MSG_GET_FONT_WIDTHS, MSG_GET_MOUSE_POSITION, MSG_GET_PAR_HEIGHT, MSG_GET_TEXT_HEIGHT, MSG_GET_TEXT_WIDTH, MSG_GET_TEXT_WIDTHS, MSG_GET_TEXT_WIDTHS_BATCH, MSG_INVALIDATE_SELECTION, MSG_IS_FONT_INSTALLED, MSG_KEY, MSG_KEY_VT100, MSG_LOCK_MOUSE_PTR, MSG_MOUSE_EVENT, MSG_MOVE_TO_BOTTOM, MSG_MOVE_TO_TOP, MSG_OPEN_MIME_RESOURCE, MSG_P2J_APPLY_EVENT, MSG_P2J_CREATE_RESOURCE, MSG_P2J_DELETE_HANDLE, MSG_P2J_INVOKE, MSG_P2J_PUBLISH, MSG_P2J_SUBSCRIBE, MSG_P2J_UNSUBSCRIBE, MSG_PAGE_LOADED, MSG_PARSE_EDITOR_CONTENT, MSG_PARTIAL, MSG_PASTE, MSG_PING_PONG, MSG_QUIT, MSG_READ_CLIPBOARD, MSG_REGISTER_HOVERABLE_WIDGET, MSG_REMOTE_CALL, MSG_REMOVE_EXPIRED_HASH, MSG_RESTACK_WINDOWS, MSG_SENT_DESKTOP_DIMENSION, MSG_SENT_FONT_HEIGHT, MSG_SENT_FONT_WIDTHS, MSG_SENT_PAR_HEIGHT, MSG_SENT_TEXT_HEIGHT, MSG_SENT_TEXT_WIDTH, MSG_SENT_TEXT_WIDTHS, MSG_SENT_TEXT_WIDTHS_BATCH, MSG_SET_CURSOR_STYLE, MSG_SET_MOUSE_PTR, MSG_SET_UPLOAD_FILE_SIZE_LIMITS, MSG_SET_WAIT_STATE, MSG_SET_WINDOW_LOC, MSG_SET_WINDOW_STATUS_TEXT, MSG_VT100, MSG_WAIT_USER_INPUT, MSG_WINDOW_ACTIVATED, MSG_WINDOW_ICONIFY, MSG_WINDOW_RESIZED, MSG_WINDOW_SENSITIVITY, MSG_WINDOW_VISIBILITY, MSG_WRITE_CLIPBOARD, PROCESS_MOUSE_WIDGETS, SET_ICONIFICATION_STATE, SET_RESIZEABLE_WINDOW
Constructor and Description |
---|
WebClientProtocol(java.lang.Object lock,
ClientProtocolHooks callbacks,
long timeout,
long wdtimeout)
Create a instance.
|
Modifier and Type | Method and Description |
---|---|
void |
beep()
The beep() should be implemented by sending a command on the remote end point.
|
WebClientProtocol.MessagesCollector |
getMessagesCollector()
Returns the unique instance of MessageCollector
|
void |
injectKey(int key)
Inject the given key into the current keystroke buffer.
|
void |
onClose(int statusCode,
java.lang.String reason)
This method is called when the connection is closed.
|
void |
onConnect(org.eclipse.jetty.websocket.api.Session session)
This method is called when the remote peer open a web socket connection.
|
void |
onError(org.eclipse.jetty.websocket.api.Session session,
java.lang.Throwable error)
This method is called on communication errors.
|
void |
onMessage(byte[] message,
int offset,
int length)
This method is called when a binary message is received.
|
void |
onMessage(java.lang.String message)
This method is called when a text message is received.
|
void |
paste(byte[] message,
int offset,
int length)
Process the incoming PASTE message from the client.
|
boolean |
processBinaryMessage(byte[] message,
int offset,
int length)
This is the worker method which is called when a binary message is received.
|
protected boolean |
processChannel(java.nio.channels.FileChannel channel)
Looks ahead the given message channel and takes a decision to read and parse this channel or
to process it by the default.
|
private void |
processChannel(java.nio.channels.FileChannel channel,
java.io.Closeable appendTask,
boolean isNotProcessed)
Handles the given message channel by the default if it has not been processed yet and then
closes this message channel and removes its reference from the map of the currently tracked
messages.
|
void |
quit()
Remote application is about to end.
|
byte[] |
readByteArray(byte[] message,
int offset,
int size)
Interpret bytes from a binary message as byte array and return the accumulated result.
|
int |
readKey()
Read the next key from the FIFO key input buffer.
|
int |
readMessageInt16(byte[] message,
int offset)
Interpret 2 bytes from a binary message as a 16-bit integer and return the accumulated
result.
|
int |
readMessageInt32(byte[] message,
int offset)
Interpret 4 bytes from a binary message as a 32-bit integer and return the accumulated
result.
|
long |
readMessageInt64(byte[] message,
int offset)
Interpret 8 bytes from a binary message as a 64-bit integer and return the accumulated
result.
|
java.lang.String |
readMessageText(byte[] message,
int offset,
int length)
Interpret bytes from a binary message as text and return the accumulated result.
|
protected void |
receiveResult(int msgId,
java.lang.Object result)
Notify the
lock that a message was received with the given type. |
void |
sendBinaryMessage(byte message)
Convenience method to send a single-byte binary message.
|
void |
sendBinaryMessage(byte[] message)
Send a binary message of arbitrary size.
|
void |
sendBinaryMessage(byte first,
byte second)
Convenience method to send a two-byte binary message.
|
void |
sendBinaryMessage(byte first,
byte second,
byte third)
Convenience method to send a three-byte binary message.
|
void |
sendBinaryMessage(byte type,
int num)
Send a binary message with a 32-bit integer of arbitrary size.
|
void |
sendBinaryMessage(byte type,
java.lang.String text)
Send a binary message with text of arbitrary size.
|
void |
sendSwitchMode()
Switch mode message.
|
void |
sendTextMessage(java.lang.String message)
Send a text message.
|
private void |
startPushWorker(org.eclipse.jetty.websocket.api.Session session)
Start pushWorker thread.
|
private void |
startWatchdog(long wdtimeout)
Start watchdog timer.
|
private void |
startWebWorker()
Start the web worker which processes tasks sent from javascript.
|
private void |
stopPushWorker()
Stop pushWorker thread.
|
private void |
stopWatchdog()
Stop watchdog timer.
|
private void |
stopWebWorker()
Stop the web worker which processes tasks sent from javascript.
|
void |
swapKeyboardMode(boolean isVT100)
Switch between P2J client and interactive client.
|
java.lang.Object |
waitForResult(int msgId)
Wait for a result to be
received for the given message type. |
int |
writeMessageInt16(byte[] message,
int offset,
int value)
Write the lower 2 bytes from a given value into a binary message at a given offset.
|
int |
writeMessageInt32(byte[] message,
int offset,
int value)
Write a 32-bit integer value into a binary message at a given offset.
|
int |
writeMessageText(byte[] message,
int offset,
java.lang.String text)
Write the given text into the binary message at the specified offset.
|
private static final java.nio.file.StandardOpenOption[] TMP_FILE_OPTIONS
private static final java.util.Set<java.nio.file.attribute.PosixFilePermission> NEW_TMP_FILE_PERMISSIONS
private static final java.lang.String TMP_FILE_PREFIX
private static final java.lang.String TMP_FILE_EXT
private final long timeout
private final long wdtimeout
private java.util.Deque<java.lang.Integer> keyBuffer
private WatchdogTimer watchdog
private PushMessagesWorker pushWorker
private volatile int connections
private WebTaskWorker webWorker
private java.util.Map<java.lang.Integer,java.lang.Object> receivedMessages
waitForResult(int)
for the expected operation.private WebClientProtocol.MessagesCollector collector
protected ClientProtocolHooks callbacks
protected boolean isVT100
protected final java.lang.Object lock
public WebClientProtocol(java.lang.Object lock, ClientProtocolHooks callbacks, long timeout, long wdtimeout)
lock
- Object to use for locking. By passing this in, the caller can share the
locking instance with this code. Use null
if an internal
lock object should be created.callbacks
- An instance with hooks to call at specific events.timeout
- Web socket timeout.wdtimeout
- Watchdog thread timeout.@OnWebSocketConnect public void onConnect(org.eclipse.jetty.websocket.api.Session session)
session
- An active link of communications with a Remote WebSocket Endpoint.@OnWebSocketMessage public void onMessage(java.lang.String message)
message
- The text message.@OnWebSocketMessage public void onMessage(byte[] message, int offset, int length)
message
- Message content as an byte array.offset
- Message offset..length
- Message size.public boolean processBinaryMessage(byte[] message, int offset, int length)
message
- Message content as an byte array.offset
- Message offset..length
- Message size.true
if the message has been handled, false
otherwise.@OnWebSocketClose public void onClose(int statusCode, java.lang.String reason)
statusCode
- The status integer code.reason
- A string representing the reason.@OnWebSocketError public void onError(org.eclipse.jetty.websocket.api.Session session, java.lang.Throwable error)
session
- An active link of communications with a Remote WebSocket Endpoint.error
- A Throwable
instance representing the error condition.public void sendTextMessage(java.lang.String message)
message
- The fully encoded (JSON) text message.public void sendBinaryMessage(byte[] message)
message
- The fully encoded binary message contents.public void sendBinaryMessage(byte type, java.lang.String text)
type
- The message type.text
- The binary message contents as text.public void sendBinaryMessage(byte type, int num)
type
- The message type.num
- The binary message contents as a num.public void sendBinaryMessage(byte message)
sendBinaryMessage(byte[])
.message
- The byte which comprises the entire binary message.public void sendBinaryMessage(byte first, byte second)
sendBinaryMessage(byte[])
.first
- The first byte of the binary message.second
- The second byte of the binary message.public void sendBinaryMessage(byte first, byte second, byte third)
sendBinaryMessage(byte[])
.first
- The first byte of the binary message.second
- The second byte of the binary message.third
- The third byte of the binary message.public int readMessageInt16(byte[] message, int offset)
message
- Message content as an byte array.offset
- Offset into the message at which the integer begins.public int readMessageInt32(byte[] message, int offset)
message
- Message content as an byte array.offset
- Offset into the message at which the integer begins.public long readMessageInt64(byte[] message, int offset)
message
- Message content as an byte array.offset
- Offset into the message at which the integer begins.public java.lang.String readMessageText(byte[] message, int offset, int length)
message
- Message content as an byte array.offset
- Offset into the message at which the text begins.length
- Location in the message where the text ends.public byte[] readByteArray(byte[] message, int offset, int size)
message
- Message content as an byte array.offset
- Offset into the message at which the data begins.size
- The length of the array of byte.public int writeMessageInt16(byte[] message, int offset, int value)
message
- Message content as an byte array.offset
- Offset into the message at which the integer should be written.value
- The 16-bit value to write.public int writeMessageInt32(byte[] message, int offset, int value)
message
- Message content as an byte array.offset
- Offset into the message at which the integer should be written.value
- The 32-bit value to write.public int writeMessageText(byte[] message, int offset, java.lang.String text)
message
- Message content as an byte array. This must be large enough to hold the text.offset
- Offset into the message at which the text should begin.text
- The message text.public int readKey()
public void paste(byte[] message, int offset, int length)
message
- Message content as an byte array.offset
- Offset into the message at which the text begins.length
- Location in the message where the text ends.public void injectKey(int key)
key
- The keystroke to inject.public void beep()
public void quit()
public void swapKeyboardMode(boolean isVT100)
isVT100
- If true
, process keys in virtual VT100 terminal mode otherwise
process for internal client screen.public void sendSwitchMode()
public java.lang.Object waitForResult(int msgId)
received
for the given message type.msgId
- The response message ID.public WebClientProtocol.MessagesCollector getMessagesCollector()
protected void receiveResult(int msgId, java.lang.Object result)
lock
that a message was received with the given type.msgId
- The response message ID.result
- The received result.protected boolean processChannel(java.nio.channels.FileChannel channel)
channel
- The given file channel.private void processChannel(java.nio.channels.FileChannel channel, java.io.Closeable appendTask, boolean isNotProcessed)
channel
- The given message channelappendTask
- The append task associated with the given message channel.isNotProcessed
- The true value means that this channel hasn't been parsed yet.private void startWatchdog(long wdtimeout)
This must only be called by a caller that owns the instance lock.
wdtimeout
- Watchdog thread timeout value.private void stopWatchdog()
This must only be called by a caller that owns the instance lock.
private void startWebWorker()
private void stopWebWorker()
private void startPushWorker(org.eclipse.jetty.websocket.api.Session session)
This must only be called by a caller that owns the instance lock.
session
- An active link of communications with a Remote WebSocket end point.private void stopPushWorker()
This must only be called by a caller that owns the instance lock.