Source: network-nack.js

  1. /**
  2. * Copyright (C) 2016-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-cxx nack.hpp https://github.com/named-data/ndn-cxx/blob/master/src/lp/nack.hpp
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /**
  21. * NetworkNack represents a network Nack packet and includes a Nack reason.
  22. * @constructor
  23. */
  24. var NetworkNack = function NetworkNack()
  25. {
  26. this.reason_ = NetworkNack.Reason.NONE;
  27. this.otherReasonCode_ = -1;
  28. };
  29. exports.NetworkNack = NetworkNack;
  30. /**
  31. * A NetworkNack.Reason specifies the reason in a NetworkNack packet. If the
  32. * reason code in the packet is not a recognized enum value, then we use
  33. * Reason.OTHER_CODE and you can call getOtherReasonCode(). We do this to keep
  34. * the recognized reason values independent of packet encoding formats.
  35. */
  36. NetworkNack.Reason = {
  37. NONE: 0,
  38. CONGESTION: 50,
  39. DUPLICATE: 100,
  40. NO_ROUTE: 150,
  41. OTHER_CODE: 0x7fff
  42. };
  43. /**
  44. * Get the network Nack reason.
  45. * @return {number} The reason enum value from NetworkNack.Reason. If this is
  46. * Reason.OTHER_CODE, then call getOtherReasonCode() to get the unrecognized
  47. * reason code.
  48. */
  49. NetworkNack.prototype.getReason = function() { return this.reason_; };
  50. /**
  51. * Get the reason code from the packet which is other than a recognized
  52. * Reason enum value. This is only meaningful if getReason() is
  53. * Reason.OTHER_CODE.
  54. * @return {number} The reason code.
  55. */
  56. NetworkNack.prototype.getOtherReasonCode = function()
  57. {
  58. return this.otherReasonCode_;
  59. };
  60. /**
  61. * Set the network Nack reason.
  62. * @param {number} reason The network Nack reason enum value from
  63. * NetworkNack.Reason. If the packet's reason code is not a recognized Reason
  64. * enum value, use Reason.OTHER_CODE and call setOtherReasonCode().
  65. */
  66. NetworkNack.prototype.setReason = function(reason) { this.reason_ = reason; };
  67. /**
  68. * Set the packet's reason code to use when the reason enum is
  69. * Reason.OTHER_CODE. If the packet's reason code is a recognized enum value,
  70. * just call setReason().
  71. * @param {number} otherReasonCode The packet's unrecognized reason code, which
  72. * must be non-negative.
  73. */
  74. NetworkNack.prototype.setOtherReasonCode = function(otherReasonCode)
  75. {
  76. if (otherReasonCode < 0)
  77. throw new Error("NetworkNack other reason code must be non-negative");
  78. this.otherReasonCode_ = otherReasonCode;
  79. };
  80. /**
  81. * Get the first header field in lpPacket which is a NetworkNack. This is
  82. * an internal method which the application normally would not use.
  83. * @param {LpPacket} lpPacket The LpPacket with the header fields to search.
  84. * @return {NetworkNack} The first NetworkNack header field, or null if not
  85. * found.
  86. */
  87. NetworkNack.getFirstHeader = function(lpPacket)
  88. {
  89. for (var i = 0; i < lpPacket.countHeaderFields(); ++i) {
  90. var field = lpPacket.getHeaderField(i);
  91. if (field instanceof NetworkNack)
  92. return field;
  93. }
  94. return null;
  95. };