Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
forwarder_interface.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 extern "C" {
19 #include <hicn/ctrl/api.h>
20 #include <hicn/util/ip_address.h>
21 }
22 
23 #ifndef ASIO_STANDALONE
24 #define ASIO_STANDALONE 1
25 #endif
26 #include <asio.hpp>
27 #include <asio/steady_timer.hpp>
28 #include <functional>
29 #include <thread>
30 #include <unordered_map>
31 
32 namespace transport {
33 
34 typedef std::function<void(uint32_t, bool)> SetRouteCallback;
35 
36 struct route_info_t {
37  int family;
38  std::string remote_addr;
39  uint16_t remote_port;
40  std::string route_addr;
41  uint8_t route_len;
42 };
43 
44 using RouteInfoPtr = std::shared_ptr<route_info_t>;
45 
47  public:
48  ForwarderInterface(asio::io_service &io_service)
49  : external_ioservice_(io_service),
50  work_(std::make_unique<asio::io_service::work>(internal_ioservice_)),
51  sock_(nullptr),
52  thread_(std::make_unique<std::thread>(
53  [this]() { internal_ioservice_.run(); })),
54  check_routes_timer_(nullptr),
55  pending_add_route_counter_(0),
56  route_id_(0),
57  closed_(false) {}
58 
60 
61  int connectToForwarder();
62 
63  void removeConnectedUserNow(uint32_t route_id);
64 
65  // to be called at the server
66  // at the client this creates a race condition
67  // and the program enters in a loop
68  void scheduleRemoveConnectedUser(uint32_t route_id);
69 
70  template <typename Callback>
71  void createFaceAndRoute(RouteInfoPtr &&route_info, Callback &&callback) {
72  internal_ioservice_.post([this, _route_info = std::move(route_info),
73  _callback = std::forward<Callback>(callback)]() {
74  pending_add_route_counter_++;
75  uint8_t max_try = 5;
76  auto timer = new asio::steady_timer(internal_ioservice_);
77  internalCreateFaceAndRoute(std::move(_route_info), max_try, timer,
78  std::move(_callback));
79  });
80  }
81 
82  int32_t getMainListenerPort();
83 
84  void close();
85 
86  private:
87  void internalRemoveConnectedUser(uint32_t route_id);
88 
89  void internalCreateFaceAndRoute(RouteInfoPtr route_info, uint8_t max_try,
90  asio::steady_timer *timer,
91  SetRouteCallback callback);
92 
93  int tryToCreateFaceAndRoute(route_info_t *route_info);
94 
95  asio::io_service &external_ioservice_;
96  asio::io_service internal_ioservice_;
97  std::unique_ptr<asio::io_service::work> work_;
98  hc_sock_t *sock_;
99  std::unique_ptr<std::thread> thread_;
100  std::unordered_map<uint32_t, RouteInfoPtr> route_status_;
101  std::unique_ptr<asio::steady_timer> check_routes_timer_;
102  uint32_t pending_add_route_counter_;
103  uint32_t route_id_;
104  bool closed_;
105 };
106 
107 } // namespace transport
transport::ForwarderInterface
Definition: forwarder_interface.h:46
transport::route_info_t
Definition: forwarder_interface.h:36
transport
Definition: forwarder_config.h:32
hc_sock_s
Definition: api_private.h:89
api.h
hICN control library API