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"); |
| 13 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 14 | var NdnProtocolInfo = function NdnProtocolInfo(){ |
| 15 | }; |
| 16 | |
| 17 | NdnProtocolInfo.ndnHubHost = null; |
| 18 | NdnProtocolInfo.ndnHubPort = null; |
| 19 | NdnProtocolInfo.ndnHubChangedListenerList = []; |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * 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] | 23 | * 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] | 24 | */ |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 25 | NdnProtocolInfo.addNdnHubChangedListener = function(listener) { |
| 26 | NdnProtocolInfo.ndnHubChangedListenerList.push(listener); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 27 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 28 | if (NdnProtocolInfo.ndnHubHost != null && NdnProtocolInfo.ndnHubPort != null) { |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 29 | try { |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 30 | listener(NdnProtocolInfo.ndnHubHost, NdnProtocolInfo.ndnHubPort); |
Jeff Thompson | f3ec9a1 | 2012-12-30 22:05:24 -0800 | [diff] [blame] | 31 | } |
| 32 | catch (ex) { |
| 33 | // Ignore error from the listener. |
| 34 | } |
| 35 | } |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
| 39 | * If host and port are different than ndnHubHost or ndnHubPort, set them and call each |
| 40 | * listener in ndnHubChangedListenerList. |
| 41 | */ |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 42 | NdnProtocolInfo.setConnectedNdnHub = function(host, port) { |
| 43 | if (host == NdnProtocolInfo.ndnHubHost && port == NdnProtocolInfo.ndnHubPort) |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 44 | // No change. |
| 45 | return; |
| 46 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 47 | NdnProtocolInfo.ndnHubHost = host; |
| 48 | NdnProtocolInfo.ndnHubPort = port; |
| 49 | for (var i = 0; i < NdnProtocolInfo.ndnHubChangedListenerList.length; ++i) { |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 50 | try { |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 51 | NdnProtocolInfo.ndnHubChangedListenerList[i](host, port); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 52 | } |
| 53 | catch (ex) { |
| 54 | // Ignore error from the listener. |
| 55 | } |
| 56 | } |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 57 | } |
| 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 65 | NdnProtocolInfo.splitUri = function(spec) { |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 66 | 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 85 | } |