NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
websocket-face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "websocket-face.hpp"
27 
28 namespace nfd {
29 
30 NFD_LOG_INIT("WebSocketFace");
31 
32 WebSocketFace::WebSocketFace(const FaceUri& remoteUri, const FaceUri& localUri,
33  websocketpp::connection_hdl hdl,
34  websocket::Server& server)
35  : Face(remoteUri, localUri)
36  , m_handle(hdl)
37  , m_server(server)
38  , m_closed(false)
39 {
40  NFD_LOG_FACE_INFO("Creating face");
42 }
43 
44 void
46 {
47  if (m_closed)
48  return;
49 
50  NFD_LOG_FACE_TRACE(__func__);
51 
52  this->emitSignal(onSendInterest, interest);
53 
54  const Block& payload = interest.wireEncode();
55  this->getMutableCounters().getNOutBytes() += payload.size();
56 
57  websocketpp::lib::error_code ec;
58  m_server.send(m_handle, payload.wire(), payload.size(),
59  websocketpp::frame::opcode::binary, ec);
60  if (ec)
61  NFD_LOG_FACE_WARN("Failed to send Interest: " << ec.message());
62 }
63 
64 void
66 {
67  if (m_closed)
68  return;
69 
70  NFD_LOG_FACE_TRACE(__func__);
71 
72  this->emitSignal(onSendData, data);
73 
74  const Block& payload = data.wireEncode();
75  this->getMutableCounters().getNOutBytes() += payload.size();
76 
77  websocketpp::lib::error_code ec;
78  m_server.send(m_handle, payload.wire(), payload.size(),
79  websocketpp::frame::opcode::binary, ec);
80  if (ec)
81  NFD_LOG_FACE_WARN("Failed to send Data: " << ec.message());
82 }
83 
84 void
86 {
87  if (m_closed)
88  return;
89 
90  NFD_LOG_FACE_INFO("Closing face");
91 
92  m_closed = true;
93  scheduler::cancel(m_pingEventId);
94  websocketpp::lib::error_code ec;
95  m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ec);
96  // ignore error on close
97  fail("Face closed");
98 }
99 
100 void
101 WebSocketFace::handleReceive(const std::string& msg)
102 {
103  // Copy message into Face internal buffer
104  if (msg.size() > ndn::MAX_NDN_PACKET_SIZE)
105  {
106  NFD_LOG_FACE_WARN("Received WebSocket message is too big (" << msg.size() << " bytes)");
107  return;
108  }
109 
110  NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes");
111  this->getMutableCounters().getNInBytes() += msg.size();
112 
113  // Try to parse message data
114  bool isOk = false;
115  Block element;
116  std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()),
117  msg.size());
118  if (!isOk)
119  {
120  NFD_LOG_FACE_WARN("Received block is invalid or too large to process");
121  return;
122  }
123 
124  if (!this->decodeAndDispatchInput(element))
125  {
126  NFD_LOG_FACE_WARN("Received unrecognized TLV block of type " << element.type());
127  // ignore unknown packet and proceed
128  }
129 }
130 
131 } // namespace nfd
void sendInterest(const Interest &interest) 1
send an Interest
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to construct block from Buffer.
Definition: block.cpp:253
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face.hpp:321
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:58
signal::Signal< Face, Interest > onSendInterest
fires when an Interest is sent out
Definition: face.hpp:86
websocketpp::server< websocketpp::config::asio > Server
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void setPersistency(ndn::nfd::FacePersistency persistency)
Definition: face.hpp:257
void close() 1
Close the face.
bool decodeAndDispatchInput(const Block &element)
Definition: face.cpp:59
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:45
signal::Signal< Face, Data > onSendData
fires when a Data is sent out
Definition: face.hpp:89
represents a face
Definition: face.hpp:57
size_t size() const
Definition: block.cpp:504
#define emitSignal(...)
(implementation detail)
Definition: signal-emit.hpp:76
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:52
void fail(const std::string &reason)
fail the face and raise onFail event if it&#39;s UP; otherwise do nothing
Definition: face.cpp:87
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face.hpp:327
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: interest.cpp:217
const ByteCounter & getNInBytes() const
received bytes
FaceCounters & getMutableCounters()
Definition: face.hpp:275
uint32_t type() const
Definition: block.hpp:346
WebSocketFace(const FaceUri &remoteUri, const FaceUri &localUri, websocketpp::connection_hdl hdl, websocket::Server &server)
void sendData(const Data &data) 1
send a Data
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
const uint8_t * wire() const
Definition: block.cpp:495
void handleReceive(const std::string &msg)
represents a Data packet
Definition: data.hpp:39
const ByteCounter & getNOutBytes() const
sent bytes
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face.hpp:330
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:39