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