Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 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. |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 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 | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 12 | /** Convert outputHex to binary, send to host:port and call listener.onReceivedData(data) |
| 13 | * where data is a byte array. |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 14 | */ |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 15 | function getAsync(host, port, outputHex, listener) { |
| 16 | readAllFromSocket(host, port, DataUtils.hexToRawString(outputHex), listener); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 17 | } |
| 18 | |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 19 | /** Send outputData to host:port, read the entire response and call listener.onReceivedData(data) |
| 20 | * where data is a byte array. |
| 21 | * Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working . |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 22 | */ |
| 23 | function readAllFromSocket(host, port, outputData, listener) { |
| 24 | var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService |
| 25 | (Components.interfaces.nsISocketTransportService); |
| 26 | var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance |
| 27 | (Components.interfaces.nsIInputStreamPump); |
| 28 | var transport = transportService.createTransport(null, 0, host, port, null); |
| 29 | var outStream = transport.openOutputStream(1, 0, 0); |
| 30 | outStream.write(outputData, outputData.length); |
| 31 | outStream.flush(); |
| 32 | var inStream = transport.openInputStream(0, 0, 0); |
| 33 | var dataListener = { |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 34 | data: [], |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 35 | calledOnReceivedData: false, |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 36 | debugNOnDataAvailable: 0, |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 37 | |
| 38 | onStartRequest: function (request, context) { |
| 39 | }, |
| 40 | onStopRequest: function (request, context, status) { |
| 41 | inStream.close(); |
| 42 | outStream.close(); |
| 43 | if (!this.calledOnReceivedData) { |
| 44 | this.calledOnReceivedData = true; |
| 45 | listener.onReceivedData(this.data); |
| 46 | } |
| 47 | }, |
| 48 | onDataAvailable: function (request, context, _inputStream, offset, count) { |
| 49 | try { |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 50 | this.debugNOnDataAvailable += 1; |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 51 | // Ignore _inputStream and use inStream. |
| 52 | // Use readInputStreamToString to handle binary data. |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 53 | var rawData = NetUtil.readInputStreamToString(inStream, count); |
| 54 | // Append to this.data. |
| 55 | this.data = this.data.concat(DataUtils.toNumbersFromString(rawData)); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 56 | |
Jeff Thompson | 744d28e | 2012-10-09 23:19:04 -0700 | [diff] [blame] | 57 | // TODO: Need to parse the input to check if a whole ccnb object has been read, as in |
| 58 | // CcnbObjectReader class: https://github.com/NDN-Routing/NDNLP/blob/master/ndnld.h#L256 . |
| 59 | // For now as a hack, try to fully decode this.data as a ContentObject. |
| 60 | try { |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 61 | var decoder = new BinaryXMLDecoder(this.data); |
| 62 | var co = new ContentObject(); |
| 63 | co.from_ccnb(decoder); |
Jeff Thompson | 744d28e | 2012-10-09 23:19:04 -0700 | [diff] [blame] | 64 | } catch (ex) { |
| 65 | // Assume the exception is because the decoder only got partial data, so read moe. |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 66 | dump("Awaiting more data at onDataAvailable call # " + |
| 67 | this.debugNOnDataAvailable + "\n"); |
Jeff Thompson | 744d28e | 2012-10-09 23:19:04 -0700 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | // We were able to parse the ContentObject, so finish. |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 71 | this.onStopRequest(); |
| 72 | } catch (ex) { |
| 73 | dump("onDataAvailable exception: " + ex + "\n"); |
| 74 | } |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | pump.init(inStream, -1, -1, 0, 0, true); |
| 79 | pump.asyncRead(dataListener, null); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | // TODO: This should be moved to the main NDN.js when we agree on how to do non-blocking get. |
| 84 | // For now, assume this is included after NDN.js and modify it. |
| 85 | /** Encode message as an Interest, send it to host:port, read the entire response and call |
| 86 | listener.onReceivedContentObject(contentObject). |
| 87 | */ |
| 88 | NDN.prototype.getAsync = function(message, listener) { |
| 89 | if (this.host != null && this.port != null) { |
| 90 | var output =''; |
| 91 | message = message.trim(); |
| 92 | if(message==null || message =="" ){ |
| 93 | dump('INVALID INPUT TO GET\n'); |
| 94 | return null; |
| 95 | } |
| 96 | |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 97 | interest = new Interest(new Name(message)); |
| 98 | interest.InterestLifetime = 4200; |
| 99 | var outputHex = encodeToHexInterest(interest); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 100 | |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 101 | var dataListener = { |
| 102 | onReceivedData : function(result) { |
| 103 | if (result == null || result == undefined || result.length == 0) |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 104 | listener.onReceivedContentObject(null); |
| 105 | else { |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 106 | var decoder = new BinaryXMLDecoder(result); |
| 107 | var co = new ContentObject(); |
| 108 | co.from_ccnb(decoder); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 109 | |
| 110 | if(LOG>2) { |
| 111 | dump('DECODED CONTENT OBJECT\n'); |
| 112 | dump(co); |
| 113 | dump('\n'); |
| 114 | } |
| 115 | |
| 116 | listener.onReceivedContentObject(co); |
| 117 | } |
| 118 | } |
| 119 | } |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame^] | 120 | |
| 121 | return getAsync(this.host, this.port, outputHex, dataListener); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 122 | } |
| 123 | else { |
| 124 | dump('ERROR host OR port NOT SET\n'); |
| 125 | } |
| 126 | } |
| 127 | |