public class TCPChannel extends Thread implements Channel, InfiniteReadWriteByteBufferInterface
Channel
class. It is
responsible for maintaining information related to a SelectionKey
, and
it's main interface to agents is its sendMessage(AbstractProcess, MLMessage)
method (obligated by the Channel
interface), which is responsible for
actually sending a message through a TCP/IP channel. Objects of this class also
store a buffer and index for use by the owner socket server.
Title: CASA Agent Infrastructure
Description:
Copyright: Copyright 2003-2014, Knowledge Science Group, University of Calgary. Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The Knowledge Science Group makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.
Company: Knowledge Science Group, University of Calgary
Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
private SocketAddress |
address
The address of the remote side of the connection
|
private InfiniteReadWriteByteBuffer |
buffer
A buffer for use by the owner socker server.
|
(package private) SelectionKey |
key
The Selection associated with this channel.
|
private SocketServerTCPIP |
owner
The socket server that "owns" this object.
|
private URLDescriptor |
remoteURL
The URL of the remote side of the connection
|
private ConcurrentLinkedQueue<Pair<AbstractProcess,MLMessage>> |
writeQueue
Used to store queued outgoing messages for this connection until the socket is ready to write.
|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Constructor and Description |
---|
TCPChannel(AbstractProcess creator,
URLDescriptor url,
SocketServerInterface owner)
Create a new channel by connecting to a remote server socket.
|
TCPChannel(URLDescriptor url,
SelectionKey key,
SocketServerInterface owner,
InfiniteReadWriteByteBuffer initBuffer)
Create a new Channel from a incoming message on a server socket.
|
Modifier and Type | Method and Description |
---|---|
int |
bytesAvailableInBuffer() |
int |
getBytesExpected() |
private SelectionKey |
getConnection(URLDescriptor url)
Utility method to return any TCPChannelGlobal currently associated with the url.
|
Pair<AbstractProcess,MLMessage> |
getNextOutMessage() |
URLDescriptor |
getURL() |
byte[] |
peakBuffer(int size) |
void |
putBytesExpected(int n)
Updates the number that will be returned by
InfiniteReadWriteByteBufferInterface.getBytesExpected() . |
byte[] |
readBuffer(int size)
Returns the first size elements from the buffer and removes them
from the buffer.
|
Status |
sendMessage(AbstractProcess sender,
MLMessage message)
This method doesn't actually immediately send a message right away, but queues the message for
sending once the socket is write-ready.
|
String |
toString()
Return a string form of the object in the form:
|
private void |
waitForKey()
The key is instantiated by
SocketServerTCPIP asynchronously, so this utility method
waits around for that to happen. |
void |
writeBuffer(byte[] data)
Adds the data to the end of the buffer.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield
private SocketAddress address
private URLDescriptor remoteURL
SelectionKey key
SocketServerTCPIP
object must update it asynchronously to the
constructor. The key will have this TCPChannelGlobal object associated with it as
its SelectionKey.attachment()
object.private SocketServerTCPIP owner
private ConcurrentLinkedQueue<Pair<AbstractProcess,MLMessage>> writeQueue
private InfiniteReadWriteByteBuffer buffer
public TCPChannel(AbstractProcess creator, URLDescriptor url, SocketServerInterface owner) throws IOException
key
is not necessarily set by the end of this constructor, but it
will be set by the next cycle of SocketServerTCPIP
by SocketServerGlobal.creator
- The agent creating this new object. Used for logging.url
- The URL of the agent to connect to.owner
- The socket server that "owns" this object.IOException
- if something goes wrong.TCPChannel(URLDescriptor url, SelectionKey key, SocketServerInterface owner, InfiniteReadWriteByteBuffer initBuffer) throws IOException
key
is not necessarily set by the end of this constructor, but it
will be set by the next cycle of SocketServerTCPIP
by SocketServerGlobal.url
- The url of the agent making the connection.key
- The key for the channel just accepted by the socket server.owner
- The socker server that "owns" this object.initBuffer
- A buffer to be used as this object's channel input buffer.IOException
- if AbstractSelectableChannel.configureBlocking(boolean)
throws an exception.public Pair<AbstractProcess,MLMessage> getNextOutMessage()
public Status sendMessage(AbstractProcess sender, MLMessage message) throws ClosedChannelException
sendMessage
in interface Channel
sender
- The sender of the message. Primarily used for logging.message
- The message to send. The destination agent is specified in the RECEIVER field.ClosedChannelException
- if the channel has been closed at the other end or is otherwise disrupted.private void waitForKey()
SocketServerTCPIP
asynchronously, so this utility method
waits around for that to happen.public URLDescriptor getURL()
private SelectionKey getConnection(URLDescriptor url)
url
- public String toString()
localAddress -> remoteAddress {[ {closed} {unbound} ]}
public byte[] peakBuffer(int size)
peakBuffer
in interface InfiniteReadWriteByteBufferInterface
public int bytesAvailableInBuffer()
bytesAvailableInBuffer
in interface InfiniteReadWriteByteBufferInterface
public byte[] readBuffer(int size)
InfiniteReadWriteByteBufferInterface
readBuffer
in interface InfiniteReadWriteByteBufferInterface
size
- Specifies the number or elements to read from the buffer.public void writeBuffer(byte[] data)
InfiniteReadWriteByteBufferInterface
InfiniteReadWriteByteBufferInterface.readBuffer(int)
.writeBuffer
in interface InfiniteReadWriteByteBufferInterface
data
- The data to be written into the end of the buffer.public int getBytesExpected()
getBytesExpected
in interface InfiniteReadWriteByteBufferInterface
InfiniteReadWriteByteBufferInterface.putBytesExpected(int)
.public void putBytesExpected(int n)
InfiniteReadWriteByteBufferInterface
InfiniteReadWriteByteBufferInterface.getBytesExpected()
.putBytesExpected
in interface InfiniteReadWriteByteBufferInterface
n
- The number to be returned by subsequent calls to InfiniteReadWriteByteBufferInterface.getBytesExpected()
.