<?xml version = "1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<!--
 * Copyright (C) 2016 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">

/*
 * This sends a faces list request to the local NFD and prints the response.
 * The local NFD must be running.
 * This is equivalent to the NFD command line command "nfd-status -f".
 * See http://redmine.named-data.net/projects/nfd/wiki/Management .
 */
function testListFaces()
{
  var result = document.getElementById('result');
  result.innerHTML = "";

  // Connect to the local forwarder with a WebSocket.
  var face = new Face({host: "localhost"});

  var interest = new Interest(new Name("/localhost/nfd/faces/list"));
  interest.setInterestLifetimeMilliseconds(4000);
  result.innerHTML += "Express interest " + interest.getName().toUri() + "</br>";

  SegmentFetcher.fetch
    (face, interest, SegmentFetcher.DontVerifySegment,
     function(content) {
       printFaceStatuses(content);
     },
     function(errorCode, message) {
       result.innerHTML += message + "</br>";
     });
}

/**
 * This is called when all the segments are received to decode the
 * encodedMessage repeated TLV FaceStatus messages and display the values.
 * @param {Blob} encodedMessage The repeated TLV-encoded FaceStatus.
 */
function printFaceStatuses(encodedMessage)
{
  var ProtoBuf = dcodeIO.ProtoBuf;
  var builder = ProtoBuf.loadProto("package ndn_message; \
 \
message FaceStatusMessage { \
  message FaceStatus { \
    required uint64 face_id = 105; \
    required string uri = 114; \
    required string local_uri = 129; \
    optional uint64 expiration_period  = 109; \
    required uint64 face_scope = 132; \
    required uint64 face_persistency = 133; \
    required uint64 link_type = 134; \
    required uint64 n_in_interests = 144; \
    required uint64 n_in_datas = 145; \
    required uint64 n_in_nacks = 151; \
    required uint64 n_out_interests = 146; \
    required uint64 n_out_datas = 147; \
    required uint64 n_out_nacks = 152; \
    required uint64 n_in_bytes = 148; \
    required uint64 n_out_bytes = 149; \
  } \
 \
  repeated FaceStatus face_status = 128; \
} \
");
  var descriptor = builder.lookup("ndn_message.FaceStatusMessage");
  var FaceStatusMessage = descriptor.build();

  var faceStatusMessage = new FaceStatusMessage();
  ProtobufTlv.decode(faceStatusMessage, descriptor, encodedMessage);

  result.innerHTML += "Faces:" + "</br>";
  for (var iEntry = 0; iEntry < faceStatusMessage.face_status.length; ++iEntry) {
    var faceStatus = faceStatusMessage.face_status[iEntry];

    // Format to look the same as "nfd-status -f".
    var line = "  faceid=" + faceStatus.face_id +
      " remote=" + faceStatus.uri +
      " local=" + faceStatus.local_uri;
    if (faceStatus.expiration_period != undefined)
      // Convert milliseconds to seconds.
      line += " expires=" +
        Math.round(faceStatus.expiration_period / 1000) + "s";
    line += " counters={" + "in={" + faceStatus.n_in_interests +
      "i " + faceStatus.n_in_datas + "d " + faceStatus.n_in_bytes + "B}" +
      " out={" + faceStatus.n_out_interests + "i "+ faceStatus.n_out_datas +
      "d " + faceStatus.n_out_bytes + "B}" + "}" +
      " " + (faceStatus.face_scope == 1 ? "local" : "non-local") +
      " " + (faceStatus.face_persistency == 2 ? "permanent" :
             faceStatus.face_persistency == 1 ? "on-demand" : "persistent") +
      " " + (faceStatus.link_type == 1 ? "multi-access" : "point-to-point");

    result.innerHTML += line + "</br>";
  }
}

    </script>

</head>
<body >
    <button onclick="testListFaces()">Test</button>

    <p id="result"></p>

</body>
</html>