blob: 14d72f2ce5a0e84eb782ab344050ccab12de5eb6 [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 Thompson275133e2013-01-20 21:33:07 -080028 * listener.onReceivedElement(element) where element is Uint8Array.
Jeff Thompson3b6bf982013-01-13 20:00:03 -080029 */
30XpcomTransport.prototype.connect = function(ndn, listener) {
31 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 = {
52 dataParts: [],
53 structureDecoder: new BinaryXMLStructureDecoder(),
54
55 onStartRequest: function (request, context) {
56 },
57 onStopRequest: function (request, context, status) {
58 },
59 onDataAvailable: function (request, context, _inputStream, offset, count) {
60 try {
61 // Use readInputStreamToString to handle binary data.
62 // TODO: Can we go directly from the stream to Uint8Array?
63 var rawData = DataUtils.toNumbersFromString
64 (NetUtil.readInputStreamToString(inStream, count));
65
66 // Process multiple objects in this packet.
67 while(true) {
68 // Scan the input to check if a whole ccnb object has been read.
69 this.structureDecoder.seek(0);
70 if (this.structureDecoder.findElementEnd(rawData)) {
71 // Got the remainder of an object. Report to the caller.
72 this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset));
Jeff Thompson07f15fb2013-01-20 20:32:29 -080073 listener.onReceivedElement(DataUtils.concatArrays(this.dataParts));
Jeff Thompson3b6bf982013-01-13 20:00:03 -080074
75 // Need to read a new object.
76 rawData = rawData.subarray(this.structureDecoder.offset, rawData.length);
77 this.dataParts = [];
78 this.structureDecoder = new BinaryXMLStructureDecoder();
79 if (rawData.length == 0)
80 // No more data in the packet.
81 return;
82
83 // else loop back to decode.
84 }
85 else {
86 // Save for a later call to concatArrays so that we only copy data once.
87 this.dataParts.push(rawData);
88 return;
89 }
90 }
91 } catch (ex) {
92 console.log("XpcomTransport.onDataAvailable exception: " + ex);
93 }
94 }
95 };
96
97 pump.init(inStream, -1, -1, 0, 0, true);
98 pump.asyncRead(dataListener, null);
99};
100
101/*
102 * Send the data over the connection created by connect.
103 */
104XpcomTransport.prototype.send = function(/* Uint8Array */ data) {
105 if (this.socket == null) {
106 console.log("XpcomTransport connection is not established.");
107 return;
108 }
Jeff Thompson17a9da82012-11-12 01:11:01 -0800109
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800110 var rawDataString = DataUtils.toString(data);
111 this.outStream.write(rawDataString, rawDataString.length);
112 this.outStream.flush();
113};
114
115XpcomTransport.prototype.expressInterest = function(ndn, interest, closure) {
116 var thisXpcomTransport = this;
117
118 if (this.socket == null || this.connectedHost != ndn.host || this.connectedPort != ndn.port) {
119 var dataListener = {
Jeff Thompson275133e2013-01-20 21:33:07 -0800120 onReceivedElement : function(element) {
121 var decoder = new BinaryXMLDecoder(element);
122 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) {
123 // TODO: handle interest properly. For now, assume the only use in getting
124 // an interest is knowing that the host is alive from NDN.ccndIdFetcher.
125 var pitEntry = NDN.getEntryForExpressedInterest(NDN.ccndIdFetcher);
126 if (pitEntry != null) {
127 // Remove PIT entry from NDN.PITTable.
128 var index = NDN.PITTable.indexOf(pitEntry);
129 if (index >= 0)
130 NDN.PITTable.splice(index, 1);
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800131
Jeff Thompson275133e2013-01-20 21:33:07 -0800132 pitEntry.closure.upcall(Closure.UPCALL_INTEREST, null);
Jeff Thompson3fa84152012-12-16 17:11:42 -0800133 }
Jeff Thompson275133e2013-01-20 21:33:07 -0800134 }
135 else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) {
136 var co = new ContentObject();
137 co.from_ccnb(decoder);
Jeff Thompson17a9da82012-11-12 01:11:01 -0800138
Jeff Thompson275133e2013-01-20 21:33:07 -0800139 var pitEntry = NDN.getEntryForExpressedInterest(co.name);
140 if (pitEntry != null) {
141 // Remove PIT entry from NDN.PITTable.
142 // TODO: This needs to be a single thread-safe transaction.
143 var index = NDN.PITTable.indexOf(pitEntry);
144 if (index >= 0)
145 NDN.PITTable.splice(index, 1);
146 }
147 if (pitEntry != null) {
148 var currentClosure = pitEntry.closure;
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800149
Jeff Thompson275133e2013-01-20 21:33:07 -0800150 // TODO: verify the content object and set kind to UPCALL_CONTENT.
151 var result = currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
152 new UpcallInfo(thisXpcomTransport.ndn, null, 0, co));
153 if (result == Closure.RESULT_OK) {
154 // success
155 }
156 else if (result == Closure.RESULT_ERR)
157 console.log("XpcomTransport: upcall returned RESULT_ERR.");
158 else if (result == Closure.RESULT_REEXPRESS) {
159 // TODO: Handl re-express interest.
160 }
161 else if (result == Closure.RESULT_VERIFY) {
162 // TODO: force verification of content.
163 }
164 else if (result == Closure.RESULT_FETCHKEY) {
165 // TODO: get the key in the key locator and re-call the interest
166 // with the key available in the local storage.
Jeff Thompson3fa84152012-12-16 17:11:42 -0800167 }
Jeff Thompson17a9da82012-11-12 01:11:01 -0800168 }
Jeff Thompson275133e2013-01-20 21:33:07 -0800169 }
170 else
171 console.log('Incoming packet is not Interest or ContentObject. Discard now.');
Jeff Thompson17a9da82012-11-12 01:11:01 -0800172 }
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800173 }
174
175 this.connect(ndn, dataListener);
176 }
Jeff Thompson17a9da82012-11-12 01:11:01 -0800177
Jeff Thompson3b6bf982013-01-13 20:00:03 -0800178 var binaryInterest = encodeToBinaryInterest(interest);
179
180 // TODO: This needs to be a single thread-safe transaction.
181 var pitEntry = new PITEntry(interest, closure);
182 NDN.PITTable.push(pitEntry);
183
184 this.send(binaryInterest);
Jeff Thompson17a9da82012-11-12 01:11:01 -0800185};