NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
websocket-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "websocket-transport.hpp"
27 
28 namespace nfd {
29 namespace face {
30 
31 NFD_LOG_INIT("WebSocketTransport");
32 
33 static bool
34 isLoopback(const boost::asio::ip::address& addr)
35 {
36  if (addr.is_loopback()) {
37  return true;
38  }
39  // Workaround for loopback IPv4-mapped IPv6 addresses
40  // see https://svn.boost.org/trac/boost/ticket/9084
41  else if (addr.is_v6()) {
42  auto addr6 = addr.to_v6();
43  if (addr6.is_v4_mapped()) {
44  return addr6.to_v4().is_loopback();
45  }
46  }
47 
48  return false;
49 }
50 
53  time::milliseconds pingInterval)
54  : m_handle(hdl)
55  , m_server(server)
56  , m_pingInterval(pingInterval)
57 {
58  const auto& sock = m_server.get_con_from_hdl(hdl)->get_socket();
59  this->setLocalUri(FaceUri(sock.local_endpoint(), "ws"));
60  this->setRemoteUri(FaceUri(sock.remote_endpoint(), "wsclient"));
61 
62  if (isLoopback(sock.local_endpoint().address()) &&
63  isLoopback(sock.remote_endpoint().address())) {
65  }
66  else {
68  }
69 
72  this->setMtu(MTU_UNLIMITED);
73 
74  this->schedulePing();
75 
76  NFD_LOG_FACE_INFO("Creating transport");
77 }
78 
79 void
80 WebSocketTransport::doSend(Transport::Packet&& packet)
81 {
82  NFD_LOG_FACE_TRACE(__func__);
83 
84  websocketpp::lib::error_code error;
85  m_server.send(m_handle, packet.packet.wire(), packet.packet.size(),
87  if (error)
88  return processErrorCode(error);
89 
90  NFD_LOG_FACE_TRACE("Successfully sent: " << packet.packet.size() << " bytes");
91 }
92 
93 void
94 WebSocketTransport::receiveMessage(const std::string& msg)
95 {
96  NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes");
97 
98  bool isOk = false;
99  Block element;
100  std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size());
101  if (!isOk) {
102  NFD_LOG_FACE_WARN("Failed to parse message payload");
103  return;
104  }
105 
106  this->receive(Transport::Packet(std::move(element)));
107 }
108 
109 void
110 WebSocketTransport::schedulePing()
111 {
112  m_pingEventId = scheduler::schedule(m_pingInterval, bind(&WebSocketTransport::sendPing, this));
113 }
114 
115 void
116 WebSocketTransport::sendPing()
117 {
118  NFD_LOG_FACE_TRACE(__func__);
119 
120  websocketpp::lib::error_code error;
121  m_server.ping(m_handle, "NFD-WebSocket", error);
122  if (error)
123  return processErrorCode(error);
124 
125  ++this->nOutPings;
126 
127  this->schedulePing();
128 }
129 
130 void
132 {
133  NFD_LOG_FACE_TRACE(__func__);
134 
135  ++this->nInPongs;
136 }
137 
138 void
140 {
141  NFD_LOG_FACE_ERROR("Pong timeout");
143  doClose();
144 }
145 
146 void
147 WebSocketTransport::processErrorCode(const websocketpp::lib::error_code& error)
148 {
149  NFD_LOG_FACE_TRACE(__func__);
150 
154  // transport is shutting down, ignore any errors
155  return;
156 
157  NFD_LOG_FACE_ERROR("Send or ping operation failed: " << error.message());
159  doClose();
160 }
161 
162 void
164 {
165  NFD_LOG_FACE_TRACE(__func__);
166 
167  m_pingEventId.cancel();
168 
169  // use the non-throwing variant and ignore errors, if any
170  websocketpp::lib::error_code error;
171  m_server.close(m_handle, websocketpp::close::status::normal, "closed by NFD", error);
172 
174 }
175 
176 } // namespace face
177 } // namespace nfd
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:151
void cancel()
cancels the event manually
Definition: scheduler.cpp:95
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
Definition: block.cpp:195
void ping(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a ping to a specific connection.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:95
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:396
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
Definition: face-log.hpp:91
stores a packet along with the remote endpoint
Definition: transport.hpp:113
static value const normal
Normal closure, meaning that the purpose for which the connection was established has been fulfilled...
Definition: close.hpp:76
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
WebSocketTransport(websocketpp::connection_hdl hdl, websocket::Server &server, time::milliseconds pingInterval)
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:426
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
Definition: endpoint.hpp:643
void receiveMessage(const std::string &msg)
Translates a message into a Block and delivers it to the link service.
void setMtu(ssize_t mtu)
Definition: transport.hpp:438
the transport is being closed due to a failure
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:408
static bool isLoopback(const boost::asio::ip::address &addr)
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
the transport is closed, and can be safely deallocated
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:384
PacketCounter nOutPings
count of outgoing Pings
PacketCounter nInPongs
count of incoming Pongs
the transport is being closed gracefully, either by the peer or by a call to close() ...
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:43
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
Definition: scheduler.cpp:47
void close(connection_hdl hdl, close::status::value const code, std::string const &reason, lib::error_code &ec)
TransportState getState() const
Definition: transport.hpp:445
void setState(TransportState newState)
set transport state
Definition: transport.cpp:174
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:117
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)
void doClose() final
performs Transport specific operations to close the transport
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-log.hpp:88