source: source/ariba/AribaModule.cpp@ 7468

Last change on this file since 7468 was 7468, checked in by Christoph Mayer, 14 years ago

-timer delete fix (noch auskommentiert), -interface cleanup

File size: 5.9 KB
Line 
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
46// ariba includes
47#include "ariba/SideportListener.h"
48#include "ariba/utility/misc/Helper.h"
49#include "ariba/utility/misc/StringFormat.h"
50#include "ariba/communication/BaseCommunication.h"
51#include "ariba/communication/EndpointDescriptor.h"
52
53using namespace ariba::utility::Helper;
54using ariba::communication::BaseCommunication;
55using ariba::communication::EndpointDescriptor;
56
57namespace ariba {
58
59use_logging_cpp(AribaModule);
60
61AribaModule::AribaModule()
62 : started(false), base_comm(NULL), sideport_sniffer(NULL) {
63 endpoints = "tcp{41322};rfcomm{10};";
64}
65
66AribaModule::~AribaModule() {
67}
68
69string AribaModule::getBootstrapHints(const Name& spoVNetName) const {
70 std::ostringstream o;
71 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
72 o << info.spovnetName.toString() << "{";
73 int i=0;
74 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
75 if (i!=0) o << ",";
76 if( node.desc != NULL ) o << node.desc->toString();
77 i++;
78 }
79 o << "}";
80 }
81 return o.str();
82}
83
84void AribaModule::addBootstrapHints(string boot_info) {
85 using namespace boost::xpressive;
86 using namespace ariba::utility::string_format;
87 using namespace ariba::utility::Helper;
88 using namespace std;
89
90 smatch match;
91 if (regex_search(boot_info, match, robjects)) {
92 regex_nav nav = match;
93 for (int i = 0; i < nav.size(); i++) {
94 string type = nav[i][robject_id].str();
95 string data = nav[i][robject_data].str();
96 data = data.substr(1, data.size() - 2);
97 Name name(type);
98 EndpointDescriptor* desc = EndpointDescriptor::fromString(data);
99 logging_debug("Added bootstap info for " << type << ": " << desc->toString() );
100 addBootstrapNode(name, desc);
101 }
102 }
103}
104
105void AribaModule::addBootstrapNode(const Name& spovnet,
106 communication::EndpointDescriptor* desc) {
107
108 // set bootstrap node
109 BootstrapNode node;
110 node.timestamp = 0;
111 node.desc = desc;
112 bool added = false;
113
114 // add node to existing bootstrap list
115 BOOST_FOREACH( BootstrapInfo& info, bootstrapNodes ){
116 if (info.spovnetName == spovnet) {
117 info.nodes.push_back(node);
118 added = true;
119 break;
120 }
121 }
122
123 // create new entry
124 if (!added) {
125 BootstrapInfo info;
126 info.spovnetName = spovnet;
127 info.nodes.push_back(node);
128 bootstrapNodes.push_back(info);
129 }
130
131 logging_debug( "Added bootstrap info: " << getBootstrapHints() );
132}
133
134const communication::EndpointDescriptor* AribaModule::getBootstrapNode(
135 const Name& spovnet) const {
136 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
137 if( info.spovnetName == spovnet ) {
138 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
139 if( node.desc != NULL ) return node.desc;
140 }
141 }
142 }
143 return NULL;
144}
145
146void AribaModule::registerSideportListener(SideportListener* sideport){
147 sideport_sniffer = sideport;
148}
149
150// @see Module.h
151void AribaModule::initialize() {
152
153 // preconditions
154 assert(!started);
155
156 // init variables
157 base_comm = NULL;
158}
159
160// @see Module.h
161void AribaModule::start() {
162
163 // preconditions
164 assert(base_comm == NULL);
165 assert(!started);
166
167 // create the base communication component
168 started = true;
169 base_comm = new BaseCommunication();
170 base_comm->setEndpoints(endpoints);
171}
172
173// @see Module.h
174void AribaModule::stop() {
175
176 // preconditions
177 assert(base_comm != NULL);
178 assert(started);
179
180 // delete base communication component
181 started = false;
182 delete base_comm;
183}
184
185// @see Module.h
186string AribaModule::getName() const {
187 return "ariba";
188}
189
190// @see Module.h
191void AribaModule::setProperty(string key, string value) {
192 if (key == "endpoints") endpoints = value;
193 else if (key == "bootstrap.hints") addBootstrapHints(value);
194}
195
196// @see Module.h
197const string AribaModule::getProperty(string key) const {
198 if (key == "endpoints") return endpoints; // TODO: return local endpoints
199 else if (key == "bootstrap.hints") return getBootstrapHints();
200 return "";
201}
202
203// @see Module.h
204const vector<string> AribaModule::getProperties() const {
205 vector<string> properties;
206 properties.push_back("endpoints");
207 properties.push_back("bootstrap.hints");
208 return properties;
209}
210
211const string AribaModule::getLocalEndpoints() {
212 return base_comm->getEndpointDescriptor().toString();
213}
214
215} // namespace ariba
Note: See TracBrowser for help on using the repository browser.