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
00046
00047 #include "ariba/SideportListener.h"
00048 #include "ariba/utility/misc/Helper.h"
00049 #include "ariba/utility/misc/StringFormat.h"
00050 #include "ariba/communication/BaseCommunication.h"
00051 #include "ariba/communication/EndpointDescriptor.h"
00052
00053 using namespace ariba::utility::Helper;
00054 using ariba::communication::BaseCommunication;
00055 using ariba::communication::EndpointDescriptor;
00056
00057 namespace ariba {
00058
00059 use_logging_cpp(AribaModule);
00060
00061 AribaModule::AribaModule()
00062 : base_comm(NULL), sideport_sniffer(NULL), started(false) {
00063
00064 endpoints = "tcp{41322};rfcomm{10};";
00065
00066
00067
00068
00069 }
00070
00071 AribaModule::~AribaModule() {
00072 }
00073
00074 string AribaModule::getBootstrapHints(const Name& spoVNetName) const {
00075 std::ostringstream o;
00076 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
00077 o << info.spovnetName.toString() << "{";
00078 int i=0;
00079 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
00080 if (i!=0) o << ",";
00081 if( node.desc != NULL ) o << node.desc->toString();
00082 i++;
00083 }
00084 o << "}";
00085 }
00086 return o.str();
00087 }
00088
00089 void AribaModule::addBootstrapHints(string boot_info) {
00090 using namespace boost::xpressive;
00091 using namespace ariba::utility::string_format;
00092 using namespace ariba::utility::Helper;
00093 using namespace std;
00094
00095 smatch match;
00096 if (regex_search(boot_info, match, robjects)) {
00097 regex_nav nav = match;
00098 for (int i = 0; i < nav.size(); i++) {
00099 string type = nav[i][robject_id].str();
00100 string data = nav[i][robject_data].str();
00101 data = data.substr(1, data.size() - 2);
00102 Name name(type);
00103 EndpointDescriptor* desc = EndpointDescriptor::fromString(data);
00104 logging_debug("Added bootstap info for " << type << ": " << desc->toString() );
00105 addBootstrapNode(name, desc);
00106 }
00107 }
00108 }
00109
00110 void AribaModule::addBootstrapNode(const Name& spovnet,
00111 communication::EndpointDescriptor* desc) {
00112
00113
00114 BootstrapNode node;
00115 node.timestamp = 0;
00116 node.desc = desc;
00117 bool added = false;
00118
00119
00120 BOOST_FOREACH( BootstrapInfo& info, bootstrapNodes ){
00121 if (info.spovnetName == spovnet) {
00122 info.nodes.push_back(node);
00123 added = true;
00124 break;
00125 }
00126 }
00127
00128
00129 if (!added) {
00130 BootstrapInfo info;
00131 info.spovnetName = spovnet;
00132 info.nodes.push_back(node);
00133 bootstrapNodes.push_back(info);
00134 }
00135
00136 logging_debug( "Added bootstrap info: " << getBootstrapHints() );
00137 }
00138
00139 const communication::EndpointDescriptor* AribaModule::getBootstrapNode(
00140 const Name& spovnet) const {
00141 BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
00142 if( info.spovnetName == spovnet ) {
00143 BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
00144 if( node.desc != NULL ) return node.desc;
00145 }
00146 }
00147 }
00148 return NULL;
00149 }
00150
00151 void AribaModule::registerSideportListener(SideportListener* sideport){
00152 sideport_sniffer = sideport;
00153 }
00154
00155
00156 void AribaModule::initialize() {
00157
00158
00159 assert(!started);
00160
00161
00162 base_comm = NULL;
00163 }
00164
00165
00166 void AribaModule::start() {
00167
00168
00169 assert(base_comm == NULL);
00170 assert(!started);
00171
00172
00173 started = true;
00174 base_comm = new BaseCommunication();
00175 base_comm->setEndpoints(endpoints);
00176 }
00177
00178
00179 void AribaModule::stop() {
00180
00181
00182 assert(base_comm != NULL);
00183 assert(started);
00184
00185
00186 started = false;
00187 delete base_comm;
00188 }
00189
00190
00191 string AribaModule::getName() const {
00192 return "ariba";
00193 }
00194
00195
00196 void AribaModule::setProperty(string key, string value) {
00197 if (key == "endpoints") endpoints = value;
00198 else if (key == "bootstrap.hints") addBootstrapHints(value);
00199 }
00200
00201
00202 const string AribaModule::getProperty(string key) const {
00203 if (key == "endpoints") return endpoints;
00204 else if (key == "bootstrap.hints") return getBootstrapHints();
00205 }
00206
00207
00208 const vector<string> AribaModule::getProperties() const {
00209 vector<string> properties;
00210 properties.push_back("endpoints");
00211 properties.push_back("bootstrap.hints");
00212 return properties;
00213 }
00214
00215 const string AribaModule::getLocalEndpoints() {
00216 return base_comm->getEndpointDescriptor().toString();
00217 }
00218
00219 }