blob: 448d8bf3f80c8d91207b2dc155d52c013acb5a69 [file] [log] [blame]
Jeff Thompson287a3182012-11-11 18:12:20 -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
8<head>
9 <title>NDN Put via WebSocket</title>
10
11 <script type="text/javascript" src="../Helper.js"></script>
12
13 <script type="text/javascript">
14 var ndn = new NDN();
Jeff Thompson5b265a72012-11-12 01:13:08 -080015 ndn.transport.connectWebSocket(ndn);
Jeff Thompson287a3182012-11-11 18:12:20 -080016
17 var AsyncPutClosure = function AsyncPutClosure() {
18 // Inherit from Closure.
19 Closure.call(this);
20 };
21
22 AsyncPutClosure.prototype.upcall = function(kind, upcallInfo) {
23 if (kind == Closure.UPCALL_FINAL) {
24 // Do nothing.
25 } else if (kind == Closure.UPCALL_INTEREST) {
26 // Extract the Name from the Interest. var name = upcallInfo.Interest.name;Íž
27 // Check that we want to respond to upcallInfo.interest. If yes, construct a ContentObject
28 // response, put it to the NDN and return Closure.RESULT_INTEREST_CONSUMED. Else fall
29 // through to return Content.RESULT_OK.
30 // See linemaker.py for more details.
31 console.log('AsyncPutClosure.upcall() called.');
32 var content = document.getElementById('content').value;
33 var interest = upcallInfo.interest;
Jeff Thompson5b265a72012-11-12 01:13:08 -080034 var nameStr = escape(interest.name.getName());
Jeff Thompson287a3182012-11-11 18:12:20 -080035
36 var si = new SignedInfo();
37 si.setFields();
38
39 var answer = DataUtils.toNumbersFromString(content);
40
41 var co = new ContentObject(new Name(nameStr), si, answer, new Signature());
42 co.sign();
43 var hex = encodeToHexContentObject(co);
44
45 var bytearray = new Uint8Array(hex.length / 2);
46 for (var i = 0; i < hex.length; i = i + 2) {
47 bytearray[i / 2] = '0x' + hex.substr(i, 2);
48 }
49
Jeff Thompson5b265a72012-11-12 01:13:08 -080050 upcallInfo.ndn.transport.ws.send(bytearray.buffer);
Jeff Thompson287a3182012-11-11 18:12:20 -080051 console.log('ws.send() finised.');
52
53 return Closure.RESULT_INTEREST_CONSUMED;
54 }
55 return Closure.RESULT_OK;
56 };
57
58
59 function run() {
60 var contentName = document.getElementById('contentname').value;
61
Jeff Thompson5b265a72012-11-12 01:13:08 -080062 var result = ndn.registerPrefix(contentName, new AsyncPutClosure());
Jeff Thompson287a3182012-11-11 18:12:20 -080063
64 document.getElementById('result').innerHTML = 'Content name \'' + contentName
65 +'\' published. Result: ' + result;
66 }
67
68 </script>
69
70</head>
71<body >
72 <form>
73 <div>
74 <p>Please Enter a Content Name:</p>
75
76 <input id="contentname" type="text" name="CONTENTNAME" value="/wentao.shang/regtest001" />
77
78 <p>Please Enter the Content:</p>
79
80 <textarea id="content" cols="40" rows="5" name="CONTENT" >This works!</textarea>
81
82 <br />
83 </div>
84 </form>
85 <div>
86 <button onclick="run()">Publish Content</button>
87 </div>
88
89 <p id="result"></p>
90
91</body>
92</html>