Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 1 | <?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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 15 | ndn.transport.connectWebSocket(ndn);
|
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 16 |
|
| 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 34 | var nameStr = escape(interest.name.getName());
|
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 35 |
|
| 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 50 | upcallInfo.ndn.transport.ws.send(bytearray.buffer);
|
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 51 | 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 Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame^] | 62 | var result = ndn.registerPrefix(contentName, new AsyncPutClosure());
|
Jeff Thompson | 287a318 | 2012-11-11 18:12:20 -0800 | [diff] [blame] | 63 |
|
| 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> |