AI2 Component  (Version nb184)
CloudDBJedisListener.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2017-2020 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 //Notification Listener from: http://stackoverflow.com/questions/26406303/redis-key-expire-notification-with-jedis
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.util.Log;
10 
13 
14 import java.util.List;
15 import java.util.Set;
16 
17 import org.json.JSONException;
18 
19 import redis.clients.jedis.Jedis;
20 import redis.clients.jedis.JedisPubSub;
21 
22 public class CloudDBJedisListener extends JedisPubSub {
23  private static final boolean DEBUG = false;
24  public CloudDB cloudDB;
25  private Thread myThread;
26  private static String LOG_TAG = "CloudDB"; // Yep, same as the CloudDB component.
27  // This is on purpose because when we
28  // are looking at logs for CloudDB
29  // we want to know about us as well
30 
31  public CloudDBJedisListener(CloudDB thisCloudDB){
32  cloudDB = thisCloudDB;
33  myThread = Thread.currentThread();
34  }
35 
36  @Override
37  public void onSubscribe(String channel, int subscribedChannels) {
38  if (DEBUG) {
39  Log.d(LOG_TAG, "onSubscribe " + channel + " " + subscribedChannels);
40  }
41  }
42 
43  @Override
44  public void onMessage(String channel, String message) {
45  if (DEBUG) {
46  Log.d(LOG_TAG, "onMessage channel " + channel + ", message: " + message);
47  }
48  try {
49  // Message is a JSON encoded list of the tag that was just set and its value
50  List<Object> data = null;
51  data = (List<Object>) JsonUtil.getObjectFromJson((String) message, false);
52  if (DEBUG) {
53  Log.d(LOG_TAG, "onMessage: data = " + data);
54  }
55  String tag = (String) data.get(0); // The variable that was changed
56  List<Object> valueList = (List<Object>) data.get(1);
57  for (Object value : valueList) {
58  // Note: DataChanged will arrange to dispatch the event
59  // on the UI thread.
61  if (retValue == null) {
62  cloudDB.DataChanged(tag, value);
63  } else {
64  cloudDB.DataChanged(tag, retValue);
65  }
66  }
67  } catch (JSONException e) {
68  Log.e(LOG_TAG, "onMessage: JSONException", e);
69  // CloudDBError arranges to generate the error UI on the
70  // UI Thread
71  cloudDB.CloudDBError("System Error: " + e.getMessage());
72  }
73  }
74 
75  public void terminate() {
76  myThread.interrupt();
77  }
78 
79  //add other Unimplemented methods
80 }
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.CloudDBJedisListener.CloudDBJedisListener
CloudDBJedisListener(CloudDB thisCloudDB)
Definition: CloudDBJedisListener.java:31
com.google.appinventor.components.runtime.util.CloudDBJedisListener.terminate
void terminate()
Definition: CloudDBJedisListener.java:75
com.google.appinventor.components
com.google.appinventor.components.runtime.util.JsonUtil
Definition: JsonUtil.java:42
com.google.appinventor.components.runtime.CloudDB
Definition: CloudDB.java:110
com.google.appinventor.components.runtime.util.CloudDBJedisListener.onSubscribe
void onSubscribe(String channel, int subscribedChannels)
Definition: CloudDBJedisListener.java:37
com.google.appinventor.components.runtime.util.CloudDBJedisListener.onMessage
void onMessage(String channel, String message)
Definition: CloudDBJedisListener.java:44
com.google.appinventor.components.runtime.CloudDB.CloudDBError
void CloudDBError(final String message)
Definition: CloudDB.java:1141
com.google.appinventor.components.runtime.CloudDB.getForm
Form getForm()
Definition: CloudDB.java:1167
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.CloudDB.DataChanged
void DataChanged(final String tag, final Object value)
Definition: CloudDB.java:1115
com.google.appinventor.components.runtime.util.JsonUtil.getJsonRepresentationIfValueFileName
static String getJsonRepresentationIfValueFileName(Object value)
Definition: JsonUtil.java:376
com.google.appinventor.components.runtime.util.JsonUtil.getObjectFromJson
static Object getObjectFromJson(String jsonString)
Definition: JsonUtil.java:317
com.google
com
com.google.appinventor.components.runtime.util.CloudDBJedisListener.cloudDB
CloudDB cloudDB
Definition: CloudDBJedisListener.java:24
com.google.appinventor.components.runtime.util.CloudDBJedisListener
Definition: CloudDBJedisListener.java:22
com.google.appinventor