blob: 643b918e93c8b2c3639db708b2ddc6183d644e2f [file] [log] [blame]
Jeff Thompson17a9da82012-11-12 01:11:01 -08001/*
2 * @author: Jeff Thompson
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.
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
12var XpcomTransport = function XpcomTransport() {
13};
14
15XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
16 var binaryInterest = encodeToBinaryInterest(interest);
17
18 var dataListener = {
19 onReceivedData : function(data) {
20 if (data == null || data == undefined || data.length == 0)
21 dump("NDN.expressInterest: received empty data from socket.\n");
22 else {
23 var decoder = new BinaryXMLDecoder(data);
24 var co = new ContentObject();
25 co.from_ccnb(decoder);
26
27 if(LOG>2) {
28 dump("DECODED CONTENT OBJECT\n");
29 dump(co);
30 dump("\n");
31 }
32
33 // TODO: verify the content object and set kind to UPCALL_CONTENT.
34 var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
35 new UpcallInfo(ndn, interest, 0, co));
36 if (result == Closure.RESULT_OK) {
37 // success
38 }
39 else if (result == Closure.RESULT_ERR)
40 dump("NDN.expressInterest: upcall returned RESULT_ERR.\n");
41 else if (result == Closure.RESULT_REEXPRESS)
42 XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
43 else if (result == Closure.RESULT_VERIFY) {
44 // TODO: force verification of content.
45 }
46 else if (result == Closure.RESULT_FETCHKEY) {
47 // TODO: get the key in the key locator and re-call the interest
48 // with the key available in the local storage.
49 }
50 }
51 }
52 }
53
54 XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
55};
56
57/** Send outputData (Uint8Array) to host:port, read the entire response and call
58 * listener.onReceivedData(data) where data is Uint8Array.
59 * Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working .
60 */
61XpcomTransport.readAllFromSocket = function(host, port, outputData, listener) {
62 var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
63 (Components.interfaces.nsISocketTransportService);
64 var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
65 (Components.interfaces.nsIInputStreamPump);
66 var transport = transportService.createTransport(null, 0, host, port, null);
67 var outStream = transport.openOutputStream(1, 0, 0);
68 var rawDataString = DataUtils.toString(outputData);
69 outStream.write(rawDataString, rawDataString.length);
70 outStream.flush();
71 var inStream = transport.openInputStream(0, 0, 0);
72 var dataListener = {
73 data: new Uint8Array(0),
74 structureDecoder: new BinaryXMLStructureDecoder(),
75 calledOnReceivedData: false,
76
77 onStartRequest: function (request, context) {
78 },
79 onStopRequest: function (request, context, status) {
80 inStream.close();
81 outStream.close();
82 if (!this.calledOnReceivedData) {
83 this.calledOnReceivedData = true;
84 listener.onReceivedData(this.data);
85 }
86 },
87 onDataAvailable: function (request, context, _inputStream, offset, count) {
88 if (this.calledOnReceivedData)
89 // Already finished. Ignore extra data.
90 return;
91
92 try {
93 // Ignore _inputStream and use inStream.
94 // Use readInputStreamToString to handle binary data.
95 var rawData = NetUtil.readInputStreamToString(inStream, count);
96 this.data = DataUtils.concatFromString(this.data, rawData);
97
98 // Scan the input to check if a whole ccnb object has been read.
99 if (this.structureDecoder.findElementEnd(this.data))
100 // Finish.
101 this.onStopRequest();
102 } catch (ex) {
103 dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n");
104 }
105 }
106 };
107
108 pump.init(inStream, -1, -1, 0, 0, true);
109 pump.asyncRead(dataListener, null);
110}
111