use of org.eclipse.ecf.datashare.IChannel in project ecf by eclipse.
the class ChannelTest method testBiSendMessages.
public void testBiSendMessages() throws Exception {
final IChannel ch0 = getChannelContainer(0).getChannel(channelID);
final IChannel ch1 = getChannelContainer(1).getChannel(channelID);
ID target1 = getClient(1).getConnectedID();
ID target0 = getClient(0).getConnectedID();
for (int i = 0; i < SEND_MESSAGE_COUNT; i++) {
ch0.sendMessage(target1, new String("hello. msg#=" + i).getBytes());
ch1.sendMessage(target0, new String("hello. msg#=" + i).getBytes());
}
sleep(SLEEPTIME);
}
use of org.eclipse.ecf.datashare.IChannel in project ecf by eclipse.
the class ChannelTest method testSendMessage.
public void testSendMessage() throws Exception {
final IChannel ch0 = getChannelContainer(0).getChannel(channelID);
ID target1 = getClient(1).getConnectedID();
ch0.sendMessage(target1, new String("hello").getBytes());
sleep(SLEEPTIME);
}
use of org.eclipse.ecf.datashare.IChannel in project ecf by eclipse.
the class NIODatashareContainer method createChannel.
public final IChannel createChannel(ID channelId, IChannelListener listener, Map properties) throws ECFException {
// $NON-NLS-1$
Assert.isNotNull(channelId, "Channel id cannot be null");
IChannel channel = createNIOChannel(channelId, listener, properties);
if (channel != null) {
storeChannel(channel);
fireChannelContainerActivatedEvent(channelId);
}
return channel;
}
use of org.eclipse.ecf.datashare.IChannel in project ecf by eclipse.
the class NIODatashareContainer method disconnect.
private void disconnect() {
if (connectionThread != null) {
connectionThread.interrupt();
connectionThread = null;
}
synchronized (pendingConnections) {
pendingConnections.clear();
}
synchronized (pendingSockets) {
for (int i = 0; i < pendingSockets.size(); i++) {
SocketChannel channel = (SocketChannel) pendingSockets.get(i);
Util.closeChannel(channel);
}
pendingSockets.clear();
}
synchronized (channels) {
for (Iterator it = channels.values().iterator(); it.hasNext(); ) {
final IChannel channel = (IChannel) it.next();
// dispose the channel in a SafeRunner so exceptions don't
// prevent us from disposing other channels
SafeRunner.run(new ISafeRunnable() {
public void run() throws Exception {
channel.dispose();
}
public void handleException(Throwable t) {
log(new Status(IStatus.ERROR, Util.PLUGIN_ID, "Error disposing channel: " + channel, // $NON-NLS-1$
t));
}
});
}
channels.clear();
}
}
use of org.eclipse.ecf.datashare.IChannel in project ecf by eclipse.
the class NIODatashareContainer method handshake.
/**
* Performs a handshake operation with the remote peer.
*
* @param socketChannel
* the socket channel to handshake with
* @param data
* the data sent from the channel thus far
* @throws ClassNotFoundException
* if a deserialization error occurs
* @throws IOException
* if an IO error occurs while reading or writing data
*/
private void handshake(SocketChannel socketChannel, ChannelData data) throws ClassNotFoundException, IOException {
// retrieve the data that was sent
byte[] message = data.getData();
// read in the response
ByteArrayInputStream bais = new ByteArrayInputStream(message);
ObjectInputStream ois = new ObjectInputStream(bais);
// first response should be the channel id
ID channelId = (ID) ois.readObject();
synchronized (channels) {
// retrieve the channel that corresponds to that id
IChannel channel = getChannel(channelId);
if (channel == null) {
// can't find a channel that corresponds to the id, close the
// socket
Util.closeChannel(socketChannel);
} else {
// open another ObjectInputStream because each object is
// serialized separately
ois = new ObjectInputStream(bais);
// next id is the id of the remote user
ID peerId = (ID) ois.readObject();
// store the peer id and the corresponding socket in the
// retrieved NIO channel
NIOChannel datashare = (NIOChannel) channel;
datashare.put(peerId, socketChannel);
// check if we have any bytes left to read
int available = bais.available();
if (available != 0) {
// if there are extra bytes that means this is data that
// the sender has sent to us, we must process these messages
byte[] received = new byte[available];
// copy the remaining information
System.arraycopy(message, message.length - available, received, 0, available);
// process the received data
datashare.processIncomingMessage(socketChannel, received);
}
}
}
}
Aggregations