Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java')
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java294
1 files changed, 102 insertions, 192 deletions
diff --git a/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java
index 9b81dc1e5..7321bcec8 100644
--- a/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java
+++ b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java
@@ -11,20 +11,13 @@
package org.eclipse.ecf.internal.datashare;
import java.io.IOException;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
import java.util.Map;
-import java.util.Random;
import org.eclipse.ecf.core.ISharedObject;
import org.eclipse.ecf.core.ISharedObjectConfig;
+import org.eclipse.ecf.core.SharedObjectDescription;
import org.eclipse.ecf.core.SharedObjectInitException;
import org.eclipse.ecf.core.events.ISharedObjectActivatedEvent;
-import org.eclipse.ecf.core.events.ISharedObjectContainerDepartedEvent;
-import org.eclipse.ecf.core.events.ISharedObjectContainerJoinedEvent;
-import org.eclipse.ecf.core.events.ISharedObjectMessageEvent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.Event;
@@ -33,188 +26,105 @@ import org.eclipse.ecf.core.util.Event;
*/
public class Agent implements ISharedObject {
- private final Random random = new Random();
-
- private Object sharedData;
-
- private ISharedObjectConfig config;
-
- private final HashMap elections = new HashMap();
-
- public Agent() {
- }
-
- public Agent(Object sharedData) {
- this.sharedData = sharedData;
- }
-
- public Object getSharedData() {
- return sharedData;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ecf.core.ISharedObject#init(org.eclipse.ecf.core.ISharedObjectConfig)
- */
- public synchronized void init(ISharedObjectConfig config)
- throws SharedObjectInitException {
- this.config = config;
- Map params = config.getProperties();
- if (params != null)
- sharedData = params.get("sharedData");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.core.util.Event)
- */
- public void handleEvent(Event event) {
- if (event instanceof ISharedObjectActivatedEvent) {
- ISharedObjectActivatedEvent e = (ISharedObjectActivatedEvent) event;
- if (e.getActivatedID().equals(config.getSharedObjectID()))
- handleActivated();
- } else if (event instanceof ISharedObjectContainerJoinedEvent) {
- ISharedObjectContainerJoinedEvent e = (ISharedObjectContainerJoinedEvent) event;
- handleJoined(e.getJoinedContainerID());
- } else if (event instanceof ISharedObjectContainerDepartedEvent) {
- ISharedObjectContainerDepartedEvent e = (ISharedObjectContainerDepartedEvent) event;
- handleDeparted(e.getDepartedContainerID());
- } else if (event instanceof ISharedObjectMessageEvent) {
- ISharedObjectMessageEvent e = (ISharedObjectMessageEvent) event;
- if (e.getData() instanceof Vote)
- handleVote((Vote) e.getData(), e.getRemoteContainerID());
- else if (e.getData() instanceof Elected)
- handleElected((Elected) e.getData());
- }
- }
-
- private void handleActivated() {
- // tell client we're ready
- }
-
- private void handleJoined(ID containerID) {
- long ticket = random.nextLong();
- Election election = new Election(ticket, config.getContext()
- .getGroupMemberIDs());
- synchronized (elections) {
- elections.put(containerID, election);
- }
-
- try {
- config.getContext()
- .sendMessage(null, new Vote(ticket, containerID));
- } catch (IOException e) {
- handleError(e);
- }
- }
-
- private void handleVote(Vote msg, ID containerID) {
- synchronized (elections) {
- Election election = (Election) elections.get(msg.getElectionID());
- if (election != null) {
- switch (election.processVote(msg.getTicket(), containerID)) {
- case Election.WON:
- processVictory(msg.getElectionID());
- case Election.LOST:
- elections.remove(msg.getElectionID());
- }
- }
- }
- }
-
- private void handleElected(Elected msg) {
- synchronized (elections) {
- elections.remove(msg.getElectionID());
- }
- }
-
- private void handleDeparted(ID containerID) {
- synchronized (elections) {
- for (Iterator i = elections.entrySet().iterator(); i.hasNext();) {
- Map.Entry entry = (Map.Entry) i.next();
- Election election = (Election) entry.getValue();
- switch (election.disqualify(containerID)) {
- case Election.WON:
- processVictory((ID) entry.getKey());
- case Election.LOST:
- i.remove();
- break;
- }
- }
- }
- }
-
- private void processVictory(ID electionID) {
- try {
- config.getContext().sendMessage(null, new Elected(electionID));
- } catch (IOException e) {
- handleError(e);
- }
- }
-
- private void handleError(Throwable t) {
- t.printStackTrace();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ecf.core.ISharedObject#handleEvents(org.eclipse.ecf.core.util.Event[])
- */
- public void handleEvents(Event[] events) {
- for (int i = 0; i < events.length; ++i)
- handleEvent(events[i]);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.core.identity.ID)
- */
- public synchronized void dispose(ID containerID) {
- config = null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ecf.core.ISharedObject#getAdapter(java.lang.Class)
- */
- public Object getAdapter(Class clazz) {
- return null;
- }
-
- private class Election {
-
- public static final short LOST = 0;
-
- public static final short WON = 1;
-
- public static final short UNKNOWN = 2;
-
- private final long ticket;
-
- private final HashSet members;
-
- public Election(long ticket, ID[] members) {
- this.ticket = ticket;
- this.members = new HashSet(Arrays.asList(members));
- }
-
- public short processVote(long ticket, ID containerID) {
- if (this.ticket < ticket)
- return LOST;
- else {
- members.remove(containerID);
- return members.isEmpty() ? WON : UNKNOWN;
- }
- }
-
- public short disqualify(ID containerID) {
- members.remove(containerID);
- return members.isEmpty() ? WON : UNKNOWN;
- }
- }
+ private Object sharedData;
+
+ private ISharedObjectConfig config;
+
+ private IBootstrap bootstrap;
+
+ public Agent() {
+ }
+
+ public Agent(Object sharedData) {
+ this.sharedData = sharedData;
+ }
+
+ public Object getSharedData() {
+ return sharedData;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ecf.core.ISharedObject#init(org.eclipse.ecf.core.ISharedObjectConfig)
+ */
+ public synchronized void init(ISharedObjectConfig config)
+ throws SharedObjectInitException {
+ this.config = config;
+ Map params = config.getProperties();
+ if (params != null)
+ sharedData = params.get("sharedData");
+
+ bootstrap = new LazyElectionBootstrap();
+ bootstrap.setAgent(this);
+ bootstrap.init(config);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.core.util.Event)
+ */
+ public void handleEvent(Event event) {
+ if (event instanceof ISharedObjectActivatedEvent) {
+ ISharedObjectActivatedEvent e = (ISharedObjectActivatedEvent) event;
+ if (e.getActivatedID().equals(config.getSharedObjectID()))
+ handleActivated();
+ }
+
+ bootstrap.handleEvent(event);
+ }
+
+ private void handleActivated() {
+ if (config.getHomeContainerID().equals(
+ config.getContext().getLocalContainerID()))
+ try {
+ config.getContext().sendCreate(
+ null,
+ new SharedObjectDescription(config.getSharedObjectID(),
+ getClass()));
+ } catch (IOException e) {
+ handleError(e);
+ }
+
+ // TODO tell client we're ready
+ }
+
+ public void doBootstrap(ID containerID) {
+ // TODO bootstrap the new member
+ }
+
+ private void handleError(Throwable t) {
+ t.printStackTrace();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ecf.core.ISharedObject#handleEvents(org.eclipse.ecf.core.util.Event[])
+ */
+ public void handleEvents(Event[] events) {
+ for (int i = 0; i < events.length; ++i)
+ handleEvent(events[i]);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.core.identity.ID)
+ */
+ public synchronized void dispose(ID containerID) {
+ bootstrap.dispose(containerID);
+ bootstrap = null;
+ config = null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ecf.core.ISharedObject#getAdapter(java.lang.Class)
+ */
+ public Object getAdapter(Class clazz) {
+ return null;
+ }
}

Back to the top