Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub"]; |
| 7 | |
| 8 | const Cc = Components.classes; |
| 9 | const Ci = Components.interfaces; |
| 10 | const Cr = Components.results; |
| 11 | |
| 12 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 13 | |
| 14 | var ndnHubHost = null; |
| 15 | var ndnHubPort = null; |
| 16 | var ndnHubChangedListenerList = []; |
| 17 | |
| 18 | /* |
| 19 | * When the NDN hub host or port is changed, the system calls listener(host, port). |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 20 | * If the current host and port are not null, call listener with the values to initialize. |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 21 | */ |
| 22 | function addNdnHubChangedListener(listener) { |
| 23 | ndnHubChangedListenerList.push(listener); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 24 | |
| 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 Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* |
| 36 | * If host and port are different than ndnHubHost or ndnHubPort, set them and call each |
| 37 | * listener in ndnHubChangedListenerList. |
| 38 | */ |
| 39 | function 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 | } |