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 | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 12 | var XpcomTransport = function XpcomTransport() { |
| 13 | this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort |
| 14 | (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu", |
| 15 | "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"], |
| 16 | 9695); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) { |
| 20 | var binaryInterest = encodeToBinaryInterest(interest); |
| 21 | |
| 22 | var dataListener = { |
| 23 | onReceivedData : function(data) { |
| 24 | if (data == null || data == undefined || data.length == 0) |
| 25 | dump("NDN.expressInterest: received empty data from socket.\n"); |
| 26 | else { |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 27 | var decoder = new BinaryXMLDecoder(data); |
| 28 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { |
| 29 | // TODO: handle interest |
| 30 | if (closure.upcall(Closure.UPCALL_INTEREST, null) == Closure.RESULT_OK) |
| 31 | // success |
| 32 | return true; |
| 33 | } |
| 34 | else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { |
| 35 | var co = new ContentObject(); |
| 36 | co.from_ccnb(decoder); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 37 | |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 38 | // TODO: verify the content object and set kind to UPCALL_CONTENT. |
| 39 | var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 40 | new UpcallInfo(ndn, interest, 0, co)); |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 41 | if (result == Closure.RESULT_OK) |
| 42 | // success |
| 43 | return true; |
| 44 | else if (result == Closure.RESULT_ERR) |
| 45 | dump("NDN.expressInterest: upcall returned RESULT_ERR.\n"); |
| 46 | else if (result == Closure.RESULT_REEXPRESS) { |
| 47 | XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener); |
| 48 | return true; |
| 49 | } |
| 50 | else if (result == Closure.RESULT_VERIFY) { |
| 51 | // TODO: force verification of content. |
| 52 | } |
| 53 | else if (result == Closure.RESULT_FETCHKEY) { |
| 54 | // TODO: get the key in the key locator and re-call the interest |
| 55 | // with the key available in the local storage. |
| 56 | } |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 57 | } |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 58 | else |
| 59 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 60 | } |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 61 | |
| 62 | return false; |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener); |
| 67 | }; |
| 68 | |
| 69 | /** Send outputData (Uint8Array) to host:port, read the entire response and call |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 70 | * listener.onReceivedData(data) where data is Uint8Array and returns true if the data is consumed, |
| 71 | * false if need to keep reading. |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 72 | * Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working . |
| 73 | */ |
| 74 | XpcomTransport.readAllFromSocket = function(host, port, outputData, listener) { |
| 75 | var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService |
| 76 | (Components.interfaces.nsISocketTransportService); |
| 77 | var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance |
| 78 | (Components.interfaces.nsIInputStreamPump); |
| 79 | var transport = transportService.createTransport(null, 0, host, port, null); |
| 80 | var outStream = transport.openOutputStream(1, 0, 0); |
| 81 | var rawDataString = DataUtils.toString(outputData); |
| 82 | outStream.write(rawDataString, rawDataString.length); |
| 83 | outStream.flush(); |
| 84 | var inStream = transport.openInputStream(0, 0, 0); |
| 85 | var dataListener = { |
Jeff Thompson | fab4a7d | 2012-11-28 21:24:26 -0800 | [diff] [blame] | 86 | dataParts: [], |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 87 | structureDecoder: new BinaryXMLStructureDecoder(), |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 88 | dataIsConsumed: false, |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 89 | |
| 90 | onStartRequest: function (request, context) { |
| 91 | }, |
| 92 | onStopRequest: function (request, context, status) { |
| 93 | inStream.close(); |
| 94 | outStream.close(); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 95 | }, |
| 96 | onDataAvailable: function (request, context, _inputStream, offset, count) { |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 97 | if (this.dataIsConsumed) |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 98 | // Already finished. Ignore extra data. |
| 99 | return; |
| 100 | |
| 101 | try { |
| 102 | // Ignore _inputStream and use inStream. |
| 103 | // Use readInputStreamToString to handle binary data. |
Jeff Thompson | fab4a7d | 2012-11-28 21:24:26 -0800 | [diff] [blame] | 104 | // TODO: Can we go directly from the stream to Uint8Array? |
| 105 | var rawData = DataUtils.toNumbersFromString |
| 106 | (NetUtil.readInputStreamToString(inStream, count)); |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 107 | |
Jeff Thompson | 3fa8415 | 2012-12-16 17:11:42 -0800 | [diff] [blame] | 108 | // Process multiple objects in this packet. |
| 109 | while(true) { |
| 110 | // Scan the input to check if a whole ccnb object has been read. |
| 111 | this.structureDecoder.seek(0); |
| 112 | if (this.structureDecoder.findElementEnd(rawData)) { |
| 113 | // Got the remainder of an object. Report to the caller. |
| 114 | this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset)); |
| 115 | if (listener.onReceivedData(DataUtils.concatArrays(this.dataParts))) { |
| 116 | this.dataIsConsumed = true; |
| 117 | this.onStopRequest(); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Need to read a new object. |
| 122 | rawData = rawData.subarray(this.structureDecoder.offset, rawData.length); |
| 123 | this.dataParts = []; |
| 124 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 125 | if (rawData.length == 0) |
| 126 | // No more data in the packet. |
| 127 | return; |
| 128 | // else loop back to decode. |
| 129 | } |
| 130 | else { |
| 131 | // Save for a later call to concatArrays so that we only copy data once. |
| 132 | this.dataParts.push(rawData); |
| 133 | return; |
| 134 | } |
| 135 | } |
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 136 | } catch (ex) { |
| 137 | dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n"); |
| 138 | } |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | pump.init(inStream, -1, -1, 0, 0, true); |
| 143 | pump.asyncRead(dataListener, null); |
| 144 | } |
| 145 | |