[P2play-commit] SF.net SVN: p2play: [52] trunk/P2Play/src/main/java/org/p2play/lobby
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2007-03-23 20:23:48
|
Revision: 52
http://p2play.svn.sourceforge.net/p2play/?rev=52&view=rev
Author: tisoft
Date: 2007-03-23 13:23:47 -0700 (Fri, 23 Mar 2007)
Log Message:
-----------
LobbySockets work now
Modified Paths:
--------------
trunk/P2Play/src/main/java/org/p2play/lobby/ClientServerGameInfo.java
trunk/P2Play/src/main/java/org/p2play/lobby/GameInfo.java
trunk/P2Play/src/main/java/org/p2play/lobby/Lobby.java
trunk/P2Play/src/main/java/org/p2play/lobby/LobbyListener.java
trunk/P2Play/src/main/java/org/p2play/lobby/LobbyServerSocket.java
trunk/P2Play/src/main/java/org/p2play/lobby/LobbySocket.java
trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyDeserializer.java
trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyMessage.java
trunk/P2Play/src/main/java/org/p2play/lobby/impl/PublishGameInfoMessage.java
trunk/P2Play/src/main/java/org/p2play/lobby/impl/ScribeLobby.java
Added Paths:
-----------
trunk/P2Play/src/main/java/org/p2play/lobby/LobbyFactory.java
trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyApplication.java
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/ClientServerGameInfo.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/ClientServerGameInfo.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/ClientServerGameInfo.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby;
import java.io.IOException;
@@ -4,24 +22,20 @@
import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.NodeHandle;
-import rice.p2p.commonapi.appsocket.AppSocketReceiver;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
public class ClientServerGameInfo implements GameInfo{
private String name;
private NodeHandle handle;
-
- private Endpoint endpoint;
public ClientServerGameInfo(Endpoint endpoint, InputBuffer buffer) throws IOException{
this.name=buffer.readUTF();
this.handle=endpoint.readNodeHandle(buffer);
}
- public ClientServerGameInfo(Endpoint endpoint, String name, AppSocketReceiver receiver){
+ public ClientServerGameInfo(Endpoint endpoint, String name){
this(endpoint,name,endpoint.getLocalNodeHandle());
- endpoint.accept(receiver);
}
protected ClientServerGameInfo(Endpoint endpoint, String name, NodeHandle handle) {
@@ -38,10 +52,6 @@
return handle;
}
- public void connect(AppSocketReceiver receiver){
- endpoint.connect(handle, receiver, 60);
- }
-
public void serialize(OutputBuffer buf) throws IOException {
buf.writeUTF(name);
handle.serialize(buf);
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/GameInfo.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/GameInfo.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/GameInfo.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,11 +1,30 @@
+/*
+ * Copyright 2007 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.lobby;
import java.io.IOException;
+import java.io.Serializable;
import rice.p2p.commonapi.NodeHandle;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
-public interface GameInfo {
+public interface GameInfo extends Serializable{
public String getName();
public NodeHandle getHandle();
public void serialize(OutputBuffer buf) throws IOException;
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/Lobby.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/Lobby.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/Lobby.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby;
@@ -9,4 +27,6 @@
public void addLobbyListener(LobbyListener listener);
public void removeLobbyListener(LobbyListener listener);
+
+ public GameInfo getGameInfo(String string);
}
Added: trunk/P2Play/src/main/java/org/p2play/lobby/LobbyFactory.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/LobbyFactory.java (rev 0)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/LobbyFactory.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2007 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.lobby;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javolution.util.FastMap;
+
+import org.p2play.lobby.impl.LobbyApplication;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Node;
+import rice.p2p.commonapi.NodeHandle;
+
+public class LobbyFactory {
+ private Node node;
+
+ private Map<String, Endpoint> endpointMap;
+
+ public LobbyFactory(Node node) {
+ super();
+ this.node = node;
+ endpointMap = new FastMap<String, Endpoint>();
+ }
+
+ public LobbyServerSocket createServerSocket(String name) throws IOException {
+ Endpoint endpoint = node.buildEndpoint(new LobbyApplication(), name);
+
+ LobbyServerSocket serverSocket = new LobbyServerSocket(endpoint);
+
+ endpoint.accept(serverSocket);
+
+ endpoint.register();
+
+ endpointMap.put(name, endpoint);
+
+ return serverSocket;
+ }
+
+ public LobbySocket createSocket(String name, NodeHandle handle) throws IOException {
+ Endpoint endpoint = endpointMap.get(name);
+
+ if (endpoint == null) {
+ endpoint=node.buildEndpoint(new LobbyApplication(), "test");
+
+ endpoint.register();
+ endpointMap.put(name, endpoint);
+ }
+ LobbySocket lobbySocket = new LobbySocket(endpoint, handle);
+
+ return lobbySocket;
+ }
+}
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/LobbyListener.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/LobbyListener.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/LobbyListener.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby;
public interface LobbyListener {
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/LobbyServerSocket.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/LobbyServerSocket.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/LobbyServerSocket.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby;
import java.io.IOException;
@@ -4,6 +22,7 @@
import java.net.ServerSocket;
import java.net.Socket;
+import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.appsocket.AppSocket;
import rice.p2p.commonapi.appsocket.AppSocketReceiver;
@@ -13,8 +32,11 @@
private LobbySocket lobbySocket;
- public LobbyServerSocket() throws IOException {
+ private Endpoint endpoint;
+
+ public LobbyServerSocket(Endpoint endpoint) throws IOException {
super();
+ this.endpoint=endpoint;
}
public void receiveException(AppSocket socket, Exception e) {
@@ -36,6 +58,7 @@
e.printStackTrace();
}
socket.register(true, true, 30000, lobbySocket);
+ endpoint.accept(this);
mutex.notifyAll();
}
@@ -54,4 +77,8 @@
return lobbySocket;
}
}
+
+ public Endpoint getEndpoint() {
+ return endpoint;
+ }
}
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/LobbySocket.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/LobbySocket.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/LobbySocket.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby;
import java.io.IOException;
@@ -5,8 +23,10 @@
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
+import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
+import java.util.Random;
import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.NodeHandle;
@@ -27,6 +47,8 @@
private AppSocket socket;
+ private InetAddress address;
+
public LobbySocket() throws IOException {
buffer = new ByteBuffer[2];
buffer[0] = ByteBuffer.wrap(new byte[1024]);
@@ -38,6 +60,12 @@
outputStream = new PipedOutputStream();
pipedInputStream = new PipedInputStream(
(PipedOutputStream) outputStream);
+
+ Random random=new Random();
+
+ byte[] b=new byte[4];
+ random.nextBytes(b);
+ address=InetAddress.getByAddress(b);
}
public LobbySocket(Endpoint endpoint, NodeHandle handle) throws IOException {
@@ -54,11 +82,13 @@
boolean canWrite) {
try {
if (canRead) {
- socket.read(buffer, 0, 1);
+ long i=socket.read(buffer, 0, 1);
pipedOutputStream.write(buffer[0].array(),
- buffer[0].position(), buffer[0].remaining());
+ 0, (int)i);
pipedOutputStream.flush();
buffer[0].rewind();
+
+ System.out.println("Read: "+i);
}
if (canWrite && pipedInputStream.available() > 0) {
@@ -66,9 +96,9 @@
pipedInputStream.read(b);
buffer[1] = ByteBuffer.wrap(b);
long i=socket.write(buffer, 1, 1);
+
System.out.println("Wrote: "+i);
- socket.shutdownOutput();
- }
+ }
socket.register(true, true, 30000, this);
} catch (Exception e) {
@@ -78,6 +108,7 @@
public void receiveSocket(AppSocket socket) {
this.socket=socket;
+ socket.register(true, true, 30000, this);
}
@Override
@@ -94,4 +125,9 @@
public synchronized void close() throws IOException {
socket.close();
}
+
+ @Override
+ public InetAddress getInetAddress() {
+ return address;
+ }
}
Added: trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyApplication.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyApplication.java (rev 0)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyApplication.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2007 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.lobby.impl;
+
+import rice.p2p.commonapi.Application;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.Message;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.commonapi.RouteMessage;
+
+public class LobbyApplication implements Application {
+
+ public void deliver(Id id, Message message) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public boolean forward(RouteMessage message) {
+ return true;
+ }
+
+ public void update(NodeHandle handle, boolean joined) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyDeserializer.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyDeserializer.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyDeserializer.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby.impl;
import java.io.IOException;
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyMessage.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyMessage.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/impl/LobbyMessage.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,5 +1,28 @@
+/*
+ * Copyright 2007 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.lobby.impl;
-public interface LobbyMessage {
+import java.io.Serializable;
+
+import rice.p2p.commonapi.Message;
+import rice.p2p.scribe.ScribeContent;
+
+public interface LobbyMessage extends Message, ScribeContent, Serializable{
public static final short TYPE_PUBLISH_GAME=1;
}
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/impl/PublishGameInfoMessage.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/impl/PublishGameInfoMessage.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/impl/PublishGameInfoMessage.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,3 +1,21 @@
+/*
+ * Copyright 2007 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.lobby.impl;
import java.io.IOException;
@@ -8,10 +26,8 @@
import rice.p2p.commonapi.Endpoint;
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 class PublishGameInfoMessage implements RawMessage,RawScribeContent,LobbyMessage {
+public class PublishGameInfoMessage implements LobbyMessage {
private GameInfo gameInfo;
Modified: trunk/P2Play/src/main/java/org/p2play/lobby/impl/ScribeLobby.java
===================================================================
--- trunk/P2Play/src/main/java/org/p2play/lobby/impl/ScribeLobby.java 2007-03-20 12:28:02 UTC (rev 51)
+++ trunk/P2Play/src/main/java/org/p2play/lobby/impl/ScribeLobby.java 2007-03-23 20:23:47 UTC (rev 52)
@@ -1,7 +1,28 @@
+/*
+ * Copyright 2007 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.lobby.impl;
+import java.util.Map;
+
import javolution.util.FastCollection;
import javolution.util.FastList;
+import javolution.util.FastMap;
import javolution.util.FastCollection.Record;
import org.p2play.lobby.GameInfo;
@@ -21,7 +42,7 @@
import rice.p2p.scribe.ScribeImpl;
import rice.p2p.scribe.Topic;
-public class ScribeLobby implements Lobby, Application, ScribeClient {
+public class ScribeLobby implements Lobby, Application, ScribeClient, LobbyListener {
private FastCollection<LobbyListener> listeners;
@@ -32,16 +53,19 @@
private Endpoint endpoint;
private Topic topic;
+
+ private Map<String, GameInfo> gameInfoMap;
public ScribeLobby(Node node) {
-
listeners=new FastList<LobbyListener>();
+ this.addLobbyListener(this);
publishedGames=new FastList<GameInfo>();
+ gameInfoMap=new FastMap<String, GameInfo>();
endpoint=node.buildEndpoint(this, "lobby_endpoint");
- LobbyDeserializer deserializer=new LobbyDeserializer(endpoint);
- endpoint.setDeserializer(deserializer);
+ //LobbyDeserializer deserializer=new LobbyDeserializer(endpoint);
+ //endpoint.setDeserializer(deserializer);
scribe=new ScribeImpl(node,"lobby_scribe");
- scribe.setContentDeserializer(deserializer);
+ //scribe.setContentDeserializer(deserializer);
topic=new Topic(node.getIdFactory(),"lobby_toppc");
endpoint.register();
@@ -60,10 +84,22 @@
publishedGames.remove(gameInfo);
}
- public void publishGame(GameInfo gameInfo) {
+ public void publishGame(final GameInfo gameInfo) {
publishedGames.add(gameInfo);
- scribe.publish(topic, new PublishGameInfoMessage(gameInfo));
+ //for now republish every 5 seconds
+ new Thread(new Runnable(){public void run() {
+ while (true) {
+ scribe.publish(topic, new PublishGameInfoMessage(gameInfo));
+
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }}).start();
}
public void deliver(Id id, Message message) {
@@ -119,5 +155,15 @@
}
+ public void gameCreated(GameInfo gameInfo) {
+ gameInfoMap.put(gameInfo.getName(), gameInfo);
+ System.out.println(gameInfo+" created.");
+ }
+ public void gameRemoved(GameInfo gameInfo) {
+ gameInfoMap.remove(gameInfo.getName());
+ }
+ public GameInfo getGameInfo(String name) {
+ return gameInfoMap.get(name);
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|