[P2play-commit] SF.net SVN: p2play: [50] P2PlayLegacy
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2007-03-20 12:25:27
|
Revision: 50
http://p2play.svn.sourceforge.net/p2play/?rev=50&view=rev
Author: tisoft
Date: 2007-03-20 05:17:08 -0700 (Tue, 20 Mar 2007)
Log Message:
-----------
Initial import.
Added Paths:
-----------
P2PlayLegacy/.classpath
P2PlayLegacy/.project
P2PlayLegacy/readme.txt
P2PlayLegacy/src/
P2PlayLegacy/src/de/
P2PlayLegacy/src/de/upb/
P2PlayLegacy/src/de/upb/mmog/
P2PlayLegacy/src/de/upb/mmog/net/
P2PlayLegacy/src/de/upb/mmog/net/data/
P2PlayLegacy/src/de/upb/mmog/net/data/AbstractData.java
P2PlayLegacy/src/de/upb/mmog/net/data/DataContainerImpl.java
P2PlayLegacy/src/de/upb/mmog/net/data/DataPastContent.java
P2PlayLegacy/src/de/upb/mmog/net/data/DummyDataManager.java
P2PlayLegacy/src/de/upb/mmog/net/data/GameStateData.java
P2PlayLegacy/src/de/upb/mmog/net/data/PastDataManager.java
P2PlayLegacy/src/de/upb/mmog/net/data/StringData.java
P2PlayLegacy/src/de/upb/mmog/net/region/
P2PlayLegacy/src/de/upb/mmog/net/region/impl/
P2PlayLegacy/src/de/upb/mmog/net/region/impl/DummyClient.java
P2PlayLegacy/src/de/upb/mmog/net/region/impl/GameRegion.java
P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedDynamicRegionManager.java
P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedStaticNetworkRegionManager.java
P2PlayLegacy/src/de/upb/mmog/net/region/impl/ObjectGameAction.java
P2PlayLegacy/src/de/upb/mmog/net/region/impl/PeerToPeerRegionManager.java
P2PlayLegacy/src/de/upb/mmog/net/resource/
P2PlayLegacy/src/de/upb/mmog/net/resource/PastResourceManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/
P2PlayLegacy/src/de/upb/mmog/net/security/AccessControlManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/KeyManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/SignableKeyPair.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/
P2PlayLegacy/src/de/upb/mmog/net/security/impl/CAKeyManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/DummyAccessControlManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/DummyKeyManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/ScribeKeyManager.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/ScribeKeyRequestMessage.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/ScribeKeyResponseMessage.java
P2PlayLegacy/src/de/upb/mmog/net/security/impl/SecurityUtils.java
P2PlayLegacy/src/de/upb/mmog/net/test/
P2PlayLegacy/src/de/upb/mmog/net/test/DummyGameState.java
P2PlayLegacy/src/de/upb/mmog/net/test/debug/
P2PlayLegacy/src/de/upb/mmog/net/test/debug/DebugClient.java
P2PlayLegacy/src/de/upb/mmog/net/test/debug/PastDebugDialog.java
P2PlayLegacy/src/de/upb/mmog/net/test/gui/
P2PlayLegacy/src/de/upb/mmog/net/test/gui/logon/
P2PlayLegacy/src/de/upb/mmog/net/test/gui/logon/ConnectDialog.java
P2PlayLegacy/src/de/upb/mmog/net/test/gui/past/
P2PlayLegacy/src/de/upb/mmog/net/test/gui/past/PastInsertRetrieveDialog.java
P2PlayLegacy/src/de/upb/mmog/net/test/gui/region/
P2PlayLegacy/src/de/upb/mmog/net/test/gui/region/RegionDialog.java
P2PlayLegacy/src/de/upb/mmog/net/test/gui/util/
P2PlayLegacy/src/de/upb/mmog/net/test/gui/util/HistoryManager.java
P2PlayLegacy/src/de/upb/mmog/net/test/gui/util/JComboBoxWithHistory.java
P2PlayLegacy/src/de/upb/mmog/net/test/vfs/
P2PlayLegacy/src/de/upb/mmog/net/test/vfs/Test.java
Added: P2PlayLegacy/.classpath
===================================================================
--- P2PlayLegacy/.classpath (rev 0)
+++ P2PlayLegacy/.classpath 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry combineaccessrules="false" kind="src" path="/P2Play"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Added: P2PlayLegacy/.project
===================================================================
--- P2PlayLegacy/.project (rev 0)
+++ P2PlayLegacy/.project 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>P2PlayLegacy</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
Added: P2PlayLegacy/readme.txt
===================================================================
--- P2PlayLegacy/readme.txt (rev 0)
+++ P2PlayLegacy/readme.txt 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,3 @@
+This project contains the old source files from the project group at University of Paderborn. They will be moved to the other projects, and this project will be removed then.
+
+They are all licensed under the GNU Lesser General Public License.
\ No newline at end of file
Added: P2PlayLegacy/src/de/upb/mmog/net/data/AbstractData.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/AbstractData.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/AbstractData.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,77 @@
+package de.upb.mmog.net.data;
+
+import org.p2play.data.Data;
+import org.p2play.security.Signature;
+
+/**
+ * Abstract super-class for Data Objects.
+ * implements Signature handling
+ */
+public abstract class AbstractData implements Data {
+
+ private String identifier;
+
+ private Signature[] signatures;
+
+
+
+ /**
+ * Constructor for AbstractData.
+ * @param identifier String
+ */
+ public AbstractData(String identifier) {
+ super();
+ this.identifier = identifier;
+ signatures=new Signature[0];
+ }
+
+ /**
+ * Method getIdentifier.
+ * @return String
+ * @see org.p2play.data.Data#getIdentifier()
+ */
+ public final String getIdentifier() {
+ return identifier;
+ }
+
+ /**
+ * Method getVersion.
+ * @return int
+ * @see org.p2play.data.Data#getVersion()
+ */
+ public abstract int getVersion();
+
+ /**
+ * Method getSignatures.
+ * @return Signature[]
+ * @see de.upb.mmog.net.security.Signed#getSignatures()
+ */
+ public final Signature[] getSignatures() {
+ return signatures;
+ }
+
+ /**
+ * Method getData.
+ * @return byte[]
+ * @see de.upb.mmog.net.security.Signed#getData()
+ */
+ public abstract byte[] getData();
+
+ /**
+ * Method addSignature.
+ * @param signature Signature
+ * @see de.upb.mmog.net.security.Signable#addSignature(Signature)
+ */
+ public final void addSignature(Signature signature) {
+ synchronized (signatures) {
+ Signature[] newSignatures = new Signature[signatures.length + 1];
+ System
+ .arraycopy(signatures, 0, newSignatures, 0,
+ signatures.length);
+ newSignatures[signatures.length] = signature;
+
+ signatures=newSignatures;
+ }
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/data/DataContainerImpl.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/DataContainerImpl.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/DataContainerImpl.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,120 @@
+package de.upb.mmog.net.data;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.p2play.data.Data;
+import org.p2play.data.DataContainer;
+import org.p2play.data.DataListener;
+
+import rice.Continuation;
+
+public class DataContainerImpl implements Continuation, DataContainer {
+ private Data data;
+
+ private String name;
+
+ private List<DataListener> listeners = new LinkedList<DataListener>();
+
+ private Exception exception;
+
+ public DataContainerImpl(String name) {
+ this.name = name;
+ }
+
+ DataContainerImpl(Data data) {
+ this(data.getIdentifier());
+ this.data = data;
+ }
+
+ /* (non-Javadoc)
+ * @see org.p2play.data.impl.DataContainer#getData()
+ */
+ public Data getData() {
+ if (exception != null) {
+ throw new RuntimeException(exception);
+ }
+ return data;
+ }
+
+ /* (non-Javadoc)
+ * @see org.p2play.data.impl.DataContainer#getName()
+ */
+ public String getName() {
+ return name;
+ }
+
+ void setData(Data data) {
+ this.data = data;
+ this.exception = null;
+ notifyListeners();
+ }
+
+ void setException(Exception exception) {
+ this.data = null;
+ this.exception = exception;
+ notifyListeners();
+ }
+
+ /* (non-Javadoc)
+ * @see org.p2play.data.impl.DataContainer#getException()
+ */
+ public Exception getException() {
+ return exception;
+ }
+
+ /* (non-Javadoc)
+ * @see org.p2play.data.impl.DataContainer#isReady()
+ */
+ public boolean isReady() {
+ return data != null || exception != null;
+ }
+
+ public void receiveResult(Object result) {
+ if (result != null) {
+ setData(((DataPastContent) result).getData());
+ } else {
+ setData(null);
+ }
+ }
+
+ public void receiveException(Exception result) {
+ setException(result);
+ result.printStackTrace();
+ }
+
+ /* (non-Javadoc)
+ * @see org.p2play.data.impl.DataContainer#removeDataListener(org.p2play.data.DataListener)
+ */
+ public void removeDataListener(DataListener listener) {
+ synchronized (listeners) {
+ listeners.remove(listener);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.p2play.data.impl.DataContainer#addDataListener(org.p2play.data.DataListener)
+ */
+ public void addDataListener(DataListener listener) {
+ synchronized (listeners) {
+ listeners.add(listener);
+ }
+ }
+
+ private void notifyListeners() {
+ synchronized (listeners) {
+
+ Iterator<DataListener> iterator = listeners.iterator();
+ while (iterator.hasNext()) {
+ DataListener listener = iterator.next();
+
+ if (exception != null) {
+ listener.dataException(this);
+ } else {
+ listener.dataUpdated(this);
+ }
+ }
+ }
+ }
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/data/DataPastContent.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/DataPastContent.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/DataPastContent.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,173 @@
+package de.upb.mmog.net.data;
+
+import org.p2play.data.Data;
+
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastContent;
+import rice.p2p.past.PastContentHandle;
+import rice.p2p.past.PastException;
+import rice.p2p.past.gc.GCPast;
+import rice.p2p.past.gc.GCPastContent;
+import rice.p2p.past.gc.GCPastContentHandle;
+import rice.p2p.past.gc.GCPastMetadata;
+import rice.p2p.scribe.ScribeContent;
+import de.upb.mmog.net.security.AccessControlManager;
+
+/**
+ */
+public class DataPastContent implements GCPastContent, ScribeContent {
+
+ private Id id;
+
+ private Data data;
+
+ private long version = 0;
+
+ private long expiration;
+
+ /**
+ * Constructor for DataPastContent.
+ * @param id Id
+ * @param data Data
+ * @param version long
+ * @param expiration long
+ */
+ public DataPastContent(Id id, Data data, long version,
+ long expiration) {
+ super();
+ this.id = id;
+ this.data=data;
+ this.version = version;
+ this.expiration = expiration;
+ }
+
+ /**
+ * Constructor for DataPastContent.
+ * @param id Id
+ * @param data Data
+ */
+ public DataPastContent(Id id, Data data) {
+ this(id, data, 0,
+ GCPast.INFINITY_EXPIRATION);
+ }
+
+ /**
+ * Method getData.
+ * @return Data
+ */
+ public Data getData() {
+ return data;
+ }
+
+ /**
+ * Method getVersion.
+ * @return long
+ * @see rice.p2p.past.gc.GCPastContent#getVersion()
+ */
+ public long getVersion() {
+ return version;
+ }
+
+ /**
+ * Method getHandle.
+ * @param local GCPast
+ * @param expiration long
+ * @return GCPastContentHandle
+ * @see rice.p2p.past.gc.GCPastContent#getHandle(GCPast, long)
+ */
+ public GCPastContentHandle getHandle(final GCPast local, final long expiration) {
+ final NodeHandle handle=local.getLocalNodeHandle();
+ return new GCPastContentHandle() {
+ public Id getId() {
+ return DataPastContent.this.getId();
+ }
+ public NodeHandle getNodeHandle() {
+ return handle;
+ }
+ public long getExpiration() {
+ return expiration;
+ }
+ public long getVersion() {
+ return DataPastContent.this.getVersion();
+ }
+ };
+ }
+
+ /**
+ * Method getMetadata.
+ * @param expiration long
+ * @return GCPastMetadata
+ * @see rice.p2p.past.gc.GCPastContent#getMetadata(long)
+ */
+ public GCPastMetadata getMetadata(long expiration) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ * Method checkInsert.
+ * @param id Id
+ * @param existingContent PastContent
+ * @return PastContent
+ * @throws PastException
+ * @see rice.p2p.past.PastContent#checkInsert(Id, PastContent)
+ */
+ public PastContent checkInsert(Id id, PastContent existingContent)
+ throws PastException {
+
+ if(AccessControlManager.getAccessControlManager().checkAccess(getData(),getData().getIdentifier())){
+ return this;
+ }
+ else
+ {
+ throw new PastException("Access denied for "+data.getIdentifier());
+ }
+ }
+
+ /**
+ * Method getHandle.
+ * @param local Past
+ * @return PastContentHandle
+ * @see rice.p2p.past.PastContent#getHandle(Past)
+ */
+ public PastContentHandle getHandle(final Past local) {
+ final NodeHandle handle=local.getLocalNodeHandle();
+ return new PastContentHandle() {
+ public Id getId() {
+ return DataPastContent.this.getId();
+ }
+ public NodeHandle getNodeHandle() {
+ return handle;
+ }
+ };
+ }
+
+ /**
+ * Method getId.
+ * @return Id
+ * @see rice.p2p.past.PastContent#getId()
+ */
+ public Id getId() {
+ return id;
+ }
+
+ /**
+ * Method isMutable.
+ * @return boolean
+ * @see rice.p2p.past.PastContent#isMutable()
+ */
+ public boolean isMutable() {
+ return true;
+ }
+
+ /**
+ * Method getExpiration.
+ * @return long
+ */
+ public long getExpiration() {
+ return expiration;
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/data/DummyDataManager.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/DummyDataManager.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/DummyDataManager.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,47 @@
+package de.upb.mmog.net.data;
+
+import java.util.HashMap;
+
+import org.p2play.data.Data;
+import org.p2play.data.DataContainer;
+import org.p2play.data.DataListener;
+import org.p2play.data.DataManager;
+
+/**
+ * A dummy Data Manager, that stores all data in a local HashMap.
+ */
+public class DummyDataManager implements DataManager {
+
+ private static HashMap<String,DataContainerImpl> hashMap=new HashMap<String,DataContainerImpl>();
+
+ /**
+ * Method loadData.
+ * @param identifier String
+ * @return Data
+ * @see org.p2play.data.DataManager#loadData(String)
+ */
+ public DataContainer loadData(String identifier) {
+ return hashMap.get(identifier);
+ }
+
+ /**
+ * Method storeData.
+ * @param data Data
+ * @see org.p2play.data.DataManager#storeData(Data)
+ */
+ public void storeData(Data data) {
+ DataContainerImpl dataContainerImpl=hashMap.get(data.getIdentifier());
+ if(dataContainerImpl==null){
+ dataContainerImpl=new DataContainerImpl(data);
+ hashMap.put(data.getIdentifier(), dataContainerImpl);
+ }
+ else{
+ dataContainerImpl.setData(data);
+ }
+ }
+
+ public DataContainer loadData(String identifier, DataListener listener) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/data/GameStateData.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/GameStateData.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/GameStateData.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,40 @@
+package de.upb.mmog.net.data;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+
+import org.p2play.region.GameState;
+
+public class GameStateData extends AbstractData{
+
+ private GameState gameState;
+
+ public GameStateData(String identifier, GameState gameState) {
+ super(identifier);
+ this.gameState=gameState;
+ }
+
+
+ @Override
+ public int getVersion() {
+ return 0;
+ }
+
+ @Override
+ public byte[] getData() {
+ ByteArrayOutputStream bos=new ByteArrayOutputStream();
+
+ try {
+ new ObjectOutputStream(bos).writeObject(gameState);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return bos.toByteArray();
+ }
+
+ public GameState getGameState() {
+ return gameState;
+ }
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/data/PastDataManager.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/PastDataManager.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/PastDataManager.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,350 @@
+package de.upb.mmog.net.data;
+
+
+import java.util.HashMap;
+
+import org.p2play.data.Data;
+import org.p2play.data.DataContainer;
+import org.p2play.data.DataListener;
+import org.p2play.data.DataManager;
+import org.p2play.security.SignablePublicKey;
+
+import rice.Continuation;
+import rice.environment.logging.Logger;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.past.Past;
+import rice.p2p.scribe.Scribe;
+import rice.p2p.scribe.ScribeClient;
+import rice.p2p.scribe.ScribeContent;
+import rice.p2p.scribe.Topic;
+import rice.pastry.commonapi.PastryIdFactory;
+
+/**
+ * A Data Manager that stores Data in the Past network
+ */
+public class PastDataManager implements DataManager, ScribeClient {
+ private Past past;
+
+ private Scribe scribe;
+
+ private HashMap<String, DataContainerImpl> dataMap;
+
+ private Logger logger;
+
+ /**
+ * Constructor for PastDataManager.
+ *
+ * @param id
+ * Id
+ * @param past
+ * Past
+ * @param scribe
+ * Scribe
+ * @param keyManager
+ * KeyManager
+ * @param accessControlManager
+ * AccessControlManager
+ */
+ public PastDataManager(Id id, Past past, Scribe scribe) {
+ super();
+
+ this.logger = past.getEnvironment().getLogManager().getLogger(
+ PastDataManager.class, "");
+
+ this.past = past;
+ this.scribe = scribe;
+
+ this.dataMap = new HashMap<String, DataContainerImpl>();
+ }
+
+ /**
+ * Method loadData.
+ *
+ * @param identifier
+ * String
+ * @return Data
+ * @see org.p2play.data.DataManager#loadData(String)
+ */
+ public DataContainer loadData(String identifier) {
+
+ DataContainerImpl container = dataMap.get(identifier);
+ if (container == null) {
+ container = new DataContainerImpl(identifier);
+
+ final Id lookupKey = new PastryIdFactory(past.getEnvironment())
+ .buildId(identifier);
+
+ // subcribe to the scribe topic for this data
+ scribe.subscribe(getTopic(identifier), this);
+
+ past.lookup(lookupKey, container);
+
+ }
+ return container;
+ }
+
+ public DataContainer loadData(String identifier, DataListener listener) {
+ DataContainer container = loadData(identifier);
+ container.addDataListener(listener);
+ return container;
+ }
+
+ /**
+ * Method storeData.
+ *
+ * @param data
+ * Data
+ * @see org.p2play.data.DataManager#storeData(Data)
+ */
+ public void storeData(Data data) {
+ DataContainerImpl dataContainerImpl = dataMap.get(data.getIdentifier());
+
+ if (dataContainerImpl == null) {
+ dataContainerImpl = new DataContainerImpl(data);
+
+ // subcribe to the scribe topic for this data
+ scribe.subscribe(getTopic(data.getIdentifier()), this);
+
+ dataMap.put(data.getIdentifier(), dataContainerImpl);
+ } else {
+ dataContainerImpl.setData(data);
+ }
+
+ final Id lookupKey = new PastryIdFactory(past.getEnvironment())
+ .buildId(data.getIdentifier());
+
+ // send the signature of the data as multicast into the scribe group
+ scribe.publish(getTopic(data.getIdentifier()), new DataPastContent(
+ lookupKey, data));
+
+ past.insert(new DataPastContent(lookupKey, data), new Continuation() {
+ public void receiveException(Exception result) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void receiveResult(Object result) {
+ };
+ });
+ }
+
+ /**
+ * Method getTopic.
+ *
+ * @param name
+ * String
+ * @return Topic
+ */
+ private Topic getTopic(String name) {
+ return new Topic(new PastryIdFactory(scribe.getEnvironment()),
+ "past.data." + name);
+ }
+
+ /**
+ * Method anycast.
+ *
+ * @param topic
+ * Topic
+ * @param content
+ * ScribeContent
+ * @return boolean
+ * @see rice.p2p.scribe.ScribeClient#anycast(Topic, ScribeContent)
+ */
+ public boolean anycast(Topic topic, ScribeContent content) {
+ return false;
+ }
+
+ /**
+ * Method deliver.
+ *
+ * @param topic
+ * Topic
+ * @param content
+ * ScribeContent
+ * @see rice.p2p.scribe.ScribeClient#deliver(Topic, ScribeContent)
+ */
+ public void deliver(Topic topic, ScribeContent content) {
+ if (content instanceof DataPastContent) {
+ DataPastContent response = (DataPastContent) content;
+
+ DataContainerImpl container = dataMap.get(response.getData()
+ .getIdentifier());
+ if (container == null) {
+ container = new DataContainerImpl(response.getData());
+ dataMap.put(container.getName(), container);
+ } else {
+ container.setData(response.getData());
+ }
+ }
+
+ }
+
+ /**
+ * Method childAdded.
+ *
+ * @param topic
+ * Topic
+ * @param child
+ * NodeHandle
+ * @see rice.p2p.scribe.ScribeClient#childAdded(Topic, NodeHandle)
+ */
+ public void childAdded(Topic topic, NodeHandle child) {
+ logger.log("Added child " + child + " to " + topic);
+ }
+
+ /**
+ * Method childRemoved.
+ *
+ * @param topic
+ * Topic
+ * @param child
+ * NodeHandle
+ * @see rice.p2p.scribe.ScribeClient#childRemoved(Topic, NodeHandle)
+ */
+ public void childRemoved(Topic topic, NodeHandle child) {
+ logger.log("Removed child " + child + " from " + topic);
+ }
+
+ /**
+ * Method subscribeFailed.
+ *
+ * @param topic
+ * Topic
+ * @see rice.p2p.scribe.ScribeClient#subscribeFailed(Topic)
+ */
+ public void subscribeFailed(Topic topic) {
+ logger.log("Subcribe failed :" + topic);
+ }
+
+}
+
+/**
+ * Waits for a Resource to be delivered
+ *
+ * @author markush
+ *
+ * @version $Revision: 1.11 $
+ */
+class DataPastContentContinuation implements Continuation {
+ static final DataPastContent EMPTY_DATA_PAST_CONTENT = new EmptyResource();
+
+ private DataPastContent data;
+
+ private Exception exception;
+
+ public DataPastContentContinuation() {
+ this.data = EMPTY_DATA_PAST_CONTENT;
+ }
+
+ /**
+ * Method getData.
+ *
+ * @return DataPastContent
+ * @throws Exception
+ */
+ public DataPastContent getData() throws Exception {
+ if (exception != null) {
+ throw exception;
+ }
+ return data;
+ }
+
+ /**
+ * Method receiveException.
+ *
+ * @param result
+ * Exception
+ * @see rice.Continuation#receiveException(Exception)
+ */
+ public void receiveException(Exception result) {
+ exception = result;
+ }
+
+ /**
+ * Method receiveResult.
+ *
+ * @param result
+ * Object
+ * @see rice.Continuation#receiveResult(Object)
+ */
+ public void receiveResult(Object result) {
+ if (result instanceof DataPastContent || result == null) {
+ data = (DataPastContent) result;
+ }
+
+ }
+
+}
+
+/**
+ * Dummy Resource, used in the busy wait loop
+ *
+ * @author markush
+ *
+ * @version $Revision: 1.11 $
+ */
+class EmptyResource extends DataPastContent {
+
+ public EmptyResource() {
+ super(null, null);
+ }
+
+}
+
+/**
+ */
+class DataSignResponse implements ScribeContent {
+
+ private String identifier;
+
+ private byte[] signature;
+
+ private SignablePublicKey signer;
+
+ /**
+ * Constructor for DataSignResponse.
+ *
+ * @param identifier
+ * String
+ * @param signature
+ * byte[]
+ * @param signer
+ * Id
+ */
+ public DataSignResponse(String identifier, byte[] signature,
+ SignablePublicKey signer) {
+ super();
+ this.identifier = identifier;
+ this.signature = signature;
+ this.signer = signer;
+ }
+
+ /**
+ * Method getIdentifier.
+ *
+ * @return String
+ */
+ public String getIdentifier() {
+ return identifier;
+ }
+
+ /**
+ * Method getSignature.
+ *
+ * @return byte[]
+ */
+ public byte[] getSignature() {
+ return signature;
+ }
+
+ /**
+ * Method getSigner.
+ *
+ * @return Id
+ */
+ public SignablePublicKey getSigner() {
+ return signer;
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/data/StringData.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/data/StringData.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/data/StringData.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,65 @@
+package de.upb.mmog.net.data;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * A Data-class that can store Strings
+ */
+public class StringData extends AbstractData {
+
+ private String data;
+
+ /**
+ * Constructor for StringData.
+ * @param identifier String
+ * @param data String
+ */
+ public StringData(String identifier, String data) {
+ super(identifier);
+ this.data = data;
+ }
+
+ /**
+ * the String as a byte aray
+ * @return byte[]
+ * @see de.upb.mmog.net.security.Signed#getData()
+ */
+ @Override
+ public byte[] getData() {
+ try {
+ return data.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * Method getVersion.
+ * @return int
+ * @see org.p2play.data.Data#getVersion()
+ */
+ @Override
+ public int getVersion() {
+ return 0;
+ }
+
+ /**
+ * Method getStringData.
+ * @return String
+ */
+ public String getStringData() {
+ return data;
+ }
+
+ /**
+ * Method toString.
+ * @return String
+ */
+ @Override
+ public String toString() {
+ return "StringData: " + getIdentifier() + " : " + data;
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/region/impl/DummyClient.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/region/impl/DummyClient.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/region/impl/DummyClient.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,21 @@
+package de.upb.mmog.net.region.impl;
+
+import org.p2play.region.Client;
+import org.p2play.region.GameState;
+import org.p2play.region.Region;
+
+public class DummyClient implements Client {
+
+ public static final DummyClient DUMMY_CLIENT=new DummyClient();
+
+ public void handleGameStateChanged(Region region, GameState gameState) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void handleRegionChange(Region oldRegion, Region newRegion, Object gameObject) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/region/impl/GameRegion.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/region/impl/GameRegion.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/region/impl/GameRegion.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,38 @@
+package de.upb.mmog.net.region.impl;
+
+import org.p2play.region.Region;
+
+/**
+ * Simple Region, where the game name is the region name
+ * @author markus
+ *
+ */
+public class GameRegion implements Region {
+
+ private String game;
+
+ public GameRegion(String game) {
+ super();
+ this.game = game;
+ }
+
+ public String getGame() {
+ return game;
+ }
+
+ public String getRegionIdentifier() {
+ // TODO Auto-generated method stub
+ return game;
+ }
+
+ @Override
+ public int hashCode() {
+ return getRegionIdentifier().hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj.equals(getRegionIdentifier());
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedDynamicRegionManager.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedDynamicRegionManager.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedDynamicRegionManager.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,242 @@
+package de.upb.mmog.net.region.impl;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.p2play.io.Serializer;
+import org.p2play.region.Client;
+import org.p2play.region.GameAction;
+import org.p2play.region.GameState;
+import org.p2play.region.Region;
+import org.p2play.region.RegionInstance;
+import org.p2play.region.RegionManager;
+import org.p2play.region.ServerFactory;
+import org.p2play.region.impl.NetworkHandler;
+import org.p2play.region.impl.ServerHandler;
+import org.p2play.region.impl.TimeManager;
+import org.p2play.region.message.GameActionMessage;
+import org.p2play.region.message.GameStateMessage;
+import org.p2play.region.message.JoinMessage;
+import org.p2play.region.message.RegionMessage;
+import org.p2play.scripting.pnuts.api.region.RegionChangeGameAction;
+import org.p2play.util.ListMap;
+
+import rice.p2p.commonapi.Node;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastImpl;
+import rice.p2p.scribe.Scribe;
+import rice.p2p.scribe.ScribeImpl;
+import rice.persistence.LRUCache;
+import rice.persistence.MemoryStorage;
+import rice.persistence.StorageManagerImpl;
+import de.upb.mmog.net.data.PastDataManager;
+
+public class GroupedDynamicRegionManager implements RegionManager, Runnable {
+ private ListMap<Region, Client> clientMap;
+
+ private HashMap<Region, GameState> gameStateMap;
+
+ private List<GameAction> clientInteractionQueue;
+
+ private NetworkHandler networkHandler;
+
+ private static int tickGap = 100;
+
+ private TimeManager timeManager;
+
+ private ServerHandler serverHandler;
+
+ private int instances;
+ // The names of the instances of the regions where this is the controller
+ // of.
+ private ListMap<Region, RegionInstance> regionInstances; // <regionName,
+
+ public GroupedDynamicRegionManager(Serializer serializer, Node node,
+ ServerFactory serverFactory) {
+
+ instances=3;
+
+ regionInstances=new ListMap<Region, RegionInstance>();
+
+ timeManager = TimeManager.create(node);
+
+ clientMap = new ListMap<Region, Client>();
+ gameStateMap = new HashMap<Region, GameState>();
+
+ clientInteractionQueue = new LinkedList<GameAction>();
+
+ networkHandler = new NetworkHandler(serializer, this, node);
+
+ Past past = new PastImpl(node, new StorageManagerImpl(node
+ .getIdFactory(), new MemoryStorage(node.getIdFactory()),
+ new LRUCache(new MemoryStorage(node.getIdFactory()),
+ 1000 * 1000, node.getEnvironment())), 3, "gamestate");
+ Scribe scribe = new ScribeImpl(node, "gamestate");
+ serverHandler = new ServerHandler(this, new PastDataManager(node
+ .getId(), past, scribe), serverFactory);
+
+ new Thread(this).start();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.upb.mmog.net.region.RegionManager#joinRegion(java.lang.String,
+ * de.upb.mmog.net.region.Client)
+ */
+ public void joinRegion(Region region, Client client) {
+ if (client != serverHandler)
+ System.out.println(client + " joins " + region);
+ synchronized (clientMap) {
+ clientMap.add(region, client);
+ networkHandler.joinRegion(region, timeManager.getGameTime(),instances);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.upb.mmog.net.region.RegionManager#leaveRegion(java.lang.String,
+ * de.upb.mmog.net.region.Client)
+ */
+ public void leaveRegion(Region region, Client client) {
+ if (client != serverHandler)
+ System.out.println(client + " leaves " + region);
+ synchronized (clientMap) {
+ clientMap.remove(region, client);
+
+ if (clientMap.isEmpty(region)) {
+ // leave the region, if no server process is running on us
+ if (!serverHandler.hasServer(region)) {
+ networkHandler.leaveRegion(region);
+ }
+ }
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.upb.mmog.net.region.RegionManager#scheduleInteraction(de.upb.mmog.net.region.Client,
+ * de.upb.mmog.net.region.Interaction)
+ */
+ public void scheduleInteraction(Client client, GameAction interaction) {
+ synchronized (clientInteractionQueue) {
+ clientInteractionQueue.add(interaction);
+ }
+ }
+
+ /**
+ * 1. execute server processes 2. update client processes with new game
+ * state 3. send game state change requests
+ */
+ @SuppressWarnings("unchecked")
+ public void run() {
+ while (true) {
+ long time = System.currentTimeMillis();
+ Iterator<Region> servers = serverHandler.iteratorOfRegions();
+ while (servers.hasNext()) {
+ Region server = servers.next();
+
+ serverHandler.tick(server);
+
+ Iterator<RegionInstance> iterator=regionInstances.iterator(server);
+ while (iterator.hasNext()) {
+ RegionInstance instance = iterator.next();
+ GameState gameState = serverHandler.getGameState(server);
+ if(gameState.getGameTime()%instances==instance.getInstance()) {
+ networkHandler.sendGameState(gameState,instance);
+ }else{
+ networkHandler.sendGameStateHash(gameState, instance);
+ }
+ }
+
+ }
+ // TODO: clear old requests
+
+ // Iterate over the regions
+ synchronized (clientMap) {
+ Iterator<Region> regions = clientMap.keySet().iterator();
+
+ while (regions.hasNext()) {
+ Region region = regions.next();
+
+ GameState gameState = gameStateMap.get(region);// remove(region);
+
+ // Iterate over the controllers
+ Iterator<Client> clients = clientMap.iterator(region);
+ while (clients.hasNext()) {
+ Client client = clients.next();
+ // update the client with the current game state
+ if (region != null && gameState != null) {
+ client.handleGameStateChanged(region, gameState);
+ }
+ }
+ }
+
+ }
+
+ synchronized (clientInteractionQueue) {
+ Iterator<GameAction> requests = clientInteractionQueue
+ .iterator();
+ while (requests.hasNext()) {
+ GameAction request = requests.next();
+
+ networkHandler.sendInteraction(request);
+ requests.remove();
+ }
+ }
+
+ try {
+ Thread.sleep(Math.max(1, tickGap + time
+ - System.currentTimeMillis()));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ public void handleMessageReceived(RegionMessage message) {
+ if (message instanceof GameActionMessage) {
+ GameActionMessage im = (GameActionMessage) message;
+
+ // TODO gefuckelt
+ synchronized (clientMap) {
+ if (im.getGameAction() instanceof RegionChangeGameAction) {
+ RegionChangeGameAction rcga = (RegionChangeGameAction) im
+ .getGameAction();
+ System.out.println("!!!Got Regionchange: "
+ + rcga.getSourceRegion() + "->" + rcga.getRegion());
+ System.out.println(clientMap);
+ if (!rcga.getSourceRegion().equals(rcga.getRegion())) {
+ Iterator<Client> iterator = clientMap.iterator(rcga
+ .getSourceRegion());
+ while (iterator.hasNext()) {
+ Client client = iterator.next();
+
+ client.handleRegionChange(rcga.getSourceRegion(), rcga.getRegion(), rcga
+ .getGameObject());
+ }
+ }
+ }
+ }
+
+ serverHandler.addGameAction(im.getRegion(), im.getGameAction());
+ } else if (message instanceof GameStateMessage) {
+ GameStateMessage gsm = (GameStateMessage) message;
+
+ gameStateMap.put(gsm.getRegion(), gsm.getGameState());
+ } else if (message instanceof JoinMessage) {
+ JoinMessage jm = (JoinMessage) message;
+
+ regionInstances.add(jm.getRegion(), jm.getRegionInstance());
+
+ serverHandler.startServer(jm.getRegion(), jm.getGameTime());
+ }// TODO: add handling of leave message
+ }
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedStaticNetworkRegionManager.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedStaticNetworkRegionManager.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/region/impl/GroupedStaticNetworkRegionManager.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,477 @@
+package de.upb.mmog.net.region.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.p2play.io.Serializer;
+import org.p2play.region.Client;
+import org.p2play.region.GameAction;
+import org.p2play.region.GameState;
+import org.p2play.region.Region;
+import org.p2play.region.RegionInstance;
+import org.p2play.region.RegionManager;
+import org.p2play.region.Server;
+import org.p2play.region.ServerFactory;
+import org.p2play.region.impl.NetworkHandler;
+import org.p2play.region.impl.TimeManager;
+import org.p2play.region.message.GameActionMessage;
+import org.p2play.region.message.GameStateHashMessage;
+import org.p2play.region.message.GameStateMessage;
+import org.p2play.region.message.JoinMessage;
+import org.p2play.region.message.RegionMessage;
+import org.p2play.util.EmptyIterator;
+import org.p2play.util.ListMap;
+
+import rice.environment.logging.Logger;
+import rice.p2p.commonapi.Node;
+
+public final class GroupedStaticNetworkRegionManager implements Runnable,
+ RegionManager {
+ private Logger logger;
+
+ private ListMap<Region, GameState> receivedGameStates;
+
+ //All received game state hashes, key=region
+ private ListMap<Region, GameStateHashMessage> receivedGameStateHashes;
+
+ // The names of the instances of the regions where this is the controller
+ // of.
+ private ListMap<Region, RegionInstance> regionInstances; // <regionName,
+
+ // InstanceName>
+
+ // The Names of the Instances which send the last gameStates
+ private HashMap<Region, RegionInstance> lastSendingInstances; // <regionName,
+
+ // InstanceName>
+
+ private HashMap<RegionInstance, Long> lastGameStateTimes;
+
+ // all clients in the specified region
+ private HashMap<Region, List<Client>> clientMap;
+
+ private HashMap<Region, Server> serverMap;
+
+ // Gamestate for each region for the clients only
+ private HashMap<Region, GameState> clientGameStateMap;
+
+ // Gamestate for each region for the regionController
+ private HashMap<Region, GameState> regionControllerGameStateMap;
+
+ // Interaction Queue which is inserted in the Gamestate in each tick
+ private List<GameAction> clientInteractionQueue;
+
+ // all saved gamesActions for each region
+ private HashMap<Region, List<GameAction>> serverInteractionMap;
+
+ private NetworkHandler networkHandler;
+
+ private ServerFactory serverFactory;
+
+ // time between two ticks in ms
+ private static int tickGap = 100;
+
+ // save last ticktime for each region
+ // necessary too detect a failure of the regionController
+ private HashMap<Region, Long> lastTickTime;
+
+ private TimeManager timeManager;
+
+ private int instances;
+
+ public GroupedStaticNetworkRegionManager(Serializer serializer, Node node,
+ ServerFactory serverFactory, int instances) {
+ this.logger = node.getEnvironment().getLogManager().getLogger(
+ this.getClass(), "");
+
+ this.serverFactory = serverFactory;
+ this.instances = instances;
+
+ receivedGameStates = new ListMap<Region, GameState>();
+ receivedGameStateHashes= new ListMap<Region, GameStateHashMessage>();
+ regionInstances = new ListMap<Region, RegionInstance>();
+ lastSendingInstances = new HashMap<Region, RegionInstance>();
+ lastGameStateTimes = new HashMap<RegionInstance, Long>();
+ clientMap = new HashMap<Region, List<Client>>();
+ serverMap = new HashMap<Region, Server>();
+ lastTickTime = new HashMap<Region, Long>();
+
+ clientGameStateMap = new HashMap<Region, GameState>();
+ regionControllerGameStateMap = new HashMap<Region, GameState>();
+
+ clientInteractionQueue = new LinkedList<GameAction>();
+ serverInteractionMap = new HashMap<Region, List<GameAction>>();
+
+ networkHandler = new NetworkHandler(serializer, this, node);
+
+ timeManager = TimeManager.create(node);
+
+ new Thread(this).start();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.upb.mmog.net.region.RegionManager#joinRegion(java.lang.String,
+ * de.upb.mmog.net.region.Client)
+ */
+ public void joinRegion(Region region, Client client) {
+ synchronized (clientMap) {
+ List<Client> list = clientMap.get(region);
+
+ if (list == null) {
+ list = new LinkedList<Client>();
+ clientMap.put(region, list);
+ }
+
+ if (!list.contains(client)) {
+ list.add(client);
+ }
+
+ networkHandler.joinRegion(region, timeManager.getGameTime(),
+ instances);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.upb.mmog.net.region.RegionManager#leaveRegion(java.lang.String,
+ * de.upb.mmog.net.region.Client)
+ */
+ public void leaveRegion(Region region, Client client) {
+ synchronized (clientMap) {
+ List<Client> list = clientMap.get(region);
+
+ if (list != null) {
+ if (list.contains(client)) {
+ list.remove(client);
+ }
+
+ if (list.isEmpty()) {
+ // last one, remove the list
+ clientMap.remove(region);
+
+ // and leave the region, if no server process is running on
+ // us
+ if (!serverMap.containsKey(region)) {
+ networkHandler.leaveRegion(region);
+ }
+ }
+ }
+ }
+
+ }
+
+ /*
+ * //Client ruft dies auf wenn er was machen will
+ *
+ * @see de.upb.mmog.net.region.RegionManager#scheduleInteraction(de.upb.mmog.net.region.Client,
+ * de.upb.mmog.net.region.Interaction)
+ */
+ public void scheduleInteraction(Client client, GameAction interaction) {
+ synchronized (clientInteractionQueue) {
+ clientInteractionQueue.add(interaction);
+ }
+ }
+
+ /**
+ * 1. execute server processes 2. update client processes with new game
+ * state 3. send game state change requests
+ */
+ @SuppressWarnings("unchecked")
+ public void run() {
+ while (true) {
+ long time = System.currentTimeMillis();
+
+ long gameTime = timeManager.getGameTime();
+
+ //System.out.println("Ticking game time: " + gameTime);
+ //System.out.println(lastGameStateTimes);
+
+ // check all gamestates we received and pick the one that has the
+ // most same hashes
+ Iterator<Region> regions = receivedGameStates.clonedIterator();
+ while (regions.hasNext()) {
+ Region region = regions.next();
+
+ //System.out.println("region: " + region);
+
+ // find the gamestate of the region that has the most correct
+ // hashes
+ HashMap<String, Integer> counter = new HashMap<String, Integer>();
+
+ synchronized(receivedGameStates){
+ Iterator<GameState> gamestates = receivedGameStates
+ .iterator(region);
+
+ //System.out.println(receivedGameStates);
+
+ int max = 0;
+ GameState bestState = null;
+
+ while (gamestates.hasNext()) {
+ GameState gameState = gamestates.next();
+
+ /*if (gameState.getGameTime() <= gameTime - 1) {
+ continue;
+ }*/
+
+ String hash = String.valueOf(gameState.hash());
+
+ if (!counter.containsKey(hash)) {
+ counter.put(hash, 1);
+ } else {
+ counter.put(hash, counter.get(hash) + 1);
+ }
+
+ int nmax = Math.max(max, counter.get(hash));
+
+ if (nmax > max) {
+ bestState = gameState;
+ }
+
+ max = nmax;
+ }
+
+ //System.out.println(counter);
+ //System.out.println("GameState: " + bestState + " has most: " + max);
+
+ if (bestState != null) {
+ clientGameStateMap.put(bestState.getRegion(), bestState);
+ regionControllerGameStateMap.put(bestState.getRegion(),
+ bestState);
+ }else {
+ logger.log("No Gamestate for tick "+gameTime+" for region "+region);
+ }
+
+ receivedGameStates.remove(region);
+ }
+ }
+
+ // first test which regioncontrollers are still running and restart
+ // the others
+ HashSet<Region> set = new HashSet<Region>();
+ set.addAll(serverMap.keySet());
+ set.addAll(clientMap.keySet());
+
+ // Iterator<String> regions = set.iterator();
+ /*
+ * while (regions.hasNext()) { String region = regions.next();
+ *
+ * for (int i = 0; i < instances; i++) { String instance =
+ * networkHandler.getInstanceName(region, i); if
+ * (lastGameStateTimes.get(instance) != null && (gameTime -
+ * lastGameStateTimes.get(instance) > 50)) { // got a message too
+ * long ago (5 ticks) // rejoin networkHandler.joinRegion(region,
+ * instance, gameTime); logger.log("Rejoining instance: " +
+ * instance); } }
+ * }
+ */
+
+ Iterator<Server> servers = ((HashMap<String, Server>) serverMap
+ .clone()).values().iterator();
+ while (servers.hasNext()) {
+ Server server = servers.next();
+
+ List<GameAction> requests = serverInteractionMap.remove(server
+ .getRegion());
+
+ GameState gameState = regionControllerGameStateMap.get(server
+ .getRegion());
+ if (gameState != null) {
+ while (gameState.getGameTime() < gameTime) {
+ gameState = server.tick(gameState, requests);
+ }
+ regionControllerGameStateMap.put(server.getRegion(),
+ gameState);
+ synchronized(regionInstances){
+ Iterator<RegionInstance> instances = regionInstances
+ .iterator(server.getRegion());
+ while (instances.hasNext()) {
+ RegionInstance instance = instances.next();
+ if (networkHandler.isRoot(instance)) {
+ if((gameTime+instance.getInstance())%this.instances==0)
+ {
+ //System.out.println("Sending GameState!");
+ networkHandler.sendGameState(gameState, instance);
+ }
+ else{
+ //send the hash of the gameState
+ //System.out.println("Sending Hash!");
+ networkHandler.sendGameStateHash(gameState, instance);
+ }
+ } else {
+ logger
+ .log("I'm no longer controller for instance: "
+ + instance);
+ regionInstances
+ .remove(server.getRegion(), instance);
+ networkHandler.joinRegion(instance, gameTime);
+ }
+
+ }
+ }
+ // TODO: close server if all instances are closed
+ // TODO: send diffs
+ // scribeThread.sendGameStateDiff(diff);
+ }
+ }
+ // TODO: clear old requests
+
+ // Iterate over the regions
+ synchronized (clientMap) {
+
+ regions = clientMap.keySet().iterator();
+
+ while (regions.hasNext()) {
+ Region region = regions.next();
+
+ GameState gameState = clientGameStateMap.get(region);// remove(region);
+
+ // Iterate over the controllers
+ Iterator<Client> clients = iteratorOfClients(region);
+ while (clients.hasNext()) {
+ Client client = clients.next();
+
+ // update the client with the current game state
+ if (region != null && gameState != null) {
+ client.handleGameStateChanged(region, gameState);
+ }
+ }
+
+ }
+ }
+
+ // schicke alle gesammelten Client-Kommandos los
+ synchronized (clientInteractionQueue) {
+ Iterator<GameAction> requests = clientInteractionQueue
+ .iterator();
+ while (requests.hasNext()) {
+ GameAction request = requests.next();
+ // logger.log("Sending interaction, region:
+ // "+request.getRegion()+", Instance: "+
+ // regionInstances.get(request.getRegion()));
+ // locate instance which send the last gamestate and send a
+ // interaction-request to
+ // String actuRegionInstance =
+ // regionInstances.get(request.getRegion());
+ networkHandler.sendInteraction(request);
+ requests.remove();
+ }
+ }
+
+ try {
+ Thread.sleep(Math.max(1, tickGap + time
+ - System.currentTimeMillis()));
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ private Iterator<Client> iteratorOfClients(Region region) {
+ if (clientMap.containsKey(region)) {
+ return clientMap.get(region).iterator();
+ } else {
+ return new EmptyIterator<Client>();
+ }
+ }
+
+ public void handleMessageReceived(RegionMessage message) {
+ if (message instanceof GameActionMessage) {
+ GameActionMessage im = (GameActionMessage) message;
+
+ synchronized (serverInteractionMap) {
+
+ List<GameAction> list = serverInteractionMap
+ .get(im.getRegionInstance().getRegion());
+
+ if (list == null) {
+ list = new LinkedList<GameAction>();
+ serverInteractionMap.put(im.getRegionInstance().getRegion(), list);
+ }
+
+ list.add(im.getGameAction());
+
+ }
+ } else if (message instanceof GameStateMessage) {
+
+ // set the "lastSendingInstances"
+ lastSendingInstances.put(message.getRegionInstance().getRegion(), message
+ .getRegionInstance());
+
+ lastTickTime.put(message.getRegionInstance().getRegion(), System.currentTimeMillis());
+ GameStateMessage gsm = (GameStateMessage) message;
+
+ lastGameStateTimes.put(message.getRegionInstance(), gsm
+ .getGameState().getGameTime());
+
+ receivedGameStates.add(message.getRegionInstance().getRegion(), gsm.getGameState());
+ } else if (message instanceof JoinMessage) {
+ JoinMessage jm = (JoinMessage) message;
+ // start the server if needed
+ Server server = serverMap.get(jm.getRegionInstance().getRegion());
+ regionInstances.add(jm.getRegionInstance().getRegion(), jm.getRegionInstance());
+ if (server == null) {
+
+ server = serverFactory.getServer(jm.getRegionInstance().getRegion());
+ // timeWhereItBecameController=System.currentTimeMillis();
+
+ logger.log("Created controller instance \""
+ + jm.getRegionInstance() + "\" for region : "
+ + jm.getRegionInstance().getRegion());
+
+ long randomSeed = 0;
+ try {
+ randomSeed = new DataInputStream(new ByteArrayInputStream(
+ networkHandler.getTopic(new RegionInstance(jm.getRegionInstance().getRegion(),0)).getId()
+ .toByteArray())).readLong();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ GameState gameState = server.init(jm.getGameTime(), randomSeed);
+ regionControllerGameStateMap.put(jm.getRegionInstance().getRegion(), gameState);
+ synchronized (serverMap) {
+ serverMap.put(jm.getRegionInstance().getRegion(), server);
+ }
+
+ networkHandler.joinRegion(jm.getRegionInstance().getRegion(), jm.getGameTime());
+
+ //TODO: auch gefrickelt...
+ //join all neigbours
+ Iterator<Region> neighbouts = server.getNeighbourRegionIdentifiers().iterator();
+ while(neighbouts.hasNext()){
+ Region region=neighbouts.next();
+ joinRegion(region, DummyClient.DUMMY_CLIENT);
+ }
+
+ }// TODO: add handling of leave message
+
+ // send the current game state to the client
+ // scribeThread.sendGameState(jm.getSender(),
+ // gameStateMap.get(jm.getRegion()));
+ }
+ else if(message instanceof GameStateHashMessage) {
+
+ GameStateHashMessage gshm=(GameStateHashMessage)message;
+
+ //System.out.println("Region hash: "+gshm.getRegion());
+ receivedGameStateHashes.add(gshm.getRegion(),gshm);
+
+ //System.out.println("Received GameStatHash!");
+
+ // TODO test the hash with the actual gameState
+ //System.out.println("GameState Hash: "+gshm.getHash());
+ }
+ }
+}
\ No newline at end of file
Added: P2PlayLegacy/src/de/upb/mmog/net/region/impl/ObjectGameAction.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/region/impl/ObjectGameAction.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/region/impl/ObjectGameAction.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,40 @@
+package de.upb.mmog.net.region.impl;
+
+import org.p2play.region.GameAction;
+import org.p2play.region.Region;
+
+
+public class ObjectGameAction implements GameAction {
+
+ private Region region;
+ private long gameTime;
+ private Object object;
+
+ public ObjectGameAction(Region region,long gameTime, Object object) {
+ super();
+ this.region = region;
+ this.gameTime = gameTime;
+ this.object = object;
+ }
+
+ public long getGameTime() {
+ return gameTime;
+ }
+
+
+
+ public Object getObject() {
+ return object;
+ }
+
+
+
+ public Region getRegion() {
+ return region;
+ }
+
+ public boolean sendToAll() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/region/impl/PeerToPeerRegionManager.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/region/impl/PeerToPeerRegionManager.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/region/impl/PeerToPeerRegionManager.java 2007-03-20 12:17:08 UTC (rev 50)
@@ -0,0 +1,229 @@
+package de.upb.mmog.net.region.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.p2play.io.Serializer;
+import org.p2play.region.Client;
+import org.p2play.region.GameAction;
+import org.p2play.region.GameState;
+import org.p2play.region.Region;
+import org.p2play.region.RegionInstance;
+import org.p2play.region.RegionManager;
+import org.p2play.region.Server;
+import org.p2play.region.ServerFactory;
+import org.p2play.region.impl.NetworkHandler;
+import org.p2play.region.impl.TimeManager;
+import org.p2play.region.message.GameActionMessage;
+import org.p2play.region.message.GameStateMessage;
+import org.p2play.region.message.JoinMessage;
+import org.p2play.region.message.RegionMessage;
+import org.p2play.util.ListMap;
+
+import rice.environment.logging.Logger;
+import rice.p2p.commonapi.Node;
+
+public class PeerToPeerRegionManager implements RegionManager, Runnable {
+ private NetworkHandler networkHandler;
+
+ private Logger logger;
+
+ private ServerFactory serverFactory;
+
+ private ListMap<Region, Client> clientMap;
+
+ private HashMap<Region, Server> serverMap;
+
+ private ListMap<Region, GameAction> serverActionMap;
+
+ private HashMap<Region, GameState> gameStateMap;
+
+ private List<GameAction> clientActionQueue;
+
+ private static int tickGap = 100;
+
+ private TimeManager timeManager;
+
+ public PeerToPeerRegionManager(Serializer serializer, Node node, ServerFactory serverFactory) {
+ this.logger = node.getEnvironment().getLogManager().getLogger(
+ this.getClass(), "");
+ this.serverFactory = serverFactory;
+ networkHandler = new NetworkHandler(serializer, this, node);
+
+ this.clientMap = new ListMap<Region, Client>();
+ this.serverMap = new HashMap<Region, Server>();
+ this.serverActionMap = new ListMap<Region, GameAction>();
+ this.gameStateMap = new HashMap<Region, GameState>();
+ this.clientActionQueue = new LinkedList<GameAction>();
+ this.timeManager=TimeManager.create(node);
+
+ new Thread(this).start();
+ }
+
+ public void joinRegion(Region region, Client client) {
+ startServer(region,timeManager.getGameTime());
+ networkHandler.joinRegion(region,timeManager.getGameTime());
+ clientMap.add(region, client);
+ }
+
+ public void leaveRegion(Region region, Client client) {
+ networkHandler.leaveRegion(region);
+ clientMap.remove(region, client);
+ }
+
+ public void scheduleInteraction(Client client, GameAction interaction) {
+ clientActionQueue.add(interaction);
+ }
+
+ public void handleMessageReceived(RegionMessage message) {
+ if (message instanceof GameActionMessage) {
+ GameActionMessage im = (GameActionMessage) message;
+ serverActionMap.add(im.getRegion(), im.getGameAction());
+ } else if (message instanceof GameStateMessage) {
+ GameStateMessage gsm = (GameStateMessage) message;
+
+ gameStateMap.put(gsm.getRegion(), gsm.getGameState());
+ } else if (message instanceof JoinMessage) {
+ JoinMessage jm = (JoinMessage) message;
+
+ Server server = startServer(jm.getRegion(),jm.getGameTime());
+
+ // send the current game state to the client
+ networkHandler.sendGameState(jm.getSender(), gameStateMap
+ .get(server.getRegion()));
+ }
+ // TODO: add handling of leave message
+ }
+
+ private Server startServer(Region region, long gameTime) {
+ // start the server if needed
+ Server server = serverMap.get(region);
+ if (server == null) {
+ server = serverFactory.getServer(region);
+
+ long randomSeed=0;
+ try {
+ randomSeed = new DataInputStream(new ByteArrayInputStream(networkHandler.getTopic(new RegionInstance(region,0)).getId().toByteArray())).readLong();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ GameState gameState = server.init(randomSeed, gameTime);
+ gameStateMap.put(region, gameState);
+
+ // TODO: tick to current time
+ synchronized (serverMap) {
+ serverMap.put(region, server);
+ }
+
+ networkHandler.joinRegion(region,gameTime);
+
+ //TODO: auch gefrickelt...
+ //join all neigbours
+ /*Iterator<Region> neighbouts = server.getNeighbourRegionIdentifiers().iterator();
+ while(neighbouts.hasNext()){
+ Region region=neighbouts.next();
+ joinRegion(region, dummyClient);
+ }*/
+
+
+ logger.log("Created controller for: " + region);
+ }
+ return server;
+ }
+
+ /**
+ * 1. execute server processes 2. update client processes with new game
+ * state 3. send game state change requests
+ */
+ @SuppressWarnings("unchecked")
+ public void run() {
+ while (true) {
+ long time = System.currentTimeMillis();
+ Iterator<Server> servers = ((HashMap<String, Server>)serverMap.clone()).values().iterator();
+ while (servers.hasNext()) {
+ Server server = servers.next();
+
+ GameState gameState = gameStateMap.get(server.getRegion());
+
+ if (gameState != null) {
+
+ Iterator<GameAction> requests = serverActionMap
+ .iterator(server.getRegion());
+
+ List<GameAction> currentActionList = new LinkedList<GameAction>();
+
+ while (requests.hasNext()) {
+ GameAction action = requests.next();
+
+ /*if (action.getGameTime() < gameState.getGameTime()) {
+ // remove old
+ requests.remove();
+ } else if (action.getGameTime() == gameState
+ .getGameTime()) {*/
+ // handle current
+ currentActionList.add(action);
+ requests.remove();
+ /*} else {
+ // leave too new
+ }*/
+ }
+
+ gameState=server.tick(gameState, currentActionList);
+ gameStateMap.put(server.getRegion(), gameState);
+ //logger.log("Hash of gamestate " +gameState.getRegion()+" "+ gameState.getGameTime() + " "
+ // + gameState.hash());
+ }
+
+ }
+ // TODO: clear old requests
+
+ // Iterate over the regions
+ Iterator<Region> regions = clientMap.keySet().iterator();
+
+ while (regions.hasNext()) {
+ Region region = regions.next();
+
+ GameState gameState = gameStateMap.get(region);//remove(region);
+
+ // Iterate over the controllers
+ Iterator<Client> clients = clientMap.iterator(region);
+ while (clients.hasNext()) {
+ Client client = clients.next();
+
+ // update the client with the current game state
+ if (region != null && gameState != null) {
+ client.handleGameStateChanged(region, gameState);
+ }
+ }
+
+ }
+
+ synchronized (clientActionQueue) {
+ Iterator<GameAction> requests = clientActionQueue.iterator();
+ while (requests.hasNext()) {
+ GameAction request = requests.next();
+ GameState state = gameStateMap.get(request.getRegion());
+ if (state != null) {
+ networkHandler.sendExtendedInteraction(request, state
+ .getGameTime(), state.hash());
+ requests.remove();
+ }
+ }
+ }
+
+ try {
+ Thread.sleep(Math.max(1, tickGap + time
+ - System.currentTimeMillis()));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+}
Added: P2PlayLegacy/src/de/upb/mmog/net/resource/PastResourceManager.java
===================================================================
--- P2PlayLegacy/src/de/upb/mmog/net/resource/PastResourceManager.java (rev 0)
+++ P2PlayLegacy/src/de/upb/mmog/net/resource/PastResourceManager.java 2007-03-20 12:17:08 UTC (rev 50)
@...
[truncated message content] |