public class TcpClient
extends java.lang.Object
implements java.lang.Runnable
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.BlockingQueue<Message> |
completedMsgQueue
When a client writes a message to the server, once the response has been
retrieved, this queue is updated with the Message instance describing the
pair.
|
private java.lang.String |
hostname |
private boolean |
keepAlive
When this is set to false, if the thread containing this client instance is
interrupted, the thread will die.
|
private int |
portNum |
static int |
RTLSDRD_DEFAULT_TCP_PORT_NUMBER
Default port number used by rtlsdrd
|
private java.util.concurrent.BlockingQueue<Message> |
sendMsgQueue
New messages to be sent to the server are placed in this queue.
|
private static java.lang.String |
SOCKET_READER_THREAD_NAME
Name to be given to the thread associated with the socketWriter instance.
|
private static java.lang.String |
SOCKET_WRITER_THREAD_NAME
Name to be given to the thread associated with the socketWriter instance.
|
private TcpSocketReader |
socketReader
The instance of TcpSocketReader associated with this client, which is used to
handle socket reads.
|
private java.lang.Thread |
socketReaderThread
The thread which the socketReader instance is running in
|
private TcpSocketWriter |
socketWriter
The instance of TcpSocketWriter associated with this client, which is used to
handle socket writes.
|
private java.lang.Thread |
socketWriterThread
The thread which the socketWriter instance is running in.
|
private static java.lang.String |
TCP_CLIENT_DEFAULT_THREAD_NAME
The name that the user should call this client
|
private java.net.Socket |
tcpSocket
Socket used to interact w/ the server
|
private static int |
THREAD_JOIN_WAIT_TIME_MS
When terminate() is called, the TcpSocketWriter and TcpSocketReader threads
will have this parameter as their join() timeout.
|
Constructor and Description |
---|
TcpClient(java.lang.String hostname,
int portNum)
Instantiates a new TcpClient
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addToCompletedMsgQueue(Message completedMsg)
Add a completed Message instance (i.e.
|
Message |
getCompletedMessage()
THIS CALL BLOCKS on completedMsgQueue.
|
int |
getCompletedMessageQueueLength() |
static java.lang.String |
getDefaultThreadName() |
(package private) TcpSocketReader |
getTcpSocketReader()
Returns this TcpClient's socketReader member.
|
private void |
initMembers() |
private void |
logMsg(java.lang.String msg)
Logs/outputs a message.
|
void |
run()
When this TcpClient is run as a thread, this function will be called.
|
void |
sendToServer(java.lang.String msg)
Writes a message to the server.
|
(package private) Message |
takeMsgFromSendMsgQueue()
Calls take() on the send message queue, causing the calling thread to block.
|
void |
terminate()
Stops the TcpClient thread as well as the TcpSocketWriter and TcpSocketReader
threads.
|
private void |
terminateSocketReaderThread()
Requests that the socketReader thread die.
|
private void |
terminateSocketWriterThread()
Requests that the socketWriter thread die.
|
public static final int RTLSDRD_DEFAULT_TCP_PORT_NUMBER
private static final java.lang.String TCP_CLIENT_DEFAULT_THREAD_NAME
private static final int THREAD_JOIN_WAIT_TIME_MS
private static final java.lang.String SOCKET_WRITER_THREAD_NAME
private static final java.lang.String SOCKET_READER_THREAD_NAME
private java.net.Socket tcpSocket
private TcpSocketWriter socketWriter
private TcpSocketReader socketReader
private java.lang.Thread socketWriterThread
private java.lang.Thread socketReaderThread
private final java.util.concurrent.BlockingQueue<Message> completedMsgQueue
private final java.util.concurrent.BlockingQueue<Message> sendMsgQueue
private boolean keepAlive
private final java.lang.String hostname
private final int portNum
public TcpClient(java.lang.String hostname, int portNum) throws java.io.IOException
hostname
- The hostname of the server (can be an IP or textual name to be
resolved w/ DNS)portNum
- The port number to connect to on the serverjava.net.UnknownHostException
- If the host's IP address couldn't be determinedjava.io.IOException
- If there was an error creating the socket, which can also occur
during the instantiation of socketWriter and socketReaderprivate void initMembers() throws java.io.IOException
java.io.IOException
public void run()
run
in interface java.lang.Runnable
private void logMsg(java.lang.String msg)
msg
- The msg to logpublic void sendToServer(java.lang.String msg)
msg
- The message to send to the serverprivate void terminateSocketWriterThread() throws java.lang.InterruptedException
java.lang.InterruptedException
- If this thread is interrupted when calling join()private void terminateSocketReaderThread() throws java.lang.InterruptedException
java.lang.InterruptedException
- If this thread is interrupted when calling join()public void terminate() throws java.io.IOException, java.lang.InterruptedException
java.io.IOException
- If an IO error occurs when closing tcpSocketjava.lang.InterruptedException
- If this thread is interrupted when terminating either of the
reader or writer threads.void addToCompletedMsgQueue(Message completedMsg)
completedMsg
- A completed Message to add to completedMsgQueuepublic Message getCompletedMessage() throws java.lang.InterruptedException
java.lang.InterruptedException
- If the calling thread is interrupted when waiting on
completedMsgQueue.TcpSocketReader getTcpSocketReader()
public int getCompletedMessageQueueLength()
public static java.lang.String getDefaultThreadName()
Message takeMsgFromSendMsgQueue() throws java.lang.InterruptedException
java.lang.InterruptedException
- If the take() operation was interrupted.