Search in sources :

Example 11 with IChannel

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);
}
Also used : IChannel(org.eclipse.ecf.datashare.IChannel) ID(org.eclipse.ecf.core.identity.ID)

Example 12 with IChannel

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);
}
Also used : IChannel(org.eclipse.ecf.datashare.IChannel) ID(org.eclipse.ecf.core.identity.ID)

Example 13 with IChannel

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;
}
Also used : IChannel(org.eclipse.ecf.datashare.IChannel)

Example 14 with IChannel

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();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) SocketChannel(java.nio.channels.SocketChannel) IChannel(org.eclipse.ecf.datashare.IChannel) Iterator(java.util.Iterator) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ECFException(org.eclipse.ecf.core.util.ECFException) IOException(java.io.IOException)

Example 15 with IChannel

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);
            }
        }
    }
}
Also used : IChannel(org.eclipse.ecf.datashare.IChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) ID(org.eclipse.ecf.core.identity.ID) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

IChannel (org.eclipse.ecf.datashare.IChannel)15 ID (org.eclipse.ecf.core.identity.ID)8 IChannelContainerAdapter (org.eclipse.ecf.datashare.IChannelContainerAdapter)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Iterator (java.util.Iterator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 SocketChannel (java.nio.channels.SocketChannel)1 HashMap (java.util.HashMap)1 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 Job (org.eclipse.core.runtime.jobs.Job)1 IDCreateException (org.eclipse.ecf.core.identity.IDCreateException)1 ECFException (org.eclipse.ecf.core.util.ECFException)1 IChannelListener (org.eclipse.ecf.datashare.IChannelListener)1