blob: f741fb05035eb69d5e8445905fbd87e0a9077157 [file] [log] [blame]
Wentao Shangfcb16262013-01-20 14:42:46 -08001<?xml version = "1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3"DTD/xhtml1-strict.dtd">
4<html xmlns = "http://www.w3.org/1999/xhtml">
5<meta charset="UTF-8">
6
7<head>
8 <title>NDN Get File via WebSocket</title>
9
10 <script type="text/javascript" src="../tools/build/ndn-js.js"></script>
11
12 <script type="text/javascript">
13 //hostip = "131.179.196.232";
14 hostip = "localhost";
15 var ndncon = new NDN({port:9696,host:hostip});
16 ndncon.transport.connectWebSocket(ndncon);
17
18///////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /*
20 * Closure for calling expressInterest to fetch big file.
21 */
22 var ContentClosure = function ContentClosure(ndn, T0) {
23 // Inherit from Closure.
24 Closure.call(this);
25
26 this.T0 = T0; // start time
27 this.ndn = ndn;
28 this.totalBlocks = 0; // total # of segments delivered to usr;
29 // TODO: in this test script we simply discard the content after it is received
30 // should consider some way to buffer the whole data in future refactor --- SWT
31
32 // We should always start with the first element so the first content object cannot be ooo data
33 //this.firstReceivedSegmentNumber = null;
34 //this.firstReceivedContentObject = null;
35
36 // statistic counters
37 this.ooo_count = 0; // out-of-order content object counter;
38 // when this counter reaches 3, apply fast retransmission alg.
39 this.pkt_recved = 0; // total # of content object received
40 this.timed_out = 0; // totle # of timed out interest
41 this.interest_sent = 1; // there is an initial interest before calling the closure upcall
42 this.dups = 0; // total # of dup content segments
43
44 this.max_window = 32; // max window size
45 this.max_retrans = 5; // max # of trial before give up; if we fail on one segment, the entire process is aborted
46
47 this.snd_una = 0; // pointer to unacked segments
48 this.snd_wnd = 1; // current window size
49 this.snd_nxt = 1; // pointer to next interest to be sent
50
51 this.ooo_table_size = 128;
52 this.ooo_table = []; // hash table to mark ooo segments
53
54 this.terminated = false; // Set this flag after we receive all the segments;
55 };
56
57 ContentClosure.prototype.upcall = function(kind, upcallInfo) {
58 this.pkt_recved++;
59
60 if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
61 if (this.terminated == false) {
62 this.timed_out++;
63
64 // Reduce window size to 1
65 this.snd_wnd = 1;
66
67 // Retransmit interest for this segment
68 this.ndn.expressInterest(upcallInfo.interest.name, this);
69 //console.log("Resend interest sent for " + upcallInfo.interest.name.getName());
70 document.getElementById('content').innerHTML += "<p>Resend interest sent for "
71 + upcallInfo.interest.name.getName() + "</p>";
72 this.interest_sent++;
73
74 document.getElementById('content').innerHTML += "<p>Interest " + upcallInfo.interest.name.getName() + " time out.</p>";
75 document.getElementById('content').innerHTML += "<p>Total number of blocks: " + this.totalBlocks + "</p>";
76 document.getElementById('content').innerHTML += "<p>Total number of packets: " + this.pkt_recved + "</p>";
77 document.getElementById('content').innerHTML += "<p>Total number of dup segments: " + this.dups + "</p>";
78 document.getElementById('content').innerHTML += "<p>Total number of interest sent: " + this.interest_sent + "</p>";
79 document.getElementById('content').innerHTML += "<p>Total number of time-out interest: " + this.timed_out + "</p>";
80
81 document.getElementById('content').innerHTML += "<p>SND_UNA: " + this.snd_una + "</p>";
82 document.getElementById('content').innerHTML += "<p>SND_WND: " + this.snd_wnd + "</p>";
83 document.getElementById('content').innerHTML += "<p>SND_NXT: " + this.snd_nxt + "</p>";
84 }
85 return Closure.RESULT_OK;
86 }
87
88 if (kind == Closure.UPCALL_CONTENT_BAD) {
89 console.log("NdnProtocol.ContentClosure: signature verification failed");
90 console.log(upcallInfo.contentObject.name.getName());
91 console.log(DataUtils.toHex(upcallInfo.contentObject.signature.Witness).toLowerCase());
92 return Closure.RESULT_OK;
93 }
94
95 if (!(kind == Closure.UPCALL_CONTENT ||
96 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
97 // The upcall is not for us.
98 return Closure.RESULT_ERR;
99
100 var contentObject = upcallInfo.contentObject;
101 if (contentObject.content == null) {
102 console.log("NdnProtocol.ContentClosure: contentObject.content is null");
103 return Closure.RESULT_ERR;
104 }
105
106 // Use the segmentNumber to load multiple segments.
107 var segmentNumber = DataUtils.bigEndianToUnsignedInt
108 (contentObject.name.components[contentObject.name.components.length - 1]);
109 //console.log("Seg # " + segmentNumber + " received");
110 //console.log("Seg name " + contentObject.name.getName());
111
112 /*
113 if (this.firstReceivedSegmentNumber == null) {
114 // This is the first call.
115 this.firstReceivedSegmentNumber = segmentNumber;
116 if (segmentNumber != 0) {
117 // Special case: Save this content object for later and request segment zero.
118 // SWT: this should not happen in normal case;
119 // ccnd should always return the first segment if no seg# is specified
120 this.firstReceivedContentObject = contentObject;
121 var componentsForZero = contentObject.name.components.slice
122 (0, contentObject.name.components.length - 1);
123 componentsForZero.push([0]);
124 this.ndn.expressInterest(new Name(componentsForZero), this);
125 return Closure.RESULT_OK;
126 }
127 }
128 */
129
130 // Process received data here...
131 // Count content length
132 //nameStr = escape(contentObject.name.getName());
133 //document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
134 //document.getElementById('content').innerHTML += "<p>Content buffer length: " + contentObject.content.length + "</p>";
135
136 /*
137 // Check for the special case if the saved content is for the next segment that we need.
138 if (this.firstReceivedContentObject != null &&
139 this.firstReceivedSegmentNumber == segmentNumber + 1) {
140 // Substitute the saved contentObject send its content and keep going.
141 contentObject = this.firstReceivedContentObject;
142 segmentNumber = segmentNumber + 1;
143 // Clear firstReceivedContentObject to save memory.
144 this.firstReceivedContentObject = null;
145
146 // Process received data here...
147 // Count content length
148 //nameStr = escape(contentObject.name.getName());
149 //document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
150 //document.getElementById('content').innerHTML += "<p>Content buffer length: " + contentObject.content.length + "</p>";
151 this.totalBlocks++;
152 }
153 */
154
155 // Record final seg# if present
156 var finalSegmentNumber = null;
157 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
158 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
159
160 // Check for out-of-order segment
161 if (segmentNumber != this.snd_una) {
162 //console.log("Out-of-order segment #" + segmentNumber + " received.");
163 document.getElementById('content').innerHTML += "<p>Out-of-order segment #" + segmentNumber + " received.</p>";
164 this.ooo_count++;
165
166 if (segmentNumber >= this.snd_nxt || segmentNumber < this.snd_una) {
167 // Discard segment that is out of window
168 //console.log("Out-of-window segment #" + segmentNumber);
169 document.getElementById('content').innerHTML += "<p>Out-of-window segment #" + segmentNumber + "</p>";
170 return Closure.RESULT_OK;
171 }
172 // Mark this segment in hash table
173 var slot = segmentNumber % this.ooo_table_size;
174 this.ooo_table[slot] = segmentNumber;
175
176 if (this.ooo_count == 3) {
177 // Fast retransmit
178 // TODO: expressInterest for seg# = this.snd_una
179 //this.snd_wnd = Math.floor(this.snd_wnd / 2) + 3;
180 } else if (this.ooo_count > 3) {
181 //this.snd_wnd++;
182 // TODO: send a new interest if allowed by snd_wnd
183 // SWT: what if we never receive the first unacked segment???
184 }
185
186 return Closure.RESULT_OK;
187 }
188
189 // In-order segment; slide window forward
190 this.snd_una++
191 this.totalBlocks++;
192 var slot = this.snd_una % this.ooo_table_size;
193 while (this.ooo_table[slot] != undefined) {
194 // continue to move forward until we reach a hole in the seg# sequence
195 this.ooo_table[slot] = undefined;
196 this.snd_una++;
197 this.totalBlocks++;
198 slot = this.snd_una % this.ooo_table_size;
199 }
200
201 if (finalSegmentNumber != null && this.snd_una == finalSegmentNumber + 1) {
202 // All blocks before final block, including final block, is received. Mission complete.
203 // Record stop time and print statistic result
204 this.terminated = true;
205 var T1 = new Date();
206 document.getElementById('content').innerHTML += "<p>Final block received.</p>";
207 document.getElementById('content').innerHTML += "<p>Time elapsed: " + (T1 - this.T0) + " ms</p>";
208 document.getElementById('content').innerHTML += "<p>Total number of blocks: " + this.totalBlocks + "</p>";
209 document.getElementById('content').innerHTML += "<p>Total number of packets: " + this.pkt_recved + "</p>";
210 document.getElementById('content').innerHTML += "<p>Total number of dup segments: " + this.dups + "</p>";
211 document.getElementById('content').innerHTML += "<p>Total number of interest sent: " + this.interest_sent + "</p>";
212 document.getElementById('content').innerHTML += "<p>Total number of time-out interest: " + this.timed_out + "</p>";
213 return Closure.RESULT_OK;
214 }
215
216 // Adjust window size
217 if (this.snd_wnd < this.max_window) {
218 this.snd_wnd++;
219 //console.log("Window size after adjust: " + this.snd_wnd);
220 }
221
222 // Send the next interest if allowed by snd_wnd
223 var nextNameComponents = contentObject.name.components.slice(0, contentObject.name.components.length - 1);
224 //console.log("SND_UNA: " + this.snd_una);
225 //console.log("SND_NXT: " + this.snd_nxt);
226 while (this.snd_nxt - this.snd_una < this.snd_wnd) {
227 // Make a name for the next segment and get it.
228 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(this.snd_nxt);
229 // Put a 0 byte in front.
230 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
231 nextSegmentNumber.set(segmentNumberPlus1, 1);
232
233 nextNameComponents.push(nextSegmentNumber);
234
235 var nextName = new Name(nextNameComponents);
236 this.ndn.expressInterest(nextName, this);
237 //console.log("Interest sent for seg # " + this.snd_nxt + " name " + nextName.getName());
238 this.interest_sent++;
239
240 this.snd_nxt++;
241 nextNameComponents.pop(); // Remove segment number from components
242 }
243
244 return Closure.RESULT_OK;
245 };
246
247 /*
248 * Convert the big endian Uint8Array to an unsigned int.
249 * Don't check for overflow.
250 */
251 function ArrayToNum(bytes) {
252 var result = 0;
253 for (var i = 0; i < bytes.length; ++i) {
254 result = result * 10;
255 result += (bytes[i] - 48);
256 }
257 return result;
258 }
259
260 /*
261 * Convert the int value to a new big endian Uint8Array and return.
262 * If value is 0 or negative, return Uint8Array(0).
263 */
264 function NumToArray(value) {
265 value = Math.round(value);
266 if (value <= 0)
267 return new Uint8Array(0);
268
269 numString = value.toString();
270 var size = numString.length;
271 var result = new Uint8Array(size);
272 for (i = 0; i < size; i++) {
273 result[i] = numString.charCodeAt(i);
274 }
275 return result;
276 }
277///////////////////////////////////////////////////////////////////////////////////////////////////////////
278
279 /*
280 var AsyncGetClosure = function AsyncGetClosure(T0) {
281 this.T0 = T0; // Start time
282 // Inherit from Closure.
283 Closure.call(this);
284 };
285
286 AsyncGetClosure.prototype.upcall = function(kind, upcallInfo) {
287 //console.log("Closure.upcall() executed.");
288 if (kind == Closure.UPCALL_FINAL) {
289 // Do nothing.
290 } else if (kind == Closure.UPCALL_CONTENT) {
291 var T1 = new Date();
292
293 var content = upcallInfo.contentObject;
294 nameStr = escape(content.name.getName());
295 document.getElementById('content').innerHTML += "<p>Time elapsed: " + (T1 - this.T0) + " ms</p>";
296 document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
297 document.getElementById('content').innerHTML += "<p>Content buffer length: " + content.content.length + "</p>";
298 //console.log("In callback, nameStr: " + nameStr);
299 //console.log("In callback, content: ");
300 //console.log(content.content.length);
301 //document.getElementById('content').innerHTML += contentObjectToHtml(content);
302 } else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
303 console.log("Closure.upcall called with interest time out.");
304 }
305 return Closure.RESULT_OK;
306 };
307 */
308
309 function run() {
310 document.getElementById('content').innerHTML += "<p>Started...</p>";
311 //ndn.expressInterest(new Name(document.getElementById('interest').value), new AsyncGetClosure( new Date() ));
312 var name = new Name(document.getElementById('interest').value);
313 ndncon.expressInterest( name,
314 new ContentClosure( ndncon, new Date() ));
315 }
316
317 </script>
318
319</head>
320<body >
321
322 <form>
323 Please Enter an Interest:<br />
324 <input id="interest" type="text" name="INTEREST" size="50" value="/wentao.shang/mars/%00" />
325 </form>
326
327 <button onclick="run()">Fetch Content</button>
328
329 <p id="content">Result: <br/></p>
330
331</body>
332</html>