blob: c99af335b7021e819935c5f128b887709af71689 [file] [log] [blame]
Wentao Shangf8b4a7d2012-12-25 12:52:07 -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;
27 this.ndn = ndn;
28 this.totalBlocks = 0;
29
30 this.firstReceivedSegmentNumber = null;
31 this.firstReceivedContentObject = null;
32 }
33
34 ContentClosure.prototype.upcall = function(kind, upcallInfo) {
35 if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
36 var T1 = new Date();
37 document.getElementById('content').innerHTML += "<p>Interest time out.</p>";
38 document.getElementById('content').innerHTML += "<p>Time elapsed: " + (T1 - this.T0) + " ms including 4s timeout</p>";
39 document.getElementById('content').innerHTML += "<p>Total number of blocks: " + this.totalBlocks + "</p>";
40 return Closure.RESULT_OK;
41 }
Wentao Shangb42483a2013-01-03 15:32:32 -080042 /*
43 if (kind == Closure.UPCALL_CONTENT_BAD) {
44 console.log("NdnProtocol.ContentClosure: signature verification failed");
45 return Closure.RESULT_OK;
46 }
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080047
48 if (!(kind == Closure.UPCALL_CONTENT ||
49 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
50 // The upcall is not for us.
51 return Closure.RESULT_ERR;
Wentao Shangb42483a2013-01-03 15:32:32 -080052 */
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080053 var contentObject = upcallInfo.contentObject;
54 if (contentObject.content == null) {
Wentao Shangb42483a2013-01-03 15:32:32 -080055 console.log("NdnProtocol.ContentClosure: contentObject.content is null");
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080056 return Closure.RESULT_ERR;
57 }
58
59 // Use the segmentNumber to load multiple segments.
60 //var segmentNumber = ArrayToNum(contentObject.name.components[contentObject.name.components.length - 1]);
61 var segmentNumber = DataUtils.bigEndianToUnsignedInt
62 (contentObject.name.components[contentObject.name.components.length - 1]);
63 //console.log(segmentNumber);
64
65 if (this.firstReceivedSegmentNumber == null) {
66 // This is the first call.
67 this.firstReceivedSegmentNumber = segmentNumber;
68 if (segmentNumber != 0) {
69 // Special case: Save this content object for later and request segment zero.
70 this.firstReceivedContentObject = contentObject;
71 var componentsForZero = contentObject.name.components.slice
72 (0, contentObject.name.components.length - 1);
73 componentsForZero.push([0]);
74 this.ndn.expressInterest(new Name(componentsForZero), this);
75 return Closure.RESULT_OK;
76 }
77 }
78
79 // Process received data here...
80 // Count content length
81 //nameStr = escape(contentObject.name.getName());
82 //document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
83 //document.getElementById('content').innerHTML += "<p>Content buffer length: " + contentObject.content.length + "</p>";
84 this.totalBlocks++;
85
86 // Check for the special case if the saved content is for the next segment that we need.
87 if (this.firstReceivedContentObject != null &&
88 this.firstReceivedSegmentNumber == segmentNumber + 1) {
89 // Substitute the saved contentObject send its content and keep going.
90 contentObject = this.firstReceivedContentObject;
91 segmentNumber = segmentNumber + 1;
92 // Clear firstReceivedContentObject to save memory.
93 this.firstReceivedContentObject = null;
94
95 // Process received data here...
96 // Count content length
97 //nameStr = escape(contentObject.name.getName());
98 //document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
99 //document.getElementById('content').innerHTML += "<p>Content buffer length: " + contentObject.content.length + "</p>";
100 this.totalBlocks++;
101 }
102
103 var finalSegmentNumber = null;
104 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
105 //finalSegmentNumber = ArrayToNum(contentObject.signedInfo.finalBlockID);
106 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
107
108 if (finalSegmentNumber == null || segmentNumber != finalSegmentNumber) {
109 // Make a name for the next segment and get it.
110 //var segmentNumberPlus1 = NumToArray(segmentNumber + 1);
111 // Make a name for the next segment and get it.
112 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
113 // Put a 0 byte in front.
114 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
115 nextSegmentNumber.set(segmentNumberPlus1, 1);
116
117 var components = contentObject.name.components.slice
118 (0, contentObject.name.components.length - 1);
119 components.push(nextSegmentNumber);
120 //components.push(segmentNumberPlus1);
121 this.ndn.expressInterest(new Name(components), this);
122 }
123 else {
124 // Final block received.
125 // Record stop time
126 var T1 = new Date();
127 document.getElementById('content').innerHTML += "<p>Final block received.</p>";
128 document.getElementById('content').innerHTML += "<p>Time elapsed: " + (T1 - this.T0) + " ms</p>";
129 document.getElementById('content').innerHTML += "<p>Total number of blocks: " + this.totalBlocks + "</p>";
130 }
131
132 return Closure.RESULT_OK;
133 };
134
135 /*
136 * Convert the big endian Uint8Array to an unsigned int.
137 * Don't check for overflow.
138 */
139 function ArrayToNum(bytes) {
140 var result = 0;
141 for (var i = 0; i < bytes.length; ++i) {
142 result = result * 10;
143 result += (bytes[i] - 48);
144 }
145 return result;
146 };
147
148 /*
149 * Convert the int value to a new big endian Uint8Array and return.
150 * If value is 0 or negative, return Uint8Array(0).
151 */
152 function NumToArray(value) {
153 value = Math.round(value);
154 if (value <= 0)
155 return new Uint8Array(0);
156
157 numString = value.toString();
158 var size = numString.length;
159 var result = new Uint8Array(size);
160 for (i = 0; i < size; i++) {
161 result[i] = numString.charCodeAt(i);
162 }
163 return result;
164 };
165///////////////////////////////////////////////////////////////////////////////////////////////////////////
166
167 /*
168 var AsyncGetClosure = function AsyncGetClosure(T0) {
169 this.T0 = T0; // Start time
170 // Inherit from Closure.
171 Closure.call(this);
172 };
173
174 AsyncGetClosure.prototype.upcall = function(kind, upcallInfo) {
175 //console.log("Closure.upcall() executed.");
176 if (kind == Closure.UPCALL_FINAL) {
177 // Do nothing.
178 } else if (kind == Closure.UPCALL_CONTENT) {
179 var T1 = new Date();
180
181 var content = upcallInfo.contentObject;
182 nameStr = escape(content.name.getName());
183 document.getElementById('content').innerHTML += "<p>Time elapsed: " + (T1 - this.T0) + " ms</p>";
184 document.getElementById('content').innerHTML += "<p>Name string: " + nameStr + "</p>";
185 document.getElementById('content').innerHTML += "<p>Content buffer length: " + content.content.length + "</p>";
186 //console.log("In callback, nameStr: " + nameStr);
187 //console.log("In callback, content: ");
188 //console.log(content.content.length);
189 //document.getElementById('content').innerHTML += contentObjectToHtml(content);
190 } else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
191 console.log("Closure.upcall called with interest time out.");
192 }
193 return Closure.RESULT_OK;
194 };
195 */
196
197 function run() {
198 document.getElementById('content').innerHTML += "<p>Started...</p>";
199 //ndn.expressInterest(new Name(document.getElementById('interest').value), new AsyncGetClosure( new Date() ));
200 var name = new Name(document.getElementById('interest').value);
201 ndncon.expressInterest( name,
202 new ContentClosure( ndncon, new Date() ));
203 }
204
205 </script>
206
207</head>
208<body >
209
210 <form>
211 Please Enter an Interest:<br />
212 <input id="interest" type="text" name="INTEREST" size="50" value="/wentao.shang/choir_jail.png" />
213 </form>
214
215 <button onclick="run()">Fetch Content</button>
216
217 <p id="content">Result: <br/></p>
218
219</body>
220</html>