diff options
Diffstat (limited to 'same-android/src/main/java/com')
3 files changed, 44 insertions, 14 deletions
diff --git a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java index a1d71f9..a8ac1e5 100644 --- a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java +++ b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java @@ -1,18 +1,37 @@ package com.orbekk; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.orbekk.discovery.SameService; import com.orbekk.net.Broadcaster; import com.orbekk.same.ClientApp; import com.orbekk.same.ClientServiceImpl; import com.orbekk.same.SameInterface; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; import android.util.Log; +import android.view.View; -public class HelloAndroidActivity extends Activity { - - private static String TAG = "master"; +public class HelloAndroidActivity extends Activity { + private Logger logger = LoggerFactory.getLogger(getClass()); + public void createNetwork(View unused) { + logger.info("Creating network"); + Intent intent = new Intent(this, SameService.class); + intent.setAction("create"); + startService(intent); + } + + public void joinNetwork(View unused) { + logger.info("Joining network"); + Intent intent = new Intent(this, SameService.class); + intent.setAction("join"); + startService(intent); + } + /** * Called when the activity is first created. * @param savedInstanceState If the activity is being re-initialized after @@ -25,16 +44,16 @@ public class HelloAndroidActivity extends Activity { System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv6Addresses", "false"); + + setContentView(R.layout.main); - Log.i(TAG, "onCreate"); - // setContentView(R.layout.main); - ClientApp client = new ClientApp(); - SameInterface client_ = client.getClient(10015, "ClientNetwork", - "http://10.0.0.6:10010/"); - GameController controller = GameController.create( - GameController.newPlayer(), client_); - setContentView(new GameView(this, controller)); - +// ClientApp client = new ClientApp(); +// SameInterface client_ = client.getClient(10015, "ClientNetwork", +// "http://10.0.0.6:10010/"); +// GameController controller = GameController.create( +// GameController.newPlayer(), client_); +// setContentView(new GameView(this, controller)); + // Broadcast broadcast = new Broadcast(this); // broadcast.sendBroadcast("Broadcast test".getBytes(), 10010); // Broadcaster broadcaster = new Broadcaster(); diff --git a/same-android/src/main/java/com/orbekk/discovery/Broadcast.java b/same-android/src/main/java/com/orbekk/discovery/Broadcast.java index 5f592db..c674f65 100644 --- a/same-android/src/main/java/com/orbekk/discovery/Broadcast.java +++ b/same-android/src/main/java/com/orbekk/discovery/Broadcast.java @@ -41,7 +41,7 @@ public class Broadcast { public synchronized boolean sendBroadcast(byte[] data, int port) { try { - socket = new DatagramSocket(port); + socket = new DatagramSocket(0); socket.setBroadcast(true); DatagramPacket packet = new DatagramPacket(data, data.length, getBroadcastAddress(), port); socket.send(packet); diff --git a/same-android/src/main/java/com/orbekk/discovery/SameService.java b/same-android/src/main/java/com/orbekk/discovery/SameService.java index 149b882..154239f 100644 --- a/same-android/src/main/java/com/orbekk/discovery/SameService.java +++ b/same-android/src/main/java/com/orbekk/discovery/SameService.java @@ -12,7 +12,10 @@ import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orbekk.same.MasterApp; + public class SameService extends Service { + final static int PORT = 15066; private Logger logger = LoggerFactory.getLogger(getClass()); private Thread discoveryThread = null; @@ -26,7 +29,7 @@ public class SameService extends Service { @Override public void run() { while (!Thread.interrupted()) { byte[] data = new byte[1024]; - DatagramPacket packet = broadcast.receiveBroadcast(15066); + DatagramPacket packet = broadcast.receiveBroadcast(PORT); String content = new String(packet.getData(), 0, packet.getLength()); Message message = Message.obtain(); message.obj = content; @@ -49,6 +52,11 @@ public class SameService extends Service { } }; + private void sendBroadcastDiscovery() { + byte[] data = "Discover".getBytes(); + new Broadcast(this).sendBroadcast(data, PORT); + } + @Override public IBinder onBind(Intent intent) { return null; @@ -64,6 +72,9 @@ public class SameService extends Service { discoveryThread.start(); } } + if (intent.getAction().equals("join")) { + sendBroadcastDiscovery(); + } return START_STICKY; } |