[P2play-commit] SF.net SVN: p2play: [9] trunk/P2Play/src
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-26 22:18:38
|
Revision: 9
http://p2play.svn.sourceforge.net/p2play/?rev=9&view=rev
Author: tisoft
Date: 2006-12-26 14:18:35 -0800 (Tue, 26 Dec 2006)
Log Message:
-----------
transfered from university cvs
added license header
changed package name to org.p2play.*
Added Paths:
-----------
trunk/P2Play/src/org/
trunk/P2Play/src/org/p2play/
trunk/P2Play/src/org/p2play/io/
trunk/P2Play/src/org/p2play/io/Serializer.java
trunk/P2Play/src/org/p2play/io/impl/
trunk/P2Play/src/org/p2play/io/impl/JavaSerializerImpl.java
trunk/P2Play/src/org/p2play/net/
trunk/P2Play/src/org/p2play/net/region/
trunk/P2Play/src/org/p2play/net/region/Client.java
trunk/P2Play/src/org/p2play/net/region/GameAction.java
trunk/P2Play/src/org/p2play/net/region/GameState.java
trunk/P2Play/src/org/p2play/net/region/Region.java
trunk/P2Play/src/org/p2play/net/region/RegionInstance.java
trunk/P2Play/src/org/p2play/net/region/RegionManager.java
trunk/P2Play/src/org/p2play/net/region/Server.java
trunk/P2Play/src/org/p2play/net/region/ServerFactory.java
trunk/P2Play/src/org/p2play/net/region/message/
trunk/P2Play/src/org/p2play/net/region/message/GameActionMessage.java
trunk/P2Play/src/org/p2play/net/region/message/GameStateHashMessage.java
trunk/P2Play/src/org/p2play/net/region/message/GameStateMessage.java
trunk/P2Play/src/org/p2play/net/region/message/JoinMessage.java
trunk/P2Play/src/org/p2play/net/region/message/MessageDeserializer.java
trunk/P2Play/src/org/p2play/net/region/message/RegionMessage.java
trunk/P2Play/src/org/p2play/scripted/
trunk/P2Play/src/org/p2play/util/
trunk/P2Play/src/org/p2play/util/Hashable.java
trunk/P2Play/src/org/p2play/util/Hasher.java
trunk/P2Play/src/org/p2play/util/NetRandom.java
Added: trunk/P2Play/src/org/p2play/io/Serializer.java
===================================================================
--- trunk/P2Play/src/org/p2play/io/Serializer.java (rev 0)
+++ trunk/P2Play/src/org/p2play/io/Serializer.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.io;
+
+import java.io.IOException;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+/**
+ * This interface is used to serialize/deserialize Objects.
+ *
+ * @author Markus Heberling <ma...@ti...>
+ *
+ */
+public interface Serializer {
+ /**
+ * This method takes an object and writes its contents into the given output buffer.
+ * @param object
+ * @param outputBuffer
+ * @throws IOException
+ */
+ public void serialize(Object object, OutputBuffer outputBuffer) throws IOException;
+
+ /**
+ * The method reads one object out of the biffer and returns it
+ * @param inputBuffer
+ * @return
+ * @throws IOException
+ */
+ public Object deserialize(InputBuffer inputBuffer) throws IOException;
+}
Added: trunk/P2Play/src/org/p2play/io/impl/JavaSerializerImpl.java
===================================================================
--- trunk/P2Play/src/org/p2play/io/impl/JavaSerializerImpl.java (rev 0)
+++ trunk/P2Play/src/org/p2play/io/impl/JavaSerializerImpl.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.io.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.p2play.io.Serializer;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+/**
+ * Serialize/deserialize objects with java serialization
+ *
+ * @author Markus Heberling <ma...@ti...>
+ *
+ */
+public class JavaSerializerImpl implements Serializer{
+ public Object deserialize(InputBuffer inputBuffer) throws IOException {
+ byte[] byteArray=new byte[inputBuffer.readInt()];
+ inputBuffer.read(byteArray);
+ ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
+ ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
+ try {
+ return objectInputStream.readObject();
+ } catch (ClassNotFoundException e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+
+ public void serialize(Object object, OutputBuffer outputBuffer) throws IOException {
+ ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
+ ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
+
+ objectOutputStream.writeObject(object);
+
+ byte[] byteArray = byteArrayOutputStream.toByteArray();
+ outputBuffer.writeInt(byteArray.length);
+ outputBuffer.write(byteArray, 0, byteArray.length);
+ }
+}
Added: trunk/P2Play/src/org/p2play/net/region/Client.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/Client.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/Client.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+/**
+ * A "client" process, that wants to get updated when the game state changes
+ * @author markus
+ *
+ */
+public interface Client {
+ /**
+ * Set the new Game State
+ * The client may now display it
+ * @param gameState the new Game State to set
+ */
+ public void handleGameStateChanged(Region region, GameState gameState);
+
+ public void handleRegionChange(Region oldRegion, Region newRegion, Object gameObject);
+}
Added: trunk/P2Play/src/org/p2play/net/region/GameAction.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/GameAction.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/GameAction.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+import java.io.Serializable;
+
+public interface GameAction extends Serializable{
+ /**
+ * The region we want to change
+ * @return The region we want to change
+ */
+ public Region getRegion();
+
+ public long getGameTime();
+
+ public boolean sendToAll();
+}
Added: trunk/P2Play/src/org/p2play/net/region/GameState.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/GameState.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/GameState.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Random;
+
+public interface GameState extends Serializable {
+
+ public abstract Random getRandom();
+
+ /**
+ * The virtual game time this game state is valid for
+ * @return the virtual game time this game state is valid for
+ */
+ public abstract long getGameTime();
+
+ /**
+ * The Region identifier for the region, this game state belongs to
+ * @return the Region identifier for the region, this game state belongs to
+ */
+ public abstract Region getRegion();
+
+ public abstract List<Region> getNeighbourRegions();
+
+ public abstract boolean isIdle();
+
+ public abstract String toString();
+
+ /**
+ * Increase game time by one.
+ *
+ */
+ public abstract void tick();
+
+ public abstract int hash();
+
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/net/region/Region.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/Region.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/Region.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+import java.io.Serializable;
+
+public interface Region extends Serializable{
+ /**
+ *
+ * @return The name of the game, that we represent
+ */
+ public String getGame();
+
+ /**
+ *
+ * @return A textual form of our region
+ */
+ public String getRegionIdentifier();
+}
Added: trunk/P2Play/src/org/p2play/net/region/RegionInstance.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/RegionInstance.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/RegionInstance.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+import java.io.Serializable;
+
+import org.p2play.net.region.Region;
+
+
+public class RegionInstance implements Serializable{
+ private Region region;
+
+ private int instance;
+
+ public RegionInstance(Region region, int instance) {
+ super();
+ this.region = region;
+ this.instance = instance;
+ }
+
+ public Region getRegion() {
+ return region;
+ }
+
+ public int getInstance() {
+ return instance;
+ }
+
+ @Override
+ public String toString() {
+ return region.getRegionIdentifier() + (instance != 0 ? instance : "");
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ RegionInstance other= ((RegionInstance)obj);
+
+ return other.region.equals(this.region)&&other.instance==this.instance;
+ }
+}
Added: trunk/P2Play/src/org/p2play/net/region/RegionManager.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/RegionManager.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/RegionManager.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+import org.p2play.net.region.message.RegionMessage;
+
+public interface RegionManager {
+
+ /**
+ * Called when a client wants to join a specific region
+ * @param region
+ * @param client
+ */
+ public abstract void joinRegion(Region region, Client client);
+
+ /**
+ * Called when a client wants to leave a region
+ * @param region
+ * @param client
+ */
+ public abstract void leaveRegion(Region region, Client client);
+
+ /**
+ * the given request is send over the network to the region controller
+ * @param client
+ * @param interaction
+ */
+ public abstract void scheduleInteraction(Client client,
+ GameAction interaction);
+
+ abstract void handleMessageReceived(RegionMessage message);
+
+
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/net/region/Server.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/Server.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/Server.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+import java.util.List;
+
+/**
+ * A "server" process that controlls a region.
+ * @author markus
+ *
+ */
+public interface Server {
+ /**
+ * The Identifier of the Region we controll
+ * @return the Identifier of the Region we controll
+ */
+ public Region getRegion();
+
+ /**
+ * The logical neighbours of this region (e.g. all directly connected regions)
+ * @return the logical neighbours of this region
+ */
+ public List<Region> getNeighbourRegionIdentifiers();
+
+ public GameState init(long gameTime, long randomSeed);
+
+ /**
+ * Returns true, if there are no players in this region
+ * @param gameState
+ * @return
+ */
+ public boolean isEmpty(GameState gameState);
+
+ /**
+ * Ticks the GameState with the Interactions.
+ * The interaction have to be applied to the GameState in the order of occurance in the list
+ * This method should block until all interactions have been applied
+ * interactions may be null, if no requests have to be processed
+ * @param interactions
+ * @return the difference between the old and the new game state
+ */
+ public GameState tick(GameState gameState, List<GameAction> interactions);
+}
Added: trunk/P2Play/src/org/p2play/net/region/ServerFactory.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/ServerFactory.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/ServerFactory.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region;
+
+public interface ServerFactory{
+ public Server getServer(Region region);
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/net/region/message/GameActionMessage.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/message/GameActionMessage.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/message/GameActionMessage.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region.message;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+import org.p2play.net.region.GameAction;
+import org.p2play.net.region.RegionInstance;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameActionMessage extends RegionMessage {
+ private GameAction gameAction;
+
+
+ public GameActionMessage(Serializer serializer, Id sender, GameAction interaction, RegionInstance regionInstance) {
+ super(serializer, sender, regionInstance);
+ this.gameAction = interaction;
+ }
+
+ GameActionMessage(Serializer serializer, InputBuffer buf, Endpoint endpoint) throws IOException {
+ super(serializer, buf, endpoint);
+ gameAction=(GameAction) serializer.deserialize(buf);
+ }
+
+ public GameAction getGameAction() {
+ return gameAction;
+ }
+
+ public short getType() {
+ return GAMEACTION_MESSAGE;
+ }
+
+ @Override
+ public void doSerialize(OutputBuffer buf) throws IOException {
+ getSerializer().serialize(gameAction, buf);
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/net/region/message/GameStateHashMessage.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/message/GameStateHashMessage.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/message/GameStateHashMessage.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region.message;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.RegionInstance;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameStateHashMessage extends RegionMessage {
+
+ private int hash;
+ private long tick;
+
+ public GameStateHashMessage(Serializer serializer, Id sender, GameState gameState, RegionInstance instance)
+ {
+ super(serializer, sender,instance);
+ this.hash=gameState.hash();
+ this.tick=gameState.getGameTime();
+ }
+
+ GameStateHashMessage(Serializer serializer, InputBuffer buf, Endpoint endpoint) throws IOException {
+ super(serializer, buf, endpoint);
+ this.hash=buf.readInt();
+ this.tick=buf.readLong();
+ }
+
+ @Override
+ public void doSerialize(OutputBuffer buf) throws IOException {
+ buf.writeInt(this.hash);
+ buf.writeLong(this.tick);
+ }
+
+ public int getHash()
+ {
+ return this.hash;
+ }
+
+ public long getTick()
+ {
+ return this.tick;
+ }
+
+ public short getType() {
+ return GAME_STATE_HASH_MESSAGE ;
+ }
+
+
+}
Added: trunk/P2Play/src/org/p2play/net/region/message/GameStateMessage.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/message/GameStateMessage.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/message/GameStateMessage.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region.message;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.RegionInstance;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class GameStateMessage extends RegionMessage {
+ private GameState gameState;
+
+ public GameStateMessage(Serializer serializer, Id sender, GameState gameState,
+ RegionInstance instance) {
+ super(serializer, sender, instance);
+ this.gameState = gameState;
+ }
+
+ GameStateMessage(Serializer serializer, InputBuffer buf, Endpoint endpoint) throws IOException {
+ super(serializer, buf, endpoint);
+ this.gameState = (GameState) serializer.deserialize(buf);
+ }
+
+ public GameState getGameState() {
+ return gameState;
+ }
+
+ public short getType() {
+ return GAMESTATE_MESSAGE;
+ }
+
+ @Override
+ public void doSerialize(OutputBuffer buf) throws IOException {
+ getSerializer().serialize(gameState, buf);
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/net/region/message/JoinMessage.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/message/JoinMessage.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/message/JoinMessage.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region.message;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+import org.p2play.net.region.RegionInstance;
+
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class JoinMessage extends RegionMessage {
+ private long gameTime;
+
+ public JoinMessage(Serializer serializer, Id sender, RegionInstance regionInstance, long gameTime) {
+ super(serializer, sender, regionInstance);
+ this.gameTime = gameTime;
+ }
+
+ JoinMessage(Serializer serializer, InputBuffer buf, Endpoint endpoint) throws IOException {
+ super(serializer, buf, endpoint);
+ gameTime=buf.readLong();
+ }
+
+ public long getGameTime() {
+ return gameTime;
+ }
+
+ public short getType() {
+ return JOIN_MESSAGE;
+ }
+
+ @Override
+ public void doSerialize(OutputBuffer buf) throws IOException {
+ buf.writeLong(gameTime);
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/net/region/message/MessageDeserializer.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/message/MessageDeserializer.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/message/MessageDeserializer.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region.message;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Message;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.scribe.ScribeContent;
+import rice.p2p.scribe.rawserialization.ScribeContentDeserializer;
+
+public class MessageDeserializer implements
+ rice.p2p.commonapi.rawserialization.MessageDeserializer, ScribeContentDeserializer {
+ private Endpoint endpoint;
+ private Serializer serializer;
+
+ public MessageDeserializer(Serializer serializer, Endpoint endpoint) {
+ super();
+ this.serializer=serializer;
+ this.endpoint = endpoint;
+ }
+
+ public Message deserialize(InputBuffer buf, short type, int priority,
+ NodeHandle sender) throws IOException {
+ return (Message)deserialize(buf, endpoint, type);
+ }
+
+ public ScribeContent deserializeScribeContent(InputBuffer buf, Endpoint endpoint, short contentType) throws IOException {
+ return (ScribeContent)deserialize(buf, endpoint, contentType);
+ }
+
+ private Object deserialize(InputBuffer buf, Endpoint endpoint,short type) throws IOException{
+ switch (type) {
+ case RegionMessage.JOIN_MESSAGE:
+ return new JoinMessage(serializer, buf,endpoint);
+ case RegionMessage.GAMESTATE_MESSAGE:
+ return new GameStateMessage(serializer, buf,endpoint);
+ case RegionMessage.GAMEACTION_MESSAGE:
+ return new GameActionMessage(serializer, buf,endpoint);
+ case RegionMessage.GAME_STATE_HASH_MESSAGE:
+ return new GameStateHashMessage(serializer, buf,endpoint);
+ }
+ System.out.println("Found unknown message: "+type);
+ return null;
+ }
+}
Added: trunk/P2Play/src/org/p2play/net/region/message/RegionMessage.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/message/RegionMessage.java (rev 0)
+++ trunk/P2Play/src/org/p2play/net/region/message/RegionMessage.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.net.region.message;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+import org.p2play.net.region.Region;
+import org.p2play.net.region.RegionInstance;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+import rice.p2p.commonapi.rawserialization.RawMessage;
+import rice.p2p.scribe.rawserialization.RawScribeContent;
+
+public abstract class RegionMessage implements RawMessage, RawScribeContent {
+ public static final short GAMESTATE_MESSAGE = 100;
+
+ public static final short JOIN_MESSAGE = 200;
+
+ public static final short GAMEACTION_MESSAGE = 300;
+
+ public static final short GAME_STATE_HASH_MESSAGE = 400;
+
+ private RegionInstance regionInstance;
+
+ private Id sender;
+
+ private Serializer serializer;
+
+ public RegionMessage(Serializer serializer, Id sender, RegionInstance regionInstance) {
+ super();
+ this.serializer = serializer;
+ this.sender = sender;
+ this.regionInstance = regionInstance;
+ }
+
+ protected RegionMessage(Serializer serializer, InputBuffer buf, Endpoint endpoint)
+ throws IOException {
+ this.serializer=serializer;
+ sender = endpoint.readId(buf, buf.readShort());
+ regionInstance=new RegionInstance((Region)serializer.deserialize(buf),buf.readInt());
+ }
+
+ public final Id getSender() {
+ return sender;
+ }
+
+ public int getPriority() {
+ return 0;
+ }
+
+ public Region getRegion() {
+ return regionInstance.getRegion();
+ }
+
+ public RegionInstance getRegionInstance() {
+ return regionInstance;
+ }
+
+ public void setRegionInstance(RegionInstance regionInstance) {
+ this.regionInstance = regionInstance;
+ }
+
+ public final void serialize(OutputBuffer buf) throws IOException {
+ buf.writeShort(sender.getType());
+ sender.serialize(buf);
+
+ serializer.serialize(regionInstance.getRegion(), buf);
+ buf.writeInt(regionInstance.getInstance());
+
+ doSerialize(buf);
+ }
+
+ public abstract void doSerialize(OutputBuffer buf) throws IOException;
+
+ protected final Serializer getSerializer() {
+ return serializer;
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/util/Hashable.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/Hashable.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/Hashable.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util;
+
+public interface Hashable {
+ public int hash();
+}
Added: trunk/P2Play/src/org/p2play/util/Hasher.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/Hasher.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/Hasher.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Random;
+
+
+public class Hasher {
+ @SuppressWarnings("unchecked")
+ public static int hash(Object object) {
+ //System.out.println("Hashing: " + object);
+ if (object == null) {
+ return 0;
+ } else if (object instanceof Hashable) {
+ return hash((Hashable) object);
+
+ }else if (object instanceof byte[]) {
+ return hash((byte[]) object);
+
+ } else if (object instanceof Map) {
+ return hash((Map) object);
+
+ }else if (object instanceof Collection) {
+ return hash((Collection<Object>) object);
+
+ }else if (object instanceof String) {
+ return hash((String) object);
+
+ } else if (object instanceof Boolean) {
+ return hash((Boolean) object);
+
+ } else if (object instanceof Integer) {
+ return hash((Integer) object);
+
+ } else if (object instanceof Float) {
+ return hash((Float) object);
+
+ }else if (object instanceof Random) {
+ return hash((Random) object);
+
+ } else {
+ System.out
+ .println("Hasher got called with invalid object of type: "
+ + object.getClass());
+ return 0;
+ }
+ }
+
+ private static int hash(String object) {
+ try {
+ return hash(object.getBytes("UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ return 0;
+ }
+ private static int hash(byte[] object) {
+ int hash=0;
+
+ for (byte element : object) {
+ hash=hash^element;
+ }
+
+ return 0;
+ }
+ private static int hash(Hashable object) {
+ return object.hash();
+ }
+ private static int hash(Random object) {
+ //clone the random object, so that it doesn't get changed during hashing and take the next int as hash
+ ByteArrayOutputStream bos=new ByteArrayOutputStream();
+ Random cloned;
+ try {
+ new ObjectOutputStream (bos).writeObject(object);
+ cloned = (Random) new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())).readObject();
+ } catch (IOException e) {
+ cloned=new Random(0);
+ }catch (ClassNotFoundException e) {
+ cloned=new Random(0);
+ }
+
+ return cloned.nextInt();
+ }
+
+ private static int hash(Integer object) {
+ return object.intValue();
+ }
+
+ private static int hash(Boolean object) {
+ return object==true ? 1 : 0;
+ }
+
+ private static int hash(Float object) {
+ return Float.floatToIntBits(object.floatValue());
+ }
+ private static int hash(Collection<Object> col) {
+ int hash = 0;
+ Iterator<Object> iterator = col.iterator();
+
+ while (iterator.hasNext()) {
+ Object object = iterator.next();
+
+ hash^=(Hasher.hash(object));
+ }
+ return hash;
+ }
+ @SuppressWarnings("unchecked")
+ private static int hash(Map map) {
+ return hash(map.values());
+ }
+}
Added: trunk/P2Play/src/org/p2play/util/NetRandom.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/NetRandom.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/NetRandom.java 2006-12-26 22:18:35 UTC (rev 9)
@@ -0,0 +1,249 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util;
+
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.util.Random;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+/**
+ * changed readObject and writeObject to use much less space
+ *
+ * @author Markus Heberling <ma...@ti...>
+ *
+ */
+
+public class NetRandom extends Random {
+
+ private long seed;
+
+ private final static long multiplier = 0x5DEECE66DL;
+ private final static long mask = (1L << 48) - 1;
+
+ /**
+ * Creates a new random number generator. This constructor sets the seed of
+ * the random number generator to a value very likely to be distinct from
+ * any other invocation of this constructor.
+ */
+ public NetRandom() {
+ super();
+ }
+
+ /**
+ * Creates a new random number generator using a single <code>long</code>
+ * seed: <blockquote>
+ *
+ * <pre>
+ * public Random(long seed) {
+ * setSeed(seed);
+ * }
+ * </pre>
+ *
+ * </blockquote> Used by method <tt>next</tt> to hold the state of the
+ * pseudorandom number generator.
+ *
+ * @param seed
+ * the initial seed.
+ * @see java.util.Random#setSeed(long)
+ */
+ public NetRandom(long seed) {
+ super(seed);
+ }
+
+ /**
+ * Sets the seed of this random number generator using a single
+ * <code>long</code> seed. The general contract of <tt>setSeed</tt> is
+ * that it alters the state of this random number generator object so as to
+ * be in exactly the same state as if it had just been created with the
+ * argument <tt>seed</tt> as a seed. The method <tt>setSeed</tt> is
+ * implemented by class Random as follows: <blockquote>
+ *
+ * <pre>
+ * synchronized public void setSeed(long seed) {
+ * this.seed = (seed ˆ 0x5DEECE66DL) & ((1L << 48) - 1);
+ * haveNextNextGaussian = false;
+ * }
+ * </pre>
+ *
+ * </blockquote> The implementation of <tt>setSeed</tt> by class
+ * <tt>Random</tt> happens to use only 48 bits of the given seed. In
+ * general, however, an overriding method may use all 64 bits of the long
+ * argument as a seed value.
+ *
+ * Note: Although the seed value is an AtomicLong, this method must still be
+ * synchronized to ensure correct semantics of haveNextNextGaussian.
+ *
+ * @param seed
+ * the initial seed.
+ */
+ @Override
+ synchronized public void setSeed(long seed) {
+ this.seed = (seed ^ multiplier) & mask;
+ haveNextNextGaussian = false;
+ }
+
+ /**
+ * Generates the next pseudorandom number. Subclass should override this, as
+ * this is used by all other methods.
+ * <p>
+ * The general contract of <tt>next</tt> is that it returns an
+ * <tt>int</tt> value and if the argument bits is between <tt>1</tt> and
+ * <tt>32</tt> (inclusive), then that many low-order bits of the returned
+ * value will be (approximately) independently chosen bit values, each of
+ * which is (approximately) equally likely to be <tt>0</tt> or <tt>1</tt>.
+ * The method <tt>next</tt> is implemented by class <tt>Random</tt> as
+ * follows: <blockquote>
+ *
+ * <pre>
+ * synchronized protected int next(int bits) {
+ * seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
+ * return (int) (seed >>> (48 - bits));
+ * }
+ * </pre>
+ *
+ * </blockquote> This is a linear congruential pseudorandom number
+ * generator, as defined by D. H. Lehmer and described by Donald E. Knuth in
+ * <i>The Art of Computer Programming,</i> Volume 2: <i>Seminumerical
+ * Algorithms</i>, section 3.2.1.
+ *
+ * @param bits
+ * random bits
+ * @return the next pseudorandom value from this random number generator's
+ * sequence.
+ * @since JDK1.1
+ */
+ @Override
+ protected int next(int bits) {
+ seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
+ return (int) (seed >>> (48 - bits));
+ }
+
+ private double nextNextGaussian;
+
+ private boolean haveNextNextGaussian = false;
+
+ /**
+ * Returns the next pseudorandom, Gaussian ("normally") distributed
+ * <code>double</code> value with mean <code>0.0</code> and standard
+ * deviation <code>1.0</code> from this random number generator's
+ * sequence.
+ * <p>
+ * The general contract of <tt>nextGaussian</tt> is that one
+ * <tt>double</tt> value, chosen from (approximately) the usual normal
+ * distribution with mean <tt>0.0</tt> and standard deviation <tt>1.0</tt>,
+ * is pseudorandomly generated and returned. The method
+ * <tt>nextGaussian</tt> is implemented by class <tt>Random</tt> as
+ * follows: <blockquote>
+ *
+ * <pre>
+ * synchronized public double nextGaussian() {
+ * if (haveNextNextGaussian) {
+ * haveNextNextGaussian = false;
+ * return nextNextGaussian;
+ * } else {
+ * double v1, v2, s;
+ * do {
+ * v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0
+ * v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0
+ * s = v1 * v1 + v2 * v2;
+ * } while (s >= 1 || s == 0);
+ * double multiplier = Math.sqrt(-2 * Math.log(s) / s);
+ * nextNextGaussian = v2 * multiplier;
+ * haveNextNextGaussian = true;
+ * return v1 * multiplier;
+ * }
+ * }
+ * </pre>
+ *
+ * </blockquote> This uses the <i>polar method</i> of G. E. P. Box, M. E.
+ * Muller, and G. Marsaglia, as described by Donald E. Knuth in <i>The Art
+ * of Computer Programming</i>, Volume 2: <i>Seminumerical Algorithms</i>,
+ * section 3.4.1, subsection C, algorithm P. Note that it generates two
+ * independent values at the cost of only one call to <tt>Math.log</tt>
+ * and one call to <tt>Math.sqrt</tt>.
+ *
+ * @return the next pseudorandom, Gaussian ("normally") distributed
+ * <code>double</code> value with mean <code>0.0</code> and
+ * standard deviation <code>1.0</code> from this random number
+ * generator's sequence.
+ */
+ @Override
+ synchronized public double nextGaussian() {
+ if (haveNextNextGaussian) {
+ haveNextNextGaussian = false;
+ return nextNextGaussian;
+ } else {
+ double v1, v2, s;
+ do {
+ v1 = 2 * nextDouble() - 1; // between -1 and 1
+ v2 = 2 * nextDouble() - 1; // between -1 and 1
+ s = v1 * v1 + v2 * v2;
+ } while (s >= 1 || s == 0);
+ double multiplier = Math.sqrt(-2 * Math.log(s) / s);
+ nextNextGaussian = v2 * multiplier;
+ haveNextNextGaussian = true;
+ return v1 * multiplier;
+ }
+ }
+
+ /**
+ * Reconstitute the <tt>Random</tt> instance from a stream (that is,
+ * deserialize it). The seed is read in as long for historical reasons, but
+ * it is converted to an AtomicLong.
+ */
+ private void readObject(java.io.ObjectInputStream s)
+ throws java.io.IOException, ClassNotFoundException {
+ seed = s.readLong();
+ haveNextNextGaussian = s.readBoolean();
+ if (haveNextNextGaussian)
+ nextNextGaussian = s.readDouble();
+ }
+
+ public NetRandom(InputBuffer buffer) throws IOException {
+ seed = buffer.readLong();
+ haveNextNextGaussian = buffer.readBoolean();
+ if (haveNextNextGaussian)
+ nextNextGaussian = buffer.readDouble();
+ }
+
+ /**
+ * Save the <tt>Random</tt> instance to a stream. The seed of a Random is
+ * serialized as a long for historical reasons.
+ *
+ */
+ synchronized private void writeObject(ObjectOutputStream s)
+ throws IOException {
+ s.writeLong(seed);
+ s.writeBoolean(haveNextNextGaussian);
+ if (haveNextNextGaussian)
+ s.writeDouble(nextNextGaussian);
+ }
+
+ synchronized public void doSerialize(OutputBuffer buffer)
+ throws IOException {
+ buffer.writeLong(seed);
+ buffer.writeBoolean(haveNextNextGaussian);
+ if (haveNextNextGaussian)
+ buffer.writeDouble(nextNextGaussian);
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|