Class TcpSocketChannel
java.lang.Object
org.harctoolbox.harchardware.comm.TcpSocketChannel
- All Implemented Interfaces:
Closeable
,AutoCloseable
,IBytesCommand
,ICommandLineDevice
,IHarcHardware
public class TcpSocketChannel
extends Object
implements ICommandLineDevice, IBytesCommand, AutoCloseable
This a helper class, to bundle the socket operations in a unified manner.
It can be instantiated, possibly in multiple instances.
It is not meant to be inherited from, or exported.
It should therefore throw low-level exceptions, not HarcHardwareException.
-
Constructor Summary
ConstructorDescriptionTcpSocketChannel
(String hostIp, int portNumber, int timeout, boolean verbose, TcpSocketPort.ConnectionMode connectionMode) TcpSocketChannel
(InetAddress inetAddress, int portNumber, int timeout, boolean verbose, TcpSocketPort.ConnectionMode connectionMode) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
close
(boolean force) void
connect()
void
getIn()
getOut()
boolean
Returns the hardware version (not the version of the driver software).boolean
isValid()
Checks validity of an instance.void
open()
Opens the device with previously set parameters.byte[]
readBytes
(int length) Reads a line of text.readString
(boolean wait) Reads a line of text.boolean
ready()
Tells whether this stream is ready to be read.void
sendBytes
(byte[] cmd) void
sendString
(String cmd) Sends a command (a String) to the instance.void
setDebug
(int debug) Sets a debug parameter.void
setTimeout
(int timeout) Set timeout in milliseconds.void
setVerbose
(boolean verbose) Sets a verbosity flag, causing commands to be executed verbosely.
-
Constructor Details
-
TcpSocketChannel
public TcpSocketChannel(String hostIp, int portNumber, int timeout, boolean verbose, TcpSocketPort.ConnectionMode connectionMode) throws UnknownHostException - Parameters:
hostIp
-portNumber
-timeout
-verbose
-connectionMode
-- Throws:
UnknownHostException
-
TcpSocketChannel
public TcpSocketChannel(InetAddress inetAddress, int portNumber, int timeout, boolean verbose, TcpSocketPort.ConnectionMode connectionMode) - Parameters:
inetAddress
-portNumber
-timeout
-verbose
-connectionMode
-
-
-
Method Details
-
connect
- Throws:
IOException
-
close
- Parameters:
force
-- Throws:
IOException
-
getOut
-
getIn
-
getBufferedIn
-
isValid
public boolean isValid()Description copied from interface:IHarcHardware
Checks validity of an instance. Exact meaning can vary.- Specified by:
isValid
in interfaceIHarcHardware
- Returns:
- validity of the instance.
-
setTimeout
Description copied from interface:IHarcHardware
Set timeout in milliseconds. Exact semantics may be dependent on the hardware.- Specified by:
setTimeout
in interfaceIHarcHardware
- Parameters:
timeout
- Timeout in milliseconds.- Throws:
SocketException
-
setVerbose
public void setVerbose(boolean verbose) Description copied from interface:IHarcHardware
Sets a verbosity flag, causing commands to be executed verbosely. Exact semantic depends on the implementation. Typically, all communication with the device is echoed to stderr.- Specified by:
setVerbose
in interfaceIHarcHardware
- Parameters:
verbose
- on or off
-
setDebug
public void setDebug(int debug) Description copied from interface:IHarcHardware
Sets a debug parameter. Exact semantic depends on the implementation.- Specified by:
setDebug
in interfaceIHarcHardware
-
sendString
Description copied from interface:ICommandLineDevice
Sends a command (a String) to the instance.- Specified by:
sendString
in interfaceICommandLineDevice
- Parameters:
cmd
- Command string to be sent.- Throws:
IOException
-
readString
Description copied from interface:ICommandLineDevice
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.- Specified by:
readString
in interfaceICommandLineDevice
- Returns:
- A String containing the contents of the line, not including any line-termination characters, or null if no input available.
- Throws:
IOException
-
readString
Description copied from interface:ICommandLineDevice
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.- Specified by:
readString
in interfaceICommandLineDevice
- Parameters:
wait
- if true, wait until something arrives, otherwise return null if nothing there.- Returns:
- A String containing the contents of the line, not including any line-termination characters, or null if no input available.
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
sendBytes
- Specified by:
sendBytes
in interfaceIBytesCommand
- Throws:
IOException
-
readBytes
- Specified by:
readBytes
in interfaceIBytesCommand
- Throws:
IOException
-
getVersion
Description copied from interface:IHarcHardware
Returns the hardware version (not the version of the driver software). May be null if no relevant information available.- Specified by:
getVersion
in interfaceIHarcHardware
- Returns:
- Version string. Semantics can vary.
-
open
public void open()Description copied from interface:IHarcHardware
Opens the device with previously set parameters.- Specified by:
open
in interfaceIHarcHardware
-
ready
Description copied from interface:ICommandLineDevice
Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.- Specified by:
ready
in interfaceICommandLineDevice
- Returns:
- True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
- Throws:
IOException
- If an I/O error occurs
-
flushInput
- Specified by:
flushInput
in interfaceICommandLineDevice
- Throws:
IOException
-
getVerbose
public boolean getVerbose()
-