From 5b27e3c7b289792b6520a099280a6c1bc28c20e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Wed, 18 Jan 2012 21:08:26 +0100 Subject: Hack: Use Same to synchronize state in the android app. --- .../src/main/java/com/orbekk/GameView.java | 26 ++++++++++++++++++---- .../main/java/com/orbekk/HelloAndroidActivity.java | 8 ++++--- same/src/main/java/com/orbekk/same/ClientApp.java | 2 +- .../java/com/orbekk/same/ClientServiceImpl.java | 16 +++++++++++++ same/src/main/java/com/orbekk/same/State.java | 12 ++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/same-android/src/main/java/com/orbekk/GameView.java b/same-android/src/main/java/com/orbekk/GameView.java index 4accd7a..edb41cb 100644 --- a/same-android/src/main/java/com/orbekk/GameView.java +++ b/same-android/src/main/java/com/orbekk/GameView.java @@ -3,6 +3,10 @@ package com.orbekk; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orbekk.same.State.Component; +import com.orbekk.same.ClientService; +import com.orbekk.same.ClientServiceImpl; + import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; @@ -15,6 +19,7 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback { private GameThread thread; static class GameThread extends Thread { + private Logger logger = LoggerFactory.getLogger(getClass()); private int height = 0; private int width = 0; private float posX; @@ -23,10 +28,13 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback { private Context context; private Paint background; private Paint paint; + private ClientServiceImpl client; - public GameThread(SurfaceHolder holder, Context context) { + public GameThread(SurfaceHolder holder, Context context, + ClientServiceImpl client) { this.holder = holder; this.context = context; + this.client = client; posX = 100; posY = 100; paint = new Paint(); @@ -60,17 +68,27 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback { } } - private void setPosition(float x, float y) { + private synchronized void setPosition(float x, float y) { posX = x; posY = y; run(); + long rev = 0; + Component c = client.getState("position"); + if (c != null) { + rev = c.getRevision(); + } + + if (client.sendStateUpdate("position", this.posX + "," + this.posY, + rev + 1)) { + logger.warn("Unable to set state."); + } } } - public GameView(Context context) { + public GameView(Context context, ClientServiceImpl client) { super(context); getHolder().addCallback(this); - thread = new GameThread(getHolder(), context); + thread = new GameThread(getHolder(), context, client); } @Override diff --git a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java index cc90a3a..9df93d9 100644 --- a/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java +++ b/same-android/src/main/java/com/orbekk/HelloAndroidActivity.java @@ -2,6 +2,7 @@ package com.orbekk; import com.orbekk.net.Broadcaster; import com.orbekk.same.ClientApp; +import com.orbekk.same.ClientServiceImpl; import android.app.Activity; import android.os.Bundle; @@ -26,15 +27,16 @@ public class HelloAndroidActivity extends Activity { Log.i(TAG, "onCreate"); // setContentView(R.layout.main); - setContentView(new GameView(this)); + ClientApp client = new ClientApp(); + ClientServiceImpl client_ = client.getClient(10015, "ClientNetwork", + "http://10.0.0.6:10010/"); + setContentView(new GameView(this, client_)); // Broadcast broadcast = new Broadcast(this); // broadcast.sendBroadcast("Broadcast test".getBytes(), 10010); // Broadcaster broadcaster = new Broadcaster(); // Log.i(TAG, "Broadcast success: " + broadcaster.sendBroadcast(10010, "Broadcast test from Android".getBytes())); // -// ClientApp client = new ClientApp(); -// client.run(10015, "ClientNetwork", "http://10.0.0.6:10010/"); } @Override diff --git a/same/src/main/java/com/orbekk/same/ClientApp.java b/same/src/main/java/com/orbekk/same/ClientApp.java index 5829c5d..970f16d 100644 --- a/same/src/main/java/com/orbekk/same/ClientApp.java +++ b/same/src/main/java/com/orbekk/same/ClientApp.java @@ -12,7 +12,7 @@ public class ClientApp { private Server server; private static final int timeout = 1000; - public ClientService getClient(int port, String networkName, + public ClientServiceImpl getClient(int port, String networkName, String masterUrl) { logger.info("Starting client with port:{}, networkName:{}, masterUrl:{}", new Object[]{port, networkName, masterUrl}); diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java index 3f84c2e..46cdd86 100644 --- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java @@ -53,6 +53,22 @@ public class ClientServiceImpl implements ClientService, UrlReceiver { } } + public boolean sendStateUpdate(String componentName, String data, + long revision) { + String masterUrl = state.getDataOf(".masterUrl"); + MasterService master = connections.getMaster(masterUrl); + try { + return master.updateStateRequest(componentName, data, revision); + } catch (Exception e) { + logger.error("Unable to contact master. Update fails."); + return false; + } + } + + public State.Component getState(String name) { + return state.getComponent(name); + } + State testGetState() { return state; } diff --git a/same/src/main/java/com/orbekk/same/State.java b/same/src/main/java/com/orbekk/same/State.java index 58cb5b5..f8ba6a4 100644 --- a/same/src/main/java/com/orbekk/same/State.java +++ b/same/src/main/java/com/orbekk/same/State.java @@ -67,6 +67,18 @@ public class State { return false; } } + + /** + * Get a copy of a component. + */ + public Component getComponent(String name) { + Component component = state.get(name); + if (component != null) { + return new Component(component); + } else { + return null; + } + } public String getDataOf(String componentName) { Component component = state.get(componentName); -- cgit v1.2.3