Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | **/ |
| 25 | |
| 26 | #ifndef NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |
| 28 | |
| 29 | #include "channel.hpp" |
| 30 | #include "core/global-io.hpp" |
| 31 | #include "core/scheduler.hpp" |
| 32 | #include "websocket-face.hpp" |
| 33 | |
| 34 | namespace nfd { |
| 35 | |
| 36 | /** |
| 37 | * \brief Class implementing WebSocket-based channel to create faces |
| 38 | * |
| 39 | * |
| 40 | */ |
| 41 | class WebSocketChannel : public Channel |
| 42 | { |
| 43 | public: |
| 44 | /** |
| 45 | * \brief Exception of WebSocketChannel |
| 46 | */ |
| 47 | class Error : public std::runtime_error |
| 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& what) |
| 52 | : runtime_error(what) |
| 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * \brief Create WebSocket channel for the local endpoint |
| 59 | * |
| 60 | * To enable creation of faces upon incoming connections, |
| 61 | * one needs to explicitly call WebSocketChannel::listen method. |
| 62 | * The created socket is bound to the localEndpoint. |
| 63 | * |
| 64 | * \throw WebSocketChannel::Error if bind on the socket fails |
| 65 | */ |
| 66 | explicit |
| 67 | WebSocketChannel(const websocket::Endpoint& localEndpoint); |
| 68 | |
| 69 | virtual |
| 70 | ~WebSocketChannel(); |
| 71 | |
| 72 | /** |
| 73 | * \brief Enable listening on the local endpoint, accept connections, |
| 74 | * and create faces when remote host makes a connection |
| 75 | * \param onFaceCreated Callback to notify successful creation of the face |
| 76 | * |
| 77 | * \throws WebSocketChannel::Error if called multiple times |
| 78 | */ |
| 79 | void |
| 80 | listen(const FaceCreatedCallback& onFaceCreated); |
| 81 | |
| 82 | /** |
| 83 | * \brief Get number of faces in the channel |
| 84 | */ |
| 85 | size_t |
| 86 | size() const; |
| 87 | |
| 88 | private: |
| 89 | void |
| 90 | handleMessage(websocketpp::connection_hdl hdl, websocket::Server::message_ptr msg); |
| 91 | |
| 92 | void |
| 93 | handleOpen(websocketpp::connection_hdl hdl); |
| 94 | |
| 95 | void |
| 96 | handleClose(websocketpp::connection_hdl hdl); |
| 97 | |
| 98 | private: |
| 99 | websocket::Endpoint m_localEndpoint; |
| 100 | |
| 101 | websocket::Server m_server; |
| 102 | |
| 103 | /** |
| 104 | * Callbacks for face creation. |
| 105 | * New communications are detected using async_receive_from. |
| 106 | * Its handler has a fixed signature. No space for the face callback |
| 107 | */ |
| 108 | FaceCreatedCallback m_onFaceCreatedCallback; |
| 109 | |
| 110 | typedef std::map< websocketpp::connection_hdl, shared_ptr<WebSocketFace> > ChannelFaceMap; |
| 111 | ChannelFaceMap m_channelFaces; |
| 112 | |
| 113 | /** |
| 114 | * \brief If true, it means the function listen has already been called |
| 115 | */ |
| 116 | bool m_isListening; |
| 117 | |
| 118 | }; |
| 119 | |
| 120 | } // namespace nfd |
| 121 | |
| 122 | #endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |