IBR-DTNSuite  0.10
vsocket.h
Go to the documentation of this file.
1 /*
2  * vsocket.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef VSOCKET_H_
23 #define VSOCKET_H_
24 
25 #include <ibrcommon/data/File.h>
27 #include <ibrcommon/net/socket.h>
29 #include <ibrcommon/net/vaddress.h>
30 #include <ibrcommon/thread/Mutex.h>
32 #include <string>
33 #include <list>
34 #include <set>
35 #include <map>
36 
37 namespace ibrcommon
38 {
42  typedef std::set<basesocket*> socketset;
43 
45  {
46  public:
48  {};
49  };
50 
52  {
53  public:
55  {};
56  };
57 
58  class vsocket
59  {
60  public:
64  vsocket();
65 
69  virtual ~vsocket();
70 
76  void add(basesocket *socket);
77 
85  void add(basesocket *socket, const vinterface &iface);
86 
91  void remove(basesocket *socket);
92 
97  size_t size() const;
98 
102  socketset getAll() const;
103 
109  socketset get(const vinterface &iface) const;
110 
114  void destroy();
115 
119  void clear();
120 
124  void up() throw (socket_exception);
125 
129  void down() throw ();
130 
136  void select(socketset *readset, socketset *writeset, socketset *errorset, struct timeval *tv = NULL) throw (socket_exception);
137 
138  private:
139  class pipesocket : public basesocket
140  {
141  public:
142  pipesocket();
143  virtual ~pipesocket();
144  virtual void up() throw (socket_exception);
145  virtual void down() throw (socket_exception);
146 
147  void read(char *buf, size_t len) throw (socket_exception);
148  void write(const char *buf, size_t len) throw (socket_exception);
149 
150  int getOutput() const throw (socket_exception);
151 
152  private:
153  int _output_fd;
154  };
155 
156  class SocketState : public ibrcommon::Conditional {
157  public:
158  class state_exception : public Exception
159  {
160  public:
161  state_exception(string error) : Exception(error)
162  {};
163  };
164 
165  enum STATE {
166  NONE,
167  SAFE_DOWN,
168  DOWN,
169  DOWN_REQUEST,
170  PENDING_DOWN,
171  PENDING_UP,
172  IDLE,
173  SELECT,
174  SAFE_REQUEST,
175  SAFE
176  };
177 
178  SocketState(STATE initial);
179  virtual ~SocketState();
180 
181  STATE get() const;
182 
183  void set(STATE s) throw (state_exception);
184  void wait(STATE s, STATE abortstate = NONE) throw (state_exception);
185  void setwait(STATE s, STATE abortstate = NONE) throw (state_exception);
186 
187  private:
188  void __change(STATE s);
189  std::string __getname(STATE s) const;
190 
191  STATE _state;
192  };
193 
194  class SafeLock
195  {
196  public:
197  SafeLock(SocketState &state, vsocket &sock);
198  virtual ~SafeLock();
199  private:
200  SocketState &_state;
201  };
202 
203  class SelectGuard
204  {
205  public:
206  SelectGuard(SocketState &state, int &counter);
207  virtual ~SelectGuard();
208  private:
209  SocketState &_state;
210  int &_counter;
211  };
212 
213  void interrupt();
214 
215  ibrcommon::Mutex _socket_lock;
216  socketset _sockets;
217  std::map<vinterface, socketset> _socket_map;
218 
219  pipesocket _pipe;
220 
221  SocketState _state;
222  int _select_count;
223  };
224 }
225 
226 #endif /* VSOCKET_H_ */