blob: bf8bf55469519582a5c18c2505e80b85f13375ac [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
Jeff Thompson8107ec82013-01-12 21:53:27 -08006var EXPORTED_SYMBOLS = ["addNdnHubChangedListener", "setConnectedNdnHub", "splitUri"];
Jeff Thompson6ad5c362012-12-27 17:57:02 -08007
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 }
Jeff Thompson8107ec82013-01-12 21:53:27 -080054}
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 */
62function 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 Thompson6ad5c362012-12-27 17:57:02 -080082}