Package org.hcjf.io.net
Class NetServiceConsumer<S extends NetSession,D>
- java.lang.Object
-
- org.hcjf.io.net.NetServiceConsumer<S,D>
-
- All Implemented Interfaces:
ServiceConsumer
public abstract class NetServiceConsumer<S extends NetSession,D> extends java.lang.Object implements ServiceConsumer
This consumer provide an interface for the net service.- Author:
- javaito
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNetServiceConsumer.DecoupledActionInterface to decoupled the read action.
-
Constructor Summary
Constructors Constructor Description NetServiceConsumer(java.lang.Integer port, NetService.TransportLayerProtocol protocol)
-
Method Summary
Modifier and Type Method Description protected voidaddDecoupledAction(NetServiceConsumer.DecoupledAction decoupledAction)Add a new decoupled action into the queue.abstract ScheckSession(S session, D payLoad, NetPackage netPackage)Check the channel session.protected abstract Ddecode(NetPackage netPackage)This method decode the net package to obtain the implementation datavoiddecoupleIoAction(java.lang.Integer actionQueueSize, java.lang.Integer workersNumber)This method activate the decoupled io actions.abstract voiddestroySession(NetSession session)Destroy the session.protected voiddisconnect(S session, java.lang.String message)Disconnect the specific session.protected abstract byte[]encode(D payLoad)This method decode the implementation data.java.lang.StringgetName()This method return a name to identify the consumer.java.lang.IntegergetPort()Return the port of the consumer.NetService.TransportLayerProtocolgetProtocol()Return the transport layer protocol of the consumer.protected NetServicegetService()Returns the net service instance of the consumer.byte[]getShutdownFrame(S session)Returns the shutdown frame to send before the net service shutdown.protected DgetShutdownPackage(S session)Returns the shutdown package to send before the net service shutdown.java.util.Map<java.net.SocketOption,java.lang.Object>getSocketOptions()Return the socket options of the implementation.protected javax.net.ssl.SSLEnginegetSSLEngine()This method should create the ssl engine for the consumer.longgetWriteWaitForTimeout()Return the waiting time to write a package.java.lang.BooleanisDecoupledIoAction()protected voidonCheckSessionError(S session, D payLoad, NetPackage netPackage, java.lang.Throwable cause)When an exception is occurred while checking session, this method is called to process the package according the exception information.voidonConnect(NetPackage netPackage)This method abstracts the connection event to use the entities of the domain's implementation.protected voidonConnect(S session, D payLoad, NetPackage netPackage)Method that must be implemented by the custom implementation to know when a session is connectedvoidonDisconnect(NetPackage netPackage)This method abstracts the disconnection event to use the entities of the domain's implementation.protected voidonDisconnect(S session, NetPackage netPackage)Method must be implemented by the custom implementation to known when a session is disconnectedvoidonRead(NetPackage netPackage)When the net service receive data call this method to process the packageprotected voidonRead(S session, D payLoad, NetPackage netPackage)When the net service receive data, this method is called to process the package.voidonWrite(NetPackage netPackage)When the net service write data then call this method to process the package.protected voidonWrite(S session, NetPackage netPackage)When the net service write data then call this method to process the package.voidsetService(NetService service)This method ser the reference to net service, this method only can be called from the net service that will be associatedvoidsetWriteWaitForTimeout(long writeWaitForTimeout)Set the waiting time to write a package.protected voidwrite(S session, D payLoad)This method writes some data over the session indicated, this operation generate a blocking until the net service confirm that the data was written over the communication channelprotected voidwrite(S session, D payLoad, boolean waitFor)This method writes some data over the session indicated.
-
-
-
Constructor Detail
-
NetServiceConsumer
public NetServiceConsumer(java.lang.Integer port, NetService.TransportLayerProtocol protocol)
-
-
Method Detail
-
isDecoupledIoAction
public final java.lang.Boolean isDecoupledIoAction()
-
decoupleIoAction
public final void decoupleIoAction(java.lang.Integer actionQueueSize, java.lang.Integer workersNumber)This method activate the decoupled io actions.- Parameters:
actionQueueSize- Size of the actions queue.workersNumber- Number of workers to execute the actions.
-
addDecoupledAction
protected final void addDecoupledAction(NetServiceConsumer.DecoupledAction decoupledAction)
Add a new decoupled action into the queue.- Parameters:
decoupledAction- Decoupled action instance.
-
getName
public java.lang.String getName()
This method return a name to identify the consumer.- Returns:
- Consumer name.
-
getWriteWaitForTimeout
public long getWriteWaitForTimeout()
Return the waiting time to write a package.- Returns:
- Waiting time to write a apackage.
-
setWriteWaitForTimeout
public void setWriteWaitForTimeout(long writeWaitForTimeout)
Set the waiting time to write a package.- Parameters:
writeWaitForTimeout- Waiting time to write a package.
-
setService
public final void setService(NetService service)
This method ser the reference to net service, this method only can be called from the net service that will be associated- Parameters:
service- Net service that will be associated.- Throws:
java.lang.SecurityException- If the method was called from other method that not is NetService.registerConsumer().
-
getService
protected final NetService getService()
Returns the net service instance of the consumer.- Returns:
- Net service instance.
-
getPort
public final java.lang.Integer getPort()
Return the port of the consumer.- Returns:
- Port.
-
getSSLEngine
protected javax.net.ssl.SSLEngine getSSLEngine()
This method should create the ssl engine for the consumer.- Returns:
- SSL engine implementation.
-
getProtocol
public final NetService.TransportLayerProtocol getProtocol()
Return the transport layer protocol of the consumer.- Returns:
- Transport layer consumer.
-
disconnect
protected final void disconnect(S session, java.lang.String message)
Disconnect the specific session.- Parameters:
session- Net session.message- Disconnection message.
-
getShutdownFrame
public final byte[] getShutdownFrame(S session)
Returns the shutdown frame to send before the net service shutdown.- Parameters:
session- Session to create the shutdown frame.- Returns:
- Shutdown frame
-
getShutdownPackage
protected D getShutdownPackage(S session)
Returns the shutdown package to send before the net service shutdown.- Parameters:
session- Session to create the package.- Returns:
- Shutdown package.
-
write
protected final void write(S session, D payLoad) throws java.io.IOException
This method writes some data over the session indicated, this operation generate a blocking until the net service confirm that the data was written over the communication channel- Parameters:
session- Net session.payLoad- Data to be written- Throws:
java.io.IOException- Exception for the io operations
-
write
protected final void write(S session, D payLoad, boolean waitFor) throws java.io.IOException
This method writes some data over the session indicated.- Parameters:
session- Net session.payLoad- Data to be written.waitFor- If this parameter is true then the operation generate a blocking over the communication channel.- Throws:
java.io.IOException- Exception for io operations
-
onConnect
public final void onConnect(NetPackage netPackage)
This method abstracts the connection event to use the entities of the domain's implementation.- Parameters:
netPackage- Connection package.
-
onConnect
protected void onConnect(S session, D payLoad, NetPackage netPackage)
Method that must be implemented by the custom implementation to know when a session is connected- Parameters:
session- Connected session.payLoad- Decoded package payload.netPackage- Original package.
-
onDisconnect
public final void onDisconnect(NetPackage netPackage)
This method abstracts the disconnection event to use the entities of the domain's implementation.- Parameters:
netPackage- Disconnection package.
-
onDisconnect
protected void onDisconnect(S session, NetPackage netPackage)
Method must be implemented by the custom implementation to known when a session is disconnected- Parameters:
session- Disconnected session.netPackage- Original package.
-
onRead
public final void onRead(NetPackage netPackage)
When the net service receive data call this method to process the package- Parameters:
netPackage- Net package.
-
onRead
protected void onRead(S session, D payLoad, NetPackage netPackage)
When the net service receive data, this method is called to process the package.- Parameters:
session- Net session.payLoad- Net package decodednetPackage- Net package.
-
onCheckSessionError
protected void onCheckSessionError(S session, D payLoad, NetPackage netPackage, java.lang.Throwable cause)
When an exception is occurred while checking session, this method is called to process the package according the exception information.- Parameters:
session- Net session.payLoad- Net package decodednetPackage- Net package.cause- Error cause.
-
onWrite
public final void onWrite(NetPackage netPackage)
When the net service write data then call this method to process the package.- Parameters:
netPackage- Net package.
-
onWrite
protected void onWrite(S session, NetPackage netPackage)
When the net service write data then call this method to process the package.- Parameters:
session- Net session.netPackage- Net package.
-
encode
protected abstract byte[] encode(D payLoad)
This method decode the implementation data.- Parameters:
payLoad- Implementation data.- Returns:
- Implementation data encoded.
-
decode
protected abstract D decode(NetPackage netPackage)
This method decode the net package to obtain the implementation data- Parameters:
netPackage- Net package.- Returns:
- Return the implementation data.
-
destroySession
public abstract void destroySession(NetSession session)
Destroy the session.- Parameters:
session- Net session to be destroyed
-
checkSession
public abstract S checkSession(S session, D payLoad, NetPackage netPackage)
Check the channel session.- Parameters:
session- Created session.payLoad- Decoded package.netPackage- Net package.- Returns:
- Updated session.
-
getSocketOptions
public java.util.Map<java.net.SocketOption,java.lang.Object> getSocketOptions()
Return the socket options of the implementation.- Returns:
- Socket options.
-
-