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