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 | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 35 | structureDecoder: new BinaryXMLStructureDecoder(), |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 36 | calledOnReceivedData: false, |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 37 | debugNOnDataAvailable: 0, |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 38 | |
| 39 | onStartRequest: function (request, context) { |
| 40 | }, |
| 41 | onStopRequest: function (request, context, status) { |
| 42 | inStream.close(); |
| 43 | outStream.close(); |
| 44 | if (!this.calledOnReceivedData) { |
| 45 | this.calledOnReceivedData = true; |
| 46 | listener.onReceivedData(this.data); |
| 47 | } |
| 48 | }, |
| 49 | onDataAvailable: function (request, context, _inputStream, offset, count) { |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 50 | if (this.calledOnReceivedData) |
| 51 | // Already finished. Ignore extra data. |
| 52 | return; |
| 53 | |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 54 | try { |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 55 | this.debugNOnDataAvailable += 1; |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 56 | // Ignore _inputStream and use inStream. |
| 57 | // Use readInputStreamToString to handle binary data. |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 58 | var rawData = NetUtil.readInputStreamToString(inStream, count); |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 59 | this.data = this.data.concat(DataUtils.toNumbersFromString(rawData)); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 60 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 61 | // Scan the input to check if a whole ccnb object has been read. |
| 62 | if (this.structureDecoder.findElementEnd(this.data)) |
| 63 | // Finish. |
| 64 | this.onStopRequest(); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 65 | } catch (ex) { |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame^] | 66 | dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n"); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | pump.init(inStream, -1, -1, 0, 0, true); |
| 72 | pump.asyncRead(dataListener, null); |
| 73 | } |