diff --git a/Galaxy/src/org/galaxy/GameInitiator.java b/Galaxy/src/org/galaxy/GameInitiator.java index 957608d..21c79f2 100644 --- a/Galaxy/src/org/galaxy/GameInitiator.java +++ b/Galaxy/src/org/galaxy/GameInitiator.java @@ -1,10 +1,14 @@ package org.galaxy; +import java.net.DatagramSocket; import java.net.InetAddress; +import java.net.SocketException; import java.util.Date; import org.galaxy.net.HttpUtil; +import org.galaxy.net.UDPUtil; import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class GameInitiator { @@ -15,6 +19,7 @@ public class GameInitiator { protected String baseUrl = BASE_SERVER_URL; protected HttpUtil httpUtil = new HttpUtil(); + DatagramSocket socket = null; public interface CallbackStartMultiplay { void timedout(); @@ -45,14 +50,19 @@ protected void enlist(int port, String name) throws Exception { } - public void startMultiplayer(CallbackStartMultiplay callbackStart, long timeout) { + public void startMultiplayer(CallbackStartMultiplay callbackStart, int myport, long timeout) throws SocketException { Date start = new Date(); - + + socket = new DatagramSocket(myport); + try { while (new Date().getTime() - start.getTime() < timeout) { JSONArray players = getPlayers(); if (players.size() > 0) { - + JSONObject playerJson = (JSONObject) players.get(0); + + socket.connect(InetAddress.getByName((String) playerJson.get("ip")), Integer.parseInt((String) playerJson.get("port"))); + UDPUtil.send(socket, "connect"); } Thread.sleep(Math.min(1000, timeout / 4)); diff --git a/Galaxy/src/org/galaxy/Remote.java b/Galaxy/src/org/galaxy/Remote.java index 783d86e..934fcb2 100644 --- a/Galaxy/src/org/galaxy/Remote.java +++ b/Galaxy/src/org/galaxy/Remote.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.List; +import org.galaxy.net.UDPUtil; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -21,10 +22,22 @@ public class Remote { Game game = null; List newShips = new ArrayList(); + public final static int STATE_WAITING_FOR_CONNECT = 1; + public final static int STATE_CONNECTED = 2; + + public int state; + + public boolean isFirst() { return first; } + public Remote(int myPort) throws SocketException { + socket = new DatagramSocket(myPort); + state = STATE_WAITING_FOR_CONNECT; + } + + public Remote(Game game, String host, int port, Party party) { this.game = game; @@ -59,29 +72,21 @@ public void run() { new Thread(runnable).start(); } + + protected void processConnect() { + if (state == STATE_WAITING_FOR_CONNECT) { + + + } + } + protected void receiveThread() { while (true) { byte[] buffer = new byte[100000]; - DatagramPacket packet = new DatagramPacket(buffer, buffer.length); + try { - socket.receive(packet); - String s = new String(buffer, packet.getOffset(), packet.getLength(), "UTF-8"); - System.out.println("received: " + s); - JSONArray array = (JSONArray) JSONValue.parse(s); - System.out.println("array: " + array); - if (array != null) { - synchronized(newShips) { - for (int i=0; i ships) { public void sendMove(List ships) { String message = shipsToJson(ships); - byte[] bytes; try { - bytes = message.toString().getBytes("UTF-8"); - System.out.println("size: " + bytes.length); - DatagramPacket packet = new DatagramPacket(bytes, bytes.length); - socket.send(packet); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + UDPUtil.send(socket, message); } catch (IOException e) { e.printStackTrace(); diff --git a/Galaxy/src/org/galaxy/net/UDPUtil.java b/Galaxy/src/org/galaxy/net/UDPUtil.java new file mode 100644 index 0000000..c098cf1 --- /dev/null +++ b/Galaxy/src/org/galaxy/net/UDPUtil.java @@ -0,0 +1,45 @@ +package org.galaxy.net; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; + +import org.galaxy.Ship; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +public class UDPUtil { + + /** + * receive a packet from udp + * @param socket + * @return + * @throws IOException + */ + public static String receive(DatagramSocket socket) throws IOException { + byte[] buffer = new byte[100000]; + DatagramPacket packet = new DatagramPacket(buffer, buffer.length); + socket.receive(packet); + return new String(buffer, packet.getOffset(), packet.getLength(), "UTF-8"); + } + + + /** + * send a packet + * @param socket + * @param message + * @throws IOException + */ + public static void send(DatagramSocket socket, String message) throws IOException { + byte[] bytes; + try { + bytes = message.toString().getBytes("UTF-8"); + DatagramPacket packet = new DatagramPacket(bytes, bytes.length); + socket.send(packet); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} diff --git a/Galaxy/test/org/galaxy/GameInitiatorTest.java b/Galaxy/test/org/galaxy/GameInitiatorTest.java index 4aea68b..29944bb 100644 --- a/Galaxy/test/org/galaxy/GameInitiatorTest.java +++ b/Galaxy/test/org/galaxy/GameInitiatorTest.java @@ -1,9 +1,13 @@ package org.galaxy; +import static org.junit.Assert.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import java.net.DatagramSocket; + import org.galaxy.net.HttpUtil; +import org.galaxy.net.UDPUtil; import org.json.simple.JSONArray; import org.junit.Before; import org.junit.Test; @@ -30,18 +34,41 @@ public void testServer() throws Exception { } @Test - public void testStartMultiplayer() throws Exception { + public void testStartMultiplayerTimeout() throws Exception { JSONArray players = getPlayers(); System.out.println("players: " + players); GameInitiator.CallbackStartMultiplay callback = mock(GameInitiator.CallbackStartMultiplay.class); - startMultiplayer(callback, 500); + startMultiplayer(callback, 10002, 500); verify(callback).timedout(); } + @Test + public void testStartMultiplayerConnectExistingPeer() throws Exception { + DatagramSocket socket = new DatagramSocket(10001); + + try { + enlist(10001, "Heinz"); + + + JSONArray players = getPlayers(); + System.out.println("players: " + players); + + GameInitiator.CallbackStartMultiplay callback = mock(GameInitiator.CallbackStartMultiplay.class); + startMultiplayer(callback, 10003, 500); + + String message = UDPUtil.receive(socket); + assertEquals("connect", message); + + } + finally { + socket.close(); + } + } + }