From 95f52f078e618e8c2cc0ccfd0b995051fe9bb5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 28 Feb 2012 13:47:54 +0100 Subject: ClientInterfaceBridge: Queue pending operations. ClientInterfaceBridge queues operations when it is not connected to the service. --- .../orbekk/same/android/ClientInterfaceBridge.java | 45 ++++++++++++---------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'same-android/src/main/java') diff --git a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java index faa1072..8ed38a7 100644 --- a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java +++ b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java @@ -22,7 +22,6 @@ import com.orbekk.same.SameService; import com.orbekk.same.State; import com.orbekk.same.State.Component; import com.orbekk.same.StateChangedListener; -import com.orbekk.same.UpdateConflict; import com.orbekk.same.VariableFactory; import com.orbekk.util.DelayedOperation; @@ -32,6 +31,8 @@ public class ClientInterfaceBridge implements ClientInterface { new ArrayList(); private Map ongoingOperations = new HashMap(); + /** This is used to queue operations until connected to the service. */ + private ArrayList pendingOperations = new ArrayList(); private int nextOperationNumber = 0; class ResponseHandler extends Handler { @@ -80,6 +81,7 @@ public class ClientInterfaceBridge implements ClientInterface { } catch (RemoteException e) { e.printStackTrace(); } + sendPendingOperations(); } } @@ -147,32 +149,35 @@ public class ClientInterfaceBridge implements ClientInterface { } @Override - public DelayedOperation set(Component component) { + public synchronized DelayedOperation set(Component component) { DelayedOperation op = createOperation(); - if (serviceMessenger == null) { - logger.warn("Not connected to service. Ignore update: {}", component); - completeOperation(op.getIdentifier(), - DelayedOperation.Status.createError( - "Not connected to service.")); - return op; - } - Message message = Message.obtain(null, SameService.SET_STATE); message.arg1 = op.getIdentifier(); message.setData(new ComponentBundle(component).getBundle()); message.replyTo = responseMessenger; - try { - logger.info("Sending update to service. No state."); - serviceMessenger.send(message); - logger.info("Service finished update."); - } catch (RemoteException e) { - e.printStackTrace(); - completeOperation(op.getIdentifier(), - DelayedOperation.Status.createError( - "Error contacting service: " + e.getMessage())); - } + pendingOperations.add(message); + sendPendingOperations(); return op; } + + private synchronized void sendPendingOperations() { + if (serviceMessenger == null) { + logger.warn("Not connected to service. Delaying operations {}", + pendingOperations); + return; + } + for (Message message : pendingOperations) { + try { + serviceMessenger.send(message); + } catch (RemoteException e) { + e.printStackTrace(); + completeOperation(message.arg1, + DelayedOperation.Status.createError( + "Error contacting service: " + e.getMessage())); + } + } + pendingOperations.clear(); + } @Override public void addStateListener(StateChangedListener listener) { -- cgit v1.2.3