summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorMark Spruiell <mes@zeroc.com>2015-03-11 13:00:43 -0700
committerMark Spruiell <mes@zeroc.com>2015-03-11 13:00:43 -0700
commit5166c1c2ec79223221fbfcadf1f8ef5e7bb9bae9 (patch)
treec919eb10cefb7b31ca0030405632c3100fa7ec68 /android
parentFixed OutputUtil to not use char* in the API (diff)
downloadice-5166c1c2ec79223221fbfcadf1f8ef5e7bb9bae9.tar.bz2
ice-5166c1c2ec79223221fbfcadf1f8ef5e7bb9bae9.tar.xz
ice-5166c1c2ec79223221fbfcadf1f8ef5e7bb9bae9.zip
ICE-6232 - UI demos should use dispatcher
Diffstat (limited to 'android')
-rw-r--r--android/demo/chat/src/main/java/com/zeroc/chat/service/AppSession.java276
-rw-r--r--android/demo/chat/src/main/java/com/zeroc/chat/service/ChatService.java2
-rw-r--r--android/demo/hello/src/main/java/com/zeroc/hello/HelloApp.java11
-rw-r--r--android/demo/library/src/main/java/com/zeroc/library/controller/LoginController.java95
-rw-r--r--android/demo/library/src/main/java/com/zeroc/library/controller/SessionController.java6
5 files changed, 219 insertions, 171 deletions
diff --git a/android/demo/chat/src/main/java/com/zeroc/chat/service/AppSession.java b/android/demo/chat/src/main/java/com/zeroc/chat/service/AppSession.java
index 82faea8863f..e98e1fd43e2 100644
--- a/android/demo/chat/src/main/java/com/zeroc/chat/service/AppSession.java
+++ b/android/demo/chat/src/main/java/com/zeroc/chat/service/AppSession.java
@@ -40,7 +40,7 @@ public class AppSession
private String _hostname;
private String _error;
- public AppSession(Resources resources, Handler handler, String hostname, String username,
+ public AppSession(Resources resources, final Handler handler, String hostname, String username,
String password, boolean secure)
throws Glacier2.CannotCreateSessionException, Glacier2.PermissionDeniedException
{
@@ -48,6 +48,16 @@ public class AppSession
Ice.InitializationData initData = new Ice.InitializationData();
+ initData.dispatcher = new Ice.Dispatcher()
+ {
+ @Override
+ public void
+ dispatch(Runnable runnable, Ice.Connection connection)
+ {
+ handler.post(runnable);
+ }
+ };
+
initData.properties = Ice.Util.createProperties();
initData.properties.setProperty("Ice.RetryIntervals", "-1");
initData.properties.setProperty("Ice.Trace.Network", "0");
@@ -102,7 +112,42 @@ public class AppSession
adapter.activate();
_lastSend = System.currentTimeMillis();
- _refreshTimeout = (_router.getSessionTimeout() * 1000) / 2;
+ int acmTimeout = 0;
+ try
+ {
+ acmTimeout = _router.getACMTimeout();
+ }
+ catch(Ice.OperationNotExistException ex)
+ {
+ }
+ if(acmTimeout <= 0)
+ {
+ acmTimeout = (int)_router.getSessionTimeout();
+ }
+
+ Ice.Connection connection = _router.ice_getCachedConnection();
+ assert(connection != null);
+ connection.setCallback(new Ice.ConnectionCallback()
+ {
+ @Override
+ public void heartbeat(Ice.Connection con)
+ {
+ }
+
+ @Override
+ public void closed(Ice.Connection con)
+ {
+ destroyWithError("Connection closed");
+ }
+ });
+
+ if(acmTimeout > 0)
+ {
+ connection.setACM(new Ice.IntOptional(acmTimeout), null,
+ new Ice.Optional<Ice.ACMHeartbeat>(Ice.ACMHeartbeat.HeartbeatAlways));
+ }
+
+ _refreshTimeout = (acmTimeout * 1000) / 2;
}
synchronized public long getRefreshTimeout()
@@ -157,26 +202,25 @@ public class AppSession
}
_lastSend = System.currentTimeMillis();
- Chat.Callback_ChatSession_send cb = new Chat.Callback_ChatSession_send()
- {
- @Override
- public void exception(Ice.LocalException ex)
+ _session.begin_send(t, new Chat.Callback_ChatSession_send()
{
- destroyWithError(ex.toString());
- }
+ @Override
+ public void exception(Ice.LocalException ex)
+ {
+ destroyWithError(ex.toString());
+ }
- @Override
- public void exception(Ice.UserException ex)
- {
- destroyWithError(ex.toString());
- }
+ @Override
+ public void exception(Ice.UserException ex)
+ {
+ destroyWithError(ex.toString());
+ }
- @Override
- public void response(long ret)
- {
- }
- };
- _session.begin_send(t, cb);
+ @Override
+ public void response(long ret)
+ {
+ }
+ });
}
// This method is only called by the UI thread.
@@ -237,28 +281,6 @@ public class AppSession
return false;
}
- try
- {
- _session.begin_ice_ping(new Ice.Callback_Object_ice_ping()
- {
- @Override
- public void response()
- {
- }
-
- @Override
- public void exception(final Ice.LocalException ex)
- {
- destroyWithError(ex.toString());
- }
- });
- }
- catch(Ice.LocalException e)
- {
- destroyWithError(e.toString());
- return false;
- }
-
return true;
}
@@ -269,130 +291,134 @@ public class AppSession
private class ChatCallbackI extends Chat._ChatRoomCallbackDisp
{
- synchronized public void init(String[] users, Ice.Current current)
+ public void init(String[] users, Ice.Current current)
{
+ List<ChatRoomListener> copy;
final List<String> u = Arrays.asList(users);
- _users.clear();
- _users.addAll(u);
- // No replay event for init.
- final List<ChatRoomListener> copy = new ArrayList<ChatRoomListener>(_listeners);
- _handler.post(new Runnable()
+ synchronized(this)
{
- public void run()
- {
- for(ChatRoomListener listener : copy)
- {
- listener.init(u);
- }
- }
- });
- }
+ _users.clear();
+ _users.addAll(u);
- synchronized public void join(final long timestamp, final String name, Ice.Current current)
- {
- _replay.add(new ChatEventReplay()
- {
- public void replay(ChatRoomListener cb)
- {
- cb.join(timestamp, name);
- }
- });
- if(_replay.size() > MAX_MESSAGES)
+ // No replay event for init.
+ copy = new ArrayList<ChatRoomListener>(_listeners);
+ }
+
+ for(ChatRoomListener listener : copy)
{
- _replay.removeFirst();
+ listener.init(u);
}
+ }
- _users.add(name);
+ public void join(final long timestamp, final String name, Ice.Current current)
+ {
+ List<ChatRoomListener> copy;
- final List<ChatRoomListener> copy = new ArrayList<ChatRoomListener>(_listeners);
- _handler.post(new Runnable()
+ synchronized(this)
{
- public void run()
+ _replay.add(new ChatEventReplay()
{
- for(ChatRoomListener listener : copy)
+ public void replay(ChatRoomListener cb)
{
- listener.join(timestamp, name);
+ cb.join(timestamp, name);
}
- }
- });
- }
-
- synchronized public void leave(final long timestamp, final String name, Ice.Current current)
- {
- _replay.add(new ChatEventReplay()
- {
- public void replay(ChatRoomListener cb)
+ });
+ if(_replay.size() > MAX_MESSAGES)
{
- cb.leave(timestamp, name);
+ _replay.removeFirst();
}
- });
- if(_replay.size() > MAX_MESSAGES)
+
+ _users.add(name);
+
+ copy = new ArrayList<ChatRoomListener>(_listeners);
+ }
+
+ for(ChatRoomListener listener : copy)
{
- _replay.removeFirst();
+ listener.join(timestamp, name);
}
+ }
- _users.remove(name);
+ public void leave(final long timestamp, final String name, Ice.Current current)
+ {
+ List<ChatRoomListener> copy;
- final List<ChatRoomListener> copy = new ArrayList<ChatRoomListener>(_listeners);
- _handler.post(new Runnable()
+ synchronized(this)
{
- public void run()
+ _replay.add(new ChatEventReplay()
{
- for(ChatRoomListener listener : copy)
+ public void replay(ChatRoomListener cb)
{
- listener.leave(timestamp, name);
+ cb.leave(timestamp, name);
}
- }
- });
- }
-
- synchronized public void send(final long timestamp, final String name, final String message, Ice.Current current)
- {
- _replay.add(new ChatEventReplay()
- {
- public void replay(ChatRoomListener cb)
+ });
+ if(_replay.size() > MAX_MESSAGES)
{
- cb.send(timestamp, name, message);
+ _replay.removeFirst();
}
- });
- if(_replay.size() > MAX_MESSAGES)
+
+ _users.remove(name);
+
+ copy = new ArrayList<ChatRoomListener>(_listeners);
+ }
+
+ for(ChatRoomListener listener : copy)
{
- _replay.removeFirst();
+ listener.leave(timestamp, name);
}
+ }
- final List<ChatRoomListener> copy = new ArrayList<ChatRoomListener>(_listeners);
- _handler.post(new Runnable()
+ public void send(final long timestamp, final String name, final String message, Ice.Current current)
+ {
+ List<ChatRoomListener> copy;
+
+ synchronized(this)
{
- public void run()
+ _replay.add(new ChatEventReplay()
{
- for(ChatRoomListener listener : copy)
+ public void replay(ChatRoomListener cb)
{
- listener.send(timestamp, name, message);
+ cb.send(timestamp, name, message);
}
+ });
+ if(_replay.size() > MAX_MESSAGES)
+ {
+ _replay.removeFirst();
}
- });
+
+ copy = new ArrayList<ChatRoomListener>(_listeners);
+ }
+
+ for(ChatRoomListener listener : copy)
+ {
+ listener.send(timestamp, name, message);
+ }
}
-
+
public static final long serialVersionUID = 1;
}
+ //
// Any exception destroys the session.
- synchronized private void destroyWithError(final String msg)
+ //
+ // Should only be called from the main thread.
+ //
+ private void destroyWithError(final String msg)
{
- destroy();
- _error = msg;
+ List<ChatRoomListener> copy;
- final List<ChatRoomListener> copy = new ArrayList<ChatRoomListener>(_listeners);
- _handler.post(new Runnable()
+ synchronized(this)
{
- public void run()
- {
- for(ChatRoomListener listener : copy)
- {
- listener.error();
- }
- }
- });
+ destroy();
+ _error = msg;
+
+ copy = new ArrayList<ChatRoomListener>(_listeners);
+ }
+
+ for(ChatRoomListener listener : copy)
+ {
+ listener.error();
+ }
}
}
diff --git a/android/demo/chat/src/main/java/com/zeroc/chat/service/ChatService.java b/android/demo/chat/src/main/java/com/zeroc/chat/service/ChatService.java
index 2714e04d9b1..66d06556142 100644
--- a/android/demo/chat/src/main/java/com/zeroc/chat/service/ChatService.java
+++ b/android/demo/chat/src/main/java/com/zeroc/chat/service/ChatService.java
@@ -194,7 +194,7 @@ public class ChatService extends Service implements com.zeroc.chat.service.Servi
synchronized public String getSessionError()
{
- return _session.getError();
+ return _session != null ? _session.getError() : "";
}
synchronized private void postLoginFailure(final String loginError)
diff --git a/android/demo/hello/src/main/java/com/zeroc/hello/HelloApp.java b/android/demo/hello/src/main/java/com/zeroc/hello/HelloApp.java
index d427c99cc96..26f779b3ca1 100644
--- a/android/demo/hello/src/main/java/com/zeroc/hello/HelloApp.java
+++ b/android/demo/hello/src/main/java/com/zeroc/hello/HelloApp.java
@@ -77,6 +77,17 @@ public class HelloApp extends Application
try
{
Ice.InitializationData initData = new Ice.InitializationData();
+
+ initData.dispatcher = new Ice.Dispatcher()
+ {
+ @Override
+ public void
+ dispatch(Runnable runnable, Ice.Connection connection)
+ {
+ _uiHandler.post(runnable);
+ }
+ };
+
initData.properties = Ice.Util.createProperties();
initData.properties.setProperty("Ice.Trace.Network", "3");
diff --git a/android/demo/library/src/main/java/com/zeroc/library/controller/LoginController.java b/android/demo/library/src/main/java/com/zeroc/library/controller/LoginController.java
index a67928da233..a20572e6f9a 100644
--- a/android/demo/library/src/main/java/com/zeroc/library/controller/LoginController.java
+++ b/android/demo/library/src/main/java/com/zeroc/library/controller/LoginController.java
@@ -47,50 +47,13 @@ public class LoginController
}
}
- public LoginController(Resources resources, final String hostname, final boolean secure, final boolean glacier2,
- Listener listener)
+ public LoginController(final Resources resources, final String hostname, final boolean secure,
+ final boolean glacier2, Listener listener)
{
_handler = new Handler();
_loginListener = listener;
_loginListener.onLoginInProgress();
- Ice.InitializationData initData = new Ice.InitializationData();
-
- initData.properties = Ice.Util.createProperties();
- if(glacier2)
- {
- initData.properties.setProperty("Ice.RetryIntervals", "-1");
- initData.properties.setProperty("Ice.ACM.Timeout", "0");
- }
- initData.properties.setProperty("Ice.Trace.Network", "0");
-
- if(secure)
- {
- initData.properties.setProperty("IceSSL.Trace.Security", "0");
- initData.properties.setProperty("IceSSL.TruststoreType", "BKS");
- initData.properties.setProperty("IceSSL.Password", "password");
- initData.properties.setProperty("Ice.InitPlugins", "0");
- initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory");
-
- // SDK versions < 21 only support TLSv1 with SSLEngine.
- if(Build.VERSION.SDK_INT < 21)
- {
- initData.properties.setProperty("IceSSL.Protocols", "tls1_0");
- }
-
- java.io.InputStream certStream;
- certStream = resources.openRawResource(R.raw.client);
- _communicator = Ice.Util.initialize(initData);
-
- IceSSL.Plugin plugin = (IceSSL.Plugin)_communicator.getPluginManager().getPlugin("IceSSL");
- plugin.setTruststoreStream(certStream);
- _communicator.getPluginManager().initializePlugins();
- }
- else
- {
- _communicator = Ice.Util.initialize(initData);
- }
-
new Thread(new Runnable()
{
public void run()
@@ -98,6 +61,53 @@ public class LoginController
try
{
long refreshTimeout;
+
+ Ice.InitializationData initData = new Ice.InitializationData();
+
+ initData.dispatcher = new Ice.Dispatcher()
+ {
+ @Override
+ public void
+ dispatch(Runnable runnable, Ice.Connection connection)
+ {
+ _handler.post(runnable);
+ }
+ };
+
+ initData.properties = Ice.Util.createProperties();
+ if(glacier2)
+ {
+ initData.properties.setProperty("Ice.RetryIntervals", "-1");
+ }
+ initData.properties.setProperty("Ice.Trace.Network", "0");
+
+ if(secure)
+ {
+ initData.properties.setProperty("IceSSL.Trace.Security", "0");
+ initData.properties.setProperty("IceSSL.TruststoreType", "BKS");
+ initData.properties.setProperty("IceSSL.Password", "password");
+ initData.properties.setProperty("Ice.InitPlugins", "0");
+ initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory");
+
+ // SDK versions < 21 only support TLSv1 with SSLEngine.
+ if(Build.VERSION.SDK_INT < 21)
+ {
+ initData.properties.setProperty("IceSSL.Protocols", "tls1_0");
+ }
+
+ java.io.InputStream certStream;
+ certStream = resources.openRawResource(R.raw.client);
+ _communicator = Ice.Util.initialize(initData);
+
+ IceSSL.Plugin plugin = (IceSSL.Plugin)_communicator.getPluginManager().getPlugin("IceSSL");
+ plugin.setTruststoreStream(certStream);
+ _communicator.getPluginManager().initializePlugins();
+ }
+ else
+ {
+ _communicator = Ice.Util.initialize(initData);
+ }
+
SessionAdapter session = null;
if(glacier2)
@@ -122,12 +132,14 @@ public class LoginController
postLoginFailure("Glacier2 proxy is invalid.");
return;
}
+
Glacier2.SessionPrx glacier2session = router.createSession("dummy", "none");
- final Demo.Glacier2SessionPrx sess = Demo.Glacier2SessionPrxHelper
- .uncheckedCast(glacier2session);
+ final Demo.Glacier2SessionPrx sess =
+ Demo.Glacier2SessionPrxHelper.uncheckedCast(glacier2session);
final Demo.LibraryPrx library = sess.getLibrary();
refreshTimeout = (router.getSessionTimeout() * 1000) / 2;
+
session = new SessionAdapter()
{
public void destroy()
@@ -173,6 +185,7 @@ public class LoginController
final Demo.SessionPrx sess = factory.create();
final Demo.LibraryPrx library = sess.getLibrary();
refreshTimeout = (factory.getSessionTimeout() * 1000) / 2;
+
session = new SessionAdapter()
{
public void destroy()
diff --git a/android/demo/library/src/main/java/com/zeroc/library/controller/SessionController.java b/android/demo/library/src/main/java/com/zeroc/library/controller/SessionController.java
index 00bb20447d8..b80dd4ceba2 100644
--- a/android/demo/library/src/main/java/com/zeroc/library/controller/SessionController.java
+++ b/android/demo/library/src/main/java/com/zeroc/library/controller/SessionController.java
@@ -34,8 +34,7 @@ public class SessionController
_timeout = timeout; // seconds.
}
- synchronized public void
- run()
+ synchronized public void run()
{
while(!_terminated)
{
@@ -62,8 +61,7 @@ public class SessionController
}
}
- synchronized private void
- terminate()
+ synchronized private void terminate()
{
_terminated = true;
notify();