This class implements a message store that can be used to provide a blocking
mechanism for transport protocol messages.
| Method from com.sshtools.j2ssh.transport.SshMessageStore Detail: |
public void addMessage(byte[] msgdata) throws MessageNotRegisteredException, InvalidMessageException {
Integer messageId = new Integer(msgdata[5]);
if (!isRegisteredMessage(messageId)) {
throw new MessageNotRegisteredException(messageId);
}
Class cls = (Class) register.get(SshMessage.getMessageId(msgdata));
try {
SshMessage msg = (SshMessage) cls.newInstance();
msg.fromByteArray(new ByteArrayReader(msgdata));
addMessage(msg);
} catch (IllegalAccessException iae) {
throw new InvalidMessageException(
"Illegal access for implementation class " + cls.getName());
} catch (InstantiationException ie) {
throw new InvalidMessageException("Instantiation failed for class " +
cls.getName());
}
}
|
public synchronized void addMessage(SshMessage msg) throws MessageNotRegisteredException {
// Add the message
messages.add(messages.size(), msg);
synchronized (listeners) {
if (listeners.size() > 0) {
for (Iterator it = listeners.iterator(); it.hasNext();) {
((SshMessageListener) it.next()).messageReceived(msg);
}
}
}
// Notify the threads
notifyAll();
}
|
public void addMessageListener(SshMessageListener listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
|
public synchronized void breakWaiting() {
notifyAll();
}
|
public synchronized void close() {
isClosed = true;
// We need to notify all anyway as if there are messages still available
// it should not affect the waiting threads as they are waiting for their
// own messages to be received because non were avaialable in the first place
//if (messages.size()< =0) {
notifyAll();
//}
}
|
public SshMessage createMessage(byte[] msgdata) throws MessageNotRegisteredException, InvalidMessageException {
Integer messageId = SshMessage.getMessageId(msgdata);
if (!isRegisteredMessage(messageId)) {
throw new MessageNotRegisteredException(messageId);
}
Class cls = (Class) register.get(SshMessage.getMessageId(msgdata));
try {
SshMessage msg = (SshMessage) cls.newInstance();
msg.fromByteArray(new ByteArrayReader(msgdata));
return msg;
} catch (IllegalAccessException iae) {
throw new InvalidMessageException(
"Illegal access for implementation class " + cls.getName());
} catch (InstantiationException ie) {
throw new InvalidMessageException("Instantiation failed for class " +
cls.getName());
}
}
|
public synchronized SshMessage getMessage(int[] messageIdFilter) throws InterruptedException, MessageStoreEOFException {
try {
return getMessage(messageIdFilter, 0);
} catch (MessageNotAvailableException e) {
// This should never happen but throw just in case
throw new MessageStoreEOFException();
}
}
Get a message from the store. This method will block until a message
with an id matching the supplied filter arrives, or the message store
closes. The message is removed from the store.
|
public synchronized SshMessage getMessage(int messageId) throws InterruptedException, MessageStoreEOFException {
try {
return getMessage(messageId, 0);
} catch (MessageNotAvailableException e) {
// This should never happen by throw jsut in case
throw new MessageStoreEOFException();
}
}
Get a message from the store. This method will block until a message
with an id matching the supplied id arrives, or the message store
closes. The message is removed from the store.
|
public synchronized SshMessage getMessage(int[] messageIdFilter,
int timeout) throws MessageNotAvailableException, InterruptedException, MessageStoreEOFException {
if ((messages.size() < = 0) && isClosed) {
throw new MessageStoreEOFException();
}
if (messageIdFilter == null) {
return nextMessage();
}
SshMessage msg;
boolean firstPass = true;
if (timeout < 0) {
timeout = 0;
}
while ((messages.size() > 0) || !isClosed) {
// lookup the message
msg = lookupMessage(messageIdFilter, true);
if (msg != null) {
return msg;
} else {
// If this is the second time and there's no message, then throw
if (!firstPass && (timeout > 0)) {
throw new MessageNotAvailableException();
}
}
// Now wait
if (!isClosed) {
wait((timeout == 0) ? interrupt : timeout);
}
firstPass = false;
}
throw new MessageStoreEOFException();
}
Get a message from the store. This method will block until a message
with an id matching the supplied filter arrives, the specified timeout
is reached or the message store closes. The message is removed from the
store.
|
public synchronized SshMessage getMessage(int messageId,
int timeout) throws MessageNotAvailableException, InterruptedException, MessageStoreEOFException {
singleIdFilter[0] = messageId;
return getMessage(singleIdFilter, timeout);
}
Get a message from the store. This method will block until a message
with an id matching the supplied id arrives,the specified timeout is
reached or the message store closes. The message will be removed from
the store.
|
public Object[] getRegisteredMessageIds() {
return register.keySet().toArray();
}
|
public boolean hasMessages() {
return messages.size() > 0;
}
|
public boolean isClosed() {
return isClosed;
}
|
public boolean isRegisteredMessage(Integer messageId) {
return register.containsKey(messageId);
}
|
public synchronized SshMessage nextMessage() throws InterruptedException, MessageStoreEOFException {
if ((messages.size() < = 0) && isClosed) {
throw new MessageStoreEOFException();
}
// If there are no messages available then wait untill there are.
while ((messages.size() < = 0) && !isClosed) {
wait(interrupt);
}
if (messages.size() > 0) {
return (SshMessage) messages.remove(0);
} else {
throw new MessageStoreEOFException();
}
}
Get the next message in the store or wait until a new message arrives.
The message is removed from the store.
|
public synchronized SshMessage peekMessage(int[] messageIdFilter) throws MessageNotAvailableException, InterruptedException, MessageStoreEOFException {
return peekMessage(messageIdFilter, 0);
}
|
public synchronized SshMessage peekMessage(int messageId) throws MessageNotAvailableException, InterruptedException, MessageStoreEOFException {
return peekMessage(messageId, 0);
}
|
public synchronized SshMessage peekMessage(int[] messageIdFilter,
int timeout) throws MessageNotAvailableException, InterruptedException, MessageStoreEOFException {
SshMessage msg;
// Do a straight lookup
msg = lookupMessage(messageIdFilter, false);
if (msg != null) {
return msg;
}
// If were willing to wait the wait and look again
if (timeout > 0) {
if (log.isDebugEnabled()) {
log.debug("No message so waiting for " +
String.valueOf(timeout) + " milliseconds");
}
wait(timeout);
msg = lookupMessage(messageIdFilter, false);
if (msg != null) {
return msg;
}
}
// Nothing even after a wait so throw the relevant exception
if (isClosed) {
throw new MessageStoreEOFException();
} else {
throw new MessageNotAvailableException();
}
}
Get a message from the store without removing it; only blocking for the
number of milliseconds specified in the timeout field. If timeout is
zero, the method will not block.
|
public synchronized SshMessage peekMessage(int messageId,
int timeout) throws MessageNotAvailableException, InterruptedException, MessageStoreEOFException {
singleIdFilter[0] = messageId;
return peekMessage(singleIdFilter, timeout);
}
Get a message from the store without removing it, only blocking for the
number of milliseconds specified in the timeout field.
|
public void registerMessage(int messageId,
Class implementor) {
Integer id = new Integer(messageId);
register.put(id, implementor);
}
|
public synchronized void removeMessage(SshMessage msg) {
messages.remove(msg);
}
|
public int size() {
return messages.size();
}
|