IBR-DTN  1.0.0
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"
29 #include <ibrdtn/data/Bundle.h>
32 #include <ibrcommon/thread/Thread.h>
33 #include <ibrcommon/thread/Queue.h>
34 #include <ibrcommon/thread/RWMutex.h>
35 #include <ibrcommon/thread/Mutex.h>
36 #include <ibrcommon/Exceptions.h>
37 
38 namespace dtn
39 {
40  namespace api
41  {
42  class NativeSessionException : public ibrcommon::Exception
43  {
44  public:
45  NativeSessionException(string what = "An error happened.") throw() : ibrcommon::Exception(what)
46  {
47  };
48  };
49 
50  class BundleNotFoundException : public ibrcommon::Exception
51  {
52  public:
53  BundleNotFoundException(string what = "Bundle not found.") throw() : ibrcommon::Exception(what)
54  {
55  };
56  };
57 
59  public:
63  virtual ~NativeSessionCallback() = 0;
64 
70  virtual void notifyBundle(const dtn::data::BundleID &id) throw () = 0;
71 
76  virtual void notifyStatusReport(const dtn::data::EID &source, const dtn::data::StatusReportBlock &report) throw () = 0;
77 
82  virtual void notifyCustodySignal(const dtn::data::EID &source, const dtn::data::CustodySignalBlock &custody) throw () = 0;
83  };
84 
85  class NativeSession {
86  static const std::string TAG;
87 
88  public:
90  REG1 = 0,
91  REG2 = 1
92  };
93 
99 
104  NativeSession(NativeSessionCallback *session_cb, NativeSerializerCallback *serializer_cb, const std::string &handle);
105 
109  virtual ~NativeSession();
110 
115  void destroy() throw ();
116 
120  const dtn::data::EID& getNodeEID() const throw ();
121 
126  void setEndpoint(const std::string &suffix) throw (NativeSessionException);
127 
132  void resetEndpoint() throw ();
133 
137  void addEndpoint(const std::string &suffix) throw (NativeSessionException);
138 
142  void removeEndpoint(const std::string &suffix) throw (NativeSessionException);
143 
148  void addRegistration(const dtn::data::EID &eid) throw (NativeSessionException);
149 
153  void removeRegistration(const dtn::data::EID &eid) throw (NativeSessionException);
154 
158  void clearRegistration() throw ();
159 
163  std::vector<std::string> getSubscriptions() throw ();
164 
169  void next(RegisterIndex ri) throw (BundleNotFoundException);
170 
174  void load(RegisterIndex ri, const dtn::data::BundleID &id) throw (BundleNotFoundException);
175 
179  void get(RegisterIndex ri) throw ();
180 
184  void getInfo(RegisterIndex ri) throw ();
185 
189  void free(RegisterIndex ri) throw (BundleNotFoundException);
190 
194  void clear(RegisterIndex ri) throw ();
195 
199  void delivered(const dtn::data::BundleID &id) const throw (BundleNotFoundException);
200 
204  dtn::data::BundleID send(RegisterIndex ri) throw ();
205 
209  void put(RegisterIndex ri, const dtn::data::Bundle &b) throw ();
210 
214  void put(RegisterIndex ri, const dtn::data::PrimaryBlock &b) throw ();
215 
225  void write(RegisterIndex ri, const char *buf, const size_t len, const size_t offset = std::string::npos) throw ();
226 
236  void read(RegisterIndex ri, char *buf, size_t &len, const size_t offset = 0) throw ();
237 
242  void receive() throw (NativeSessionException);
243 
247  const std::string& getHandle() const;
248 
249  private:
250  // local registration
251  dtn::api::Registration _registration;
252 
253  class BundleReceiver : public dtn::core::EventReceiver<dtn::routing::QueueBundleEvent>
254  {
255  public:
256  BundleReceiver(NativeSession &session);
257  virtual ~BundleReceiver();
258 
259  void raiseEvent(const dtn::routing::QueueBundleEvent &evt) throw ();
260 
261  private:
262  NativeSession &_session;
263  } _receiver;
264 
268  void fireNotificationBundle(const dtn::data::BundleID &id) throw ();
269 
273  void fireNotificationAdministrativeRecord(const dtn::data::MetaBundle &bundle);
274 
278  void fireNotificationStatusReport(const dtn::data::EID &source, const dtn::data::StatusReportBlock &report) throw ();
279 
283  void fireNotificationCustodySignal(const dtn::data::EID &source, const dtn::data::CustodySignalBlock &custody) throw ();
284 
285  // callback
286  ibrcommon::RWMutex _cb_mutex;
287  NativeSessionCallback *_session_cb;
288  NativeSerializerCallback *_serializer_cb;
289 
290  // default endpoint
291  dtn::data::EID _endpoint;
292 
293  // local bundle register
294  dtn::data::Bundle _bundle[2];
295 
296  // local bundle queue
297  ibrcommon::Queue<dtn::data::BundleID> _bundle_queue;
298  };
299  } /* namespace net */
300 } /* namespace dtn */
301 #endif /* NATIVESESSION_H_ */
void read(RegisterIndex ri, char *buf, size_t &len, const size_t offset=0)
void setEndpoint(const std::string &suffix)
BundleNotFoundException(string what="Bundle not found.")
Definition: NativeSession.h:53
void removeEndpoint(const std::string &suffix)
std::vector< std::string > getSubscriptions()
void write(RegisterIndex ri, const char *buf, const size_t len, const size_t offset=std::string::npos)
const dtn::data::EID & getNodeEID() const
virtual void notifyCustodySignal(const dtn::data::EID &source, const dtn::data::CustodySignalBlock &custody)=0
NativeSession(NativeSessionCallback *session_cb, NativeSerializerCallback *serializer_cb)
void load(RegisterIndex ri, const dtn::data::BundleID &id)
void removeRegistration(const dtn::data::EID &eid)
NativeSessionException(string what="An error happened.")
Definition: NativeSession.h:45
void put(RegisterIndex ri, const dtn::data::Bundle &b)
void next(RegisterIndex ri)
void free(RegisterIndex ri)
void clear(RegisterIndex ri)
void delivered(const dtn::data::BundleID &id) const
void addEndpoint(const std::string &suffix)
const std::string & getHandle() const
void getInfo(RegisterIndex ri)
virtual void notifyBundle(const dtn::data::BundleID &id)=0
void get(RegisterIndex ri)
void addRegistration(const dtn::data::EID &eid)
dtn::data::BundleID send(RegisterIndex ri)
virtual void notifyStatusReport(const dtn::data::EID &source, const dtn::data::StatusReportBlock &report)=0