use of org.eclipse.ecf.datashare.events.IChannelEvent in project ecf by eclipse.
the class RssClientSOContainer method main.
public static final void main(String[] args) throws Exception {
// Get server identity
// String targetURL =
// "http://"+java.net.InetAddress.getLocalHost().getHostName();
String targetURL = "http://feeds.feedburner.com";
if (args.length > 0) {
targetURL = args[0];
}
final ContainerTypeDescription contd = new ContainerTypeDescription(RssContainerInstantiator.class.getName(), RssContainerInstantiator.class.getName(), null);
ContainerFactory.getDefault().addDescription(contd);
final RssClientSOContainer container = new RssClientSOContainer();
// now connect to rss service
final ID serverID = IDFactory.getDefault().createStringID(targetURL);
container.connect(serverID, null);
// get IMergeableChannelContainer adapter
final IMergeableChannelContainerAdapter channelContainer = (IMergeableChannelContainerAdapter) container.getAdapter(IMergeableChannelContainerAdapter.class);
// create channel listener
final IChannelListener listener = new IChannelListener() {
public void handleChannelEvent(IChannelEvent event) {
System.out.println("listener.handleChannelEvent(" + event + ")");
}
};
// create a new channel
final ID channelID = IDFactory.getDefault().createStringID("/reuters/worldNews/");
// ID channelID = IDFactory.getDefault().createStringID("/feed.xml");
final IMergeableChannel channel = channelContainer.createMergeableChannel(channelID, listener, new HashMap());
if (channel instanceof FeedSharedObject) {
// get remote feed (subscribed)
final RssFeed remoteFeed = ((FeedSharedObject) channel).getFeed();
// get local feed (published)
final File feedFile = new File("feed.xml");
RssFeed localFeed = RssFeed.load(feedFile);
if (localFeed == null) {
localFeed = new RssFeed(remoteFeed.getTitle(), remoteFeed.getLink(), remoteFeed.getDescription());
localFeed.setVersion(RssVersion.RSS_2_0);
}
// merge remote feed with local one
localFeed.merge(remoteFeed);
// add a new item to feed
localFeed.addItem(new RssItem("New Google Item", "This is a new item", "http://www.google.com"));
// publish updated feed
localFeed.save(feedFile);
// print item titles
final java.util.List items = localFeed.getItems();
for (int i = 0; i < items.size(); i++) {
System.out.println(" " + i + " " + ((RssItem) items.get(i)).getTitle());
}
}
// remove the channel
channelContainer.removeChannel(channelID);
// disconnect the service
container.disconnect();
container.dispose();
System.out.println("Exiting.");
}
use of org.eclipse.ecf.datashare.events.IChannelEvent in project ecf by eclipse.
the class DatashareClientApplication method createChannelListener.
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("Client received message from " + messageEvent.getFromContainerID().getName() + ": " + new String(messageEvent.getData()));
}
}
};
}
use of org.eclipse.ecf.datashare.events.IChannelEvent 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.IChannelEvent in project ecf by eclipse.
the class NIODatashareTest method testChannelConnectEvent.
public void testChannelConnectEvent() throws Exception {
final ID[] eventIds = new ID[2];
channelA = createChannel(channelContainerA, new IChannelListener() {
public void handleChannelEvent(IChannelEvent e) {
if (e instanceof IChannelConnectEvent) {
IChannelConnectEvent event = (IChannelConnectEvent) e;
eventIds[0] = event.getChannelID();
eventIds[1] = event.getTargetID();
}
}
});
containerA.connect(null, null);
assertEquals(channelA.getID(), eventIds[0]);
assertEquals(containerA.getConnectedID(), eventIds[1]);
}
use of org.eclipse.ecf.datashare.events.IChannelEvent 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]);
}
Aggregations