Connectors.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_PPI_Connectors_
18 #define HH_SENF_PPI_Connectors_ 1
19 
20 // Custom includes
21 #include <deque>
22 #include <boost/noncopyable.hpp>
23 #include <boost/scoped_ptr.hpp>
24 #include <senf/Utils/safe_bool.hh>
25 #include <senf/Utils/Exception.hh>
26 #include <senf/Packets/Packets.hh>
27 #include "predecl.hh"
28 #include "detail/Callback.hh"
29 #include "Throttling.hh"
30 #include "ModuleManager.hh"
31 
32 //#include "Connectors.mpp"
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34 
35 namespace senf {
36 namespace ppi {
37 namespace connector {
38 
144  { IncompatibleConnectorsException() : senf::Exception("Incompatible connectors") {} };
145 
147  { DuplicateConnectionException() : senf::Exception("Duplicate connection") {} };
148 
149 
156  class Connector
157  : ModuleManager::Initializable, boost::noncopyable
158  {
161  public:
162  Connector & peer() const;
163  module::Module & module() const;
164 
165  bool connected() const;
166 
167  void disconnect();
168 
170 
171 #ifndef SENF_PPI_NOTRACE
172  enum TraceState { NO_TRACING, TRACE_IDS, TRACE_CONTENTS };
173 
174  static void staticTracingState(TraceState state);
175  void tracingState(TraceState state);
176  TraceState tracingState() const;
177  static void tracingFilter(std::string const & traceFilter);
178 #endif
179 
180  protected:
181  Connector();
182  virtual ~Connector();
183 
184  void connect(Connector & target);
185 
186 #ifndef SENF_PPI_NOTRACE
187  void trace(Packet const & p, char const * label);
188  void throttleTrace(char const * label, char const * type);
189 #endif
190 
191  void unregisterConnector();
192 
193  virtual void v_disconnected();
194  virtual void v_connected();
195 
196  private:
197  virtual std::type_info const & v_packetTypeId();
198 
199  void setModule(module::Module & module);
200 
201  Connector * peer_;
202  module::Module * module_;
203 
204  console::ScopedDirectory<> consoleDir_;
205 
206 #ifndef SENF_PPI_NOTRACE
207  TraceState traceState_;
208  static TraceState staticTraceState_;
209  static std::string traceFilter_;
210 #endif
211 
212  friend class module::Module;
213  };
214 
232  : public virtual Connector
233  {
234  public:
235  ~PassiveConnector();
236 
237  template <class Handler>
238  void onRequest(Handler handler);
239 
251  bool throttled() const;
252  bool nativeThrottled() const;
253 
254  void throttle();
255  void unthrottle();
256 
257  ActiveConnector & peer() const;
258 
259  protected:
261 
262  void emit();
263 
264  virtual void v_disconnected();
265  virtual void v_connected();
266 
267  private:
268  virtual void v_init();
269 
270  // Called by the routing to change the throttling state from forwarding routes
271  void notifyThrottle();
272  void notifyUnthrottle();
273 
274  // Internal members to emit throttling notifications to the connected peer
275  void emitThrottle();
276  void emitUnthrottle();
277 
278  // Called after unthrottling the connector
279  virtual void v_unthrottleEvent();
280 
281  // called by ForwardingRoute to register a new route
282  void registerRoute(ForwardingRoute & route);
283  void unregisterRoute(ForwardingRoute & route);
284 
285  ActiveConnector * peer_;
286 
287  typedef ppi::detail::Callback<>::type Callback;
288  Callback callback_;
289 
290  bool remoteThrottled_;
291  bool nativeThrottled_;
292 
293  typedef std::vector<ForwardingRoute*> Routes;
294  Routes routes_;
295 
297  };
298 
310  : public virtual Connector
311  {
312  typedef ppi::detail::Callback<>::type Callback;
313  public:
314  ~ActiveConnector();
315 
316  template <class Handler>
317  void onThrottle(Handler handler);
318 
327  void onThrottle();
328 
329  template <class Handler>
330  void onUnthrottle(Handler handler);
331 
340  void onUnthrottle();
341 
342  bool throttled() const;
343 
344  PassiveConnector & peer() const;
345 
346  protected:
347  ActiveConnector();
348 
349  virtual void v_disconnected();
350  virtual void v_connected();
351 
352  private:
353  virtual void v_init();
354 
355  // called by the peer() to forward throttling notifications
356  void notifyThrottle();
357  void notifyUnthrottle();
358 
359  // called by ForwardingRoute to register a new route
360  void registerRoute(ForwardingRoute & route);
361  void unregisterRoute(ForwardingRoute & route);
362 
363  PassiveConnector * peer_;
364 
365  Callback throttleCallback_;
366  Callback unthrottleCallback_;
367 
368  typedef std::vector<ForwardingRoute*> NotifyRoutes;
369  NotifyRoutes notifyRoutes_;
370 
371  bool throttled_;
372 
374  friend class PassiveConnector;
375  };
376 
398  : public virtual Connector
399  {
400  typedef std::deque<Packet> Queue;
401  public:
402  typedef Queue::const_iterator queue_iterator;
403  typedef Queue::size_type size_type;
404 
405 
406  Packet const & operator()();
407 
415  Packet const & read();
416 
417  OutputConnector & peer() const;
418 
419  size_type queueSize() const;
420  bool empty() const;
421 
422  protected:
423  InputConnector();
424 
425  virtual void v_disconnected();
426  virtual void v_connected();
427 
428  private:
429  void enqueue(Packet const & p);
430 
431  virtual void v_requestEvent();
432  virtual void v_enqueueEvent();
433  virtual void v_dequeueEvent();
434 
435  OutputConnector * peer_;
436  Queue queue_;
437  Packet const * fastPacket_;
438  Packet slowPacket_;
439 
440  friend class OutputConnector;
441  };
442 
450  : public virtual Connector
451  {
452  public:
453  void operator()(Packet const & p);
454 
455  void write(Packet const & p);
456 
457  InputConnector & peer() const;
458 
459  protected:
460  OutputConnector();
461 
462  virtual void v_disconnected();
463  virtual void v_connected();
464 
465  private:
466  InputConnector * peer_;
467  };
468 
479  : public PassiveConnector, public InputConnector,
480  public safe_bool<GenericPassiveInput>
481  {
482  public:
483  GenericActiveOutput & peer() const;
484 
485  bool boolean_test() const;
486 
487  template <class ThrottlingDisc>
488  void throttlingDisc(ThrottlingDisc const & disc);
489 
493  void throttlingDisc(ThrottlingDiscipline::None_t);
495 
496  protected:
498 
499  virtual void v_disconnected();
500  virtual void v_connected();
501 
502  private:
503  void v_enqueueEvent();
504  void v_dequeueEvent();
505  void v_unthrottleEvent();
506 
507  GenericActiveOutput * peer_;
508  boost::scoped_ptr<ThrottlingDiscipline> throttlingDisc_;
509  };
510 
514  : public PassiveConnector, public OutputConnector,
515  public safe_bool<GenericPassiveOutput>
516  {
517  public:
518  GenericActiveInput & peer() const;
519 
520  bool boolean_test() const;
521 
522  void connect(GenericActiveInput & target);
523 
524  friend class GenericActiveInput;
525 
526  protected:
528 
529  virtual void v_disconnected();
530  virtual void v_connected();
531 
532  private:
533  GenericActiveInput * peer_;
534  };
535 
539  : public ActiveConnector, public InputConnector,
540  public safe_bool<GenericActiveInput>
541  {
542  public:
543  GenericPassiveOutput & peer() const;
544 
545  bool boolean_test() const;
546 
547  void request();
548 
549  protected:
551 
552  virtual void v_disconnected();
553  virtual void v_connected();
554 
555  private:
556  void v_requestEvent();
557 
558  GenericPassiveOutput * peer_;
559  };
560 
564  : public ActiveConnector, public OutputConnector,
565  public safe_bool<GenericActiveOutput>
566  {
567  public:
568  GenericPassiveInput & peer() const;
569 
570  bool boolean_test() const;
571 
572  void connect(GenericPassiveInput & target);
573 
574  protected:
576 
577  virtual void v_disconnected();
578  virtual void v_connected();
579 
580  private:
581  GenericPassiveInput * peer_;
582  };
583 
584 
585 #ifndef DOXYGEN
586 
587 # define TypedConnector_Input read
588 # define TypedConnector_Output write
589 # define TypedConnector(pType, dir) \
590  template <class PacketType> \
591  class pType ## dir \
592  : public Generic ## pType ## dir, \
593  private detail::Typed ## dir ## Mixin<pType ## dir <PacketType>, PacketType> \
594  { \
595  typedef detail::Typed ## dir ## Mixin<pType ## dir <PacketType>, PacketType> mixin; \
596  public: \
597  using mixin::operator(); \
598  using mixin::TypedConnector_ ## dir ; \
599  private: \
600  virtual std::type_info const & v_packetTypeId() \
601  { return typeid(typename PacketType::type); } \
602  friend class detail::Typed ## dir ## Mixin<pType ## dir <PacketType>, PacketType>; \
603  }; \
604  template <> \
605  class pType ## dir <Packet> : public Generic ## pType ## dir \
606  {}
607 
608  TypedConnector( Passive, Input );
609  TypedConnector( Passive, Output );
610  TypedConnector( Active, Input );
611  TypedConnector( Active, Output );
612 
613 # undef TypedConnector
614 # undef TypedConnector_Input
615 # undef TypedConnector_Output
616 
617 #else
618 
631  template <class PacketType=Packet>
633  {
634  public:
635  PacketType const & operator()();
636 
639  PacketType const & read();
640  };
641 
654  template <class PacketType=Packet>
656  {
657  public:
658  PacketType const & operator()();
659 
662  PacketType const & read();
663  };
664 
676  template <class PacketType=Packet>
678  {
679  public:
680  void operator()(PacketType const & packet);
681  void write(PacketType const & packet);
682  };
683 
696  template <class PacketType=Packet>
698  {
699  public:
700  void operator()(PacketType const & packet);
701  void write(PacketType const & packet);
702  };
703 
704 #endif
705 
706 }}}
707 
708 //-/////////////////////////////////////////////////////////////////////////////////////////////////
709 #include "Connectors.cci"
710 #include "Connectors.ct"
711 #include "Connectors.cti"
712 #endif
713 
714 
715 // Local Variables:
716 // mode: c++
717 // fill-column: 100
718 // c-file-style: "senf"
719 // indent-tabs-mode: nil
720 // ispell-local-dictionary: "american"
721 // compile-command: "scons -u test"
722 // comment-column: 40
723 // End:
Connector actively sending packets.
Definition: Connectors.hh:677
Callback public header.
Module base-class.
Definition: Module.hh:169
bool empty()
u8 type
#define SENF_LOG_CLASS_AREA()
Combination of PassiveConnector and OutputConnector.
Definition: Connectors.hh:513
Passive connector base-class.
Definition: Connectors.hh:231
Combination of ActiveConnector and InputConnector.
Definition: Connectors.hh:538
Connector actively reading packets.
Definition: Connectors.hh:632
Active connector base-class.
Definition: Connectors.hh:309
Input connector base-class.
Definition: Connectors.hh:397
Queue::size_type size_type
Unsigned type for counting queue elements.
Definition: Connectors.hh:403
ModuleManager public header.
Output connector base-class.
Definition: Connectors.hh:449
Incompatible connectors connected.
Definition: Connectors.hh:143
Combination of ActiveConnector and OutputConnector.
Definition: Connectors.hh:563
boost::function< void(Arg)> type
Definition: Callback.hh:47
void connect(connector::FastActiveOutput< PacketType > &source, connector::FastPassiveInput< PacketType > &target)
console::ScopedDirectory & consoleDir()
Queue::const_iterator queue_iterator
Iterator type of the embedded queue.
Definition: Connectors.hh:402
predecl public header
#define SENF_LOG_DEFAULT_LEVEL(level)
Connector passively providing packets.
Definition: Connectors.hh:697
Throttling public header.
Connector base-class.
Definition: Connectors.hh:156
Forwarding route base class.
Definition: Route.hh:69
Combination of PassiveConnector and InputConnector.
Definition: Connectors.hh:478
Connector passively receiving packets.
Definition: Connectors.hh:655