com.sun.grizzly.filter
Class CustomProtocolClient

java.lang.Object
  extended by com.sun.grizzly.filter.CustomProtocolClient
All Implemented Interfaces:
Client

public class CustomProtocolClient
extends Object
implements Client

Out of the box TCP/TLS Client for using CustomProtocol Comunictaion Layer. An usage example would be:


 CustomProtocolClient client = new CustomProtocolClient();
 client.start();
 client.connect(new InetSocketAddress("localhost", 8087));
 

With start() a DefaultProtocolChain is build which listens and forwards Read Events to the CustomProtocol Layer. The read bytes get internally parsed to Messages. If the Message is an Message Reply the bytes get tranfered to the RemoteInputStream the client is waiting on.

If the Message is an server broadcast the bytes are handed of as an java.io.InputStream) to {@link CustomProtocolClient#service(java.io.InputStream, java.io.OutputStream)}. Therefore a user should override {@link CustomProtocolClient#service(java.io.InputStream, java.io.OutputStream)} to process any broadcast messages.

Version:
1.0
Author:
John Vieten 28.06.2008

Constructor Summary
CustomProtocolClient()
           
CustomProtocolClient(SSLConfig sslConfig)
          Configures Client to use TLS
 
Method Summary
 void addProtocolFilter(ProtocolFilter protocolFilter)
          Adds a Filter before the CustomProtocol Filters will execute.
 void addProtocolFilter(ProtocolFilter protocolFilter, boolean beforeParser)
          Users can add addtional ProtocolFilters to the Reading Chain.
 RemoteCall callRemote()
          Sends data to server.
 void connect(InetSocketAddress address)
          Connects to an Server.
 void connect(InetSocketAddress address, InetSocketAddress proxy, String userAgent, String userName, String pass)
          Used to build up a connection to a server over an proxy.
 OutputStream getOutputStream()
           
 boolean isGzip()
          If bytes send to server are gzipped
 void onMessageError(String errorMsg)
          Gets called if some protocol sepecific error happens.
 void service(InputStream inputStream, OutputStream outputStream)
          An User should override service(java.io.InputStream, java.io.OutputStream) to process bytes that are send from an Server.
 void setBytesArrivedListener(BytesTrafficListener bytesArrivedListener)
           
 void setGzip(boolean gzip)
          If bytes send to server should be gzipped
 void setIoExceptionHandler(IOExceptionHandler ioExceptionHandler)
           
 void setSession(int session)
          Every message send to an server can have a session id attached so that server can keep track of its clients.
 void setThreadSizes(int minWorkerThreads, int maxWorkerThreads)
          Set up the Grizzly Worker Threads.
 void start()
          Setups Grizzly Controller and Components and starts Grizzly.
 void stop()
          Stops the Grizzly Framework
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CustomProtocolClient

public CustomProtocolClient()

CustomProtocolClient

public CustomProtocolClient(SSLConfig sslConfig)
Configures Client to use TLS

Parameters:
sslConfig - if null no TLS will be configured
Method Detail

isGzip

public boolean isGzip()
If bytes send to server are gzipped

Returns:
zipped

setGzip

public void setGzip(boolean gzip)
If bytes send to server should be gzipped

Parameters:
gzip - true if they should

setBytesArrivedListener

public void setBytesArrivedListener(BytesTrafficListener bytesArrivedListener)

setThreadSizes

public void setThreadSizes(int minWorkerThreads,
                           int maxWorkerThreads)
Set up the Grizzly Worker Threads.

Parameters:
minWorkerThreads - min count of grizzly workers
maxWorkerThreads - max count of grizzly workers

addProtocolFilter

public void addProtocolFilter(ProtocolFilter protocolFilter,
                              boolean beforeParser)
Users can add addtional ProtocolFilters to the Reading Chain. These filter here will be called before or after the CustomProtocol Filters will execute

Parameters:
protocolFilter - Filter for chain
beforeParser - before or after

addProtocolFilter

public void addProtocolFilter(ProtocolFilter protocolFilter)
Adds a Filter before the CustomProtocol Filters will execute.

Parameters:
protocolFilter - Filter for chain

start

public void start()
           throws Exception
Setups Grizzly Controller and Components and starts Grizzly.

Specified by:
start in interface Client
Throws:
Exception - Exception

stop

public void stop()
          throws Exception
Stops the Grizzly Framework

Specified by:
stop in interface Client
Throws:
Exception - Exception

connect

public void connect(InetSocketAddress address)
             throws IOException
Connects to an Server. Method start() must have been called before.

Specified by:
connect in interface Client
Parameters:
address - host and port
Throws:
IOException - Exception

connect

public void connect(InetSocketAddress address,
                    InetSocketAddress proxy,
                    String userAgent,
                    String userName,
                    String pass)
             throws IOException
Description copied from interface: Client
Used to build up a connection to a server over an proxy. Method Client.start() must have been called before.

Specified by:
connect in interface Client
Parameters:
address - server address
proxy - address
userAgent - proxy agent identifier
userName - if proxy needs authentification (otherwise null)
pass - if proxy needs authentification (otherwise null)
Throws:
IOException

callRemote

public RemoteCall callRemote()
                      throws IOException
Sends data to server. The caller receives an InputStream with which the server streams an reply back to the client.

Specified by:
callRemote in interface Client
Returns:
an RequestReplyHolder that blocks until data arrives
Throws:
IOException - ex

setSession

public void setSession(int session)
Every message send to an server can have a session id attached so that server can keep track of its clients.

Parameters:
session - an id

getOutputStream

public OutputStream getOutputStream()
Specified by:
getOutputStream in interface Client
Returns:
OutputStream for writing to server

service

public void service(InputStream inputStream,
                    OutputStream outputStream)
An User should override service(java.io.InputStream, java.io.OutputStream) to process bytes that are send from an Server.

Specified by:
service in interface Client
Parameters:
inputStream - bytes send from server
outputStream - reply to server

onMessageError

public void onMessageError(String errorMsg)
Description copied from interface: Client
Gets called if some protocol sepecific error happens.

Specified by:
onMessageError in interface Client

setIoExceptionHandler

public void setIoExceptionHandler(IOExceptionHandler ioExceptionHandler)
Specified by:
setIoExceptionHandler in interface Client


Copyright © 2012 Oracle Corporation. All Rights Reserved.