aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/includes/hicn/transport/interfaces/portal.h
blob: 22c8591f4d1bb02a9abec8e41b507210327c46dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <hicn/transport/core/content_object.h>
#include <hicn/transport/core/interest.h>
#include <hicn/transport/core/prefix.h>

#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
#include <asio/io_service.hpp>
#include <functional>

#define UNSET_CALLBACK 0

namespace transport {

namespace interface {

template <typename PrefixType>
class BasicBindConfig {
  static_assert(std::is_same<core::Prefix, PrefixType>::value,
                "Prefix must be a Prefix type.");

  const uint32_t standard_cs_reserved = 5000;

 public:
  template <typename T>
  BasicBindConfig(T &&prefix)
      : prefix_(std::forward<T &&>(prefix)),
        content_store_reserved_(standard_cs_reserved) {}

  template <typename T>
  BasicBindConfig(T &&prefix, uint32_t cs_reserved)
      : prefix_(std::forward<T &&>(prefix)),
        content_store_reserved_(cs_reserved) {}

  TRANSPORT_ALWAYS_INLINE const PrefixType &prefix() const { return prefix_; }

  TRANSPORT_ALWAYS_INLINE uint32_t csReserved() const {
    return content_store_reserved_;
  }

 private:
  PrefixType prefix_;
  uint32_t content_store_reserved_;
};

using BindConfig = BasicBindConfig<core::Prefix>;

class Portal {
 public:
  /**
   * Consumer callback is an abstract class containing two methods to be
   * implemented by a consumer application.
   */
  class ConsumerCallback {
   public:
    virtual void onContentObject(core::Interest &i, core::ContentObject &c) = 0;
    virtual void onTimeout(core::Interest::Ptr &&i) = 0;
    virtual void onError(std::error_code ec) = 0;
  };

  /**
   * Producer callback is an abstract class containing two methods to be
   * implemented by a producer application.
   */
  class ProducerCallback {
   public:
    virtual void onInterest(core::Interest &i) = 0;
    virtual void onError(std::error_code ec) = 0;
  };

  using OnContentObjectCallback =
      std::function<void(core::Interest &, core::ContentObject &)>;
  using OnInterestTimeoutCallback = std::function<void(core::Interest::Ptr &&)>;

  Portal();

  Portal(asio::io_service &io_service);

  /**
   * Set the consumer callback.
   *
   * @param consumer_callback - The pointer to the ConsumerCallback object.
   */
  void setConsumerCallback(ConsumerCallback *consumer_callback);

  /**
   * Set the producer callback.
   *
   * @param producer_callback - The pointer to the ProducerCallback object.
   */
  void setProducerCallback(ProducerCallback *producer_callback);

  /**
   * Connect the transport to the local hicn forwarder.
   *
   * @param is_consumer - Boolean specifying if the application on top of portal
   * is a consumer or a producer.
   */
  void connect(bool is_consumer = true);

  /**
   * Destructor.
   */
  ~Portal();

  /**
   * Check if there is already a pending interest for a given name.
   *
   * @param name - The interest name.
   */
  bool interestIsPending(const core::Name &name);

  /**
   * Send an interest through to the local forwarder.
   *
   * @param interest - The pointer to the interest. The ownership of the
   * interest is transferred by the caller to portal.
   *
   * @param on_content_object_callback - If the caller wishes to use a different
   * callback to be called for this interest, it can set this parameter.
   * Otherwise ConsumerCallback::onContentObject will be used.
   *
   * @param on_interest_timeout_callback - If the caller wishes to use a
   * different callback to be called for this interest, it can set this
   * parameter. Otherwise ConsumerCallback::onTimeout will be used.
   */
  void sendInterest(
      core::Interest::Ptr &&interest,
      OnContentObjectCallback &&on_content_object_callback = UNSET_CALLBACK,
      OnInterestTimeoutCallback &&on_interest_timeout_callback =
          UNSET_CALLBACK);

  /**
   * Register a producer name to the local forwarder and optionally set the
   * content store size in a per-face manner.
   *
   * @param config - The configuration for the local forwarder binding.
   */
  void bind(const BindConfig &config);

  void runEventsLoop();

  /**
   * Run one event and return.
   */
  void runOneEvent();

  /**
   * Send a data packet to the local forwarder. As opposite to sendInterest, the
   * ownership of the content object is not transferred to the portal.
   *
   * @param content_object - The data packet.
   */
  void sendContentObject(core::ContentObject &content_object);
  /**
   * Stop the event loop, canceling all the pending events in the event queue.
   *
   * Beware that stopping the event loop DOES NOT disconnect the transport from
   * the local forwarder, the connector underneath will stay connected.
   */
  void stopEventsLoop();

  /**
   * Disconnect the transport from the local forwarder.
   */
  void killConnection();

  /**
   * Clear the pending interest hash table.
   */
  void clear();

  /**
   * Get a reference to the io_service object.
   */
  asio::io_service &getIoService();

  /**
   * Register a route to the local forwarder.
   */
  void registerRoute(core::Prefix &prefix);

 private:
  void *implementation_;
};

}  // namespace interface
}  // namespace transport