blob: f165ddffbe01d70bf122437b922f70130b6f5893 [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 Thompson3b6bf982013-01-13 20:00:03 -080012var XpcomTransport = function XpcomTransport() {
13 this.ndn = null;
14 this.socket = null; // nsISocketTransport
15 this.outStream = null;
16 this.connectedHost = null;
17 this.connectedPort = null;
18
Jeff Thompson3fa84152012-12-16 17:11:42 -080019 this.defaultGetHostAndPort = NDN.makeShuffledGetHostAndPort
20 (["A.hub.ndn.ucla.edu", "B.hub.ndn.ucla.edu", "C.hub.ndn.ucla.edu", "D.hub.ndn.ucla.edu",
21 "E.hub.ndn.ucla.edu", "F.hub.ndn.ucla.edu", "G.hub.ndn.ucla.edu", "H.hub.ndn.ucla.edu"],
22 9695);
Jeff Thompson17a9da82012-11-12 01:11:01 -080023};
24
Jeff Thompson3b6bf982013-01-13 20:00:03 -080025/*
26 * Connect to the host and port in ndn. This replaces a previous connection.
Jeff Thompson07f15fb2013-01-20 20:32:29 -080027 * Listen on the port to read an entire binary XML encoded element and call
Jeff Thompson24189e52013-01-20 23:29:51 -080028 * elementListener.onReceivedElement(element) where element is Uint8Array.
Jeff Thompson3b6bf982013-01-13 20:00:03 -080029 */
Jeff Thompson24189e52013-01-20 23:29:51 -080030XpcomTransport.prototype.connect = function(ndn, elementListener) {
Jeff Thompson3b6bf982013-01-13 20:00:03 -080031 if (this.socket != null) {
32 try {
33 this.socket.close(0);
34 } catch (ex) {
35 console.log("XpcomTransport socket.close exception: " + ex);
36 }
37 this.socket = null;
38 }
39 this.ndn = ndn;
40
41 var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService
42 (Components.interfaces.nsISocketTransportService);
43 var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance
44 (Components.interfaces.nsIInputStreamPump);
45 this.socket = transportService.createTransport(null, 0, ndn.host, ndn.port, null);
46 this.connectedHost = ndn.host;
47 this.connectedPort = ndn.port;
48 this.outStream = this.socket.openOutputStream(1, 0, 0);
49
50 var inStream = this.socket.openInputStream(0, 0, 0);
51 var dataListener = {
Jeff Thompson24189e52013-01-20 23:29:51 -080052 elementReader: new BinaryXmlElementReader(elementListener),
Jeff Thompson3b6bf982013-01-13 20:00:03 -080053
54 onStartRequest: function (request, context) {
55 },
56 onStopRequest: function (request, context, status) {
57 },
58 onDataAvailable: function (request, context, _inputStream, offset, count) {
59 try {
60 // Use readInputStreamToString to handle binary data.
61 // TODO: Can we go directly from the stream to Uint8Array?
Jeff Thompson24189e52013-01-20 23:29:51 -080062 this.elementReader.onReceivedData(DataUtils.toNumbersFromString
63 (NetUtil.readInputStreamToString(inStream, count)));
Jeff Thompson3b6bf982013-01-13 20:00:03 -080064 } catch (ex) {
Jeff Thompson24189e52013-01-20 23:29:51 -080065 console.log("XpcomTransport.onDataAvailable exception: " + ex + "\n" + ex.stack);
Jeff Thompson3b6bf982013-01-13 20:00:03 -080066 }
67 }
68 };
69
70 pump.init(inStream, -1, -1, 0, 0, true);
71 pump.asyncRead(dataListener, null);
72};
73
74/*
75 * Send the data over the connection created by connect.
76 */
77XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
78 if (this.socket == null) {
79 console.log("XpcomTransport connection is not established.");
80 return;
81 }
Jeff Thompson17a9da82012-11-12 01:11:01 -080082
Jeff Thompson3b6bf982013-01-13 20:00:03 -080083 var rawDataString = DataUtils.toString(data);
84 this.outStream.write(rawDataString, rawDataString.length);
85 this.outStream.flush();
86};
87
88XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
89 var thisXpcomTransport = this;
90
91 if (this.socket == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) {
Jeff Thompson24189e52013-01-20 23:29:51 -080092 var elementListener = {
Jeff Thompson275133e2013-01-20 21:33:07 -080093 onReceivedElement : function(element) {
94 var decoder = new BinaryXMLDecoder(element);
95 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) {
Jeff Thompson24189e52013-01-20 23:29:51 -080096 // TODO: handle interest properly.
Jeff Thompson275133e2013-01-20 21:33:07 -080097 }
98 else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) {
99 var co = new ContentObject();
100 co.from_ccnb(decoder);
Jeff Thompson17a9da82012-11-12 01:11:01 -0800101
Jeff Thompson275133e2013-01-20 21:33:07 -0800102 var pitEntry = NDN.getEntryForExpressedInterest(co.name);
103 if (pitEntry != null) {
104 // Remove PIT entry from NDN.PITTable.
105 // TODO: This needs to be a single thread-safe transaction.
106 var index = NDN.PITTable.indexOf(pitEntry);
107 if (index >= 0)
108 NDN.PITTable.splice(index, 1);
109 }
110 if (pitEntry != null) {
111 var currentClosure = pitEntry.closure;
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800112
Jeff Thompson24189e52013-01-20 23:29:51 -0800113 // Cancel interest timer
114 clearTimeout(pitEntry.timerID);
115
Jeff Thompson275133e2013-01-20 21:33:07 -0800116 // TODO: verify the content object and set kind to UPCALL_CONTENT.
117 var result = currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
118 new UpcallInfo(thisXpcomTransport.ndn, null, 0, co));
119 if (result == Closure.RESULT_OK) {
120 // success
121 }
122 else if (result == Closure.RESULT_ERR)
123 console.log("XpcomTransport: upcall returned RESULT_ERR.");
124 else if (result == Closure.RESULT_REEXPRESS) {
125 // TODO: Handl re-express interest.
126 }
127 else if (result == Closure.RESULT_VERIFY) {
128 // TODO: force verification of content.
129 }
130 else if (result == Closure.RESULT_FETCHKEY) {
131 // TODO: get the key in the key locator and re-call the interest
132 // with the key available in the local storage.
Jeff Thompson3fa84152012-12-16 17:11:42 -0800133 }
Jeff Thompson17a9da82012-11-12 01:11:01 -0800134 }
Jeff Thompson275133e2013-01-20 21:33:07 -0800135 }
136 else
137 console.log('Incoming packet is not Interest or ContentObject. Discard now.');
Jeff Thompson17a9da82012-11-12 01:11:01 -0800138 }
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800139 }
140
Jeff Thompson24189e52013-01-20 23:29:51 -0800141 this.connect(ndn, elementListener);
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800142 }
Jeff Thompson17a9da82012-11-12 01:11:01 -0800143
Jeff Thompson24189e52013-01-20 23:29:51 -0800144 //TODO: check local content store first
145 if (closure != null) {
146 var pitEntry = new PITEntry(interest, closure);
147 // TODO: This needs to be a single thread-safe transaction on a global object.
148 NDN.PITTable.push(pitEntry);
149 closure.pitEntry = pitEntry;
150 }
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800151
Jeff Thompson24189e52013-01-20 23:29:51 -0800152 // Set interest timer
153 if (closure != null) {
154 pitEntry.timerID = setTimeout(function() {
155 if (LOG > 3) console.log("Interest time out.");
156
157 // Remove PIT entry from NDN.PITTable.
158 // TODO: Make this a thread-safe operation on the global PITTable.
159 var index = NDN.PITTable.indexOf(pitEntry);
160 //console.log(NDN.PITTable);
161 if (index >= 0)
162 NDN.PITTable.splice(index, 1);
163 //console.log(NDN.PITTable);
164 //console.log(pitEntry.interest.name.getName());
165
166 // Raise closure callback
167 closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(ndn, interest, 0, null));
168 }, interest.interestLifetime); // interestLifetime is in milliseconds.
169 //console.log(closure.timerID);
170 }
171
172 this.send(encodeToBinaryInterest(interest));
Jeff Thompson17a9da82012-11-12 01:11:01 -0800173};
Jeff Thompson24189e52013-01-20 23:29:51 -0800174