IBR-DTNSuite  0.12
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  static const std::string TAG;
86 
87  public:
89  REG1 = 0,
90  REG2 = 1
91  };
92 
98 
103  NativeSession(NativeSessionCallback *session_cb, NativeSerializerCallback *serializer_cb, const std::string &handle);
104 
108  virtual ~NativeSession();
109 
114  void destroy() throw ();
115 
119  const dtn::data::EID& getNodeEID() const throw ();
120 
125  void setEndpoint(const std::string &suffix) throw (NativeSessionException);
126 
131  void resetEndpoint() throw ();
132 
136  void addEndpoint(const std::string &suffix) throw (NativeSessionException);
137 
141  void removeEndpoint(const std::string &suffix) throw (NativeSessionException);
142 
147  void addRegistration(const dtn::data::EID &eid) throw (NativeSessionException);
148 
152  void removeRegistration(const dtn::data::EID &eid) throw (NativeSessionException);
153 
157  void clearRegistration() throw ();
158 
162  std::vector<std::string> getSubscriptions() throw ();
163 
168  void next(RegisterIndex ri) throw (BundleNotFoundException);
169 
173  void load(RegisterIndex ri, const dtn::data::BundleID &id) throw (BundleNotFoundException);
174 
178  void get(RegisterIndex ri) throw ();
179 
183  void getInfo(RegisterIndex ri) throw ();
184 
188  void free(RegisterIndex ri) throw (BundleNotFoundException);
189 
193  void clear(RegisterIndex ri) throw ();
194 
198  void delivered(const dtn::data::BundleID &id) const throw (BundleNotFoundException);
199 
203  dtn::data::BundleID send(RegisterIndex ri) throw ();
204 
208  void put(RegisterIndex ri, const dtn::data::Bundle &b) throw ();
209 
213  void put(RegisterIndex ri, const dtn::data::PrimaryBlock &b) throw ();
214 
224  void write(RegisterIndex ri, const char *buf, const size_t len, const size_t offset = std::string::npos) throw ();
225 
235  void read(RegisterIndex ri, char *buf, size_t &len, const size_t offset = 0) throw ();
236 
241  void receive() throw (NativeSessionException);
242 
246  const std::string& getHandle() const;
247 
248  private:
249  // local registration
250  dtn::api::Registration _registration;
251 
252  class BundleReceiver : public dtn::core::EventReceiver
253  {
254  public:
255  BundleReceiver(NativeSession &session);
256  virtual ~BundleReceiver();
257 
258  void raiseEvent(const dtn::core::Event *evt) throw ();
259 
260  private:
261  NativeSession &_session;
262  } _receiver;
263 
267  void fireNotificationBundle(const dtn::data::BundleID &id) throw ();
268 
272  void fireNotificationAdministrativeRecord(const dtn::data::MetaBundle &bundle);
273 
277  void fireNotificationStatusReport(const dtn::data::EID &source, const dtn::data::StatusReportBlock &report) throw ();
278 
282  void fireNotificationCustodySignal(const dtn::data::EID &source, const dtn::data::CustodySignalBlock &custody) throw ();
283 
284  // callback
285  ibrcommon::RWMutex _cb_mutex;
286  NativeSessionCallback *_session_cb;
287  NativeSerializerCallback *_serializer_cb;
288 
289  // default endpoint
290  dtn::data::EID _endpoint;
291 
292  // local bundle register
293  dtn::data::Bundle _bundle[2];
294 
295  // local bundle queue
297  };
298  } /* namespace net */
299 } /* namespace dtn */
300 #endif /* NATIVESESSION_H_ */