1 | // [License] |
---|
2 | // The Ariba-Underlay Copyright |
---|
3 | // |
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH) |
---|
5 | // |
---|
6 | // Institute of Telematics |
---|
7 | // UniversitÀt Karlsruhe (TH) |
---|
8 | // Zirkel 2, 76128 Karlsruhe |
---|
9 | // Germany |
---|
10 | // |
---|
11 | // Redistribution and use in source and binary forms, with or without |
---|
12 | // modification, are permitted provided that the following conditions are |
---|
13 | // met: |
---|
14 | // |
---|
15 | // 1. Redistributions of source code must retain the above copyright |
---|
16 | // notice, this list of conditions and the following disclaimer. |
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright |
---|
18 | // notice, this list of conditions and the following disclaimer in the |
---|
19 | // documentation and/or other materials provided with the distribution. |
---|
20 | // |
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND |
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR |
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | // |
---|
33 | // The views and conclusions contained in the software and documentation |
---|
34 | // are those of the authors and should not be interpreted as representing |
---|
35 | // official policies, either expressed or implied, of the Institute of |
---|
36 | // Telematics. |
---|
37 | // [License] |
---|
38 | |
---|
39 | #include "AribaModule.h" |
---|
40 | |
---|
41 | // boost/std includes |
---|
42 | #include <assert.h> |
---|
43 | #include <boost/regex.hpp> |
---|
44 | #include <boost/foreach.hpp> |
---|
45 | #include <boost/algorithm/string.hpp> |
---|
46 | |
---|
47 | // ariba includes |
---|
48 | #include "ariba/SideportListener.h" |
---|
49 | #include "ariba/utility/misc/Helper.h" |
---|
50 | #include "ariba/utility/misc/StringFormat.h" |
---|
51 | #include "ariba/communication/BaseCommunication.h" |
---|
52 | #include "ariba/communication/EndpointDescriptor.h" |
---|
53 | |
---|
54 | using namespace ariba::utility::Helper; |
---|
55 | using ariba::communication::BaseCommunication; |
---|
56 | using ariba::communication::EndpointDescriptor; |
---|
57 | |
---|
58 | namespace ariba { |
---|
59 | |
---|
60 | use_logging_cpp(AribaModule); |
---|
61 | const string AribaModule::BootstrapMechanismNames[5] |
---|
62 | = {"invalid", "static", "broadcast", "mdns", "sdp"}; |
---|
63 | |
---|
64 | AribaModule::AribaModule() |
---|
65 | : started(false), base_comm(NULL), sideport_sniffer(NULL) { |
---|
66 | endpoints = "tcp{41322};rfcomm{10};"; |
---|
67 | } |
---|
68 | |
---|
69 | AribaModule::~AribaModule() { |
---|
70 | } |
---|
71 | |
---|
72 | string AribaModule::getBootstrapHints(const Name& spoVNetName) const { |
---|
73 | std::ostringstream o; |
---|
74 | int i=0; |
---|
75 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) { |
---|
76 | o << info.spovnetName.toString() << "{"; |
---|
77 | if (i!=0) o << ","; |
---|
78 | |
---|
79 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) { |
---|
80 | if( node.desc != NULL ) |
---|
81 | o << node.desc->toString(); |
---|
82 | else if(node.mechanism == BootstrapMechanismBroadcast |
---|
83 | || node.mechanism == BootstrapMechanismMulticastDNS |
---|
84 | || node.mechanism == BootstrapMechanismSDP){ |
---|
85 | o << BootstrapMechanismNames[node.mechanism]; |
---|
86 | if( !node.info.empty() ) o << "{" << node.info << "}"; |
---|
87 | o << ";"; |
---|
88 | } |
---|
89 | } |
---|
90 | o << "}"; |
---|
91 | i++; |
---|
92 | } |
---|
93 | return o.str(); |
---|
94 | } |
---|
95 | |
---|
96 | void AribaModule::addBootstrapHints(string boot_info) { |
---|
97 | using namespace boost::xpressive; |
---|
98 | using namespace ariba::utility::string_format; |
---|
99 | using namespace ariba::utility::Helper; |
---|
100 | using namespace std; |
---|
101 | |
---|
102 | boost::erase_all(boot_info, " "); |
---|
103 | boost::erase_all(boot_info, "\t"); |
---|
104 | boost::erase_all(boot_info, "\n"); |
---|
105 | boost::erase_all(boot_info, "\r"); |
---|
106 | |
---|
107 | smatch match; |
---|
108 | if (regex_search(boot_info, match, robjects)) { |
---|
109 | regex_nav nav = match; |
---|
110 | for (int i = 0; i < nav.size(); i++) { |
---|
111 | string type = nav[i][robject_id].str(); |
---|
112 | string data = nav[i][robject_data].str(); |
---|
113 | data = data.substr(1, data.size() - 2); |
---|
114 | Name name(type); |
---|
115 | |
---|
116 | // find static bootstrap info --> BootstrapMechanismStatic |
---|
117 | EndpointDescriptor* desc = EndpointDescriptor::fromString(data); |
---|
118 | addBootstrapNode(name, desc, "", BootstrapMechanismStatic); |
---|
119 | |
---|
120 | // find automatic bootstrap info --> {BootstrapMechanismBroadcast, |
---|
121 | // BootstrapMechanismMulticastDNS,BootstrapMechanismSDP} |
---|
122 | typedef vector< string > split_vector_type; |
---|
123 | split_vector_type splitvec; |
---|
124 | boost::split( splitvec, data, boost::is_any_of(";") ); |
---|
125 | for(unsigned int i=0; i<splitvec.size(); i++){ |
---|
126 | string x = splitvec[i]; |
---|
127 | split_vector_type innervec; |
---|
128 | |
---|
129 | boost::split( innervec, x, boost::is_any_of("{}") ); |
---|
130 | BootstrapNode node; |
---|
131 | if(innervec.size() < 1) continue; |
---|
132 | |
---|
133 | if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismBroadcast]) |
---|
134 | node.mechanism = BootstrapMechanismBroadcast; |
---|
135 | else if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismMulticastDNS]) |
---|
136 | node.mechanism = BootstrapMechanismMulticastDNS; |
---|
137 | else if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismSDP]) |
---|
138 | node.mechanism = BootstrapMechanismSDP; |
---|
139 | else |
---|
140 | continue; |
---|
141 | |
---|
142 | if(innervec.size() > 1) |
---|
143 | node.info = innervec[1]; |
---|
144 | |
---|
145 | this->addBootstrapNode(name, node); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | logging_info( "Added bootstrap hints: " << getBootstrapHints() ); |
---|
151 | } |
---|
152 | |
---|
153 | void AribaModule::addBootstrapNode(const Name& spovnet, |
---|
154 | communication::EndpointDescriptor* desc, const string& info, |
---|
155 | const BootstrapMechanism& mechanism) { |
---|
156 | BootstrapNode node(0, desc, mechanism, info); |
---|
157 | addBootstrapNode(spovnet, node); |
---|
158 | } |
---|
159 | |
---|
160 | void AribaModule::addBootstrapNode(const Name& spovnet, const BootstrapNode& node){ |
---|
161 | bool added = false; |
---|
162 | |
---|
163 | // add node to existing bootstrap list |
---|
164 | BOOST_FOREACH( BootstrapInfo& info, bootstrapNodes ){ |
---|
165 | if (info.spovnetName == spovnet) { |
---|
166 | info.nodes.push_back(node); |
---|
167 | added = true; |
---|
168 | break; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | // create new entry |
---|
173 | if (!added) { |
---|
174 | BootstrapInfo info; |
---|
175 | info.spovnetName = spovnet; |
---|
176 | info.nodes.push_back(node); |
---|
177 | bootstrapNodes.push_back(info); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | const communication::EndpointDescriptor* AribaModule::getBootstrapNode( |
---|
182 | const Name& spovnet, const BootstrapMechanism mechanism) const { |
---|
183 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) { |
---|
184 | if( info.spovnetName == spovnet ) { |
---|
185 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) { |
---|
186 | if( node.mechanism == mechanism && node.desc != NULL ) |
---|
187 | return node.desc; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | return NULL; |
---|
192 | } |
---|
193 | |
---|
194 | string AribaModule::getBootstrapInfo( |
---|
195 | const Name& spovnet, const BootstrapMechanism mechanism) const { |
---|
196 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) { |
---|
197 | if( info.spovnetName == spovnet ) { |
---|
198 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) { |
---|
199 | if( node.mechanism == mechanism && node.desc != NULL ) |
---|
200 | return node.info; |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | return string(); |
---|
206 | } |
---|
207 | |
---|
208 | vector<AribaModule::BootstrapMechanism> AribaModule::getBootstrapMechanisms( |
---|
209 | const Name& spovnet) const { |
---|
210 | vector<AribaModule::BootstrapMechanism> ret; |
---|
211 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) { |
---|
212 | if( info.spovnetName == spovnet ) { |
---|
213 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) { |
---|
214 | if(std::find(ret.begin(), ret.end(), node.mechanism) == ret.end()) |
---|
215 | ret.push_back(node.mechanism); |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | return ret; |
---|
220 | } |
---|
221 | |
---|
222 | void AribaModule::registerSideportListener(SideportListener* sideport){ |
---|
223 | sideport_sniffer = sideport; |
---|
224 | } |
---|
225 | |
---|
226 | // @see Module.h |
---|
227 | void AribaModule::initialize() { |
---|
228 | |
---|
229 | // preconditions |
---|
230 | assert(!started); |
---|
231 | |
---|
232 | // init variables |
---|
233 | base_comm = NULL; |
---|
234 | } |
---|
235 | |
---|
236 | // @see Module.h |
---|
237 | void AribaModule::start() { |
---|
238 | |
---|
239 | // preconditions |
---|
240 | assert(base_comm == NULL); |
---|
241 | assert(!started); |
---|
242 | |
---|
243 | // create the base communication component |
---|
244 | started = true; |
---|
245 | base_comm = new BaseCommunication(); |
---|
246 | base_comm->setEndpoints(endpoints); |
---|
247 | } |
---|
248 | |
---|
249 | // @see Module.h |
---|
250 | void AribaModule::stop() { |
---|
251 | |
---|
252 | // preconditions |
---|
253 | assert(base_comm != NULL); |
---|
254 | assert(started); |
---|
255 | |
---|
256 | // delete base communication component |
---|
257 | started = false; |
---|
258 | delete base_comm; |
---|
259 | } |
---|
260 | |
---|
261 | // @see Module.h |
---|
262 | string AribaModule::getName() const { |
---|
263 | return "ariba"; |
---|
264 | } |
---|
265 | |
---|
266 | // @see Module.h |
---|
267 | void AribaModule::setProperty(string key, string value) { |
---|
268 | if (key == "endpoints") endpoints = value; |
---|
269 | else if (key == "bootstrap.hints") addBootstrapHints(value); |
---|
270 | } |
---|
271 | |
---|
272 | // @see Module.h |
---|
273 | const string AribaModule::getProperty(string key) const { |
---|
274 | if (key == "endpoints") return endpoints; // TODO: return local endpoints |
---|
275 | else if (key == "bootstrap.hints") return getBootstrapHints(); |
---|
276 | return ""; |
---|
277 | } |
---|
278 | |
---|
279 | // @see Module.h |
---|
280 | const vector<string> AribaModule::getProperties() const { |
---|
281 | vector<string> properties; |
---|
282 | properties.push_back("endpoints"); |
---|
283 | properties.push_back("bootstrap.hints"); |
---|
284 | return properties; |
---|
285 | } |
---|
286 | |
---|
287 | const string AribaModule::getLocalEndpoints() { |
---|
288 | return base_comm->getEndpointDescriptor().toString(); |
---|
289 | } |
---|
290 | |
---|
291 | } // namespace ariba |
---|