blob: c27d4164c27828e750c0d836b64c821935dcb30e [file] [log] [blame]
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07001/* 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 */
11function 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 */
24function 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
Jeff Thompson744d28e2012-10-09 23:19:04 -070054 // TODO: Need to parse the input to check if a whole ccnb object has been read, as in
55 // CcnbObjectReader class: https://github.com/NDN-Routing/NDNLP/blob/master/ndnld.h#L256 .
56 // For now as a hack, try to fully decode this.data as a ContentObject.
57 try {
58 decodeHexContentObject(DataUtils.stringToHex(this.data));
59 } catch (ex) {
60 // Assume the exception is because the decoder only got partial data, so read moe.
61 return;
62 }
63 // We were able to parse the ContentObject, so finish.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070064 this.onStopRequest();
65 } catch (ex) {
66 dump("onDataAvailable exception: " + ex + "\n");
67 }
68 }
69 };
70
71 pump.init(inStream, -1, -1, 0, 0, true);
72 pump.asyncRead(dataListener, null);
73}
74
75
76// TODO: This should be moved to the main NDN.js when we agree on how to do non-blocking get.
77// For now, assume this is included after NDN.js and modify it.
78/** Encode message as an Interest, send it to host:port, read the entire response and call
79 listener.onReceivedContentObject(contentObject).
80 */
81NDN.prototype.getAsync = function(message, listener) {
82 if (this.host != null && this.port != null) {
83 var output ='';
84 message = message.trim();
85 if(message==null || message =="" ){
86 dump('INVALID INPUT TO GET\n');
87 return null;
88 }
89
90 int = new Interest(new Name(message));
91 int.InterestLifetime = 4200;
92 var outputHex = encodeToHexInterest(int);
93
94 var hexListener = {
95 onReceivedHexData : function(result) {
96 if (LOG>0) dump('BINARY RESPONSE IS ' + result + '\n');
97
98 if (result == null || result == undefined || result =="" )
99 listener.onReceivedContentObject(null);
100 else {
Jeff Thompson744d28e2012-10-09 23:19:04 -0700101 var co = decodeHexContentObject(result);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700102
103 if(LOG>2) {
104 dump('DECODED CONTENT OBJECT\n');
105 dump(co);
106 dump('\n');
107 }
108
109 listener.onReceivedContentObject(co);
110 }
111 }
112 }
113 return getAsync(this.host, this.port, outputHex, hexListener);
114 }
115 else {
116 dump('ERROR host OR port NOT SET\n');
117 }
118}
119