Class NetAdapterSim
- java.lang.Object
-
- com.dalsemi.onewire.adapter.NetAdapterSim
-
- All Implemented Interfaces:
NetAdapterConstants
,java.lang.Runnable
public class NetAdapterSim extends java.lang.Object implements java.lang.Runnable, NetAdapterConstants
NetAdapterSim is the host (or server) component for a network-based DSPortAdapter. It actually wraps the hardware DSPortAdapter and handles connections from outside sources (NetAdapter) who want to access it.
NetAdapterSim is designed to be run in a thread, waiting for incoming connections. You can run this in the same thread as your main program or you can establish the connections yourself (presumably using some higher level of security) and then call the
handleConnection(Socket)
{@see #handleConnection(Socket)}.Once a NetAdapter is connected with the host, a version check is performed followed by a simple authentication step. The authentication is dependent upon a secret shared between the NetAdapter and the host. Both will use a default value, that each will agree with if you don't provide a secret of your own. To set the secret, add the following line to your onewire.properties file:
- NetAdapter.secret="This is my custom secret"
setSecret(String)
{@see #setSecret(String)}The NetAdapter and NetAdapterSim support multicast broadcasts for automatic discovery of compatible servers on your LAN. To start the multicast listener for this NetAdapterSim, call the
createMulticastListener()
method {@see #createMulticastListener()}.For information on creating the client component, see the JavaDocs for the
NetAdapter
.- See Also:
NetAdapter
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.dalsemi.onewire.adapter.NetAdapterConstants
NetAdapterConstants.Connection
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String
execCommand
exec command, command string to start the simulatorprotected byte[]
fakeAddress
fake address, returned from all search or getAddress commandsprotected java.util.Hashtable<java.lang.Thread,com.dalsemi.onewire.adapter.NetAdapterSim.SocketHandler>
hashHandlers
Map of all Service threads created, only for multi-threadedprotected boolean
hostRunning
boolean flags for stopping the hostprotected boolean
hostStopped
boolean flags for stopping the hostprotected static long
IGNORE_TIME_MAX
protected static long
IGNORE_TIME_MIN
protected java.io.PrintWriter
logFile
Log fileprotected MulticastListener
multicastListener
Optional, listens for datagram packets from potential clientsprotected byte[]
netAdapterSecret
secret for authentication with the serverprotected java.lang.Process
process
protected java.io.BufferedReader
processError
protected java.io.OutputStreamWriter
processInput
protected java.io.BufferedReader
processOutput
protected static java.util.Random
rand
random number generator, used to issue challenges to clientprotected java.net.ServerSocket
serverSocket
The server socket for listening for connectionsprotected static boolean
SIM_DEBUG
protected boolean
singleThreaded
boolean flag to indicate whether or not the host is single or multi-threadedprotected long
timeOfLastCommand
protected int
timeoutInSeconds
timeout for socket receive, in seconds-
Fields inherited from interface com.dalsemi.onewire.adapter.NetAdapterConstants
BUFFERED_OUTPUT, CMD_BEGINEXCLUSIVE, CMD_CANBREAK, CMD_CANDELIVERPOWER, CMD_CANDELIVERSMARTPOWER, CMD_CANFLEX, CMD_CANHYPERDRIVE, CMD_CANOVERDRIVE, CMD_CANPROGRAM, CMD_CLOSECONNECTION, CMD_DATABLOCK, CMD_ENDEXCLUSIVE, CMD_EXCLUDEFAMILY, CMD_FINDFIRSTDEVICE, CMD_FINDNEXTDEVICE, CMD_GETADDRESS, CMD_GETBIT, CMD_GETBLOCK, CMD_GETBYTE, CMD_GETSPEED, CMD_PINGCONNECTION, CMD_PUTBIT, CMD_PUTBYTE, CMD_RESET, CMD_SETNORESETSEARCH, CMD_SETPOWERDURATION, CMD_SETPOWERNORMAL, CMD_SETPROGRAMPULSEDURATION, CMD_SETSEARCHALLDEVICES, CMD_SETSEARCHONLYALARMINGDEVICES, CMD_SETSPEED, CMD_STARTBREAK, CMD_STARTPOWERDELIVERY, CMD_STARTPROGRAMPULSE, CMD_TARGETALLFAMILIES, CMD_TARGETFAMILY, DEBUG, DEFAULT_MULTICAST_GROUP, DEFAULT_MULTICAST_PORT, DEFAULT_PORT, DEFAULT_SECRET, EMPTY_CONNECTION, RET_FAILURE, RET_SUCCESS, versionUID
-
-
Constructor Summary
Constructors Constructor Description NetAdapterSim(java.lang.String execCmd, byte[] fakeAddress, java.lang.String logFile, int listenPort)
Creates a single-threaded instance of a NetAdapterSim which wraps the provided adapter.NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename)
Creates an instance of a NetAdapterSim which wraps the provided adapter.NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, boolean multiThread)
Creates an (optionally multithreaded) instance of a NetAdapterSim which wraps the provided adapter.NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, int listenPort, boolean multiThread)
Creates an (optionally multi-threaded) instance of a NetAdapterSim which wraps the provided adapter.NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, java.net.ServerSocket serverSock)
Creates an instance of a NetAdapterSim which wraps the provided adapter.NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, java.net.ServerSocket serverSock, boolean multiThread)
Creates an (optionally multi-threaded) instance of a NetAdapterSim which wraps the provided adapter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
createMulticastListener()
Creates a Multicast Listener to allow NetAdapter clients to discover this NetAdapterSim automatically.void
createMulticastListener(int port)
Creates a Multicast Listener to allow NetAdapter clients to discover this NetAdapterSim automatically.void
createMulticastListener(int port, java.lang.String group)
Creates a Multicast Listener to allow NetAdapter clients to discover this NetAdapterSim automatically.void
handleConnection(java.net.Socket sock)
Handles a socket connection.static void
main(java.lang.String[] args)
A Default Main Method, for launching NetAdapterSim getting the default adapter with the OneWireAccessProvider and listening on the default port specified by DEFAULT_PORT.void
run()
Run method for threaded NetAdapterSim.void
setSecret(java.lang.String secret)
Sets the secret used for authenticating incoming client connections.void
stopHost()
Stops all threads and kills the server socket.
-
-
-
Field Detail
-
SIM_DEBUG
protected static boolean SIM_DEBUG
-
rand
protected static final java.util.Random rand
random number generator, used to issue challenges to client
-
logFile
protected java.io.PrintWriter logFile
Log file
-
execCommand
protected java.lang.String execCommand
exec command, command string to start the simulator
-
process
protected java.lang.Process process
-
processOutput
protected java.io.BufferedReader processOutput
-
processError
protected java.io.BufferedReader processError
-
processInput
protected java.io.OutputStreamWriter processInput
-
fakeAddress
protected byte[] fakeAddress
fake address, returned from all search or getAddress commands
-
serverSocket
protected java.net.ServerSocket serverSocket
The server socket for listening for connections
-
netAdapterSecret
protected byte[] netAdapterSecret
secret for authentication with the server
-
hostStopped
protected volatile boolean hostStopped
boolean flags for stopping the host
-
hostRunning
protected volatile boolean hostRunning
boolean flags for stopping the host
-
singleThreaded
protected boolean singleThreaded
boolean flag to indicate whether or not the host is single or multi-threaded
-
hashHandlers
protected java.util.Hashtable<java.lang.Thread,com.dalsemi.onewire.adapter.NetAdapterSim.SocketHandler> hashHandlers
Map of all Service threads created, only for multi-threaded
-
multicastListener
protected MulticastListener multicastListener
Optional, listens for datagram packets from potential clients
-
timeoutInSeconds
protected int timeoutInSeconds
timeout for socket receive, in seconds
-
timeOfLastCommand
protected long timeOfLastCommand
-
IGNORE_TIME_MIN
protected static final long IGNORE_TIME_MIN
- See Also:
- Constant Field Values
-
IGNORE_TIME_MAX
protected static final long IGNORE_TIME_MAX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
NetAdapterSim
public NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename) throws java.io.IOException
Creates an instance of a NetAdapterSim which wraps the provided adapter. The host listens on the default port as specified by NetAdapterConstants.
Note that the secret used for authentication is the value specified in the onewire.properties file as "NetAdapter.secret=mySecret". To set the secret to another value, use the
setSecret(String)
method.- Parameters:
adapter
- DSPortAdapter that this NetAdapterSim will proxy commands to.- Throws:
java.io.IOException
- if a network error occurs or the listen socket cannot be created on the specified port.
-
NetAdapterSim
public NetAdapterSim(java.lang.String execCmd, byte[] fakeAddress, java.lang.String logFile, int listenPort) throws java.io.IOException
Creates a single-threaded instance of a NetAdapterSim which wraps the provided adapter. The host listens on the specified port.
Note that the secret used for authentication is the value specified in the onewire.properties file as "NetAdapter.secret=mySecret". To set the secret to another value, use the
setSecret(String)
method.- Parameters:
adapter
- DSPortAdapter that this NetAdapterSim will proxy commands to.listenPort
- the TCP/IP port to listen on for incoming connections- Throws:
java.io.IOException
- if a network error occurs or the listen socket cannot be created on the specified port.
-
NetAdapterSim
public NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, boolean multiThread) throws java.io.IOException
Creates an (optionally multithreaded) instance of a NetAdapterSim which wraps the provided adapter. The listen port is set to the default port as defined in NetAdapterConstants.
Note that the secret used for authentication is the value specified in the onewire.properties file as "NetAdapter.secret=mySecret". To set the secret to another value, use the
setSecret(String)
method.- Parameters:
adapter
- DSPortAdapter that this NetAdapterSim will proxy commands to.multiThread
- if true, multiple TCP/IP connections are allowed to interact simultaneously with this adapter.- Throws:
java.io.IOException
- if a network error occurs or the listen socket cannot be created on the specified port.
-
NetAdapterSim
public NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, int listenPort, boolean multiThread) throws java.io.IOException
Creates an (optionally multi-threaded) instance of a NetAdapterSim which wraps the provided adapter. The host listens on the specified port.
Note that the secret used for authentication is the value specified in the onewire.properties file as "NetAdapter.secret=mySecret". To set the secret to another value, use the
setSecret(String)
method.- Parameters:
adapter
- DSPortAdapter that this NetAdapterSim will proxy commands to.listenPort
- the TCP/IP port to listen on for incoming connectionsmultiThread
- if true, multiple TCP/IP connections are allowed to interact simultaneously with this adapter.- Throws:
java.io.IOException
- if a network error occurs or the listen socket cannot be created on the specified port.
-
NetAdapterSim
public NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, java.net.ServerSocket serverSock) throws java.io.IOException
Creates an instance of a NetAdapterSim which wraps the provided adapter. The host listens on the default port as specified by NetAdapterConstants.
Note that the secret used for authentication is the value specified in the onewire.properties file as "NetAdapter.secret=mySecret". To set the secret to another value, use the
setSecret(String)
method.- Parameters:
adapter
- DSPortAdapter that this NetAdapterSim will proxy commands to.serverSock
- the ServerSocket for incoming connections- Throws:
java.io.IOException
- if a network error occurs or the listen socket cannot be created on the specified port.
-
NetAdapterSim
public NetAdapterSim(java.lang.String execCmd, java.lang.String logFilename, java.net.ServerSocket serverSock, boolean multiThread) throws java.io.IOException
Creates an (optionally multi-threaded) instance of a NetAdapterSim which wraps the provided adapter. The host listens on the specified port.
Note that the secret used for authentication is the value specified in the onewire.properties file as "NetAdapter.secret=mySecret". To set the secret to another value, use the
setSecret(String)
method.- Parameters:
adapter
- DSPortAdapter that this NetAdapterSim will proxy commands to.serverSock
- the ServerSocket for incoming connectionsmultiThread
- if true, multiple TCP/IP connections are allowed to interact simultaneously with this adapter.- Throws:
java.io.IOException
- if a network error occurs or the listen socket cannot be created on the specified port.
-
-
Method Detail
-
setSecret
public void setSecret(java.lang.String secret)
Sets the secret used for authenticating incoming client connections.- Parameters:
secret
- The shared secret information used for authenticating incoming client connections.
-
createMulticastListener
public void createMulticastListener() throws java.io.IOException, java.net.UnknownHostException
Creates a Multicast Listener to allow NetAdapter clients to discover this NetAdapterSim automatically. Uses defaults for Multicast group and port.- Throws:
java.io.IOException
java.net.UnknownHostException
-
createMulticastListener
public void createMulticastListener(int port) throws java.io.IOException, java.net.UnknownHostException
Creates a Multicast Listener to allow NetAdapter clients to discover this NetAdapterSim automatically. Uses default for Multicast group.- Parameters:
port
- The port the Multicast socket will receive packets on- Throws:
java.io.IOException
java.net.UnknownHostException
-
createMulticastListener
public void createMulticastListener(int port, java.lang.String group) throws java.io.IOException, java.net.UnknownHostException
Creates a Multicast Listener to allow NetAdapter clients to discover this NetAdapterSim automatically.- Parameters:
port
- The port the Multicast socket will receive packets ongroup
- The group the Multicast socket will join- Throws:
java.io.IOException
java.net.UnknownHostException
-
run
public void run()
Run method for threaded NetAdapterSim. Maintains server socket which waits for incoming connections. Whenever a connection is received launches it services the socket or (optionally) launches a new thread for servicing the socket.- Specified by:
run
in interfacejava.lang.Runnable
-
handleConnection
public void handleConnection(java.net.Socket sock) throws java.io.IOException
Handles a socket connection. If single-threaded, the connection is serviced in the current thread. If multi-threaded, a new thread is created for servicing this connection.- Throws:
java.io.IOException
-
stopHost
public void stopHost()
Stops all threads and kills the server socket.
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception
A Default Main Method, for launching NetAdapterSim getting the default adapter with the OneWireAccessProvider and listening on the default port specified by DEFAULT_PORT.- Throws:
java.lang.Exception
-
-