As a simple first look at ECF, take a look at implementing a simple "Hello World" plugin that can send messages to other instances of the example app. As a messaging application, or example is only really useful when multiple instances are running.
So what we are going to do is create an Eclipse plugin that contributes to the workbench via the actionSets extension point. We will create a simple action
that will trigger the instantiation of our Hello World client class:
public void run(IAction action) { HelloClient client = new HelloClient(); try { client.createAndConnect(IDFactory.createStringID("ecftcp://localhost:3282/server")); } catch (Exception e) { e.printStackTrace(); } }From the example source code provided, you can see the Eclipse plugin infrastructure code in the HelloPlugin and HelloAction classes. These classes will not be discussed in this tutorial as they are not relevent to ECF.
When we create our client class, HelloClient, from our action, we will provide the class with some setup information it needs to connect to an ECF server:
public class HelloClient { public static final String DEFAULT_CONTAINER_TYPE = "ecf.generic.client"; public static final String SHARED_OBJECT_ID = "myobject"; public HelloClient() { super(); } public ISharedObjectContainer createAndConnect(ID groupID) { ISharedObjectContainer container = null; try { // Create container instance via ECF container factory container = SharedObjectContainerFactory.getDefault().createSharedObjectContainer(DEFAULT_CONTAINER_TYPE); // Create ID for shared object ID sharedObjectID = IDFactory.createStringID(SHARED_OBJECT_ID); // Create actual shared object ISharedObject sharedObject = new HelloSharedObject(); // Add shared object to container container.getSharedObjectManager().addSharedObject(sharedObjectID,sharedObject,new HashMap()); // Join group identified by groupID container.joinGroup(groupID,null); } catch (Exception e) { e.printStackTrace(); } return container; } }Note for this Hello World example to be able to join a shared container, an instance of
org.eclipse.ecf.provider.app.ServerApplication
in the
org.eclipse.ecf.example.collab
needs to be running on the same machine. The configuration information we are passing to the HelloClient is the following:
client.createAndConnect(IDFactory.createStringID("ecftcp://localhost:3282/server"));
public static final String SHARED_OBJECT_ID = "myobject";
public static final String DEFAULT_CONTAINER_TYPE = "ecf.generic.client";
org.eclipse.ecf.provider.comm.tcp.Client
class in the org.eclipse.ecf.provider
plugin.public void handleEvent(Event event) { if (event instanceof ISharedObjectActivatedEvent) { System.out.println("HELLO WORLD "+getID()+". I'm activated!"); } else if (event instanceof ISharedObjectDeactivatedEvent) { System.out.println("GOODBYE from "+getID()+". I'm deactivated!"); } else if (event instanceof ISharedObjectContainerJoinedEvent) { System.out.println("Remote "+((ISharedObjectContainerJoinedEvent)event).getJoinedContainerID()+" joined!"); } else if (event instanceof ISharedObjectContainerDepartedEvent) { System.out.println("Remote "+((ISharedObjectContainerDepartedEvent)event).getDepartedContainerID()+" departed!"); } else if (event instanceof ISharedObjectMessageEvent) { ISharedObjectMessageEvent evt = (ISharedObjectMessageEvent) event; System.out.println("Got message "+evt.getData()+" from "+evt.getSenderSharedObjectID()); } }
All the "application logic" of our Hello World example is defined in the HelloSharedObject class and this really is the core value that ECF provides. To grow our application, there are specific parts in the framework for various logical aspects of communication. For example, to add another protocol, we would only need to create a new protocol provider, and our core messaging logic would remain untouched.
The source code used in this example may be found at [].