Skip to content

Commit

Permalink
multiplaye dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Lankenau committed May 20, 2011
1 parent 673087b commit fd2c6ce
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 30 deletions.
16 changes: 13 additions & 3 deletions Galaxy/src/org/galaxy/GameInitiator.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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));
Expand Down
49 changes: 24 additions & 25 deletions Galaxy/src/org/galaxy/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,10 +22,22 @@ public class Remote {
Game game = null;
List<Ship> newShips = new ArrayList<Ship>();

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;
Expand Down Expand Up @@ -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<array.size(); i++) {
JSONObject shipJson = (JSONObject) array.get(i);
Ship ship = new Ship(game, shipJson);
newShips.add(ship);
}
}
}


String message = UDPUtil.receive(socket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Expand All @@ -104,14 +109,8 @@ public static String shipsToJson(List<Ship> ships) {

public void sendMove(List<Ship> 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();
Expand Down
45 changes: 45 additions & 0 deletions Galaxy/src/org/galaxy/net/UDPUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
31 changes: 29 additions & 2 deletions Galaxy/test/org/galaxy/GameInitiatorTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}



}

0 comments on commit fd2c6ce

Please sign in to comment.