public class MARSConnectionAdapter extends Object implements ConnectionAdapter, MARSListener
MARS
instance.Constructor and Description |
---|
MARSConnectionAdapter(MARS mars,
MARSAgent agent,
int read_buffer_limit,
InputStream in,
OutputStream out)
Creates new MARS connection adapter with specified parameters.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this connection adapter.
|
void |
marsChanged(MARSAgent agent)
|
int |
readData()
Performs single blocking read operation from connection (network socket).
|
void |
setListener(ConnectionAdapterListener listener)
Sets new listener to receive notifications about connection adapter state changes.
|
void |
start()
Starts this connection adapter.
|
int |
writeData()
Performs single blocking write operation into connection (network socket).
|
int |
writeHeartbeat()
Performs single blocking write operation into connection (network socket)
of special heartbeat packet as specified by communication protocol.
|
public MARSConnectionAdapter(MARS mars, MARSAgent agent, int read_buffer_limit, InputStream in, OutputStream out) throws UnsupportedEncodingException
mars
- instance for processing of received events or null if receive is disabled.agent
- agent for retrieving events for sending or null if send is disabled.read_buffer_limit
- limit of read buffer.in
- InputStream of the TCP/IP socket.out
- OutputStream of the TCP/IP socket.UnsupportedEncodingException
- if UTF-8 encoding cannot be found.public void marsChanged(MARSAgent agent)
MARSListener
MARSAgent
has new MARSEvent
events.
NOTE: the listener is invoked in a thread that has put new events into MARS
instance.
Therefore, proper thread-safety measures must be taken by the listener to avoid data corruption.
Also, notification processing shall be reasonably fast and shall not call external I/O operations
that may block invoking thread for a long time.
NOTE: because of parallel multi-thread processing it may happen that certain notifications are delayed and performed after all events are already retrieved and processed. That is, despite contract, it may happen that there are no new events in the specified agent when notification is actually performed.
marsChanged
in interface MARSListener
public void setListener(ConnectionAdapterListener listener)
ConnectionAdapter
setListener
in interface ConnectionAdapter
public void start()
ConnectionAdapter
start
in interface ConnectionAdapter
public void close()
ConnectionAdapter
close
in interface ConnectionAdapter
public int readData() throws Throwable
ConnectionAdapter
NOTE: This method may block either during socket read itself or during data processing.
This method shall return number of bytes read from connection. If it returns negative number, then it will be considered an EOF signal and connection will be closed.
This method generally shall pass through any exceptions to the caller. If any exception is thrown, then it will be considered an error and connection will be closed.
readData
in interface ConnectionAdapter
Throwable
public int writeData() throws Throwable
ConnectionAdapter
NOTE: This method may block only during socket write itself; it shall not block while waiting for more data availability. If this method finds that there is no data to write, it shall return immediately. When new data become available, the corresponding listener shall be notified. If this method writes only part of available data and wants to be immediately called again to write remaining data, it shall notify corresponding listener that data is available (it is allowed to do so directly from this method).
This method shall return number of bytes written into connection. If it returns negative number, then it will be considered an EOF signal and connection will be closed.
This method generally shall pass through any exceptions to the caller. If any exception is thrown, then it will be considered an error and connection will be closed.
writeData
in interface ConnectionAdapter
Throwable
public int writeHeartbeat() throws Throwable
ConnectionAdapter
ConnectionAdapter.writeData()
method
whenever it detects that no data was written into connection for specified
heartbeat period and that new heartbeat packet shall be written instead.
NOTE: This method may block only during socket write itself. Actually, this method shall not perform any other actions except writing heartbeat packet into socket. It shall not, in particular, check for business data availability to write it instead. When new data become available, the corresponding listener shall be notified.
This method shall return number of bytes written into connection. If it returns negative
number, then it will be considered an EOF signal and connection will be closed.
NOTE: If this method returns 0 bytes (i.e. no heartbeat packet was written),
then it might be immediately called again in an infinite loop fashion until heartbeat
packet is written, or business data is available, or connection is closed. Therefore,
if heartbeats are not specified by communication protocol, then heartbeat period and timeout
both shall be configured to 0 as indication that no heartbeats are required.
This method generally shall pass through any exceptions to the caller. If any exception is thrown, then it will be considered an error and connection will be closed.
writeHeartbeat
in interface ConnectionAdapter
Throwable
Copyright © 2014 Devexperts. All Rights Reserved.