IBR-DTNSuite  0.10
NativeSession.h
Go to the documentation of this file.
1 /*
2  * NativeSession.h
3  *
4  * Copyright (C) 2013 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 NATIVESESSION_H_
23 #define NATIVESESSION_H_
24 
26 #include "api/Registration.h"
27 #include "core/EventReceiver.h"
28 #include <ibrdtn/data/Bundle.h>
32 #include <ibrcommon/thread/Queue.h>
34 #include <ibrcommon/thread/Mutex.h>
35 #include <ibrcommon/Exceptions.h>
36 
37 namespace dtn
38 {
39  namespace api
40  {
42  {
43  public:
44  NativeSessionException(string what = "An error happened.") throw() : ibrcommon::Exception(what)
45  {
46  };
47  };
48 
50  {
51  public:
52  BundleNotFoundException(string what = "Bundle not found.") throw() : ibrcommon::Exception(what)
53  {
54  };
55  };
56 
58  public:
62  virtual ~NativeSessionCallback() = 0;
63 
69  virtual void notifyBundle(const dtn::data::BundleID &id) throw () = 0;
70 
75  virtual void notifyStatusReport(const dtn::data::EID &source, const dtn::data::StatusReportBlock &report) throw () = 0;
76 
81  virtual void notifyCustodySignal(const dtn::data::EID &source, const dtn::data::CustodySignalBlock &custody) throw () = 0;
82  };
83 
84  class NativeSession {
85  public:
86  static const std::string TAG;
87 
89  REG1 = 0,
90  REG2 = 1
91  };
92 
98 
102  virtual ~NativeSession();
103 
108  void destroy() throw ();
109 
113  const dtn::data::EID& getNodeEID() const throw ();
114 
119  void setEndpoint(const std::string &suffix) throw (NativeSessionException);
120 
125  void resetEndpoint() throw ();
126 
130  void addEndpoint(const std::string &suffix) throw (NativeSessionException);
131 
135  void removeEndpoint(const std::string &suffix) throw (NativeSessionException);
136 
141  void addRegistration(const dtn::data::EID &eid) throw (NativeSessionException);
142 
146  void removeRegistration(const dtn::data::EID &eid) throw (NativeSessionException);
147 
151  void clearRegistration() throw ();
152 
156  std::vector<std::string> getSubscriptions() throw ();
157 
162  void next(RegisterIndex ri) throw (BundleNotFoundException);
163 
167  void load(RegisterIndex ri, const dtn::data::BundleID &id) throw (BundleNotFoundException);
168 
172  void get(RegisterIndex ri) throw ();
173 
177  void getInfo(RegisterIndex ri) throw ();
178 
182  void free(RegisterIndex ri) throw (BundleNotFoundException);
183 
187  void clear(RegisterIndex ri) throw ();
188 
192  void delivered(const dtn::data::BundleID &id) const throw (BundleNotFoundException);
193 
197  dtn::data::BundleID send(RegisterIndex ri) throw ();
198 
202  void put(RegisterIndex ri, const dtn::data::Bundle &b) throw ();
203 
207  void put(RegisterIndex ri, const dtn::data::PrimaryBlock &b) throw ();
208 
218  void write(RegisterIndex ri, const char *buf, const size_t len, const size_t offset = std::string::npos) throw ();
219 
229  void read(RegisterIndex ri, char *buf, size_t &len, const size_t offset = 0) throw ();
230 
235  void receive() throw (NativeSessionException);
236 
237  private:
238  // local registration
239  dtn::api::Registration _registration;
240 
241  class BundleReceiver : public dtn::core::EventReceiver
242  {
243  public:
244  BundleReceiver(NativeSession &session);
245  virtual ~BundleReceiver();
246 
247  void raiseEvent(const dtn::core::Event *evt) throw ();
248 
249  private:
250  NativeSession &_session;
251  } _receiver;
252 
256  void fireNotificationBundle(const dtn::data::BundleID &id) throw ();
257 
261  void fireNotificationAdministrativeRecord(const dtn::data::MetaBundle &bundle);
262 
266  void fireNotificationStatusReport(const dtn::data::EID &source, const dtn::data::StatusReportBlock &report) throw ();
267 
271  void fireNotificationCustodySignal(const dtn::data::EID &source, const dtn::data::CustodySignalBlock &custody) throw ();
272 
273  // callback
274  ibrcommon::RWMutex _cb_mutex;
275  NativeSessionCallback *_session_cb;
276  NativeSerializerCallback *_serializer_cb;
277 
278  // default endpoint
279  dtn::data::EID _endpoint;
280 
281  // local bundle register
282  dtn::data::Bundle _bundle[2];
283 
284  // local bundle queue
286  };
287  } /* namespace net */
288 } /* namespace dtn */
289 #endif /* NATIVESESSION_H_ */