aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAng Li <angli@google.com>2017-02-22 13:07:00 -0800
committerGitHub <noreply@github.com>2017-02-22 13:07:00 -0800
commitb1f23a3dd1fce48791758fc57b0a35f8da6fe961 (patch)
tree65086d01ea136cfeed2de6e0d448f6d30142221a /examples
parent0f9ab75ee996dbf8f3f1e9f400c71068df5bfe62 (diff)
downloadmobly-snippet-lib-b1f23a3dd1fce48791758fc57b0a35f8da6fe961.tar.gz
Add support for asynchronous Rpc (#38)
* Add @AsyncRpc annotation to mark Rpc methods that trigger async events. * Add `EventCache`, which is the repo of events. * Add `EventSnippet`, which has Rpc methods for client to poll events. * Add `SnippetEvent` type to represent an event. * Add `callbackId` field to Rpc protocol's server resp msg.
Diffstat (limited to 'examples')
-rw-r--r--examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java39
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java b/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java
index 3077869..97a8b27 100644
--- a/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java
+++ b/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java
@@ -17,10 +17,20 @@
package com.google.android.mobly.snippet.example1;
import com.google.android.mobly.snippet.Snippet;
+import com.google.android.mobly.snippet.event.EventCache;
+import com.google.android.mobly.snippet.event.SnippetEvent;
+import com.google.android.mobly.snippet.rpc.AsyncRpc;
import com.google.android.mobly.snippet.rpc.Rpc;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
import java.io.IOException;
public class ExampleSnippet2 implements Snippet {
+
+ private final EventCache mEventQueue = EventCache.getInstance();
+
@Rpc(description = "Returns the given string with the prefix \"bar\"")
public String getBar(String input) {
return "bar " + input;
@@ -31,6 +41,35 @@ public class ExampleSnippet2 implements Snippet {
throw new IOException("Example exception from throwSomething()");
}
+ /**
+ * An Rpc method demonstrating the async event mechanism.
+ *
+ * Expect to see an event on the client side that looks like:
+ * {
+ * 'name': 'ExampleEvent',
+ * 'time': <timestamp>,
+ * 'data': {
+ * 'exampleData': "Here's a simple event.",
+ * 'secret': 42.24,
+ * 'isSecretive': True
+ * }
+ * }
+ *
+ * @param eventId
+ * @throws JSONException
+ */
+ @AsyncRpc(description = "This call puts an event in the event queue.")
+ public void tryEvent(String eventId) throws JSONException {
+ SnippetEvent event = new SnippetEvent(eventId, "ExampleEvent");
+ event.addData("exampleData", "Here's a simple event.");
+ event.addData("secret", 42.24);
+ event.addData("isSecretive", true);
+ JSONObject moreData = new JSONObject();
+ moreData.put("evenMoreData", "More Data!");
+ event.addData("moreData", moreData);
+ mEventQueue.postEvent(event);
+ }
+
@Override
public void shutdown() {}
}