Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | * Implement getAsync and putAsync used by NDN using nsISocketTransportService. |
| 5 | * This is used inside Firefox XPCOM modules. |
| 6 | */ |
| 7 | |
| 8 | // Assume already imported the following: |
| 9 | // Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 10 | // Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
| 11 | |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 12 | var XpcomTransport = function XpcomTransport() { |
| 13 | this.ndn = null; |
| 14 | this.socket = null; // nsISocketTransport |
| 15 | this.outStream = null; |
| 16 | this.connectedHost = null; |
| 17 | this.connectedPort = null; |
| 18 | |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 19 | this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort |
| 20 | (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu", |
| 21 | "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"], |
| 22 | 9695); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 25 | /* |
| 26 | * Connect to the host and port in ndn. This replaces a previous connection. |
Jeff Thompson | 07f15fb | 2013-01-20 20:32:29 -0800 | [diff] [blame] | 27 | * Listen on the port to read an entire binary XML encoded element and call |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 28 | * elementListener.onReceivedElement(element) where element is Uint8Array. |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 29 | */ |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 30 | XpcomTransport.prototype.connect = function(ndn, elementListener) { |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 31 | if (this.socket != null) { |
| 32 | try { |
| 33 | this.socket.close(0); |
| 34 | } catch (ex) { |
| 35 | console.log("XpcomTransport socket.close exception: " + ex); |
| 36 | } |
| 37 | this.socket = null; |
| 38 | } |
| 39 | this.ndn = ndn; |
| 40 | |
| 41 | var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService |
| 42 | (Components.interfaces.nsISocketTransportService); |
| 43 | var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance |
| 44 | (Components.interfaces.nsIInputStreamPump); |
| 45 | this.socket = transportService.createTransport(null, 0, ndn.host, ndn.port, null); |
| 46 | this.connectedHost = ndn.host; |
| 47 | this.connectedPort = ndn.port; |
| 48 | this.outStream = this.socket.openOutputStream(1, 0, 0); |
| 49 | |
| 50 | var inStream = this.socket.openInputStream(0, 0, 0); |
| 51 | var dataListener = { |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 52 | elementReader: new BinaryXmlElementReader(elementListener), |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 53 | |
| 54 | onStartRequest: function (request, context) { |
| 55 | }, |
| 56 | onStopRequest: function (request, context, status) { |
| 57 | }, |
| 58 | onDataAvailable: function (request, context, _inputStream, offset, count) { |
| 59 | try { |
| 60 | // Use readInputStreamToString to handle binary data. |
| 61 | // TODO: Can we go directly from the stream to Uint8Array? |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 62 | this.elementReader.onReceivedData(DataUtils.toNumbersFromString |
| 63 | (NetUtil.readInputStreamToString(inStream, count))); |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 64 | } catch (ex) { |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 65 | console.log("XpcomTransport.onDataAvailable exception: " + ex + "\n" + ex.stack); |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | pump.init(inStream, -1, -1, 0, 0, true); |
| 71 | pump.asyncRead(dataListener, null); |
| 72 | }; |
| 73 | |
| 74 | /* |
| 75 | * Send the data over the connection created by connect. |
| 76 | */ |
| 77 | XpcomTransport.prototype.send = function(/* Uint8Array */ data) { |
| 78 | if (this.socket == null) { |
| 79 | console.log("XpcomTransport connection is not established."); |
| 80 | return; |
| 81 | } |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 82 | |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 83 | var rawDataString = DataUtils.toString(data); |
| 84 | this.outStream.write(rawDataString, rawDataString.length); |
| 85 | this.outStream.flush(); |
| 86 | }; |
| 87 | |
| 88 | XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) { |
| 89 | var thisXpcomTransport = this; |
| 90 | |
| 91 | if (this.socket == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) { |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 92 | var elementListener = { |
Jeff Thompson | 275133e | 2013-01-20 21:33:07 -0800 | [diff] [blame] | 93 | onReceivedElement : function(element) { |
| 94 | var decoder = new BinaryXMLDecoder(element); |
| 95 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 96 | // TODO: handle interest properly. |
Jeff Thompson | 275133e | 2013-01-20 21:33:07 -0800 | [diff] [blame] | 97 | } |
| 98 | else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { |
| 99 | var co = new ContentObject(); |
| 100 | co.from_ccnb(decoder); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 101 | |
Jeff Thompson | 275133e | 2013-01-20 21:33:07 -0800 | [diff] [blame] | 102 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
| 103 | if (pitEntry != null) { |
| 104 | // Remove PIT entry from NDN.PITTable. |
| 105 | // TODO: This needs to be a single thread-safe transaction. |
| 106 | var index = NDN.PITTable.indexOf(pitEntry); |
| 107 | if (index >= 0) |
| 108 | NDN.PITTable.splice(index, 1); |
| 109 | } |
| 110 | if (pitEntry != null) { |
| 111 | var currentClosure = pitEntry.closure; |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 112 | |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 113 | // Cancel interest timer |
| 114 | clearTimeout(pitEntry.timerID); |
| 115 | |
Jeff Thompson | 275133e | 2013-01-20 21:33:07 -0800 | [diff] [blame] | 116 | // TODO: verify the content object and set kind to UPCALL_CONTENT. |
| 117 | var result = currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, |
| 118 | new UpcallInfo(thisXpcomTransport.ndn, null, 0, co)); |
| 119 | if (result == Closure.RESULT_OK) { |
| 120 | // success |
| 121 | } |
| 122 | else if (result == Closure.RESULT_ERR) |
| 123 | console.log("XpcomTransport: upcall returned RESULT_ERR."); |
| 124 | else if (result == Closure.RESULT_REEXPRESS) { |
| 125 | // TODO: Handl re-express interest. |
| 126 | } |
| 127 | else if (result == Closure.RESULT_VERIFY) { |
| 128 | // TODO: force verification of content. |
| 129 | } |
| 130 | else if (result == Closure.RESULT_FETCHKEY) { |
| 131 | // TODO: get the key in the key locator and re-call the interest |
| 132 | // with the key available in the local storage. |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 133 | } |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 134 | } |
Jeff Thompson | 275133e | 2013-01-20 21:33:07 -0800 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 138 | } |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 141 | this.connect(ndn, elementListener); |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 142 | } |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 143 | |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 144 | //TODO: check local content store first |
| 145 | if (closure != null) { |
| 146 | var pitEntry = new PITEntry(interest, closure); |
| 147 | // TODO: This needs to be a single thread-safe transaction on a global object. |
| 148 | NDN.PITTable.push(pitEntry); |
| 149 | closure.pitEntry = pitEntry; |
| 150 | } |
Jeff Thompson | 3b6bf98 | 2013-01-13 20:00:03 -0800 | [diff] [blame] | 151 | |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 152 | // Set interest timer |
| 153 | if (closure != null) { |
| 154 | pitEntry.timerID = setTimeout(function() { |
| 155 | if (LOG > 3) console.log("Interest time out."); |
| 156 | |
| 157 | // Remove PIT entry from NDN.PITTable. |
| 158 | // TODO: Make this a thread-safe operation on the global PITTable. |
| 159 | var index = NDN.PITTable.indexOf(pitEntry); |
| 160 | //console.log(NDN.PITTable); |
| 161 | if (index >= 0) |
| 162 | NDN.PITTable.splice(index, 1); |
| 163 | //console.log(NDN.PITTable); |
| 164 | //console.log(pitEntry.interest.name.getName()); |
| 165 | |
| 166 | // Raise closure callback |
| 167 | closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null)); |
| 168 | }, interest.interestLifetime); // interestLifetime is in milliseconds. |
| 169 | //console.log(closure.timerID); |
| 170 | } |
| 171 | |
| 172 | this.send(encodeToBinaryInterest(interest)); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 173 | }; |
Jeff Thompson | 24189e5 | 2013-01-20 23:29:51 -0800 | [diff] [blame] | 174 | |