ndn-js
Version:
A JavaScript client library for Named Data Networking
82 lines (68 loc) • 3.19 kB
HTML
<!--
* Copyright (C) 2014-2019 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* A copy of the GNU Lesser General Public License is in the file COPYING.
-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Encode/Decode FibEntry</title>
<script type="text/javascript" src="../../contrib/dcodeio/long.min.js"></script>
<script type="text/javascript" src="../../contrib/dcodeio/bytebuffer-ab.min.js"></script>
<script type="text/javascript" src="../../contrib/dcodeio/protobuf.min.js"></script>
<script type="text/javascript" src="../../build/ndn.js"></script>
<script type="text/javascript">
function testEncodeDecodeFibEntry()
{
var result = document.getElementById('result');
result.innerHTML = "";
var ProtoBuf = dcodeIO.ProtoBuf;
var builder = ProtoBuf.loadProtoFile("fib-entry.proto");
var descriptor = builder.lookup("ndn_message.FibEntryMessage");
var FibEntryMessage = descriptor.build();
var message = new FibEntryMessage();
message.fib_entry = new FibEntryMessage.FibEntry();
message.fib_entry.name = new FibEntryMessage.Name();
message.fib_entry.name.add("component", "ndn");
message.fib_entry.name.add("component", "ucla");
var nextHopRecord = new FibEntryMessage.NextHopRecord();
message.fib_entry.add("next_hop_records", nextHopRecord);
nextHopRecord.face_id = 16;
nextHopRecord.cost = 1;
// Encode the Protobuf message object as TLV.
var encoding = ProtobufTlv.encode(message, descriptor);
var decodedMessage = new FibEntryMessage();
ProtobufTlv.decode(decodedMessage, descriptor, encoding);
result.innerHTML += "Re-decoded FibEntry:</br>";
// This should print the same values that we put in message above.
var value = "";
value += ProtobufTlv.toName(decodedMessage.fib_entry.name.component).toUri();
value += " nexthops = {";
for (var i = 0; i < decodedMessage.fib_entry.next_hop_records.length; ++i)
value += "faceid=" + decodedMessage.fib_entry.next_hop_records[i].face_id
+ " (cost=" + decodedMessage.fib_entry.next_hop_records[i].cost + ")";
value += " }";
result.innerHTML += value + "</br>";
}
</script>
</head>
<body >
<button onclick="testEncodeDecodeFibEntry()">Test</button>
<p id="result"></p>
</body>
</html>