blob: 30c901fc9b766b13cfcab0bd8127507b84bd2260 [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 Thompson08ab3cd2012-10-08 02:56:20 -070035 calledOnReceivedData: false,
Jeff Thompson6aa338a2012-10-13 23:58:02 -070036 debugNOnDataAvailable: 0,
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070037
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 {
Jeff Thompson6aa338a2012-10-13 23:58:02 -070050 this.debugNOnDataAvailable += 1;
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070051 // Ignore _inputStream and use inStream.
52 // Use readInputStreamToString to handle binary data.
Jeff Thompson6aa338a2012-10-13 23:58:02 -070053 var rawData = NetUtil.readInputStreamToString(inStream, count);
54 // Append to this.data.
55 this.data = this.data.concat(DataUtils.toNumbersFromString(rawData));
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070056
Jeff Thompson744d28e2012-10-09 23:19:04 -070057 // 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 {
Jeff Thompson6aa338a2012-10-13 23:58:02 -070061 var decoder = new BinaryXMLDecoder(this.data);
62 var co = new ContentObject();
63 co.from_ccnb(decoder);
Jeff Thompson744d28e2012-10-09 23:19:04 -070064 } catch (ex) {
65 // Assume the exception is because the decoder only got partial data, so read moe.
Jeff Thompson6aa338a2012-10-13 23:58:02 -070066 dump("Awaiting more data at onDataAvailable call # " +
67 this.debugNOnDataAvailable + "\n");
Jeff Thompson744d28e2012-10-09 23:19:04 -070068 return;
69 }
70 // We were able to parse the ContentObject, so finish.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070071 this.onStopRequest();
72 } catch (ex) {
73 dump("onDataAvailable exception: " + ex + "\n");
74 }
75 }
76 };
77
78 pump.init(inStream, -1, -1, 0, 0, true);
79 pump.asyncRead(dataListener, null);
80}
81
82
83// TODO: This should be moved to the main NDN.js when we agree on how to do non-blocking get.
84// For now, assume this is included after NDN.js and modify it.
85/** Encode message as an Interest, send it to host:port, read the entire response and call
86 listener.onReceivedContentObject(contentObject).
87 */
88NDN.prototype.getAsync = function(message, listener) {
89 if (this.host != null && this.port != null) {
90 var output ='';
91 message = message.trim();
92 if(message==null || message =="" ){
93 dump('INVALID INPUT TO GET\n');
94 return null;
95 }
96
Jeff Thompson6aa338a2012-10-13 23:58:02 -070097 interest = new Interest(new Name(message));
98 interest.InterestLifetime = 4200;
99 var outputHex = encodeToHexInterest(interest);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700100
Jeff Thompson6aa338a2012-10-13 23:58:02 -0700101 var dataListener = {
102 onReceivedData : function(result) {
103 if (result == null || result == undefined || result.length == 0)
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700104 listener.onReceivedContentObject(null);
105 else {
Jeff Thompson6aa338a2012-10-13 23:58:02 -0700106 var decoder = new BinaryXMLDecoder(result);
107 var co = new ContentObject();
108 co.from_ccnb(decoder);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700109
110 if(LOG>2) {
111 dump('DECODED CONTENT OBJECT\n');
112 dump(co);
113 dump('\n');
114 }
115
116 listener.onReceivedContentObject(co);
117 }
118 }
119 }
Jeff Thompson6aa338a2012-10-13 23:58:02 -0700120
121 return getAsync(this.host, this.port, outputHex, dataListener);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700122 }
123 else {
124 dump('ERROR host OR port NOT SET\n');
125 }
126}
127