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