IBR-DTNSuite  0.10
Client.h
Go to the documentation of this file.
1 /*
2  * Client.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 CLIENT_H_
23 #define CLIENT_H_
24 
25 #include "ibrdtn/data/Number.h"
26 #include "ibrdtn/data/Bundle.h"
29 #include <ibrcommon/thread/Mutex.h>
31 #include <ibrcommon/Exceptions.h>
32 #include <ibrcommon/thread/Queue.h>
33 
34 namespace dtn
35 {
36  namespace api
37  {
42  {
43  public:
44  ConnectionException(string what = "A connection error occurred.") throw() : ibrcommon::Exception(what)
45  {
46  };
47  };
48 
53  {
54  public:
55  ConnectionTimeoutException(string what = "Timeout.") throw() : ConnectionException(what)
56  {
57  };
58  };
59 
64  {
65  public:
66  ConnectionAbortedException(string what = "Aborted.") throw() : ConnectionException(what)
67  {
68  };
69  };
70 
81  {
82  private:
90  class AsyncReceiver : public ibrcommon::JoinableThread
91  {
92  public:
98  AsyncReceiver(Client &client);
99 
104  virtual ~AsyncReceiver();
105 
106  protected:
112  void run() throw ();
113 
118  void __cancellation() throw ();
119 
120  private:
121  // member variable for the reference to the client object
122  Client &_client;
123 
124  bool _running;
125  };
126 
127  public:
132  {
135  };
136 
148  Client(const std::string &app, ibrcommon::socketstream &stream, const COMMUNICATION_MODE mode = MODE_BIDIRECTIONAL);
149  Client(const std::string &app, const dtn::data::EID &group, ibrcommon::socketstream &stream, const COMMUNICATION_MODE mode = MODE_BIDIRECTIONAL);
150 
154  virtual ~Client();
155 
159  void connect();
160 
165  void close();
166 
170  void abort();
171 
177  virtual void eventConnectionDown() throw ();
178 
184  virtual void eventBundleAck(const dtn::data::Length &ack) throw ();
185 
191 
196  virtual void eventTimeout() throw () {};
197 
202  virtual void eventError() throw () {};
203 
209  virtual void eventConnectionUp(const dtn::streams::StreamContactHeader&) throw () {};
210 
215  virtual void eventBundleRefused() throw () {};
216 
221  virtual void eventBundleForwarded() throw () {};
222 
226  void operator<<(const dtn::data::Bundle &b);
227 
236 
237  // public variable
238  dtn::data::Length lastack;
239 
240  protected:
246  virtual void received(const dtn::streams::StreamContactHeader&) {};
247 
253  virtual void received(const dtn::data::Bundle &b);
254 
255  private:
256  // tcp stream reference to send/receive data to the daemon
257  ibrcommon::socketstream &_stream;
258 
259  // communication mode flags
260  COMMUNICATION_MODE _mode;
261 
262  // own application suffix
263  std::string _app;
264 
265  // group to join
266  dtn::data::EID _group;
267 
268  // The asynchronous receiver thread which receives incoming bundles
269  Client::AsyncReceiver _receiver;
270 
271  // the queue for incoming bundles, when used in synchronous mode
273  };
274  }
275 }
276 
277 #endif /* CLIENT_H_ */