|
1 /* |
|
2 * Copyright 2007 Baylor University |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _PROPHET_BUNDLE_CORE_H_ |
|
18 #define _PROPHET_BUNDLE_CORE_H_ |
|
19 |
|
20 #include "prophet/Node.h" |
|
21 #include "prophet/Params.h" |
|
22 #include "prophet/BundleList.h" |
|
23 #include "prophet/BundleCore.h" |
|
24 #include "prophet/Repository.h" |
|
25 #include "contacts/Link.h" |
|
26 #include "bundling/Bundle.h" |
|
27 #include "bundling/BundleActions.h" |
|
28 #include "bundling/BundleList.h" |
|
29 #include "ProphetBundleList.h" |
|
30 #include "ProphetLinkList.h" |
|
31 #include "ProphetNodeList.h" |
|
32 #include <oasys/debug/Log.h> |
|
33 #include <oasys/thread/Lock.h> |
|
34 |
|
35 #include <string> |
|
36 |
|
37 namespace dtn |
|
38 { |
|
39 |
|
40 /** |
|
41 * Implements the prophet::BundleCore API by integrating |
|
42 * DTN's system services |
|
43 */ |
|
44 class ProphetBundleCore : public prophet::BundleCore, |
|
45 public prophet::Repository::BundleCoreRep, |
|
46 public oasys::Logger |
|
47 { |
|
48 public: |
|
49 |
|
50 /** |
|
51 * Constructor |
|
52 */ |
|
53 ProphetBundleCore(const std::string& local_eid, |
|
54 BundleActions* actions, |
|
55 oasys::SpinLock* lock); |
|
56 |
|
57 /** |
|
58 * Test constructor |
|
59 */ |
|
60 ProphetBundleCore(oasys::Builder); |
|
61 |
|
62 /** |
|
63 * Destructor |
|
64 */ |
|
65 virtual ~ProphetBundleCore(); |
|
66 |
|
67 ///@{ Virtual from prophet::BundleCore |
|
68 bool is_route(const std::string& dest_id, |
|
69 const std::string& route) const; |
|
70 bool should_fwd(const prophet::Bundle* bundle, |
|
71 const prophet::Link* link) const; |
|
72 std::string get_route(const std::string& dest_id) const; |
|
73 std::string get_route_pattern(const std::string& dest_id) const; |
|
74 u_int64_t max_bundle_quota() const; |
|
75 bool custody_accepted() const; |
|
76 void drop_bundle(const prophet::Bundle* bundle); |
|
77 bool send_bundle(const prophet::Bundle* bundle, |
|
78 const prophet::Link* link); |
|
79 bool write_bundle(const prophet::Bundle* bundle, |
|
80 const u_char* buffer, |
|
81 size_t len); |
|
82 bool read_bundle(const prophet::Bundle* bundle, |
|
83 u_char* buffer, |
|
84 size_t& len) const |
|
85 { |
|
86 return const_cast<ProphetBundleCore*>(this)->read_bundle( |
|
87 bundle,buffer,len); |
|
88 } |
|
89 prophet::Bundle* create_bundle(const std::string& src, |
|
90 const std::string& dst, |
|
91 u_int expiration); |
|
92 const prophet::BundleList& bundles() const |
|
93 { |
|
94 return bundles_.get_bundles(); |
|
95 } |
|
96 const prophet::Bundle* find(const prophet::BundleList& list, |
|
97 const std::string& eid, u_int32_t creation_ts, |
|
98 u_int32_t seqno) const; |
|
99 void update_node(const prophet::Node* node); |
|
100 void delete_node(const prophet::Node* node); |
|
101 std::string local_eid() const |
|
102 { |
|
103 return local_eid_; |
|
104 } |
|
105 std::string prophet_id(const prophet::Link* link) const; |
|
106 std::string prophet_id() const |
|
107 { |
|
108 EndpointID eid(get_route(local_eid())); |
|
109 eid.append_service_tag("prophet"); |
|
110 return eid.str(); |
|
111 } |
|
112 prophet::Alarm* create_alarm(prophet::ExpirationHandler* handler, |
|
113 u_int timeout, bool jitter = false); |
|
114 void print_log(const char* name, int level, const char* fmt, ...) |
|
115 PRINTFLIKE(4,5); |
|
116 ///@} |
|
117 |
|
118 /** |
|
119 * Implementation trick to get around const issues |
|
120 */ |
|
121 bool read_bundle(const prophet::Bundle* bundle, |
|
122 u_char* buffer, |
|
123 size_t& len); |
|
124 |
|
125 /** |
|
126 * Initialization routine for deserializing routes from |
|
127 * permanent storage |
|
128 */ |
|
129 void load_prophet_nodes(prophet::Table* nodes, |
|
130 prophet::ProphetParams* params); |
|
131 |
|
132 /** |
|
133 * Initialization routine for loading Bundle metadata |
|
134 * into Prophet's facade |
|
135 */ |
|
136 void load_dtn_bundles(const BundleList* list); |
|
137 |
|
138 /** |
|
139 * Callback for host system's shutdown routine |
|
140 */ |
|
141 void shutdown() |
|
142 { |
|
143 bundles_.clear(); |
|
144 links_.clear(); |
|
145 nodes_.clear(); |
|
146 } |
|
147 |
|
148 ///@{ Conversion between Prophet's Facade type and DTN native type |
|
149 const Bundle* get_bundle(const prophet::Bundle* b); |
|
150 const prophet::Bundle* get_bundle(const Bundle* b); |
|
151 const prophet::Bundle* get_temp_bundle(const BundleRef& b); |
|
152 const Link* get_link(const prophet::Link* link); |
|
153 const prophet::Link* get_link(const Link* link); |
|
154 ///@} |
|
155 |
|
156 ///@{ Convenience method for insert/delete into Prophet's |
|
157 /// BundleCore facade |
|
158 void add(const BundleRef& b); |
|
159 void del(const BundleRef& b); |
|
160 void add(const LinkRef& link); |
|
161 void del(const LinkRef& link); |
|
162 ///@} |
|
163 |
|
164 /** |
|
165 * Prophet's queue policy implementation |
|
166 */ |
|
167 prophet::Repository* bundles() { return bundles_.bundles(); } |
|
168 |
|
169 protected: |
|
170 friend class ProphetRouter; |
|
171 |
|
172 BundleActions* const actions_; ///< actions interface for send, delete, etc |
|
173 ProphetBundleList bundles_; ///< objects that link DTN to Prophet bundles |
|
174 ProphetLinkList links_; ///< objects that link DTN to Prophet links |
|
175 ProphetNodeList nodes_; ///< interface into persistent storage |
|
176 const std::string local_eid_; ///< route to local DTN instance |
|
177 oasys::SpinLock* const lock_; ///< shared lock with ProphetRouter |
|
178 bool test_mode_; ///< test constructor used, meaning that BundleDaemon is |
|
179 /// unavailable |
|
180 |
|
181 }; // class ProphetBundleCore |
|
182 |
|
183 }; // namespace dtn |
|
184 |
|
185 #endif // _PROPHET_BUNDLE_CORE_H_ |