net.sourceforge.obby4j
Class Protocol

java.lang.Object
  extended by java.lang.Thread
      extended by net.sourceforge.obby4j.Protocol
All Implemented Interfaces:
java.lang.Runnable

public class Protocol
extends java.lang.Thread

Provides encapsulation of low-level obby protocol messages and utility methods for converting protocol parameters into more meaningful Java types.


Nested Class Summary
static class Protocol.DocumentKeyword
          A list of all subcommand keywords of the obby_document command known to this implementation of the obby protocol.
static class Protocol.Keyword
          A list of all protocol keywords known to this implementation of the obby protocol.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Protocol(java.net.InetAddress host, int port)
          Creates a new Protocol instance to handle the low-level details of the obby protocol.
 
Method Summary
 void addListener(ProtocolListener listener)
          Register a listener to receive protocol commands.
static java.lang.String colorToHexString(java.awt.Color color)
          Converts a color to an obby protocol color hex string.
static java.awt.Color hexStringToColor(java.lang.String hexColorString)
          Converts an obby-compatoble hex string of a color to an Color instance.
static int hexStringToInt(java.lang.String obbyHexString)
          Converts an obby hex string to an int.
 void login(User user)
          Logs in to the server as the user of the Client instance.
static java.lang.String obbyDecode(java.lang.String message)
          Decodes a message transferred via the obby protocol by unescaping backslashes, newline characters and colons.
static java.lang.String obbyEncode(java.lang.String message)
          Encodes a message for transfer via the obby protocol by escaping backslashes, newline characters and colons.
 void removeListener(ProtocolListener listener)
          Unregister a listener from receiving protocol commands.
 void run()
          Handles incoming messages from the server.
 void sendMessage(java.lang.String message)
          Sends a chat message to other users.
 void subscribe(Document document)
          Subscribes this client to a document.
 void unsubscribe(Document document)
          Unsubscribes this client from a document.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Protocol

public Protocol(java.net.InetAddress host,
                int port)
         throws java.io.IOException
Creates a new Protocol instance to handle the low-level details of the obby protocol.

Parameters:
host -
port -
Throws:
java.io.IOException
Method Detail

addListener

public void addListener(ProtocolListener listener)
Register a listener to receive protocol commands.

Parameters:
listener - the listener to register

colorToHexString

public static java.lang.String colorToHexString(java.awt.Color color)
Converts a color to an obby protocol color hex string.

Parameters:
color - the color to convert
Returns:
a obby-compatible hex string of the color

hexStringToColor

public static java.awt.Color hexStringToColor(java.lang.String hexColorString)
Converts an obby-compatoble hex string of a color to an Color instance.

Parameters:
hexColorString - the color hex string to convert
Returns:
a Color instance

hexStringToInt

public static int hexStringToInt(java.lang.String obbyHexString)
Converts an obby hex string to an int.

Parameters:
obbyHexString - the obby hex string of a number
Returns:
the numeric value of the hex string as int

login

public void login(User user)
           throws java.io.IOException,
                  Net6LoginException
Logs in to the server as the user of the Client instance. This method blocks until the internal state has been synchronized with the server (logged in users, available documents).

Parameters:
user -
Throws:
java.io.IOException
Net6LoginException - if the login failed due to some protocol or obby problem

obbyDecode

public static java.lang.String obbyDecode(java.lang.String message)
Decodes a message transferred via the obby protocol by unescaping backslashes, newline characters and colons.

Parameters:
message - the message to decode
Returns:
the decoded message

obbyEncode

public static java.lang.String obbyEncode(java.lang.String message)
Encodes a message for transfer via the obby protocol by escaping backslashes, newline characters and colons.

Parameters:
message - the message to encode
Returns:
the obby encoded message

removeListener

public void removeListener(ProtocolListener listener)
Unregister a listener from receiving protocol commands.

Parameters:
listener - the listener to unregister

run

public void run()
Handles incoming messages from the server.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

sendMessage

public void sendMessage(java.lang.String message)
                 throws java.io.IOException
Sends a chat message to other users.

Parameters:
message - the message to send
Throws:
java.io.IOException

subscribe

public void subscribe(Document document)
               throws java.io.IOException
Subscribes this client to a document.

Parameters:
document - the document to subscribe to
Throws:
java.io.IOException

unsubscribe

public void unsubscribe(Document document)
                 throws java.io.IOException
Unsubscribes this client from a document.

Parameters:
document - the document to unsubscribe from
Throws:
java.io.IOException