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