GComm  0.2.3
protonet.hpp
1 //
2 // Copyright (C) 2009 Codership Oy <info@codership.com>
3 //
4 
6 // @file protonet.hpp
7 //
8 // This file defines protonet interface used by gcomm.
9 //
10 
11 
12 #ifndef GCOMM_PROTONET_HPP
13 #define GCOMM_PROTONET_HPP
14 
15 #include "gu_uri.hpp"
16 #include "gu_datetime.hpp"
17 #include "protostack.hpp"
18 #include "gu_config.hpp"
19 
20 #include "socket.hpp"
21 
22 #include <deque>
23 
24 #ifndef GCOMM_PROTONET_MAX_VERSION
25 #define GCOMM_PROTONET_MAX_VERSION 0
26 #endif // GCOMM_PROTONET_MAX_VERSION
27 
28 namespace gcomm
29 {
30  // Forward declarations
31  class Protonet;
32 }
33 
35 // Abstract Protonet interface class
36 //
38 {
39 public:
40  Protonet(gu::Config& conf, const std::string& type, int version)
41  :
42  protos_ (),
43  version_(version),
44  conf_ (conf),
45  type_ (type)
46  { }
47 
48  virtual ~Protonet() { }
49 
51  // Insert Protostack to be handled by Protonet
52  //
53  // @param pstack Pointer to Protostack
54  //
55  void insert(Protostack* pstack);
56 
58  // Erase Protostack from Protonet to stop dispatching events
59  // to Protostack
60  //
61  // @param pstack Pointer to Protostack
62  //
63  void erase(Protostack* pstack);
64 
66  // Create new Socket
67  //
68  // @param uri URI to specify Socket type
69  //
70  // @return Socket
71  //
72  virtual gcomm::SocketPtr socket(const gu::URI& uri) = 0;
73 
75  // Create new Acceptor
76  //
77  // @param uri URI to specify Acceptor type
78  //
79  // @return Acceptor
80  //
81  virtual Acceptor* acceptor(const gu::URI& uri) = 0;
82 
84  // Dispatch events until period p has passed or event
85  // loop is interrupted.
86  //
87  // @param p Period to run event_loop(), negative value means forever
88  //
89  virtual void event_loop(const gu::datetime::Period& p) = 0;
90 
92  // Iterate over Protostacks and handle timers
93  //
94  // @return Time of next known timer expiration
95  //
96  gu::datetime::Date handle_timers();
97 
99  // Interrupt event loop
100  //
101  virtual void interrupt() = 0;
102 
104  // Enter Protonet critical section
105  //
106  virtual void enter() = 0;
107 
109  // Leave Protonet critical section
110  //
111  virtual void leave() = 0;
112 
113  bool set_param(const std::string& key, const std::string& val);
114  gu::Config& conf() { return conf_; }
115 
117  // Factory method for creating Protonets
118  //
119  static Protonet* create(gu::Config& conf);
120 
121  const std::string& type() const { return type_; }
122 
123  virtual size_t mtu() const = 0;
124 
125 protected:
126 
127  std::deque<Protostack*> protos_;
128  int version_;
129  static const int max_version_ = GCOMM_PROTONET_MAX_VERSION;
130  gu::Config& conf_;
131 private:
132  std::string type_;
133 };
134 
135 #endif // GCOMM_PROTONET_HPP
Definition: protostack.hpp:24
Definition: protonet.hpp:37
Definition: socket.hpp:72