Search in sources :

Example 6 with IChannelMessageEvent

use of org.eclipse.ecf.datashare.events.IChannelMessageEvent in project ecf by eclipse.

the class Activator method handleChannelEvent.

public synchronized void handleChannelEvent(IChannelEvent e) {
    if (e instanceof IChannelMessageEvent) {
        IChannelMessageEvent msgEvent = (IChannelMessageEvent) e;
        byte[] data = msgEvent.getData();
        File file = new File(getStateLocation().toFile(), "incoming.xml.zip");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data);
            List tasks = TasksUiPlugin.getTaskListManager().getTaskListWriter().readTasks(file);
            final ITask task = (ITask) tasks.get(0);
            Set repositories = TasksUiPlugin.getTaskListManager().getTaskListWriter().readRepositories(file);
            TasksUiPlugin.getRepositoryManager().insertRepositories(repositories, TasksUiPlugin.getDefault().getRepositoriesFilePath());
            IInteractionContext context = ContextCore.getContextStore().importContext(task.getHandleIdentifier(), file);
            CompoundContextActivationContributionItem.enqueue(task, context);
            IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
            Shell aShell = null;
            for (int i = 0; i < windows.length; i++) {
                aShell = windows[i].getShell();
                if (aShell != null) {
                    break;
                }
            }
            if (aShell == null) {
                return;
            }
            final Shell shell = aShell;
            UIJob job = new UIJob("Notify of incoming shared task") {

                public IStatus runInUIThread(IProgressMonitor monitor) {
                    final IncomingSharedTaskNotificationPopup popup = new IncomingSharedTaskNotificationPopup(shell);
                    popup.setTask(task);
                    popup.open();
                    new // $NON-NLS-1$
                    UIJob(// $NON-NLS-1$
                    shell.getDisplay(), // $NON-NLS-1$
                    "Close Popup Job") {

                        public IStatus runInUIThread(IProgressMonitor monitor) {
                            Shell shell = popup.getShell();
                            if (shell != null && !shell.isDisposed()) {
                                popup.close();
                            }
                            monitor.done();
                            return Status.OK_STATUS;
                        }
                    }.schedule(5000);
                    return Status.OK_STATUS;
                }
            };
            job.schedule();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            file.delete();
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ITask(org.eclipse.mylyn.tasks.core.ITask) Set(java.util.Set) Shell(org.eclipse.swt.widgets.Shell) IInteractionContext(org.eclipse.mylyn.context.core.IInteractionContext) FileOutputStream(java.io.FileOutputStream) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) IChannelMessageEvent(org.eclipse.ecf.datashare.events.IChannelMessageEvent) File(java.io.File)

Example 7 with IChannelMessageEvent

use of org.eclipse.ecf.datashare.events.IChannelMessageEvent in project ecf by eclipse.

the class NIODatashareTest method testSendAndReply.

public void testSendAndReply() throws Exception {
    final byte[] expected1 = { 1, 2, 3 };
    final byte[] expected2 = { 4, 5, 6 };
    final byte[][] actual = new byte[2][];
    channelA = createChannel(channelContainerA, new IChannelListener() {

        public void handleChannelEvent(IChannelEvent event) {
            if (event instanceof IChannelMessageEvent) {
                actual[1] = ((IChannelMessageEvent) event).getData();
                synchronized (waitObject) {
                    waitObject.notify();
                }
            }
        }
    });
    int targetPort = channelA.getPort();
    channelB = createChannel(channelContainerB, new IChannelListener() {

        public void handleChannelEvent(IChannelEvent event) {
            if (event instanceof IChannelMessageEvent) {
                actual[0] = ((IChannelMessageEvent) event).getData();
                send(channelB, containerA.getConnectedID(), expected2);
            }
        }
    });
    channelA.sendMessage(containerB.getConnectedID(), expected1);
    channelContainerB.enqueue(new InetSocketAddress(LOCALHOST, targetPort));
    waitForCompletion(5000);
    assertEquals(expected1, actual[0]);
    assertEquals(expected2, actual[1]);
}
Also used : IChannelListener(org.eclipse.ecf.datashare.IChannelListener) IChannelEvent(org.eclipse.ecf.datashare.events.IChannelEvent) InetSocketAddress(java.net.InetSocketAddress) IChannelMessageEvent(org.eclipse.ecf.datashare.events.IChannelMessageEvent)

Example 8 with IChannelMessageEvent

use of org.eclipse.ecf.datashare.events.IChannelMessageEvent in project ecf by eclipse.

the class NIODatashareTest method testOneWaySend16k.

