blob: 3a5fc7919406857afae083e81f43a4c0ca8db8fc [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 Thompsonb2f91ea2013-01-13 15:59:26 -08006var EXPORTED_SYMBOLS = ["NdnProtocolInfo"];
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
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080014var NdnProtocolInfo = function NdnProtocolInfo(){
15};
16
17NdnProtocolInfo.ndnHubHost = null;
18NdnProtocolInfo.ndnHubPort = null;
19NdnProtocolInfo.ndnHubChangedListenerList = [];
Jeff Thompson6ad5c362012-12-27 17:57:02 -080020
21/*
22 * When the NDN hub host or port is changed, the system calls listener(host, port).
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080023 * If the current host and port are not null, call listener with the values to initialize.
Jeff Thompson6ad5c362012-12-27 17:57:02 -080024 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080025NdnProtocolInfo.addNdnHubChangedListener = function(listener) {
26 NdnProtocolInfo.ndnHubChangedListenerList.push(listener);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080027
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080028 if (NdnProtocolInfo.ndnHubHost != null && NdnProtocolInfo.ndnHubPort != null) {
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080029 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080030 listener(NdnProtocolInfo.ndnHubHost, NdnProtocolInfo.ndnHubPort);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080031 }
32 catch (ex) {
33 // Ignore error from the listener.
34 }
35 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -080036}
37
38/*
39 * If host and port are different than ndnHubHost or ndnHubPort, set them and call each
40 * listener in ndnHubChangedListenerList.
41 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080042NdnProtocolInfo.setConnectedNdnHub = function(host, port) {
43 if (host == NdnProtocolInfo.ndnHubHost && port == NdnProtocolInfo.ndnHubPort)
Jeff Thompson6ad5c362012-12-27 17:57:02 -080044 // No change.
45 return;
46
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080047 NdnProtocolInfo.ndnHubHost = host;
48 NdnProtocolInfo.ndnHubPort = port;
49 for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) {
Jeff Thompson6ad5c362012-12-27 17:57:02 -080050 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080051 NdnProtocolInfo.ndnHubChangedListenerList[i](host, port);
Jeff Thompson6ad5c362012-12-27 17:57:02 -080052 }
53 catch (ex) {
54 // Ignore error from the listener.
55 }
56 }
Jeff Thompson8107ec82013-01-12 21:53:27 -080057}
58
59/*
60 * Split the URI spec and return an object with protocol (including ':'), name,
61 * search (including '?') and hash value (including '#').
62 * All result strings are trimmed. This does not unescape the name.
63 * The name may include a host and port.
64 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080065NdnProtocolInfo.splitUri = function(spec) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080066 spec = spec.trim();
67 var result = {};
68 var preHash = spec.split('#', 1)[0];
69 result.hash = spec.substr(preHash.length).trim();
70 var preSearch = preHash.split('?', 1)[0];
71 result.search = preHash.substr(preSearch.length).trim();
72
73 preSearch = preSearch.trim();
74 var colonIndex = preSearch.indexOf(':');
75 if (colonIndex >= 0) {
76 result.protocol = preSearch.substr(0, colonIndex + 1).trim();
77 result.name = preSearch.substr(colonIndex + 1).trim();
78 }
79 else {
80 result.protocol = "";
81 result.name = preSearch;
82 }
83
84 return result;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080085}