Class RawChannel
A simple example on how to transfer a file from a client to the server:
Let's assume that the client has already looked up a remote object. Let's call it
server. The server needs to know that the client wants
to transfer a file. So the server should offer a method like this:
// method in the server object
public int openFileChannel() throws SimonRemoteException {
return Simon.prepareRawChannel(myRawChannelDataListener, this);
}
The result of the prepareRawChannel() call will be an integer,
called channelToken, which, as the name implies, identifies the
prepared raw channel:
// called on the client
int channelToken = server.openFileChannel();
With this channel token, the client can now open the raw channel:
// called on the client
RawChannel rawChannel = Simon.openRawChannel(channelToken, server);
Now the client can send raw data (in shape of a ByteBuffer) to the
server by calling rawChannel.write(), where the registered
myRawChannelDataListener on the server side handles the received
data. If the transmission has finished, just call
rawChannel.close(), and the listener on the remote station will
recognize it too and can finally close the file output stream.
A final word on"why do all this stress with this raw channel. Why don't send the data by just calling a server method?"
: Each method call on a remote object involves a lot of reflection and
serialization stuff, which results on a more or less time consuming behavior.
Using the raw channel mechanism, there's almost no reflection and no serialization needed, and
the data is transfered directly, without method lookups and so on. So the
performance should be better, at least with an increasing amount of data that
needs to be transferred.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRawChannel(Dispatcher dispatcher, org.apache.mina.core.session.IoSession session, int channelToken) Instantiates a new raw channel. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Signals on the remote station that the transmission has finished.booleanisClosed()Returntrue, is channel is already closed andfalseis still open and useable.voidwrite(ByteBuffer byteBuffer) Writes the given buffer (position 0 up to current position) to the server.
-
Constructor Details
-
RawChannel
protected RawChannel(Dispatcher dispatcher, org.apache.mina.core.session.IoSession session, int channelToken) Instantiates a new raw channel. This is done by callingSimon.openRawChannel(int, Object).- Parameters:
dispatcher- the relatedDispatchersession- the relatedIoSessionchannelToken- the channeltoken we got from the remote station by callingSimon.prepareRawChannel(RawChannelDataListener, Object)
-
-
Method Details
-
write
public void write(ByteBuffer byteBuffer) throws IllegalStateException, SimonRemoteException, RawChannelException Writes the given buffer (position 0 up to current position) to the server. This method can be called several times. The system ensures that the data receives in the order it was sent. Note: Each buffer has to be wrapped by the SIMON protocol. The overhead is about 9 byte per write() call. So you should not send too small packets, otherwise you have some bandwidth loss!
Note: Calling this method will block until the server received the data!- Parameters:
byteBuffer- the buffer who's content is written to the server- Throws:
IllegalStateException- if the channel is already closed.SimonRemoteExceptionRawChannelException- if the remoteRawChannelDataListener.write(ByteBuffer)has problems writing.
-
isClosed
public boolean isClosed()Returntrue, is channel is already closed andfalseis still open and useable.- Returns:
- closed-state of channel
-
close
Signals on the remote station that the transmission has finished. This also closes the raw channel. So after calling this method, each write() call fails!- Throws:
IllegalStateException- if the channel is already closed.SimonRemoteExceptionRawChannelException- if the remoteRawChannelDataListener.close()has problems closing.
-