Class ThincHandler

java.lang.Object
  extended by ThincHandler
Direct Known Subclasses:
ThincMsgHandler, ThincMsgInitReplyHandler, ThincSoundHandler

public abstract class ThincHandler
extends java.lang.Object

abstract class for handling network messages. it provides various methods to implement any network handler derived from this.

Author:
Young Jin Yoon

Field Summary
protected  ThincClientCanvas canvas
           
protected  ThincClient client
           
protected  java.nio.ByteBuffer clientAppData
           
protected  java.nio.ByteBuffer clientPacData
           
protected  boolean isSSL
           
protected  byte lastSent
           
protected static int MAX_DIRECT_ALLOCATE
           
protected  javax.net.ssl.SSLEngineResult rcvResult
           
protected  java.nio.ByteBuffer recv
           
protected  java.nio.channels.SocketChannel sc
           
protected  java.nio.ByteBuffer send
           
protected  java.nio.ByteBuffer serverAppData
           
protected  java.nio.ByteBuffer serverPacData
           
protected  javax.net.ssl.SSLEngine sslEngine
           
protected  javax.net.ssl.SSLSocket sslSocket
           
protected static int THINC_HEADERSIZE
           
 
Constructor Summary
ThincHandler(ThincClient tc, ThincClientCanvas ca)
           
 
Method Summary
protected  void closeSocketChannel()
          closeSocketChannel() closes the connection from the server
protected  java.nio.channels.SocketChannel createSocketChannel(java.lang.String host, int port)
          createSocketChannel() creates network connection to server
 byte getLastSent()
          getLastSent() get the thinc type of last sent messages
 java.nio.ByteBuffer getMessage(int size)
          getMessage() get messages from the server.
private  void putThincHeader(java.nio.ByteBuffer b, byte type, byte flag, short length)
          putThincHeader() puts the general thinc header into ByteBuffer
protected  int recvBytes(java.nio.ByteBuffer bb)
          recvBytes() is the primitive method used by getMessage method. it also used when version exchange required (which does not have general thinc header
 int send(ThincCMsg c)
          send() send messages to the server.
protected  int sendBytes(java.nio.ByteBuffer bb)
          sendBytes() is the primitive method used by send method. it also used when version exchange required (which does not have general thinc header
 void setSSLSocketChannel(java.lang.String host, int port)
          setSSLSocketChannel() uses SSLEngine class in Java to establish SSL connection.
 void SSLHandshake()
          SSLHandshake() do handshake for SSLEngine.
private  int unwrapSSL(java.nio.ByteBuffer bb)
          unwrapSSL() unwrap from ssl messages & send it
private  int wrapSSL(java.nio.ByteBuffer bb)
          wrapSSL() wrap into ssl messages & send it
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

THINC_HEADERSIZE

protected static final int THINC_HEADERSIZE
See Also:
Constant Field Values

MAX_DIRECT_ALLOCATE

protected static final int MAX_DIRECT_ALLOCATE
See Also:
Constant Field Values

sc

protected java.nio.channels.SocketChannel sc

recv

protected java.nio.ByteBuffer recv

send

protected java.nio.ByteBuffer send

client

protected ThincClient client

canvas

protected ThincClientCanvas canvas

lastSent

protected byte lastSent

isSSL

protected boolean isSSL

sslEngine

protected javax.net.ssl.SSLEngine sslEngine

sslSocket

protected javax.net.ssl.SSLSocket sslSocket

clientAppData

protected java.nio.ByteBuffer clientAppData

clientPacData

protected java.nio.ByteBuffer clientPacData

serverAppData

protected java.nio.ByteBuffer serverAppData

serverPacData

protected java.nio.ByteBuffer serverPacData

rcvResult

protected javax.net.ssl.SSLEngineResult rcvResult
Constructor Detail

ThincHandler

public ThincHandler(ThincClient tc,
                    ThincClientCanvas ca)
Method Detail

createSocketChannel

protected java.nio.channels.SocketChannel createSocketChannel(java.lang.String host,
                                                              int port)
                                                       throws ThincException
createSocketChannel() creates network connection to server

Parameters:
host - domain name for server
port - port number for server
Returns:
SocketChannel object to be created
Throws:
ThincException

closeSocketChannel

protected void closeSocketChannel()
                           throws ThincException
closeSocketChannel() closes the connection from the server

Throws:
ThincException

send

public int send(ThincCMsg c)
         throws ThincException
send() send messages to the server.

Parameters:
c - ThincCMsg object to send
Returns:
number of bytes sent
Throws:
ThincException

putThincHeader

private void putThincHeader(java.nio.ByteBuffer b,
                            byte type,
                            byte flag,
                            short length)
putThincHeader() puts the general thinc header into ByteBuffer

Parameters:
b - target ByteBuffer to put
type - type of the messages
flag - flag of the messages
length - length of the messages

sendBytes

protected int sendBytes(java.nio.ByteBuffer bb)
                 throws ThincException
sendBytes() is the primitive method used by send method. it also used when version exchange required (which does not have general thinc header

Parameters:
bb - ByteBuffer to send
Returns:
number of bytes sent
Throws:
ThincException

wrapSSL

private int wrapSSL(java.nio.ByteBuffer bb)
             throws ThincException
wrapSSL() wrap into ssl messages & send it

Parameters:
bb - original ByteBuffer to be encoded
Returns:
number of bytes transmitted count from original buffer
Throws:
ThincException

getMessage

public java.nio.ByteBuffer getMessage(int size)
                               throws ThincException
getMessage() get messages from the server.

Parameters:
size - ByteBuffer size to be received
Returns:
ByteBuffer received
Throws:
ThincException

recvBytes

protected int recvBytes(java.nio.ByteBuffer bb)
                 throws ThincException
recvBytes() is the primitive method used by getMessage method. it also used when version exchange required (which does not have general thinc header

Parameters:
bb - ByteBuffer to receive
Returns:
number of bytes received
Throws:
ThincException

unwrapSSL

private int unwrapSSL(java.nio.ByteBuffer bb)
               throws ThincException
unwrapSSL() unwrap from ssl messages & send it

Parameters:
bb - original ByteBuffer to receive
Returns:
number of bytes received count from original buffer
Throws:
ThincException

getLastSent

public byte getLastSent()
getLastSent() get the thinc type of last sent messages

Returns:
a thinc type of last sent messages

setSSLSocketChannel

public void setSSLSocketChannel(java.lang.String host,
                                int port)
                         throws ThincException
setSSLSocketChannel() uses SSLEngine class in Java to establish SSL connection. SSLEngine supports only non-blocking mode. We need to find another way to use SSLEngine because it is currently implemented with polling

Throws:
ThincException

SSLHandshake

public void SSLHandshake()
                  throws ThincException
SSLHandshake() do handshake for SSLEngine.

Throws:
ThincException