blob: 42273f208a6348f688622c11aca7dd2acb66c5b1 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson17a9da82012-11-12 01:11:01 -08002 * @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
Jeff Thompson3fa84152012-12-16 17:11:42 -080012var XpcomTransport = function XpcomTransport() {
13 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
14 (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu",
15 "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"],
16 9695);
Jeff Thompson17a9da82012-11-12 01:11:01 -080017};
18
19XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
20 var binaryInterest = encodeToBinaryInterest(interest);
21
22 var dataListener = {
23 onReceivedData : function(data) {
24 if (data == null || data == undefined || data.length == 0)
25 dump("NDN.expressInterest: received empty data from socket.\n");
26 else {
Jeff Thompson3fa84152012-12-16 17:11:42 -080027 var decoder = new BinaryXMLDecoder(data);
28 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) {
29 // TODO: handle interest
30 if (closure.upcall(Closure.UPCALL_INTEREST, null) == Closure.RESULT_OK)
31 // success
32 return true;
33 }
34 else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) {
35 var co = new ContentObject();
36 co.from_ccnb(decoder);
Jeff Thompson17a9da82012-11-12 01:11:01 -080037
Jeff Thompson3fa84152012-12-16 17:11:42 -080038 // TODO: verify the content object and set kind to UPCALL_CONTENT.
39 var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
Jeff Thompson17a9da82012-11-12 01:11:01 -080040 new UpcallInfo(ndn, interest, 0, co));
Jeff Thompson3fa84152012-12-16 17:11:42 -080041 if (result == Closure.RESULT_OK)
42 // success
43 return true;
44 else if (result == Closure.RESULT_ERR)
45 dump("NDN.expressInterest: upcall returned RESULT_ERR.\n");
46 else if (result == Closure.RESULT_REEXPRESS) {
47 XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
48 return true;
49 }
50 else if (result == Closure.RESULT_VERIFY) {
51 // TODO: force verification of content.
52 }
53 else if (result == Closure.RESULT_FETCHKEY) {
54 // TODO: get the key in the key locator and re-call the interest
55 // with the key available in the local storage.
56 }
Jeff Thompson17a9da82012-11-12 01:11:01 -080057 }
Jeff Thompson3fa84152012-12-16 17:11:42 -080058 else
59 console.log('Incoming packet is not Interest or ContentObject. Discard now.');
Jeff Thompson17a9da82012-11-12 01:11:01 -080060 }
Jeff Thompson3fa84152012-12-16 17:11:42 -080061
62 return false;
Jeff Thompson17a9da82012-11-12 01:11:01 -080063 }
64 }
65
66 XpcomTransport.readAllFromSocket(ndn.host, ndn.port, binaryInterest, dataListener);
67};
68
69/** Send outputData (Uint8Array) to host:port, read the entire response and call
Jeff Thompson3fa84152012-12-16 17:11:42 -080070 * listener.onReceivedData(data) where data is Uint8Array and returns true if the data is consumed,
71 * false if need to keep reading.
Jeff Thompson17a9da82012-11-12 01:11:01 -080072 * Code derived from http://stackoverflow.com/questions/7816386/why-nsiscriptableinputstream-is-not-working .
73 */
74XpcomTransport.readAllFromSocket = function(host, port, outputData, listener) {
75 var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
76 (Components.interfaces.nsISocketTransportService);
77 var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
78 (Components.interfaces.nsIInputStreamPump);
79 var transport = transportService.createTransport(null, 0, host, port, null);
80 var outStream = transport.openOutputStream(1, 0, 0);
81 var rawDataString = DataUtils.toString(outputData);
82 outStream.write(rawDataString, rawDataString.length);
83 outStream.flush();
84 var inStream = transport.openInputStream(0, 0, 0);
85 var dataListener = {
Jeff Thompsonfab4a7d2012-11-28 21:24:26 -080086 dataParts: [],
Jeff Thompson17a9da82012-11-12 01:11:01 -080087 structureDecoder: new BinaryXMLStructureDecoder(),
Jeff Thompson3fa84152012-12-16 17:11:42 -080088 dataIsConsumed: false,
Jeff Thompson17a9da82012-11-12 01:11:01 -080089
90 onStartRequest: function (request, context) {
91 },
92 onStopRequest: function (request, context, status) {
93 inStream.close();
94 outStream.close();
Jeff Thompson17a9da82012-11-12 01:11:01 -080095 },
96 onDataAvailable: function (request, context, _inputStream, offset, count) {
Jeff Thompson3fa84152012-12-16 17:11:42 -080097 if (this.dataIsConsumed)
Jeff Thompson17a9da82012-11-12 01:11:01 -080098 // Already finished. Ignore extra data.
99 return;
100
101 try {
102 // Ignore _inputStream and use inStream.
103 // Use readInputStreamToString to handle binary data.
Jeff Thompsonfab4a7d2012-11-28 21:24:26 -0800104 // TODO: Can we go directly from the stream to Uint8Array?
105 var rawData = DataUtils.toNumbersFromString
106 (NetUtil.readInputStreamToString(inStream, count));
Jeff Thompson17a9da82012-11-12 01:11:01 -0800107
Jeff Thompson3fa84152012-12-16 17:11:42 -0800108 // Process multiple objects in this packet.
109 while(true) {
110 // Scan the input to check if a whole ccnb object has been read.
111 this.structureDecoder.seek(0);
112 if (this.structureDecoder.findElementEnd(rawData)) {
113 // Got the remainder of an object. Report to the caller.
114 this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset));
115 if (listener.onReceivedData(DataUtils.concatArrays(this.dataParts))) {
116 this.dataIsConsumed = true;
117 this.onStopRequest();
118 return;
119 }
120
121 // Need to read a new object.
122 rawData = rawData.subarray(this.structureDecoder.offset, rawData.length);
123 this.dataParts = [];
124 this.structureDecoder = new BinaryXMLStructureDecoder();
125 if (rawData.length == 0)
126 // No more data in the packet.
127 return;
128 // else loop back to decode.
129 }
130 else {
131 // Save for a later call to concatArrays so that we only copy data once.
132 this.dataParts.push(rawData);
133 return;
134 }
135 }
Jeff Thompson17a9da82012-11-12 01:11:01 -0800136 } catch (ex) {
137 dump("readAllFromSocket.onDataAvailable exception: " + ex + "\n");
138 }
139 }
140 };
141
142 pump.init(inStream, -1, -1, 0, 0, true);
143 pump.asyncRead(dataListener, null);
144}
145