de.knipp.rritoolkit.tcp
Class TcpChannel

java.lang.Object
  extended by de.knipp.rritoolkit.tcp.TcpChannel
All Implemented Interfaces:
RegistryChannel, java.lang.Comparable<TcpChannel>

public class TcpChannel
extends java.lang.Object
implements RegistryChannel, java.lang.Comparable<TcpChannel>

implementation of the RegistryChannel interface for SSL TCP connections

Version:
$Revision: 14936 $
Author:
Klaus Malorny

Nested Class Summary
private static class TcpChannel.IdleMonitor
          class that monitors idle connections and closes them if need be
 
Field Summary
private  boolean closed
          flag whether connection has been closed
private static int connectionCount
          the number of connections ever allocated
private  long idleCloseTime
          the time the connection shall be closed
private  long idleTimeout
          the idle timeout
private  int instanceID
          an instance ID
private  java.io.DataInputStream inStream
          the input stream
private static long MIN_REMAIN_TIME
          the minimum time to remain until the channel is reported as closed
private static TcpChannel.IdleMonitor monitor
          the thread that closes timed-out channels
private  java.io.DataOutputStream outStream
          the output stream
private  TcpParams params
          the parameters
private static java.util.SortedSet<TcpChannel> pendingClose
          set of connections that are to be closed
private  java.net.Socket socket
          the socket
 
Constructor Summary
TcpChannel(TcpParams theParams, java.net.Socket theSocket)
          constructor
 
Method Summary
private static void checkMonitor()
          check whether the idle monitor has been started.
 void close()
          close the channel.
 int compareTo(TcpChannel obj)
          compare this instance with another one.
 boolean isClosed()
          return whether the channel has been closed
 byte[] receive()
          wait for a response from the registry.
 void send(byte[] data, boolean isXml)
          send the given message to the registry.
private  void setCloseTime(long closeTime)
          update the close time
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

params

private TcpParams params
the parameters


socket

private java.net.Socket socket
the socket


inStream

private final java.io.DataInputStream inStream
the input stream


outStream

private final java.io.DataOutputStream outStream
the output stream


closed

private boolean closed
flag whether connection has been closed


idleTimeout

private long idleTimeout
the idle timeout


idleCloseTime

private long idleCloseTime
the time the connection shall be closed


instanceID

private int instanceID
an instance ID


connectionCount

private static int connectionCount
the number of connections ever allocated


pendingClose

private static final java.util.SortedSet<TcpChannel> pendingClose
set of connections that are to be closed


monitor

private static TcpChannel.IdleMonitor monitor
the thread that closes timed-out channels


MIN_REMAIN_TIME

private static final long MIN_REMAIN_TIME
the minimum time to remain until the channel is reported as closed

See Also:
Constant Field Values
Constructor Detail

TcpChannel

public TcpChannel(TcpParams theParams,
                  java.net.Socket theSocket)
           throws java.io.IOException
constructor

Parameters:
theParams - the parameters
theSocket - the socket
Throws:
java.io.IOException - if the connection could not be established
Method Detail

checkMonitor

private static void checkMonitor()
check whether the idle monitor has been started. If not, it is started


setCloseTime

private void setCloseTime(long closeTime)
update the close time

Parameters:
closeTime - the new close time, 0 to disable the automatic closing

compareTo

public int compareTo(TcpChannel obj)
compare this instance with another one. The comparison is based on the idleCloseTime. If two times are accidentially equal, the internal instance ID is used for the comparison to make sure that two instances are only reported as equal if they are identical. Otherwise the one instance would kick out the other instance in the set.

Specified by:
compareTo in interface java.lang.Comparable<TcpChannel>
Parameters:
obj - the other object
Returns:
the result (-1 if this instance has an earlier idleCloseTime)

send

public void send(byte[] data,
                 boolean isXml)
          throws java.io.IOException
send the given message to the registry. As the transport mechanism may need to know whether the data represents the XML or key-value form of the request, it must be specified. If the channel is already closed, an exception is thrown.

Specified by:
send in interface RegistryChannel
Parameters:
data - the data representing the message
isXml - true if the message uses the XML encoding, false if the message uses key-value pairs.
Throws:
java.io.IOException - if the transmission failed

receive

public byte[] receive()
               throws java.io.IOException
wait for a response from the registry.

Specified by:
receive in interface RegistryChannel
Returns:
the message that has been received
Throws:
java.io.IOException - if the connection has been closed either before or during the call.

isClosed

public boolean isClosed()
return whether the channel has been closed

Specified by:
isClosed in interface RegistryChannel
Returns:
true if closed

close

public void close()
close the channel. Closing an already closed channel should be ignored

Specified by:
close in interface RegistryChannel


Copyright © 2005-2011 Knipp Medien und Kommunikation GmbH