public void testOneWaySend16k() throws Exception {
    final byte[][] actual = new byte[1][];
    channelA = createChannel(channelContainerA);
    int targetPort = channelA.getPort();
    channelB = createChannel(channelContainerB, new IChannelListener() {

        public void handleChannelEvent(IChannelEvent event) {
            if (event instanceof IChannelMessageEvent) {
                actual[0] = ((IChannelMessageEvent) event).getData();
                synchronized (waitObject) {
                    waitObject.notify();
                }
            }
        }
    });
    byte[] expected = new byte[16384];
    for (int i = 0; i < expected.length; i++) {
        expected[i] = (byte) (i % 128);
    }
    channelA.sendMessage(containerB.getConnectedID(), expected);
    channelContainerB.enqueue(new InetSocketAddress(LOCALHOST, targetPort));
    waitForCompletion(10000);
    assertEquals(expected, actual[0]);
}
Also used : IChannelListener(org.eclipse.ecf.datashare.IChannelListener) IChannelEvent(org.eclipse.ecf.datashare.events.IChannelEvent) InetSocketAddress(java.net.InetSocketAddress) IChannelMessageEvent(org.eclipse.ecf.datashare.events.IChannelMessageEvent)

Example 9 with IChannelMessageEvent

use of org.eclipse.ecf.datashare.events.IChannelMessageEvent in project ecf by eclipse.

the class NIOChannel method createMessageEvent.

/**
 * Creates and returns a message event corresponding to the specified
 * channel and the data that was read.
 *
 * @param channel
 *            the socket channel that the message was from
 * @param data
 *            the message from the remote peer
 * @return a message event describing the received message, may be
 *         <code>null</code> if the channel could not be identified
 */
private IChannelEvent createMessageEvent(SocketChannel channel, final byte[] data) {
    // search for the id of the corresponding channel
    for (Iterator it = connectedSockets.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();
        if (channel == entry.getValue()) {
            final ID fromId = (ID) entry.getKey();
            return new IChannelMessageEvent() {

                public byte[] getData() {
                    return data;
                }

                public ID getFromContainerID() {
                    return fromId;
                }

                public ID getChannelID() {
                    return id;
                }

                public String toString() {
                    StringBuffer buffer = new StringBuffer();
                    // $NON-NLS-1$
                    buffer.append("IChannelMessageEvent[");
                    // $NON-NLS-1$
                    buffer.append("container=").append(fromId);
                    // $NON-NLS-1$
                    buffer.append(",channel=").append(id);
                    // $NON-NLS-1$
                    buffer.append(",data=").append(data).append(']');
                    return buffer.toString();
                }
            };
        }
    }
    return null;
}
Also used : Iterator(java.util.Iterator) ID(org.eclipse.ecf.core.identity.ID) HashMap(java.util.HashMap) Map(java.util.Map) IChannelMessageEvent(org.eclipse.ecf.datashare.events.IChannelMessageEvent)

Example 10 with IChannelMessageEvent

use of org.eclipse.ecf.datashare.events.IChannelMessageEvent in project ecf by eclipse.

the class DatashareManagerApplication method createChannelListener.

/**
 * Create a channel listener that simply prints out messages to System.out
 */
protected IChannelListener createChannelListener() {
    return new IChannelListener() {

        public void handleChannelEvent(IChannelEvent event) {
            if (event instanceof IChannelMessageEvent) {
                IChannelMessageEvent messageEvent = (IChannelMessageEvent) event;
                // print to system out
                System.out.println("Received message from " + messageEvent.getFromContainerID().getName() + "\n\tmessage=" + new String(messageEvent.getData()));
            }
        }
    };
}
Also used : IChannelListener(org.eclipse.ecf.datashare.IChannelListener) IChannelEvent(org.eclipse.ecf.datashare.events.IChannelEvent) IChannelMessageEvent(org.eclipse.ecf.datashare.events.IChannelMessageEvent)

Aggregations

IChannelMessageEvent (org.eclipse.ecf.datashare.events.IChannelMessageEvent)10 IChannelListener (org.eclipse.ecf.datashare.IChannelListener)8 IChannelEvent (org.eclipse.ecf.datashare.events.IChannelEvent)8 InetSocketAddress (java.net.InetSocketAddress)3 HashMap (java.util.HashMap)3 ID (org.eclipse.ecf.core.identity.ID)3 IChannelContainerAdapter (org.eclipse.ecf.datashare.IChannelContainerAdapter)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BaseChannelConfig (org.eclipse.ecf.datashare.BaseChannelConfig)1 IChannel (org.eclipse.ecf.datashare.IChannel)1 IChannelConfig (org.eclipse.ecf.datashare.IChannelConfig)1 IInteractionContext (org.eclipse.mylyn.context.core.IInteractionContext)1 ITask (org.eclipse.mylyn.tasks.core.ITask)1 Shell (org.eclipse.swt.widgets.Shell)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1