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 | |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame^] | 6 | var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub", "splitUri"]; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 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 | } |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame^] | 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Split the URI spec and return an object with protocol (including ':'), name, |
| 58 | * search (including '?') and hash value (including '#'). |
| 59 | * All result strings are trimmed. This does not unescape the name. |
| 60 | * The name may include a host and port. |
| 61 | */ |
| 62 | function splitUri(spec) { |
| 63 | spec = spec.trim(); |
| 64 | var result = {}; |
| 65 | var preHash = spec.split('#', 1)[0]; |
| 66 | result.hash = spec.substr(preHash.length).trim(); |
| 67 | var preSearch = preHash.split('?', 1)[0]; |
| 68 | result.search = preHash.substr(preSearch.length).trim(); |
| 69 | |
| 70 | preSearch = preSearch.trim(); |
| 71 | var colonIndex = preSearch.indexOf(':'); |
| 72 | if (colonIndex >= 0) { |
| 73 | result.protocol = preSearch.substr(0, colonIndex + 1).trim(); |
| 74 | result.name = preSearch.substr(colonIndex + 1).trim(); |
| 75 | } |
| 76 | else { |
| 77 | result.protocol = ""; |
| 78 | result.name = preSearch; |
| 79 | } |
| 80 | |
| 81 | return result; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 82 | } |