blob: 8ece0f8098c833fbd2a6b61c9203cf5d69606a39 [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">
Wentao Shangdd691552013-01-25 17:52:43 -080013 hostip = "131.179.196.232";
14 //hostip = "localhost";
Wentao Shang261b4be2013-01-26 09:22:37 -080015 var ndncon = new NDN({port:9696,host:hostip});
16 //var ndncon = new NDN({port:9696,host:hostip,verify:false});
Wentao Shangfcb16262013-01-20 14:42:46 -080017
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>";
Wentao Shangd4607392013-01-24 23:08:49 -0800213
214 document.getElementById('content').innerHTML += "<p>SND_UNA: " + this.snd_una + "</p>";
215 document.getElementById('content').innerHTML += "<p>SND_WND: " + this.snd_wnd + "</p>";
216 document.getElementById('content').innerHTML += "<p>SND_NXT: " + this.snd_nxt + "</p>";
Wentao Shangfcb16262013-01-20 14:42:46 -0800217 return Closure.RESULT_OK;
218 }
219
220 // Adjust window size
221 if (this.snd_wnd < this.max_window) {
222 this.snd_wnd++;
223 //console.log("Window size after adjust: " + this.snd_wnd);
224 }
225
226 // Send the next interest if allowed by snd_wnd
227 var nextNameComponents = contentObject.name.components.slice(0, contentObject.name.components.length - 1);
228 //console.log("SND_UNA: " + this.snd_una);
229 //console.log("SND_NXT: " + this.snd_nxt);
230 while (this.snd_nxt - this.snd_una < this.snd_wnd) {
231 // Make a name for the next segment and get it.
232 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(this.snd_nxt);
233 // Put a 0 byte in front.
234 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
235 nextSegmentNumber.set(segmentNumberPlus1, 1);
236
237 nextNameComponents.push(nextSegmentNumber);
238
239 var nextName = new Name(nextNameComponents);
240 this.ndn.expressInterest(nextName, this);
241 //console.log("Interest sent for seg # " + this.snd_nxt + " name " + nextName.getName());
242 this.interest_sent++;
243
244 this.snd_nxt++;
245 nextNameComponents.pop(); // Remove segment number from components
246 }
247
248 return Closure.RESULT_OK;
249 };
250
251 /*
252 * Convert the big endian Uint8Array to an unsigned int.
253 * Don't check for overflow.
254 */
255 function ArrayToNum(bytes) {
256 var result = 0;
257 for (var i = 0; i < bytes.length; ++i) {
258 result = result * 10;
259 result += (bytes[i] - 48);
260 }
261 return result;
262 }
263
264 /*
265 * Convert the int value to a new big endian Uint8Array and return.
266 * If value is 0 or negative, return Uint8Array(0).
267 */
268 function NumToArray(value) {
269 value = Math.round(value);
270 if (value <= 0)
271 return new Uint8Array(0);
272
273 numString = value.toString();
274 var size = numString.length;
275 var result = new Uint8Array(size);
276 for (i = 0; i < size; i++) {
277 result[i] = numString.charCodeAt(i);
278 }
279 return result;
280 }
281///////////////////////////////////////////////////////////////////////////////////////////////////////////
282
283 /*
284 var AsyncGetClosure = function AsyncGetClosure(T0) {
285 this.T0 = T0; // Start time
286 // Inherit from Closure.
287 Closure.call(this);
288 };
289
290 AsyncGetClosure.prototype.upcall = function(kind, upcallInfo) {
291 //console.log("Closure.upcall() executed.");
292 if (kind == Closure.UPCALL_FINAL) {
293 // Do nothing.
294 } else if (kind == Closure.UPCALL_CONTENT) {
295 var T1 = new Date();
296
297 var content = upcallInfo.contentObject;
298 nameStr = escape(content.name.getName());
299 document.getElementById('content').innerHTML += "<p>Time elapsed: " + (T1 - this.T0) + " ms</p>";
300 document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
301 document.getElementById('content').innerHTML += "<p>Content buffer length: " + content.content.length + "</p>";
302 //console.log("In callback, nameStr: " + nameStr);
303 //console.log("In callback, content: ");
304 //console.log(content.content.length);
305 //document.getElementById('content').innerHTML += contentObjectToHtml(content);
306 } else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
307 console.log("Closure.upcall called with interest time out.");
308 }
309 return Closure.RESULT_OK;
310 };
311 */
312
313 function run() {
314 document.getElementById('content').innerHTML += "<p>Started...</p>";
315 //ndn.expressInterest(new Name(document.getElementById('interest').value), new AsyncGetClosure( new Date() ));
316 var name = new Name(document.getElementById('interest').value);
317 ndncon.expressInterest( name,
318 new ContentClosure( ndncon, new Date() ));
319 }
320
321 </script>
322
323</head>
324<body >
325
326 <form>
327 Please Enter an Interest:<br />
Wentao Shangdd691552013-01-25 17:52:43 -0800328 <input id="interest" type="text" name="INTEREST" size="50" value="/wentao.shang/mars.jpg/%00" />
Wentao Shangfcb16262013-01-20 14:42:46 -0800329 </form>
330
331 <button onclick="run()">Fetch Content</button>
332
333 <p id="content">Result: <br/></p>
334
335</body>
336</html>