Package fr.dyade.aaa.agent
Class NGNetwork.CnxHandler
- java.lang.Object
-
- fr.dyade.aaa.agent.NGNetwork.CnxHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classNGNetwork.CnxHandler.MessageInputStreamClass used to read messages through a stream.(package private) classNGNetwork.CnxHandler.MessageOutputStreamClass used to send messages through a stream.
-
Field Summary
Fields Modifier and Type Field Description (package private) ByteBufferbufinInformations for input(package private) ByteBufferbufout(package private) SocketChannelchannelThe communication socket channel(package private) longlasttryDate of last connection attemptprivate booleanlocalTrue if a "local" connection is in progress, a local connection is initiated from this server to the remote one (defined by theserverdescriptor.(package private) NGNetwork.CnxHandler.MessageInputStreammis(package private) NGNetwork.CnxHandler.MessageOutputStreammosprivate StringnameThe handler's name.(package private) intnbwriteInformations for output(package private) NGNetwork.MessageVectorsendlistFIFO list of all messages to be sentprivate ServerDescserverThe description of the remote server handled by this network sessionprivate shortsidDestination server id
-
Constructor Summary
Constructors Constructor Description CnxHandler(String name, short sid)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidack(int stamp)(package private) voidclose()private voiddoAck(int ack)Removes all messages in sendList previous to the ack'ed one.StringgetName()Returns this session's name.(package private) voidinit()(package private) booleanlocalStart()Its method is called by start in order to initiate a connection from the local server.private voidread()Method called each time the channel is Readable(package private) booleanremoteStart(SocketChannel channel, int boot)Its method is called by start method in order to reply to a connection request from a remote server.(package private) voidsend(Message msg)(package private) voidstart()private voidstartEnd()The session is well initialized, we can start the server thread that "listen" the connected socket.StringtoString()private voidwrite()Method called each time the channel is Writable
-
-
-
Field Detail
-
sid
private short sid
Destination server id
-
name
private String name
The handler's name.
-
local
private boolean local
True if a "local" connection is in progress, a local connection is initiated from this server to the remote one (defined by theserverdescriptor. This attribute is used to synchronize local and remote attempts to make connections.
-
server
private ServerDesc server
The description of the remote server handled by this network session
-
channel
SocketChannel channel
The communication socket channel
-
lasttry
long lasttry
Date of last connection attempt
-
nbwrite
int nbwrite
Informations for output
-
bufout
ByteBuffer bufout
-
sendlist
NGNetwork.MessageVector sendlist
FIFO list of all messages to be sent
-
bufin
ByteBuffer bufin
Informations for input
-
-
Constructor Detail
-
CnxHandler
CnxHandler(String name, short sid) throws IOException
- Throws:
IOException
-
-
Method Detail
-
init
void init() throws IOException, UnknownServerException
- Throws:
IOExceptionUnknownServerException
-
getName
public final String getName()
Returns this session's name.- Returns:
- this session's name.
-
start
void start() throws IOException- Throws:
IOException
-
localStart
boolean localStart()
Its method is called by start in order to initiate a connection from the local server. The corresponding code on remote server is the method remoteStart. Its method creates the socket, initiates the network connection, and negociates with remote server.
Its method can be overidden in order to change the connection protocol (introduces authentification by example, or uses SSL), but must respect somes conditions:- send a Boot object after the initialization of object streams (it is waiting by the wakeOnConnection thread),
- wait for an acknowledge,
- set the sock, ois and oos attributes at the end if the connection is correct.
In order to overide the protocol, we have to implements its method, with the remoteStart and the transmit methods.
- Returns:
- true if the connection is established, false otherwise.
-
remoteStart
boolean remoteStart(SocketChannel channel, int boot)
Its method is called by start method in order to reply to a connection request from a remote server. The corresponding code on remote server is in the method localStart().- Parameters:
channel- The SocketChannel to use.boot- The boot stamp.- Returns:
- true if the connection is established, false otherwise.
-
startEnd
private void startEnd() throws IOExceptionThe session is well initialized, we can start the server thread that "listen" the connected socket. If the maximum number of connections is reached, one connection from the pool is closed.- Throws:
IOException- an error occurs.
-
send
void send(Message msg) throws IOException
- Throws:
IOException
-
write
private void write() throws IOExceptionMethod called each time the channel is Writable- Throws:
IOException- an error occurs.
-
read
private void read() throws ExceptionMethod called each time the channel is Readable- Throws:
Exception- an error occurs.
-
doAck
private final void doAck(int ack) throws IOExceptionRemoves all messages in sendList previous to the ack'ed one. Be careful, messages in sendList are not always in stamp order. Its method should not be synchronized, it scans the list from begin to end, and it removes always the first element. Other methods using sendList just adds element at the end.- Parameters:
ack- the message to ack.- Throws:
IOException- an error occurs.
-
close
void close() throws IOException- Throws:
IOException
-
-