|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectecologylab.generic.Debug
ecologylab.services.distributed.impl.NIOCore
ecologylab.services.distributed.impl.NIONetworking
ecologylab.services.distributed.client.NIOClient
public class NIOClient
Services Client using NIO; a major difference with the NIO version is state tracking. Since the sending methods do not wait for the server to return. This object will listen for incoming messages from the server, and will send any messages that it recieves on its end. Since the underlying implementation is TCP/IP, messages sent should be sent in order, and the responses should match that order. Another major difference between this and the non-NIO version of ServicesClient is that it is StartAndStoppable.
| Field Summary | |
|---|---|
protected java.util.HashMap<java.lang.String,java.lang.String> |
headerMap
Stores the key-value pairings from a parsed HTTP-like header on an incoming message. |
protected java.lang.StringBuilder |
incomingMessageBuffer
Stores incoming character data until it can be parsed into an XML message and turned into a Java object. |
protected boolean |
isSending
|
protected java.nio.CharBuffer |
outgoingChars
|
protected java.lang.StringBuilder |
outgoingMessageBuffer
Stores outgoing character data for ResponseMessages. |
protected java.lang.StringBuilder |
outgoingMessageHeaderBuffer
Stores outgoing header character data. |
protected PreppedRequestPool |
pRequestPool
|
protected int |
reconnectAttempts
The number of times a call to reconnect() should attempt to contact the server before giving up and calling stop(). |
protected java.lang.StringBuilder |
requestBuffer
|
protected java.util.Queue<PreppedRequest> |
requestsQueue
|
protected MessageWithMetadataPool<ResponseMessage> |
responsePool
|
protected long |
selectInterval
selectInterval is passed to select() when it is called in the run loop. |
protected java.lang.String |
serverAddress
|
protected int |
startReadIndex
|
protected java.nio.channels.SocketChannel |
thisSocket
|
protected java.util.Map<java.lang.Long,PreppedRequest> |
unfulfilledRequests
A map that stores all the requests that have not yet gotten responses. |
protected int |
waitBetweenReconnectAttempts
The number of milliseconds to wait between reconnect attempts. |
| Fields inherited from class ecologylab.services.distributed.impl.NIONetworking |
|---|
byteBufferPool, connectionCount, objectRegistry, shuttingDown, translationSpace |
| Fields inherited from class ecologylab.services.distributed.impl.NIOCore |
|---|
portNumber, selector |
| Fields inherited from interface ecologylab.services.distributed.common.ClientConstants |
|---|
RECONNECT_ATTEMPTS, WAIT_BEWTEEN_RECONNECT_ATTEMPTS |
| Fields inherited from interface ecologylab.services.distributed.common.NetworkingConstants |
|---|
CHARACTER_ENCODING, CONTENT_LENGTH_STRING, CONTENT_LENGTH_STRING_LENGTH, DECODER, ENCODER, HTTP_HEADER_LINE_DELIMITER, HTTP_HEADER_TERMINATOR, MAX_HTTP_HEADER_LENGTH, MAX_PACKET_SIZE_BYTES, MAX_PACKET_SIZE_CHARACTERS, UNIQUE_IDENTIFIER_STRING |
| Constructor Summary | |
|---|---|
NIOClient(java.lang.String serverAddress,
int portNumber,
TranslationSpace messageSpace,
Scope<?> objectRegistry)
|
|
| Method Summary | |
|---|---|
void |
acceptFinished(java.nio.channels.SelectionKey key)
Queues a request to change key's interest operations back to READ. |
protected void |
acceptKey(java.nio.channels.SelectionKey key)
This method does nothing, as NIOClients do not accept incoming connections. |
protected void |
acceptReady(java.nio.channels.SelectionKey key)
|
protected void |
addUnfulfilledRequest(PreppedRequest req)
Hook method to allow subclasses to deal with unfulfilled requests in their own way. |
protected void |
checkAndDropIdleKeys()
Check for timeout on all allocated keys; deallocate those that are hanging around, but no longer in use. |
protected void |
clearSessionId()
|
boolean |
connect()
If this client is not already connected, connects to the specified serverAddress on the specified portNumber, then calls start() to begin listening for server responses and processing them, then sends handshake data and establishes the session id. |
boolean |
connected()
|
protected void |
connectReady(java.nio.channels.SelectionKey key)
|
protected boolean |
createConnection()
Side effect of calling start(). |
protected PreppedRequest |
dequeueRequest()
Returns the next request in the request queue and removes it from that queue. |
void |
disconnect()
|
void |
disconnect(boolean waitForResponses)
|
protected void |
enqueueRequestForSending(PreppedRequest request)
|
long |
generateUid()
Increments the internal tracker of the next UID, and returns the current one. |
java.lang.String |
getServer()
|
long |
getUidNoIncrement()
Returns the most recently used UID. |
protected void |
invalidateKey(java.nio.channels.SelectionKey key,
boolean permanent)
|
boolean |
isServerRunning()
Check to see if the server is running. |
PreppedRequest |
nonBlockingSendMessage(RequestMessage request)
Sends request, but does not wait for the response. |
protected void |
nullOut()
|
protected int |
parseHeader(int startChar,
java.lang.StringBuilder allIncomingChars)
Parses the header of an incoming set of characters (i.e. |
protected PreppedRequest |
prepareAndEnqueueRequestForSending(RequestMessage request)
Sets the UID for request (if necessary), enqueues it then registers write interest for the NIOClient's selection key and calls wakeup() on the selector. |
protected void |
processReadData(java.lang.Object readSessionId,
java.nio.channels.SelectionKey sk,
java.nio.ByteBuffer bytes,
int bytesRead)
This method is called whenever bytes have been read from a socket. |
protected void |
processResponse(ResponseMessage responseMessageToProcess)
Process a ResponseMessage received from the server in response to a previously-sent RequestMessage. |
protected void |
readFinished(java.nio.channels.SelectionKey key)
|
protected void |
reconnect()
Attempts to reconnect this client if it has been disconnected. |
protected void |
removeBadConnections(java.nio.channels.SelectionKey key)
|
protected int |
requestsRemaining()
Returns the number of requests remaining in the requests queue. |
ResponseMessage |
sendMessage(RequestMessage request)
Blocking send. |
ResponseMessage |
sendMessage(RequestMessage request,
int timeOutMillis)
Blocking send with timeout. |
void |
setReconnectAttempts(int reconnectAttempts)
|
void |
setServer(java.lang.String serverAddress)
|
void |
setWaitBetweenReconnectAttempts(int waitBetweenReconnectAttempts)
|
protected boolean |
shutdownOK()
|
void |
start()
|
void |
stop()
|
protected MessageWithMetadata<ResponseMessage> |
translateXMLStringToResponse(java.lang.String messageString,
int incomingUid)
Use the ServicesClient and its NameSpace to do the translation. |
protected void |
unableToRestorePreviousConnection(java.lang.String oldId,
java.lang.String newId)
Hook method to allow subclasses to deal with a failed restore after disconnect. |
void |
waitForConnect()
Try and connect to the server. |
protected void |
writeKey(java.nio.channels.SelectionKey key)
Writes the bytes from pendingWrites that belong to key. |
| Methods inherited from class ecologylab.services.distributed.impl.NIONetworking |
|---|
acquireByteBufferFromPool, connectionTerminated, enqueueBytesForWriting, readReady, terminationAction, writeReady |
| Methods inherited from class ecologylab.services.distributed.impl.NIOCore |
|---|
close, connectFinished, getPortNumber, invalidateKey, queueForAccept, queueForConnect, queueForRead, queueForWrite, run, setPendingInvalidate, writeFinished |
| Methods inherited from class ecologylab.generic.Debug |
|---|
classSimpleName, closeLoggingFile, debug, debug, debug, debug, debugA, debugA, debugA, debugI, debugI, debugI, error, error, getClassName, getClassName, getInteractive, getPackageName, getPackageName, getPackageName, initialize, level, level, level, logToFile, print, print, println, println, println, println, println, println, printlnA, printlnA, printlnA, printlnI, printlnI, printlnI, printlnI, setLoggingFile, show, show, superString, toggleInteractive, toString, toString, warning, warning, weird, weird |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.lang.Runnable |
|---|
run |
| Field Detail |
|---|
protected java.lang.String serverAddress
protected final java.nio.CharBuffer outgoingChars
protected final java.lang.StringBuilder requestBuffer
protected final java.lang.StringBuilder incomingMessageBuffer
protected final java.lang.StringBuilder outgoingMessageBuffer
protected final java.lang.StringBuilder outgoingMessageHeaderBuffer
protected final java.util.Queue<PreppedRequest> requestsQueue
protected final java.util.Map<java.lang.Long,PreppedRequest> unfulfilledRequests
protected int reconnectAttempts
protected int waitBetweenReconnectAttempts
protected long selectInterval
protected boolean isSending
protected int startReadIndex
protected final java.util.HashMap<java.lang.String,java.lang.String> headerMap
protected java.nio.channels.SocketChannel thisSocket
protected final PreppedRequestPool pRequestPool
protected final MessageWithMetadataPool<ResponseMessage> responsePool
| Constructor Detail |
|---|
public NIOClient(java.lang.String serverAddress,
int portNumber,
TranslationSpace messageSpace,
Scope<?> objectRegistry)
throws java.io.IOException
java.io.IOException| Method Detail |
|---|
public boolean connect()
ServicesClientBase.connect()
protected PreppedRequest prepareAndEnqueueRequestForSending(RequestMessage request)
throws XMLTranslationException
request -
XMLTranslationExceptionprotected void enqueueRequestForSending(PreppedRequest request)
public void disconnect(boolean waitForResponses)
protected boolean shutdownOK()
protected void nullOut()
public boolean connected()
protected boolean createConnection()
protected void unableToRestorePreviousConnection(java.lang.String oldId,
java.lang.String newId)
oldId - -
the previous session id.newId - -
the new session id given by the server after reconnect.
public PreppedRequest nonBlockingSendMessage(RequestMessage request)
throws java.io.IOException
request - the request to send to the server.
java.io.IOExceptionpublic ResponseMessage sendMessage(RequestMessage request)
ServicesClientBase.sendMessage(ecologylab.services.messages.RequestMessage)
public ResponseMessage sendMessage(RequestMessage request,
int timeOutMillis)
request - timeOutMillis -
public void start()
start in interface StartAndStoppablestart in class NIOCorepublic void stop()
stop in interface StartAndStoppablestop in class NIOCoreprotected PreppedRequest dequeueRequest()
protected int requestsRemaining()
protected void reconnect()
protected void addUnfulfilledRequest(PreppedRequest req)
req - public void disconnect()
public void setReconnectAttempts(int reconnectAttempts)
reconnectAttempts - the reconnectAttempts to setpublic void setWaitBetweenReconnectAttempts(int waitBetweenReconnectAttempts)
waitBetweenReconnectAttempts - the waitBetweenReconnectAttempts to setprotected void clearSessionId()
protected void acceptKey(java.nio.channels.SelectionKey key)
acceptKey in class NIONetworkingNIONetworking.acceptKey(java.nio.channels.SelectionKey)protected void checkAndDropIdleKeys()
NIOCore
checkAndDropIdleKeys in class NIOCoreNIOCore.checkAndDropIdleKeys()
protected void invalidateKey(java.nio.channels.SelectionKey key,
boolean permanent)
invalidateKey in class NIOCoreNIOCore.invalidateKey(java.nio.channels.SelectionKey, boolean)
protected void processReadData(java.lang.Object readSessionId,
java.nio.channels.SelectionKey sk,
java.nio.ByteBuffer bytes,
int bytesRead)
throws BadClientException
NIONetworking
processReadData in class NIONetworkingreadSessionId - the id being use for this session.bytes - the bytes read from the SocketChannel.bytesRead - the number of bytes in the bytes array.
BadClientException - if the client from which the bytes were read has transmitted something inappropriate, such as data too
large for a buffer or a possibly malicious message.ecologylab.services.distributed.impl.NIONetworking#processReadData(java.lang.Object,
java.nio.channels.SocketChannel, byte[], int)protected void removeBadConnections(java.nio.channels.SelectionKey key)
removeBadConnections in class NIOCoreNIOCore.removeBadConnections(java.nio.channels.SelectionKey)public long generateUid()
protected MessageWithMetadata<ResponseMessage> translateXMLStringToResponse(java.lang.String messageString,
int incomingUid)
throws XMLTranslationException
messageString -
XMLTranslationExceptionprotected void processResponse(ResponseMessage responseMessageToProcess)
responseMessageToProcess - public java.lang.String getServer()
public void setServer(java.lang.String serverAddress)
public long getUidNoIncrement()
public boolean isServerRunning()
public void waitForConnect()
protected int parseHeader(int startChar,
java.lang.StringBuilder allIncomingChars)
allIncomingChars - -
the characters read from an incoming stream.headerMap - -
the map into which all of the parsed headers will be placed.
protected void writeKey(java.nio.channels.SelectionKey key)
throws java.io.IOException
NIONetworking
writeKey in class NIONetworkingjava.io.IOExceptionNIONetworking.writeKey(java.nio.channels.SelectionKey)protected void acceptReady(java.nio.channels.SelectionKey key)
acceptReady in class NIOCoreNIOCore.acceptReady(java.nio.channels.SelectionKey)protected void connectReady(java.nio.channels.SelectionKey key)
connectReady in class NIOCoreNIOCore.connectReady(java.nio.channels.SelectionKey)protected void readFinished(java.nio.channels.SelectionKey key)
readFinished in class NIOCoreNIOCore.readFinished(java.nio.channels.SelectionKey)public void acceptFinished(java.nio.channels.SelectionKey key)
NIOCore
acceptFinished in class NIOCoreNIOCore.acceptFinished(java.nio.channels.SelectionKey)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||