Package com.metratec.lib.connection
Class AbstractTcpConnection
- java.lang.Object
-
- com.metratec.lib.connection.ICommConnection
-
- com.metratec.lib.connection.AbstractTcpConnection
-
- Direct Known Subclasses:
SslTcpConnection
,TcpConnection
public abstract class AbstractTcpConnection extends ICommConnection
Represents tcp connections- Author:
- man
-
-
Field Summary
-
Fields inherited from class com.metratec.lib.connection.ICommConnection
CONNECTION_LOST, DEVICE_IN_USE, ETHERNET_TIMEOUT, ETHERNET_UNKNOWN_HOST, NO_DEVICES_FOUND, NO_LIBRARY_FOUND, NOT_AVAILABLE, NOT_INITIALISED, RECV_TIMEOUT, SERIAL_NO_ACCESS, SERIAL_NOT_INITIALISED, SERIAL_PARAMETER_NOT_SET, SERIAL_PORT_NOT_EXIST, SET_CONFIGURATION, UNHANDLED_ERROR, USB_SET_BAUDRATE, USB_SET_DATA_CHARACTERISTICS, USB_SET_FLOWCONTROL, USB_SET_TIMEOUTS, USER_ERRORCODE_01, USER_ERRORCODE_02, USER_ERRORCODE_03, USER_ERRORCODE_04, USER_ERRORCODE_05, WRONG_PARAMETER
-
-
Constructor Summary
Constructors Constructor Description AbstractTcpConnection(java.net.Socket socket)
AbstractTcpConnection(java.net.Socket socket, boolean isServerConnection)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connect()
This method opens a connection.int
dataAvailable()
void
disconnect()
Closes the communication interfaceint
getConnectionTimeout()
java.util.Hashtable<java.lang.String,java.lang.Object>
getInfo()
Gets Device Informationsjava.io.InputStream
getInputStream()
java.lang.String
getIPAddress()
java.io.OutputStream
getOutputStream()
int
getPort()
int
getRecvTimeout()
java.net.Socket
getSocket()
boolean
isAlive(int time)
Test if the Ethernet Device is reachable.static boolean
isAlive(java.lang.String ipAdress, int time)
Test if a Ethernet Device is reachable.boolean
isConnected()
boolean
isServerConnection()
int
recv()
Receives a single byte.void
send(byte[] senddata)
Sends data to the connected devicevoid
setConnectionTimeout(int time)
Sets the waiting time for the connectionvoid
setIPAddress(java.lang.String ipaddress)
sets the used IP addressvoid
setPort(int port)
sets the used portvoid
setRecvTimeout(int timeout)
set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still validvoid
setSettings(java.util.Hashtable<java.lang.String,java.lang.String> settings)
Sets connections settings
-
-
-
Constructor Detail
-
AbstractTcpConnection
public AbstractTcpConnection(java.net.Socket socket) throws CommConnectionException
- Parameters:
socket
- the new socket- Throws:
CommConnectionException
- throwed with error codeICommConnection.NOT_INITIALISED
if the given socket is not connected
-
AbstractTcpConnection
public AbstractTcpConnection(java.net.Socket socket, boolean isServerConnection) throws CommConnectionException
- Parameters:
socket
- the new socketisServerConnection
- set to true if the connection is created by accepting a client - so no reconnect is available- Throws:
CommConnectionException
- throwed with error codeICommConnection.NOT_INITIALISED
if the given socket is not connected
-
-
Method Detail
-
disconnect
public void disconnect() throws CommConnectionException
Description copied from class:ICommConnection
Closes the communication interface- Specified by:
disconnect
in classICommConnection
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- UNHANDLED_ERROR
- for USB Connection:
- UNHANDLED_ERROR
- for TCP Connection:
-
isServerConnection
public boolean isServerConnection()
- Returns:
- the isServerConnection
-
connect
public void connect() throws CommConnectionException
Description copied from class:ICommConnection
This method opens a connection. Parameters are passed through the constructor- Specified by:
connect
in classICommConnection
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- ETHERNET_UNKNOWN_HOST
- ETHERNET_TIMEOUT
- WRONG_PARAMETER
- for USB Connection:
- DEVICE_IN_USE
- NO_LIBRARY_FOUND
- NO_DEVICES_FOUND
- USB_SET_BAUDRATE
- USB_SET_DATA_CHARACTERISTICS
- USB_SET_FLOWCONTROL
- USB_SET_TIMEOUTS
- for RS232 Connection:
- NO_LIBRARY_FOUND
- SERIAL_PORT_NOT_EXIST
- SERIAL_PARAMETER_NOT_SET
- DEVICE_IN_USE
- SERIAL_NO_ACCESS
- SERIAL_NO_INITIALISE
- for TCP Connection:
-
setIPAddress
public void setIPAddress(java.lang.String ipaddress)
sets the used IP address- Parameters:
ipaddress
- IP address like "192.168.1.1"
-
getIPAddress
public java.lang.String getIPAddress()
- Returns:
- the used IP address
-
setPort
public void setPort(int port)
sets the used port- Parameters:
port
- port
-
getPort
public int getPort()
- Returns:
- the used port
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in classICommConnection
- Returns:
- true if connected else false
-
recv
public int recv() throws CommConnectionException
Description copied from class:ICommConnection
Receives a single byte.- Specified by:
recv
in classICommConnection
- Returns:
- byte or -1 if no Data available (timeout)
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
setRecvTimeout
public void setRecvTimeout(int timeout) throws CommConnectionException
Description copied from class:ICommConnection
set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still valid- Specified by:
setRecvTimeout
in classICommConnection
- Parameters:
timeout
- time in milliseconds- Throws:
CommConnectionException
- possible Errorcodes:- for all connections
- SET_CONFIGURATION
- for all connections
-
send
public void send(byte[] senddata) throws CommConnectionException
Description copied from class:ICommConnection
Sends data to the connected device- Specified by:
send
in classICommConnection
- Parameters:
senddata
- data/command send to the connected device- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- WRONG_PARAMETER
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
dataAvailable
public int dataAvailable() throws CommConnectionException
- Specified by:
dataAvailable
in classICommConnection
- Returns:
- number of available signs
- Throws:
CommConnectionException
- possible Errorcodes:- for all connections
- CONNECTION_LOST
- NOT_INITIALISE
- for all connections
-
getInputStream
public java.io.InputStream getInputStream()
- Specified by:
getInputStream
in classICommConnection
- Returns:
- the
InputStream
of this connection
-
getOutputStream
public java.io.OutputStream getOutputStream()
- Specified by:
getOutputStream
in classICommConnection
- Returns:
- the
OutputStream
of this connection
-
getSocket
public java.net.Socket getSocket()
- Returns:
- the
Socket
-
getInfo
public java.util.Hashtable<java.lang.String,java.lang.Object> getInfo()
Description copied from class:ICommConnection
Gets Device Informations- Specified by:
getInfo
in classICommConnection
- Returns:
- Hashtable with the information
-
setSettings
public void setSettings(java.util.Hashtable<java.lang.String,java.lang.String> settings) throws java.lang.IllegalArgumentException
Description copied from class:ICommConnection
Sets connections settings- Specified by:
setSettings
in classICommConnection
- Parameters:
settings
- Hashtable with the special settings- Throws:
java.lang.IllegalArgumentException
-
getRecvTimeout
public int getRecvTimeout()
- Specified by:
getRecvTimeout
in classICommConnection
- Returns:
- the receive timeout
-
isAlive
public static boolean isAlive(java.lang.String ipAdress, int time) throws CommConnectionException
Test if a Ethernet Device is reachable.- Parameters:
ipAdress
- Device IP Addresstime
- Timeout- Returns:
- true if reachable, else false
- Throws:
CommConnectionException
- possible Errorcodes:- ETHERNET_UNKNOWN_HOST
- UNHANDLED_ERROR
-
isAlive
public boolean isAlive(int time) throws CommConnectionException
Test if the Ethernet Device is reachable.- Parameters:
time
- Timeout- Returns:
- true if reachable, else false
- Throws:
CommConnectionException
- possible Errorcodes:- ETHERNET_UNKNOWN_HOST
- UNHANDLED_ERROR
-
setConnectionTimeout
public void setConnectionTimeout(int time)
Description copied from class:ICommConnection
Sets the waiting time for the connection- Specified by:
setConnectionTimeout
in classICommConnection
- Parameters:
time
- connection timeout
-
getConnectionTimeout
public int getConnectionTimeout()
- Specified by:
getConnectionTimeout
in classICommConnection
- Returns:
- the waiting time for the connection
-
-