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