blob: 6c01f3e5212b2430b2bb01a8507b1e7181cde03f [file] [log] [blame]
Alexander Afanasyev31367922015-02-09 20:51:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev31367922015-02-09 20:51:10 -08004 * 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
Junxiao Shib2600172016-07-11 08:53:53 +000026#ifndef NFD_RIB_SERVICE_HPP
27#define NFD_RIB_SERVICE_HPP
Alexander Afanasyev31367922015-02-09 20:51:10 -080028
Alexander Afanasyev31367922015-02-09 20:51:10 -080029#include "core/config-file.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080030
31#include <ndn-cxx/face.hpp>
Davide Pesaventoa3148082018-04-12 18:21:54 -040032#include <ndn-cxx/mgmt/dispatcher.hpp>
Alexander Afanasyev31367922015-02-09 20:51:10 -080033#include <ndn-cxx/security/key-chain.hpp>
34#include <ndn-cxx/transport/transport.hpp>
35
36namespace nfd {
37namespace rib {
38
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080039class RibManager;
40
Alexander Afanasyev31367922015-02-09 20:51:10 -080041/**
Junxiao Shib2600172016-07-11 08:53:53 +000042 * \brief initializes and executes NFD-RIB service thread
Teng Liang04d5ce62018-08-06 10:20:24 +080043 *
44 * Only one instance of this class can be created at any time
Alexander Afanasyev31367922015-02-09 20:51:10 -080045 */
Junxiao Shib2600172016-07-11 08:53:53 +000046class Service : noncopyable
Alexander Afanasyev31367922015-02-09 20:51:10 -080047{
48public:
49 class Error : public std::runtime_error
50 {
51 public:
52 explicit
53 Error(const std::string& what)
54 : std::runtime_error(what)
55 {
56 }
57 };
58
59 /**
Junxiao Shib2600172016-07-11 08:53:53 +000060 * \brief create NFD-RIB service
61 * \param configFile absolute or relative path of configuration file
62 * \param keyChain the KeyChain
Teng Liang04d5ce62018-08-06 10:20:24 +080063 * \throw std::logic_error Instance of rib::Service has been already constructed
64 * \throw std::logic_error Instance of rib::Service is not constructed on RIB thread
Alexander Afanasyev31367922015-02-09 20:51:10 -080065 */
Junxiao Shib2600172016-07-11 08:53:53 +000066 Service(const std::string& configFile, ndn::KeyChain& keyChain);
Alexander Afanasyev31367922015-02-09 20:51:10 -080067
68 /**
Junxiao Shib2600172016-07-11 08:53:53 +000069 * \brief create NFD-RIB service
70 * \param config parsed configuration section
71 * \param keyChain the KeyChain
72 * \note This constructor overload is more appropriate for integrated environments,
73 * such as NS-3 or android. Error messages related to configuration file
74 * will use "internal://nfd.conf" as configuration filename.
Teng Liang04d5ce62018-08-06 10:20:24 +080075 * \throw std::logic_error Instance of rib::Service has been already constructed
76 * \throw std::logic_error Instance of rib::Service is not constructed on RIB thread
Alexander Afanasyev31367922015-02-09 20:51:10 -080077 */
Junxiao Shib2600172016-07-11 08:53:53 +000078 Service(const ConfigSection& config, ndn::KeyChain& keyChain);
Alexander Afanasyev31367922015-02-09 20:51:10 -080079
80 /**
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080081 * \brief Destructor
82 */
Junxiao Shib2600172016-07-11 08:53:53 +000083 ~Service();
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080084
85 /**
Junxiao Shib2600172016-07-11 08:53:53 +000086 * \brief Perform initialization of NFD-RIB instance
87 *
88 * After initialization, NFD-RIB instance can be started by running the global io_service
Alexander Afanasyev31367922015-02-09 20:51:10 -080089 */
90 void
91 initialize();
92
Teng Liang04d5ce62018-08-06 10:20:24 +080093 /**
94 * \brief Get a reference to the only instance of this class
95 * \throw std::logic_error No instance has been constructed
96 * \throw std::logic_error This function is invoked on a thread other than the RIB thread
97 */
98 static Service&
99 get();
100
Alexander Afanasyev31367922015-02-09 20:51:10 -0800101private:
Alexander Afanasyev31367922015-02-09 20:51:10 -0800102 /**
103 * \brief Look into the config file and construct appropriate transport to communicate with NFD
Junxiao Shib2600172016-07-11 08:53:53 +0000104 * If NFD-RIB instance was initialized with config file, INFO format is assumed
Alexander Afanasyev31367922015-02-09 20:51:10 -0800105 */
106 shared_ptr<ndn::Transport>
107 getLocalNfdTransport();
108
109private:
Teng Liang04d5ce62018-08-06 10:20:24 +0800110 static Service* s_instance;
111
Alexander Afanasyev31367922015-02-09 20:51:10 -0800112 std::string m_configFile;
113 ConfigSection m_configSection;
114
115 ndn::KeyChain& m_keyChain;
116 unique_ptr<ndn::Face> m_face;
Yanbiao Licf0db022016-01-29 00:54:25 -0800117 unique_ptr<ndn::mgmt::Dispatcher> m_dispatcher;
Alexander Afanasyev31367922015-02-09 20:51:10 -0800118 unique_ptr<RibManager> m_ribManager;
119};
120
121} // namespace rib
122} // namespace nfd
123
Junxiao Shib2600172016-07-11 08:53:53 +0000124#endif // NFD_RIB_SERVICE_HPP