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 
51 WebSocketTransport::WebSocketTransport(websocketpp::connection_hdl hdl,
52  websocket::Server& server,
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())) {
64  this->setScope(ndn::nfd::FACE_SCOPE_LOCAL);
65  }
66  else {
67  this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
68  }
69 
70  this->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
71  this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
72  this->setMtu(MTU_UNLIMITED);
73 
74  this->schedulePing();
75 
76  NFD_LOG_FACE_INFO("Creating transport");
77 }
78 
79 void
80 WebSocketTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)
81 {
82  if (newPersistency != ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
83  BOOST_THROW_EXCEPTION(
84  std::invalid_argument("WebSocketTransport supports only FACE_PERSISTENCY_ON_DEMAND"));
85  }
86 }
87 
88 void
89 WebSocketTransport::doSend(Transport::Packet&& packet)
90 {
91  NFD_LOG_FACE_TRACE(__func__);
92 
93  websocketpp::lib::error_code error;
94  m_server.send(m_handle, packet.packet.wire(), packet.packet.size(),
95  websocketpp::frame::opcode::binary, error);
96  if (error)
97  return processErrorCode(error);
98 
99  NFD_LOG_FACE_TRACE("Successfully sent: " << packet.packet.size() << " bytes");
100 }
101 
102 void
103 WebSocketTransport::receiveMessage(const std::string& msg)
104 {
105  NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes");
106 
107  bool isOk = false;
108  Block element;
109  std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size());
110  if (!isOk) {
111  NFD_LOG_FACE_WARN("Failed to parse message payload");
112  return;
113  }
114 
115  this->receive(Transport::Packet(std::move(element)));
116 }
117 
118 void
119 WebSocketTransport::schedulePing()
120 {
121  m_pingEventId = scheduler::schedule(m_pingInterval, bind(&WebSocketTransport::sendPing, this));
122 }
123 
124 void
125 WebSocketTransport::sendPing()
126 {
127  NFD_LOG_FACE_TRACE(__func__);
128 
129  ++this->nOutPings;
130 
131  websocketpp::lib::error_code error;
132  m_server.ping(m_handle, "NFD-WebSocket", error);
133  if (error)
134  return processErrorCode(error);
135 
136  this->schedulePing();
137 }
138 
139 void
141 {
142  NFD_LOG_FACE_TRACE(__func__);
143 
144  ++this->nInPongs;
145 }
146 
147 void
149 {
150  NFD_LOG_FACE_WARN(__func__);
152  doClose();
153 }
154 
155 void
156 WebSocketTransport::processErrorCode(const websocketpp::lib::error_code& error)
157 {
158  NFD_LOG_FACE_TRACE(__func__);
159 
163  // transport is shutting down, ignore any errors
164  return;
165 
166  NFD_LOG_FACE_WARN("Send or ping operation failed: " << error.message());
167 
169  doClose();
170 }
171 
172 void
174 {
175  NFD_LOG_FACE_TRACE(__func__);
176 
177  m_pingEventId.cancel();
178 
179  // use the non-throwing variant and ignore errors, if any
180  websocketpp::lib::error_code error;
181  m_server.close(m_handle, websocketpp::close::status::normal, "closed by NFD", error);
182 
184 }
185 
186 } // namespace face
187 } // namespace nfd
void receiveMessage(const std::string &msg)
Translates a message into a Block and delivers it to the link service.
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:362
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:74
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:386
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:95
void setMtu(ssize_t mtu)
Definition: transport.hpp:416
websocketpp::server< websocketpp::config::asio > Server
Definition: websocketpp.hpp:46
void setPersistency(ndn::nfd::FacePersistency persistency)
changes face persistency setting
Definition: transport.cpp:131
virtual void doClose() final
performs Transport specific operations to close the transport
WebSocketTransport(websocketpp::connection_hdl hdl, websocket::Server &server, time::milliseconds pingInterval)
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:119
stores a packet along with the remote endpoint
Definition: transport.hpp:113
the transport is being closed due to a failure
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-log.hpp:83
static bool isLoopback(const boost::asio::ip::address &addr)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
the transport is closed, and can be safely deallocated
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:80
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:404
the transport is requested to be closed
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
TransportState getState() const
Definition: transport.hpp:423
void setState(TransportState newState)
set transport state
Definition: transport.cpp:150
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:374
PacketCounter nOutPings
count of outgoing Pings
void cancel()
cancels the event manually
Definition: scheduler.cpp:95
PacketCounter nInPongs
count of incoming Pongs
virtual void beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) final
invoked before persistency is changed