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). |
| 20 | */ |
| 21 | function addNdnHubChangedListener(listener) { |
| 22 | ndnHubChangedListenerList.push(listener); |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | * If host and port are different than ndnHubHost or ndnHubPort, set them and call each |
| 27 | * listener in ndnHubChangedListenerList. |
| 28 | */ |
| 29 | function setConnectedNdnHub(host, port) { |
| 30 | if (host == ndnHubHost && port == ndnHubPort) |
| 31 | // No change. |
| 32 | return; |
| 33 | |
| 34 | ndnHubHost = host; |
| 35 | ndnHubPort = port; |
| 36 | for (var i = 0; i < ndnHubChangedListenerList.length; ++i) { |
| 37 | try { |
| 38 | ndnHubChangedListenerList[i](host, port); |
| 39 | } |
| 40 | catch (ex) { |
| 41 | // Ignore error from the listener. |
| 42 | } |
| 43 | } |
| 44 | } |