Class ReactorClient
- java.lang.Object
-
- org.ovirt.vdsm.jsonrpc.client.reactors.ReactorClient
-
- Direct Known Subclasses:
StompCommonClient
public abstract class ReactorClient extends java.lang.Object
Abstract implementation ofJsonRpcClient
which handles low level networking.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ReactorClient.MessageListener
-
Field Summary
Fields Modifier and Type Field Description static int
BUFFER_SIZE
protected java.nio.channels.SocketChannel
channel
static java.lang.String
CLIENT_CLOSED
protected java.util.List<ReactorClient.MessageListener>
eventListeners
protected java.util.concurrent.atomic.AtomicBoolean
half
protected java.nio.ByteBuffer
ibuff
protected java.nio.channels.SelectionKey
key
protected java.util.Deque<java.nio.ByteBuffer>
outbox
protected ClientPolicy
policy
protected Reactor
reactor
-
Constructor Summary
Constructors Constructor Description ReactorClient(Reactor reactor, java.lang.String hostname, int port)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addEventListener(ReactorClient.MessageListener el)
protected abstract byte[]
buildNetworkResponse(java.lang.String reason)
Builds network issue message for specific protocol.protected abstract void
clean()
Cleans internal state.java.util.concurrent.Future<java.lang.Void>
close()
protected void
closeChannel()
void
connect()
void
disconnect(java.lang.String message)
protected void
emitOnMessageReceived(byte[] message)
java.lang.String
getClientId()
int
getConnectionId()
java.lang.String
getHostname()
java.util.List<java.security.cert.Certificate>
getPeerCertificates()
protected abstract OneTimeCallback
getPostConnectCallback()
ClientPolicy
getRetryPolicy()
java.nio.channels.SelectionKey
getSelectionKey()
abstract boolean
isInInit()
boolean
isOpen()
long
now()
void
performAction()
protected abstract void
postConnect(OneTimeCallback callback)
Transport specific post connection functionality.abstract void
postDisconnect()
Cleans resources after disconnect.void
process()
protected abstract void
processIncoming()
Process incoming channel.protected void
processOutgoing()
protected abstract int
read(java.nio.ByteBuffer buff)
Reads provided buffer.void
removeEventListener(ReactorClient.MessageListener el)
protected <T> java.util.concurrent.FutureTask<T>
scheduleTask(java.util.concurrent.Callable<T> callable)
protected abstract void
sendHeartbeat()
Client sends protocol specific heartbeat messageabstract void
sendMessage(byte[] message)
Sends message using provided byte array.void
setClientPolicy(ClientPolicy policy)
abstract void
updateInterestedOps()
Updates selection key's operation set.protected void
updateLastIncomingHeartbeat()
protected void
updateLastOutgoingHeartbeat()
abstract void
validate(ClientPolicy policy)
Validates policy when it is set.protected abstract void
write(java.nio.ByteBuffer buff)
Writes provided buffer.
-
-
-
Field Detail
-
CLIENT_CLOSED
public static final java.lang.String CLIENT_CLOSED
- See Also:
- Constant Field Values
-
BUFFER_SIZE
public static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
half
protected final java.util.concurrent.atomic.AtomicBoolean half
-
policy
protected volatile ClientPolicy policy
-
eventListeners
protected final java.util.List<ReactorClient.MessageListener> eventListeners
-
reactor
protected final Reactor reactor
-
outbox
protected final java.util.Deque<java.nio.ByteBuffer> outbox
-
key
protected java.nio.channels.SelectionKey key
-
ibuff
protected java.nio.ByteBuffer ibuff
-
channel
protected java.nio.channels.SocketChannel channel
-
-
Constructor Detail
-
ReactorClient
public ReactorClient(Reactor reactor, java.lang.String hostname, int port)
-
-
Method Detail
-
getHostname
public java.lang.String getHostname()
-
getClientId
public java.lang.String getClientId()
-
setClientPolicy
public void setClientPolicy(ClientPolicy policy)
-
getRetryPolicy
public ClientPolicy getRetryPolicy()
-
connect
public void connect() throws ClientConnectionException
- Throws:
ClientConnectionException
-
getSelectionKey
public java.nio.channels.SelectionKey getSelectionKey()
-
addEventListener
public void addEventListener(ReactorClient.MessageListener el)
-
removeEventListener
public void removeEventListener(ReactorClient.MessageListener el)
-
emitOnMessageReceived
protected void emitOnMessageReceived(byte[] message)
-
disconnect
public final void disconnect(java.lang.String message)
-
close
public java.util.concurrent.Future<java.lang.Void> close()
-
scheduleTask
protected <T> java.util.concurrent.FutureTask<T> scheduleTask(java.util.concurrent.Callable<T> callable)
-
process
public void process() throws java.io.IOException, ClientConnectionException
- Throws:
java.io.IOException
ClientConnectionException
-
processIncoming
protected abstract void processIncoming() throws java.io.IOException, ClientConnectionException
Process incoming channel.- Throws:
java.io.IOException
- Thrown when reading issue occurred.ClientConnectionException
- Thrown when issues with connection.
-
updateLastIncomingHeartbeat
protected void updateLastIncomingHeartbeat()
-
updateLastOutgoingHeartbeat
protected void updateLastOutgoingHeartbeat()
-
processOutgoing
protected void processOutgoing() throws java.io.IOException
- Throws:
java.io.IOException
-
closeChannel
protected void closeChannel()
-
isOpen
public boolean isOpen()
-
getConnectionId
public int getConnectionId()
-
performAction
public void performAction() throws java.io.IOException
- Throws:
java.io.IOException
-
now
public long now()
-
sendMessage
public abstract void sendMessage(byte[] message) throws ClientConnectionException
Sends message using provided byte array.- Parameters:
message
- - content of the message to sent.- Throws:
ClientConnectionException
- when issues with connection.
-
read
protected abstract int read(java.nio.ByteBuffer buff) throws java.io.IOException
Reads provided buffer.- Parameters:
buff
- provided buffer to be read.- Returns:
- Number of bytes read.
- Throws:
java.io.IOException
- when networking issue occurs.
-
write
protected abstract void write(java.nio.ByteBuffer buff) throws java.io.IOException
Writes provided buffer.- Parameters:
buff
- provided buffer to be written.- Throws:
java.io.IOException
- when networking issue occurs.
-
postConnect
protected abstract void postConnect(OneTimeCallback callback) throws ClientConnectionException
Transport specific post connection functionality.- Parameters:
callback
- - callback which is executed after connection is estabilished.- Throws:
ClientConnectionException
- when issues with connection.
-
updateInterestedOps
public abstract void updateInterestedOps()
Updates selection key's operation set.
-
getPostConnectCallback
protected abstract OneTimeCallback getPostConnectCallback()
- Returns:
- Client specific
OneTimeCallback
or null. The callback is executed after the connection is established.
-
postDisconnect
public abstract void postDisconnect()
Cleans resources after disconnect.
-
isInInit
public abstract boolean isInInit()
- Returns:
true
when connection initialization is in progress like SSL hand shake.false
when connection is initialized.
-
buildNetworkResponse
protected abstract byte[] buildNetworkResponse(java.lang.String reason)
Builds network issue message for specific protocol.- Parameters:
reason
- why we want to build network response.- Returns:
- byte array containing response.
-
sendHeartbeat
protected abstract void sendHeartbeat()
Client sends protocol specific heartbeat message
-
validate
public abstract void validate(ClientPolicy policy)
Validates policy when it is set.- Parameters:
policy
- - validated policy
-
clean
protected abstract void clean()
Cleans internal state.
-
getPeerCertificates
public java.util.List<java.security.cert.Certificate> getPeerCertificates()
- Returns:
- the peer certificates of the current session
-
-