Search in sources :

Example 6 with IChannelListener

use of org.eclipse.ecf.datashare.IChannelListener in project ecf by eclipse.

the class RssClientSOContainer method createSharedObject.

private ISharedObject createSharedObject(SharedObjectTypeDescription sotypedesc, IChannelListener listener) throws SharedObjectCreateException {
    Class clazz;
    try {
        clazz = Class.forName(sotypedesc.getClassName());
    } catch (final ClassNotFoundException e) {
        throw new SharedObjectCreateException("No constructor for shared object of class " + sotypedesc.getClassName(), e);
    }
    Constructor cons = null;
    try {
        cons = clazz.getDeclaredConstructor(new Class[] { IChannelListener.class });
    } catch (final NoSuchMethodException e) {
        throw new SharedObjectCreateException("No constructor for shared object of class " + sotypedesc.getClassName(), e);
    }
    ISharedObject so = null;
    try {
        so = (ISharedObject) cons.newInstance(new Object[] { listener });
    } catch (final Exception e) {
        throw new SharedObjectCreateException("Cannot create instance of class " + sotypedesc.getClassName(), e);
    }
    return so;
}
Also used : IChannelListener(org.eclipse.ecf.datashare.IChannelListener) SharedObjectCreateException(org.eclipse.ecf.core.sharedobject.SharedObjectCreateException) Constructor(java.lang.reflect.Constructor) ISharedObject(org.eclipse.ecf.core.sharedobject.ISharedObject) ECFException(org.eclipse.ecf.core.util.ECFException) ConnectionCreateException(org.eclipse.ecf.provider.comm.ConnectionCreateException) ParseException(org.eclipse.higgins.rsse.parser.ParseException) IDCreateException(org.eclipse.ecf.core.identity.IDCreateException) IOException(java.io.IOException) SharedObjectCreateException(org.eclipse.ecf.core.sharedobject.SharedObjectCreateException)

Example 7 with IChannelListener

use of org.eclipse.ecf.datashare.IChannelListener 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.");
}
Also used : RssFeed(org.eclipse.higgins.rsse.RssFeed) IChannelListener(org.eclipse.ecf.datashare.IChannelListener) IChannelEvent(org.eclipse.ecf.datashare.events.IChannelEvent) HashMap(java.util.HashMap) ContainerTypeDescription(org.eclipse.ecf.core.ContainerTypeDescription) IMergeableChannel(org.eclipse.ecf.datashare.mergeable.IMergeableChannel) List(java.util.List) RssItem(org.eclipse.higgins.rsse.RssItem) ID(org.eclipse.ecf.core.identity.ID) StringID(org.eclipse.ecf.core.identity.StringID) IMergeableChannelContainerAdapter(org.eclipse.ecf.datashare.mergeable.IMergeableChannelContainerAdapter) File(java.io.File)

Example 8 with IChannelListener

use of org.eclipse.ecf.datashare.IChannelListener 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()));
            }
        }
    };
}
Also used : IChannelListener(org.eclipse.ecf.datashare.IChannelListener) IChannelEvent(org.eclipse.ecf.datashare.events.IChannelEvent) IChannelMessageEvent(org.eclipse.ecf.datashare.events.IChannelMessageEvent)

Example 9 with IChannelListener

use of org.eclipse.ecf.datashare.IChannelListener in project ecf by eclipse.

the class RssClientSOContainer method createChannel.

public IMergeableChannel createChannel(IChannelConfig newChannelConfig) throws ECFException {
    final IChannelListener listener = newChannelConfig.getListener();
    final SharedObjectDescription sodesc = new SharedObjectDescription(FeedSharedObject.class, IDFactory.getDefault().createGUID(), new HashMap());
    final SharedObjectTypeDescription sotypedesc = sodesc.getTypeDescription();
    ISharedObject sharedObject = null;
    if (sotypedesc.getName() != null) {
        sharedObject = SharedObjectFactory.getDefault().createSharedObject(sotypedesc, new Object[] { listener });
    } else {
        sharedObject = createSharedObject(sotypedesc, listener);
    }
    final IMergeableChannel channel = (IMergeableChannel) sharedObject.getAdapter(IMergeableChannel.class);
    if (channel == null) {
        throw new SharedObjectCreateException("Cannot coerce object " + channel + " to be of type IChannel");
    }
    ID newID = sodesc.getID();
    if (newID == null) {
        newID = IDFactory.getDefault().createGUID();
    }
    Map properties = sodesc.getProperties();
    if (properties == null) {
        properties = new HashMap();
    }
    // Now add channel to container...this will block
    getSharedObjectManager().addSharedObject(newID, sharedObject, properties);
    return channel;
}
Also used : SharedObjectDescription(org.eclipse.ecf.core.sharedobject.SharedObjectDescription) IChannelListener(org.eclipse.ecf.datashare.IChannelListener) HashMap(java.util.HashMap) SharedObjectCreateException(org.eclipse.ecf.core.sharedobject.SharedObjectCreateException) SharedObjectTypeDescription(org.eclipse.ecf.core.sharedobject.SharedObjectTypeDescription) ISharedObject(org.eclipse.ecf.core.sharedobject.ISharedObject) ISharedObject(org.eclipse.ecf.core.sharedobject.ISharedObject) IMergeableChannel(org.eclipse.ecf.datashare.mergeable.IMergeableChannel) ID(org.eclipse.ecf.core.identity.ID) StringID(org.eclipse.ecf.core.identity.StringID) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with IChannelListener

use of org.eclipse.ecf.datashare.IChannelListener 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)

Aggregations

IChannelListener (org.eclipse.ecf.datashare.IChannelListener)16 IChannelEvent (org.eclipse.ecf.datashare.events.IChannelEvent)12 IChannelMessageEvent (org.eclipse.ecf.datashare.events.IChannelMessageEvent)8 ID (org.eclipse.ecf.core.identity.ID)7 HashMap (java.util.HashMap)5 InetSocketAddress (java.net.InetSocketAddress)3 IChannelContainerAdapter (org.eclipse.ecf.datashare.IChannelContainerAdapter)3 StringID (org.eclipse.ecf.core.identity.StringID)2 ISharedObject (org.eclipse.ecf.core.sharedobject.ISharedObject)2 SharedObjectCreateException (org.eclipse.ecf.core.sharedobject.SharedObjectCreateException)2 IMergeableChannel (org.eclipse.ecf.datashare.mergeable.IMergeableChannel)2 File (java.io.File)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 List (java.util.List)1 Map (java.util.Map)1 ContainerTypeDescription (org.eclipse.ecf.core.ContainerTypeDescription)1 IDCreateException (org.eclipse.ecf.core.identity.IDCreateException)1 SharedObjectDescription (org.eclipse.ecf.core.sharedobject.SharedObjectDescription)1 SharedObjectTypeDescription (org.eclipse.ecf.core.sharedobject.SharedObjectTypeDescription)1