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 | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 6 | var EXPORTED_SYMBOLS = ["NdnProtocolInfo"]; |
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"); |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 13 | Components.utils.import("chrome://modules/content/ndn-js.jsm"); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 14 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 15 | var NdnProtocolInfo = function NdnProtocolInfo(){ |
| 16 | }; |
| 17 | |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 18 | NdnProtocolInfo.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); }, |
| 19 | verify: false }); |
| 20 | |
| 21 | // These are set once a connection is established. |
| 22 | NdnProtocolInfo.connectedNdnHubHost = null; |
| 23 | NdnProtocolInfo.connectedNdnHubPort = null; |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 24 | NdnProtocolInfo.ndnHubChangedListenerList = []; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * 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] | 28 | * 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] | 29 | */ |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 30 | NdnProtocolInfo.addNdnHubChangedListener = function(listener) { |
| 31 | NdnProtocolInfo.ndnHubChangedListenerList.push(listener); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 32 | |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 33 | if (NdnProtocolInfo.connectedNdnHubHost != null && NdnProtocolInfo.connectedNdnHubPort != null) { |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 34 | try { |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 35 | listener(NdnProtocolInfo.connectedNdnHubHost, NdnProtocolInfo.connectedNdnHubPort); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 36 | } |
| 37 | catch (ex) { |
| 38 | // Ignore error from the listener. |
| 39 | } |
| 40 | } |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
| 44 | * If host and port are different than ndnHubHost or ndnHubPort, set them and call each |
| 45 | * listener in ndnHubChangedListenerList. |
| 46 | */ |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 47 | NdnProtocolInfo.setConnectedNdnHub = function(host, port) { |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 48 | if (host == NdnProtocolInfo.connectedNdnHubHost && port == NdnProtocolInfo.connectedNdnHubPort) |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 49 | // No change. |
| 50 | return; |
| 51 | |
Jeff Thompson | 4eb992a | 2013-03-09 21:05:53 -0800 | [diff] [blame] | 52 | NdnProtocolInfo.connectedNdnHubHost = host; |
| 53 | NdnProtocolInfo.connectedNdnHubPort = port; |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 54 | for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) { |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 55 | try { |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 56 | NdnProtocolInfo.ndnHubChangedListenerList[i](host, port); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 57 | } |
| 58 | catch (ex) { |
| 59 | // Ignore error from the listener. |
| 60 | } |
| 61 | } |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 62 | } |
| 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 70 | NdnProtocolInfo.splitUri = function(spec) { |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 71 | 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 90 | } |