blob: 396aeb9cd256469845f9c72ece5a8f6035c2d092 [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");
Jeff Thompson4eb992a2013-03-09 21:05:53 -080013Components.utils.import("chrome://modules/content/ndn-js.jsm");
Jeff Thompson6ad5c362012-12-27 17:57:02 -080014
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080015var NdnProtocolInfo = function NdnProtocolInfo(){
16};
17
Jeff Thompson4eb992a2013-03-09 21:05:53 -080018NdnProtocolInfo.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); },
19 verify: false });
20
21// These are set once a connection is established.
22NdnProtocolInfo.connectedNdnHubHost = null;
23NdnProtocolInfo.connectedNdnHubPort = null;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080024NdnProtocolInfo.ndnHubChangedListenerList = [];
Jeff Thompson6ad5c362012-12-27 17:57:02 -080025
26/*
27 * When the NDN hub host or port is changed, the system calls listener(host, port).
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080028 * If the current host and port are not null, call listener with the values to initialize.
Jeff Thompson6ad5c362012-12-27 17:57:02 -080029 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080030NdnProtocolInfo.addNdnHubChangedListener = function(listener) {
31 NdnProtocolInfo.ndnHubChangedListenerList.push(listener);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080032
Jeff Thompson4eb992a2013-03-09 21:05:53 -080033 if (NdnProtocolInfo.connectedNdnHubHost != null && NdnProtocolInfo.connectedNdnHubPort != null) {
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080034 try {
Jeff Thompson4eb992a2013-03-09 21:05:53 -080035 listener(NdnProtocolInfo.connectedNdnHubHost, NdnProtocolInfo.connectedNdnHubPort);
Jeff Thompsonf3ec9a12012-12-30 22:05:24 -080036 }
37 catch (ex) {
38 // Ignore error from the listener.
39 }
40 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -080041}
42
43/*
44 * If host and port are different than ndnHubHost or ndnHubPort, set them and call each
45 * listener in ndnHubChangedListenerList.
46 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080047NdnProtocolInfo.setConnectedNdnHub = function(host, port) {
Jeff Thompson4eb992a2013-03-09 21:05:53 -080048 if (host == NdnProtocolInfo.connectedNdnHubHost && port == NdnProtocolInfo.connectedNdnHubPort)
Jeff Thompson6ad5c362012-12-27 17:57:02 -080049 // No change.
50 return;
51
Jeff Thompson4eb992a2013-03-09 21:05:53 -080052 NdnProtocolInfo.connectedNdnHubHost = host;
53 NdnProtocolInfo.connectedNdnHubPort = port;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080054 for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) {
Jeff Thompson6ad5c362012-12-27 17:57:02 -080055 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080056 NdnProtocolInfo.ndnHubChangedListenerList[i](host, port);
Jeff Thompson6ad5c362012-12-27 17:57:02 -080057 }
58 catch (ex) {
59 // Ignore error from the listener.
60 }
61 }
Jeff Thompson8107ec82013-01-12 21:53:27 -080062}
63
64/*
65 * Split the URI spec and return an object with protocol (including ':'), name,
66 * search (including '?') and hash value (including '#').
67 * All result strings are trimmed. This does not unescape the name.
68 * The name may include a host and port.
69 */
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080070NdnProtocolInfo.splitUri = function(spec) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080071 spec = spec.trim();
72 var result = {};
73 var preHash = spec.split('#', 1)[0];
74 result.hash = spec.substr(preHash.length).trim();
75 var preSearch = preHash.split('?', 1)[0];
76 result.search = preHash.substr(preSearch.length).trim();
77
78 preSearch = preSearch.trim();
79 var colonIndex = preSearch.indexOf(':');
80 if (colonIndex >= 0) {
81 result.protocol = preSearch.substr(0, colonIndex + 1).trim();
82 result.name = preSearch.substr(colonIndex + 1).trim();
83 }
84 else {
85 result.protocol = "";
86 result.name = preSearch;
87 }
88
89 return result;
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080090}