blob: a2fb10d14f68c25319282695172b753a3d956d6a [file] [log] [blame]
Jeff Thompson6ad5c362012-12-27 17:57:02 -08001/*
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub"];
7
8const Cc = Components.classes;
9const Ci = Components.interfaces;
10const Cr = Components.results;
11
12Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
13
14var ndnHubHost = null;
15var ndnHubPort = null;
16var ndnHubChangedListenerList = [];
17
18/*
19 * When the NDN hub host or port is changed, the system calls listener(host, port).
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080020 * If the current host and port are not null, call listener with the values to initialize.
Jeff Thompson6ad5c362012-12-27 17:57:02 -080021 */
22function addNdnHubChangedListener(listener) {
23 ndnHubChangedListenerList.push(listener);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080024
25 if (ndnHubHost != null && ndnHubPort != null) {
26 try {
27 listener(ndnHubHost, ndnHubPort);
28 }
29 catch (ex) {
30 // Ignore error from the listener.
31 }
32 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -080033}
34
35/*
36 * If host and port are different than ndnHubHost or ndnHubPort, set them and call each
37 * listener in ndnHubChangedListenerList.
38 */
39function setConnectedNdnHub(host, port) {
40 if (host == ndnHubHost && port == ndnHubPort)
41 // No change.
42 return;
43
44 ndnHubHost = host;
45 ndnHubPort = port;
46 for (var i = 0; i < ndnHubChangedListenerList.length; ++i) {
47 try {
48 ndnHubChangedListenerList[i](host, port);
49 }
50 catch (ex) {
51 // Ignore error from the listener.
52 }
53 }
54}