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