Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpnehrer2005-04-20 06:12:22 +0000
committerpnehrer2005-04-20 06:12:22 +0000
commita030e8ecc8c1ecfe82415db1fb700a104b867ce6 (patch)
treeb1883c46d8dfc7e70df677521fcfce2a66bda8fe
parent35b7ceeb7af2a494575d4342514e52edabe54625 (diff)
downloadorg.eclipse.ecf-a030e8ecc8c1ecfe82415db1fb700a104b867ce6.tar.gz
org.eclipse.ecf-a030e8ecc8c1ecfe82415db1fb700a104b867ce6.tar.xz
org.eclipse.ecf-a030e8ecc8c1ecfe82415db1fb700a104b867ce6.zip
Initial check-in.
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/.classpath7
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/.project28
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/META-INF/MANIFEST.MF12
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/build.properties4
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java220
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/DataSharePlugin.java85
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Elected.java33
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Version.java70
-rw-r--r--framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Vote.java40
9 files changed, 499 insertions, 0 deletions
diff --git a/framework/bundles/org.eclipse.ecf.datashare/.classpath b/framework/bundles/org.eclipse.ecf.datashare/.classpath
new file mode 100644
index 000000000..065ac06e1
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/framework/bundles/org.eclipse.ecf.datashare/.project b/framework/bundles/org.eclipse.ecf.datashare/.project
new file mode 100644
index 000000000..3f8351abb
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.ecf.datashare</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/framework/bundles/org.eclipse.ecf.datashare/META-INF/MANIFEST.MF b/framework/bundles/org.eclipse.ecf.datashare/META-INF/MANIFEST.MF
new file mode 100644
index 000000000..a1197e513
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/META-INF/MANIFEST.MF
@@ -0,0 +1,12 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: ECF DataShare Plug-in
+Bundle-SymbolicName: org.eclipse.ecf.datashare
+Bundle-Version: 1.0.0
+Bundle-ClassPath: datashare.jar
+Bundle-Activator: org.eclipse.ecf.internal.datashare.DataSharePlugin
+Bundle-Vendor: Eclipse.org
+Bundle-Localization: plugin
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.ecf
+Eclipse-AutoStart: true
diff --git a/framework/bundles/org.eclipse.ecf.datashare/build.properties b/framework/bundles/org.eclipse.ecf.datashare/build.properties
new file mode 100644
index 000000000..0029613a7
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/build.properties
@@ -0,0 +1,4 @@
+source.datashare.jar = src/
+output.datashare.jar = bin/
+bin.includes = META-INF/,\
+ datashare.jar
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
new file mode 100644
index 000000000..9b81dc1e5
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Agent.java
@@ -0,0 +1,220 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Peter Nehrer and Composent, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Peter Nehrer - initial API and implementation
+ *******************************************************************************/
+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.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;
+
+/**
+ * @author pnehrer
+ */
+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;
+ }
+ }
+}
diff --git a/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/DataSharePlugin.java b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/DataSharePlugin.java
new file mode 100644
index 000000000..deb5ad4bf
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/DataSharePlugin.java
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Peter Nehrer and Composent, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Peter Nehrer - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ecf.internal.datashare;
+
+import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.BundleContext;
+import java.util.*;
+
+/**
+ * The main plugin class to be used in the desktop.
+ * @author pnehrer
+ *
+ */
+public class DataSharePlugin extends Plugin {
+ //The shared instance.
+ private static DataSharePlugin plugin;
+ //Resource bundle.
+ private ResourceBundle resourceBundle;
+
+ /**
+ * The constructor.
+ */
+ public DataSharePlugin() {
+ super();
+ plugin = this;
+ }
+
+ /**
+ * This method is called upon plug-in activation
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ }
+
+ /**
+ * This method is called when the plug-in is stopped
+ */
+ public void stop(BundleContext context) throws Exception {
+ super.stop(context);
+ plugin = null;
+ resourceBundle = null;
+ }
+
+ /**
+ * Returns the shared instance.
+ */
+ public static DataSharePlugin getDefault() {
+ return plugin;
+ }
+
+ /**
+ * Returns the string from the plugin's resource bundle,
+ * or 'key' if not found.
+ */
+ public static String getResourceString(String key) {
+ ResourceBundle bundle = DataSharePlugin.getDefault().getResourceBundle();
+ try {
+ return (bundle != null) ? bundle.getString(key) : key;
+ } catch (MissingResourceException e) {
+ return key;
+ }
+ }
+
+ /**
+ * Returns the plugin's resource bundle,
+ */
+ public ResourceBundle getResourceBundle() {
+ try {
+ if (resourceBundle == null)
+ resourceBundle = ResourceBundle.getBundle("org.eclipse.ecf.internal.datashare.DataSharePluginResources");
+ } catch (MissingResourceException x) {
+ resourceBundle = null;
+ }
+ return resourceBundle;
+ }
+
+}
diff --git a/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Elected.java b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Elected.java
new file mode 100644
index 000000000..6f0483f58
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Elected.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Peter Nehrer and Composent, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Peter Nehrer - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ecf.internal.datashare;
+
+import java.io.Serializable;
+
+import org.eclipse.ecf.core.identity.ID;
+
+/**
+ * @author pnehrer
+ */
+public class Elected implements Serializable {
+
+ private static final long serialVersionUID = 3258130271390937656L;
+
+ private final ID electionID;
+
+ public Elected(ID electionID) {
+ this.electionID = electionID;
+ }
+
+ public ID getElectionID() {
+ return electionID;
+ }
+}
diff --git a/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Version.java b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Version.java
new file mode 100644
index 000000000..daad4e356
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Version.java
@@ -0,0 +1,70 @@
+/*******************************************************************************
+ * Copyright (c) 2004 Peter Nehrer and Composent, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Peter Nehrer - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ecf.internal.datashare;
+
+import java.io.Serializable;
+
+import org.eclipse.ecf.core.identity.ID;
+
+/**
+ * @author pnehrer
+ */
+public class Version implements Serializable {
+
+ private static final long serialVersionUID = 3258415036413456951L;
+
+ private final long sequence;
+
+ private final ID containerID;
+
+ public Version(ID sourceID) {
+ this(0, sourceID);
+ }
+
+ private Version(long sequence, ID sourceID) {
+ this.sequence = sequence;
+ this.containerID = sourceID;
+ }
+
+ public long getSequence() {
+ return sequence;
+ }
+
+ public ID getContainerID() {
+ return containerID;
+ }
+
+ public Version getNext(ID sourceID) {
+ return new Version(sequence + 1, sourceID);
+ }
+
+ public boolean equals(Object other) {
+ if (other instanceof Version) {
+ Version o = (Version) other;
+ return sequence == o.sequence && containerID.equals(o.containerID);
+ } else
+ return false;
+ }
+
+ public int hashCode() {
+ int c = 17;
+ c = 37 * c + (int) sequence;
+ c = 37 * c + containerID.hashCode();
+ return c;
+ }
+
+ public String toString() {
+ StringBuffer buf = new StringBuffer("Version[");
+ buf.append("sequence=").append(sequence).append(";");
+ buf.append("containerID=").append(containerID).append("]");
+ return buf.toString();
+ }
+}
diff --git a/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Vote.java b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Vote.java
new file mode 100644
index 000000000..f60f34427
--- /dev/null
+++ b/framework/bundles/org.eclipse.ecf.datashare/src/org/eclipse/ecf/internal/datashare/Vote.java
@@ -0,0 +1,40 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Peter Nehrer and Composent, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Peter Nehrer - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ecf.internal.datashare;
+
+import java.io.Serializable;
+
+import org.eclipse.ecf.core.identity.ID;
+
+/**
+ * @author pnehrer
+ */
+public class Vote implements Serializable {
+
+ private static final long serialVersionUID = 3977585813699507248L;
+
+ private final long ticket;
+
+ private final ID electionID;
+
+ public Vote(long ticket, ID electionID) {
+ this.ticket = ticket;
+ this.electionID = electionID;
+ }
+
+ public long getTicket() {
+ return ticket;
+ }
+
+ public ID getElectionID() {
+ return electionID;
+ }
+}

Back to the top