00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "AribaModule.h"
00040
00041
00042 #include <assert.h>
00043 #include <boost/regex.hpp>
00044 #include <boost/foreach.hpp>
00045 #include <boost/algorithm/string.hpp>
00046
00047
00048 #include "ariba/SideportListener.h"
00049 #include "ariba/utility/misc/Helper.h"
00050 #include "ariba/utility/misc/StringFormat.h"
00051 #include "ariba/communication/BaseCommunication.h"
00052 #include "ariba/communication/EndpointDescriptor.h"
00053
00054 using namespace ariba::utility::Helper;
00055 using ariba::communication::BaseCommunication;
00056 using ariba::communication::EndpointDescriptor;
00057
00058 namespace ariba {
00059
00060 use_logging_cpp(AribaModule);
00061 const string AribaModule::BootstrapMechanismNames[5]
00062 = {"invalid", "static", "broadcast", "mdns", "sdp"};
00063
00064 AribaModule::AribaModule()
00065 : started(false), base_comm(NULL), sideport_sniffer(NULL) {
00066 endpoints = "tcp{41322};rfcomm{10};";
00067 }
00068
00069 AribaModule::~AribaModule() {
00070 }
00071
00072 string AribaModule::getBootstrapHints(const Name& spoVNetName) const {
00073 std::ostringstream o;
00074 int i=0;
00075 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
00076 o << info.spovnetName.toString() << "{";
00077 if (i!=0) o << ",";
00078
00079 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
00080 if( node.desc != NULL )
00081 o << node.desc->toString();
00082 else if(node.mechanism == BootstrapMechanismBroadcast
00083 || node.mechanism == BootstrapMechanismMulticastDNS
00084 || node.mechanism == BootstrapMechanismSDP){
00085 o << BootstrapMechanismNames[node.mechanism];
00086 if( !node.info.empty() ) o << "{" << node.info << "}";
00087 o << ";";
00088 }
00089 }
00090 o << "}";
00091 i++;
00092 }
00093 return o.str();
00094 }
00095
00096 void AribaModule::addBootstrapHints(string boot_info) {
00097 using namespace boost::xpressive;
00098 using namespace ariba::utility::string_format;
00099 using namespace ariba::utility::Helper;
00100 using namespace std;
00101
00102 boost::erase_all(boot_info, " ");
00103 boost::erase_all(boot_info, "\t");
00104 boost::erase_all(boot_info, "\n");
00105 boost::erase_all(boot_info, "\r");
00106
00107 smatch match;
00108 if (regex_search(boot_info, match, robjects)) {
00109 regex_nav nav = match;
00110 for (int i = 0; i < nav.size(); i++) {
00111 string type = nav[i][robject_id].str();
00112 string data = nav[i][robject_data].str();
00113 data = data.substr(1, data.size() - 2);
00114 Name name(type);
00115
00116
00117 EndpointDescriptor* desc = EndpointDescriptor::fromString(data);
00118 addBootstrapNode(name, desc, "", BootstrapMechanismStatic);
00119
00120
00121
00122 typedef vector< string > split_vector_type;
00123 split_vector_type splitvec;
00124 boost::split( splitvec, data, boost::is_any_of(";") );
00125 for(unsigned int i=0; i<splitvec.size(); i++){
00126 string x = splitvec[i];
00127 split_vector_type innervec;
00128
00129 boost::split( innervec, x, boost::is_any_of("{}") );
00130 BootstrapNode node;
00131 if(innervec.size() < 1) continue;
00132
00133 if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismBroadcast])
00134 node.mechanism = BootstrapMechanismBroadcast;
00135 else if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismMulticastDNS])
00136 node.mechanism = BootstrapMechanismMulticastDNS;
00137 else if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismSDP])
00138 node.mechanism = BootstrapMechanismSDP;
00139 else
00140 continue;
00141
00142 if(innervec.size() > 1)
00143 node.info = innervec[1];
00144
00145 this->addBootstrapNode(name, node);
00146 }
00147 }
00148 }
00149
00150 logging_info( "Added bootstrap hints: " << getBootstrapHints() );
00151 }
00152
00153 void AribaModule::addBootstrapNode(const Name& spovnet,
00154 communication::EndpointDescriptor* desc, const string& info,
00155 const BootstrapMechanism& mechanism) {
00156 BootstrapNode node(0, desc, mechanism, info);
00157 addBootstrapNode(spovnet, node);
00158 }
00159
00160 void AribaModule::addBootstrapNode(const Name& spovnet, const BootstrapNode& node){
00161 bool added = false;
00162
00163
00164 BOOST_FOREACH( BootstrapInfo& info, bootstrapNodes ){
00165 if (info.spovnetName == spovnet) {
00166 info.nodes.push_back(node);
00167 added = true;
00168 break;
00169 }
00170 }
00171
00172
00173 if (!added) {
00174 BootstrapInfo info;
00175 info.spovnetName = spovnet;
00176 info.nodes.push_back(node);
00177 bootstrapNodes.push_back(info);
00178 }
00179 }
00180
00181 const communication::EndpointDescriptor* AribaModule::getBootstrapNode(
00182 const Name& spovnet, const BootstrapMechanism mechanism) const {
00183 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
00184 if( info.spovnetName == spovnet ) {
00185 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
00186 if( node.mechanism == mechanism && node.desc != NULL )
00187 return node.desc;
00188 }
00189 }
00190 }
00191 return NULL;
00192 }
00193
00194 string AribaModule::getBootstrapInfo(
00195 const Name& spovnet, const BootstrapMechanism mechanism) const {
00196 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
00197 if( info.spovnetName == spovnet ) {
00198 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
00199 if( node.mechanism == mechanism && node.desc != NULL )
00200 return node.info;
00201 }
00202 }
00203 }
00204
00205 return string();
00206 }
00207
00208 vector<AribaModule::BootstrapMechanism> AribaModule::getBootstrapMechanisms(
00209 const Name& spovnet) const {
00210 vector<AribaModule::BootstrapMechanism> ret;
00211 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
00212 if( info.spovnetName == spovnet ) {
00213 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
00214 if(std::find(ret.begin(), ret.end(), node.mechanism) == ret.end())
00215 ret.push_back(node.mechanism);
00216 }
00217 }
00218 }
00219 return ret;
00220 }
00221
00222 void AribaModule::registerSideportListener(SideportListener* sideport){
00223 sideport_sniffer = sideport;
00224 }
00225
00226
00227 void AribaModule::initialize() {
00228
00229
00230 assert(!started);
00231
00232
00233 base_comm = NULL;
00234 }
00235
00236
00237 void AribaModule::start() {
00238
00239
00240 assert(base_comm == NULL);
00241 assert(!started);
00242
00243
00244 started = true;
00245 base_comm = new BaseCommunication();
00246 base_comm->setEndpoints(endpoints);
00247 }
00248
00249
00250 void AribaModule::stop() {
00251
00252
00253 assert(base_comm != NULL);
00254 assert(started);
00255
00256
00257 started = false;
00258 delete base_comm;
00259 }
00260
00261
00262 string AribaModule::getName() const {
00263 return "ariba";
00264 }
00265
00266
00267 void AribaModule::setProperty(string key, string value) {
00268 if (key == "endpoints") endpoints = value;
00269 else if (key == "bootstrap.hints") addBootstrapHints(value);
00270 }
00271
00272
00273 const string AribaModule::getProperty(string key) const {
00274 if (key == "endpoints") return endpoints;
00275 else if (key == "bootstrap.hints") return getBootstrapHints();
00276 return "";
00277 }
00278
00279
00280 const vector<string> AribaModule::getProperties() const {
00281 vector<string> properties;
00282 properties.push_back("endpoints");
00283 properties.push_back("bootstrap.hints");
00284 return properties;
00285 }
00286
00287 const string AribaModule::getLocalEndpoints() {
00288 return base_comm->getEndpointDescriptor().toString();
00289 }
00290
00291 }