blob: aa12588f39bb09912b434c25137287b92a184f1c [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) {
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
Jeff Thompson6aa338a2012-10-13 23:58:02 -070090 interest = new Interest(new Name(message));
91 interest.InterestLifetime = 4200;
92 var outputHex = encodeToHexInterest(interest);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070093
Jeff Thompson6aa338a2012-10-13 23:58:02 -070094 var dataListener = {
95 onReceivedData : function(result) {
96 if (result == null || result == undefined || result.length == 0)
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070097 listener.onReceivedContentObject(null);
98 else {
Jeff Thompson6aa338a2012-10-13 23:58:02 -070099 var decoder = new BinaryXMLDecoder(result);
100 var co = new ContentObject();
101 co.from_ccnb(decoder);
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 }
Jeff Thompson6aa338a2012-10-13 23:58:02 -0700113
114 return getAsync(this.host, this.port, outputHex, dataListener);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700115 }
116 else {
117 dump('ERROR host OR port NOT SET\n');
118 }
119}
120