Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame^] | 1 | /* Implement getAsync and putAsync used by NDN using nsISocketTransportService. |
| 2 | This is used inside Firefox XPCOM modules. |
| 3 | */ |
| 4 | |
| 5 | // Assume already imported the following: |
| 6 | // Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 7 | // Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
| 8 | |
| 9 | /** Convert outputHex to binary, send to host:port and call hexListener.onReceivedHexData(hexData). |
| 10 | */ |
| 11 | function getAsync(host, port, outputHex, hexListener) { |
| 12 | var binaryListener = { |
| 13 | onReceivedData : function(data) { |
| 14 | hexListener.onReceivedHexData(DataUtils.stringToHex(data)); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | readAllFromSocket(host, port, DataUtils.hexToRawString(outputHex), binaryListener); |
| 19 | } |
| 20 | |
| 21 | /** Send outputData to host:port, read the entire response and call listener.onReceivedData(data). |
| 22 | Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working . |
| 23 | */ |
| 24 | function readAllFromSocket(host, port, outputData, listener) { |
| 25 | var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService |
| 26 | (Components.interfaces.nsISocketTransportService); |
| 27 | var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance |
| 28 | (Components.interfaces.nsIInputStreamPump); |
| 29 | var transport = transportService.createTransport(null, 0, host, port, null); |
| 30 | var outStream = transport.openOutputStream(1, 0, 0); |
| 31 | outStream.write(outputData, outputData.length); |
| 32 | outStream.flush(); |
| 33 | var inStream = transport.openInputStream(0, 0, 0); |
| 34 | var dataListener = { |
| 35 | data: "", |
| 36 | calledOnReceivedData: false, |
| 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 { |
| 50 | // Ignore _inputStream and use inStream. |
| 51 | // Use readInputStreamToString to handle binary data. |
| 52 | this.data += NetUtil.readInputStreamToString(inStream, count); |
| 53 | |
| 54 | // TODO: Don't know why, but we never receive onStopRequest, assume we have |
| 55 | // all the data and force it now. |
| 56 | this.onStopRequest(); |
| 57 | } catch (ex) { |
| 58 | dump("onDataAvailable exception: " + ex + "\n"); |
| 59 | } |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | pump.init(inStream, -1, -1, 0, 0, true); |
| 64 | pump.asyncRead(dataListener, null); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | // TODO: This should be moved to the main NDN.js when we agree on how to do non-blocking get. |
| 69 | // For now, assume this is included after NDN.js and modify it. |
| 70 | /** Encode message as an Interest, send it to host:port, read the entire response and call |
| 71 | listener.onReceivedContentObject(contentObject). |
| 72 | */ |
| 73 | NDN.prototype.getAsync = function(message, listener) { |
| 74 | if (this.host != null && this.port != null) { |
| 75 | var output =''; |
| 76 | message = message.trim(); |
| 77 | if(message==null || message =="" ){ |
| 78 | dump('INVALID INPUT TO GET\n'); |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | int = new Interest(new Name(message)); |
| 83 | int.InterestLifetime = 4200; |
| 84 | var outputHex = encodeToHexInterest(int); |
| 85 | |
| 86 | var hexListener = { |
| 87 | onReceivedHexData : function(result) { |
| 88 | if (LOG>0) dump('BINARY RESPONSE IS ' + result + '\n'); |
| 89 | |
| 90 | if (result == null || result == undefined || result =="" ) |
| 91 | listener.onReceivedContentObject(null); |
| 92 | else { |
| 93 | var co = decodeHexContentObject(result); |
| 94 | |
| 95 | if(LOG>2) { |
| 96 | dump('DECODED CONTENT OBJECT\n'); |
| 97 | dump(co); |
| 98 | dump('\n'); |
| 99 | } |
| 100 | |
| 101 | listener.onReceivedContentObject(co); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return getAsync(this.host, this.port, outputHex, hexListener); |
| 106 | } |
| 107 | else { |
| 108 | dump('ERROR host OR port NOT SET\n'); |
| 109 | } |
| 110 | } |
| 111 | |