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 | 27a9ef8 | 2012-10-21 11:27:43 -0700 | [diff] [blame] | 12 | /** Send outputData (byte array) to host:port, read the entire response and call |
| 13 | * listener.onReceivedData(data) where data is a byte array. |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 14 | * 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] | 15 | */ |
| 16 | function readAllFromSocket(host, port, outputData, listener) { |
| 17 | var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService |
Jeff Thompson | 27a9ef8 | 2012-10-21 11:27:43 -0700 | [diff] [blame] | 18 | (Components.interfaces.nsISocketTransportService); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 19 | var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance |
Jeff Thompson | 27a9ef8 | 2012-10-21 11:27:43 -0700 | [diff] [blame] | 20 | (Components.interfaces.nsIInputStreamPump); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 21 | var transport = transportService.createTransport(null, 0, host, port, null); |
| 22 | var outStream = transport.openOutputStream(1, 0, 0); |
Jeff Thompson | 27a9ef8 | 2012-10-21 11:27:43 -0700 | [diff] [blame] | 23 | var rawDataString = DataUtils.toString(outputData); |
| 24 | outStream.write(rawDataString, rawDataString.length); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 25 | outStream.flush(); |
| 26 | var inStream = transport.openInputStream(0, 0, 0); |
| 27 | var dataListener = { |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 28 | data: [], |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 29 | structureDecoder: new BinaryXMLStructureDecoder(), |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 30 | calledOnReceivedData: false, |
| 31 | |
| 32 | onStartRequest: function (request, context) { |
| 33 | }, |
| 34 | onStopRequest: function (request, context, status) { |
| 35 | inStream.close(); |
| 36 | outStream.close(); |
| 37 | if (!this.calledOnReceivedData) { |
| 38 | this.calledOnReceivedData = true; |
| 39 | listener.onReceivedData(this.data); |
| 40 | } |
| 41 | }, |
| 42 | onDataAvailable: function (request, context, _inputStream, offset, count) { |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 43 | if (this.calledOnReceivedData) |
| 44 | // Already finished. Ignore extra data. |
| 45 | return; |
| 46 | |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 47 | try { |
| 48 | // Ignore _inputStream and use inStream. |
| 49 | // Use readInputStreamToString to handle binary data. |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 50 | var rawData = NetUtil.readInputStreamToString(inStream, count); |
Jeff Thompson | 6aa338a | 2012-10-13 23:58:02 -0700 | [diff] [blame] | 51 | this.data = this.data.concat(DataUtils.toNumbersFromString(rawData)); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 53 | // Scan the input to check if a whole ccnb object has been read. |
| 54 | if (this.structureDecoder.findElementEnd(this.data)) |
| 55 | // Finish. |
| 56 | this.onStopRequest(); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 57 | } catch (ex) { |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 58 | dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n"); |
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | pump.init(inStream, -1, -1, 0, 0, true); |
| 64 | pump.asyncRead(dataListener, null); |
| 65 | } |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 66 | |