blob: 1f8a93d5098f2e63694a1ac3b5cd9bdee506b0bc [file] [log] [blame]
Jeff Thompson745026e2012-10-13 12:49:20 -07001/*
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 Thompson08ab3cd2012-10-08 02:56:20 -07006 */
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 Thompson6aa338a2012-10-13 23:58:02 -070012/** Convert outputHex to binary, send to host:port and call listener.onReceivedData(data)
13 * where data is a byte array.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070014 */
Jeff Thompson6aa338a2012-10-13 23:58:02 -070015function getAsync(host, port, outputHex, listener) {
16 readAllFromSocket(host, port, DataUtils.hexToRawString(outputHex), listener);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070017}
18
Jeff Thompson6aa338a2012-10-13 23:58:02 -070019/** 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 Thompson08ab3cd2012-10-08 02:56:20 -070022 */
23function 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 Thompson6aa338a2012-10-13 23:58:02 -070034 data: [],
Jeff Thompsondad617b2012-10-14 17:11:41 -070035 structureDecoder: new BinaryXMLStructureDecoder(),
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070036 calledOnReceivedData: false,
Jeff Thompson6aa338a2012-10-13 23:58:02 -070037 debugNOnDataAvailable: 0,
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070038
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 Thompsondad617b2012-10-14 17:11:41 -070050 if (this.calledOnReceivedData)
51 // Already finished. Ignore extra data.
52 return;
53
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070054 try {
Jeff Thompson6aa338a2012-10-13 23:58:02 -070055 this.debugNOnDataAvailable += 1;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070056 // Ignore _inputStream and use inStream.
57 // Use readInputStreamToString to handle binary data.
Jeff Thompson6aa338a2012-10-13 23:58:02 -070058 var rawData = NetUtil.readInputStreamToString(inStream, count);
Jeff Thompson6aa338a2012-10-13 23:58:02 -070059 this.data = this.data.concat(DataUtils.toNumbersFromString(rawData));
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070060
Jeff Thompsondad617b2012-10-14 17:11:41 -070061 // 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 Thompson08ab3cd2012-10-08 02:56:20 -070065 } catch (ex) {
Jeff Thompson34419762012-10-15 22:24:12 -070066 dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n");
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070067 }
68 }
69 };
70
71 pump.init(inStream, -1, -1, 0, 0, true);
72 pump.asyncRead(dataListener, null);
73}