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();
}
}
}
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]);
}
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]);
}
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;
}
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()));
}
}
};
}
Aggregations