48 | | Then, we declare the used namespaces (lines 05-07) to be able to use the functionalities. Now we get to the main method, being our starting point. After determining the location of our config file, we initialize the system by passing the config file's location to the !StartupWrapper and telling the same to start the architecture up (lines 11-15). Now we are ready to start the ping-pong service, which we first create (line 18). Then we declare the role of the node (initiator or joiner), which is written down in the config file as a bool value. Finally, we start the service up by calling the specific method in the !StartupWrapper. Now the service will run until we press the enter button. |
| 48 | Then, we declare the used namespaces (lines 05-07) to be able to use the functionalities. Now we get to the main method, being our starting point. After determining the location of our config file, we initialize the system by passing the config file's location to the !StartupWrapper and telling the same to start the architecture up (lines 15-16). Now we are ready to start the ping-pong service, which we first have to create (line 19). Finally, we start the service up by calling the specific method in the !StartupWrapper. Now the service will run until we press the enter button. |
60 | | 07 use_logging_cpp(PingPong); |
61 | | 08 ServiceID PingPong::PINGPONG_ID = ServiceID(111); |
62 | | 09 |
63 | | 10 PingPong::PingPong() : pingid( 0 ) { |
64 | | 11 } |
65 | | 12 |
66 | | 13 PingPong::~PingPong(){ |
67 | | 14 } |
68 | | 15 |
69 | | 16 void PingPong::setMode(bool startingNode){ |
70 | | 17 startping = startingNode; |
71 | | 18 } |
72 | | 19 |
73 | | 20 void PingPong::startup(UnderlayAbstraction* _abstraction){ |
74 | | 21 abstraction = _abstraction; |
75 | | 22 |
76 | | 23 logging_info( "starting up PingPong service ... " ); |
77 | | 24 |
78 | | 25 SpoVNetID spovnetid (Identifier(5000)); |
79 | | 26 |
80 | | 27 NodeID nodeid = (Configuration::instance().exists("BASE_nodeid") ? |
81 | | 28 NodeID(Identifier(Configuration::instance().read<unsigned long>("BASE_nodeid"))) : |
82 | | 29 NodeID::UNSPECIFIED); |
| 60 | 07 namespace ariba { |
| 61 | 08 namespace application { |
| 62 | 09 namespace pingpong { |
| 63 | 10 |
| 64 | 11 // logging |
| 65 | 12 use_logging_cpp( PingPong ); |
| 66 | 13 |
| 67 | 14 // the service that the pingpong wants to use |
| 68 | 15 ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 ); |
| 69 | 16 |
| 70 | 17 // construction |
| 71 | 18 PingPong::PingPong() : pingId( 0 ) { |
| 72 | 19 Timer::setInterval( 5000 ); |
| 73 | 20 } |
| 74 | 21 |
| 75 | 22 // destruction |
| 76 | 23 PingPong::~PingPong() { |
| 77 | 24 } |
| 78 | 25 |
| 79 | 26 // implementation of the startup interface |
| 80 | 27 void PingPong::startup() { |
| 81 | 28 |
| 82 | 29 logging_info( "starting up PingPong service ... " ); |
88 | | 35 uint16_t localport = Configuration::instance().exists("BASE_port") ? |
89 | | 36 Configuration::instance().read<uint16_t>("BASE_port") : |
90 | | 37 ARIBA_DEFAULT_PORT; |
91 | | 38 |
92 | | 39 if( !startping ) |
93 | | 40 context = abstraction->createSpoVNet( spovnetid, nodeid, locallocator, localport ); |
94 | | 41 else |
95 | | 42 context = abstraction->joinSpoVNet ( spovnetid, nodeid, locallocator, localport ); |
| 88 | 35 // get the configuration object |
| 89 | 36 Configuration& config = Configuration::instance(); |
| 90 | 37 |
| 91 | 38 // generate spovnet name |
| 92 | 39 Name spovnetName("pingpong"); |
| 93 | 40 |
| 94 | 41 // get initiator flag |
| 95 | 42 this->isInitiator = Configuration::instance().read<bool>("node.initiator"); |
97 | | 44 overlay = &context->getOverlay(); |
98 | | 45 overlay->bind( this, PingPong::PINGPONG_ID ); |
99 | | 46 |
100 | | 47 logging_info( "PingPong started up" ); |
101 | | 48 |
102 | | 49 |
103 | | 50 |
104 | | 51 } |
| 97 | 44 // get node name |
| 98 | 45 Name nodeName = Name::UNSPECIFIED; |
| 99 | 46 if (config.exists("node.name")) nodeName = config.read<string> ("node.name"); |
| 100 | 47 |
| 101 | 48 // configure ariba module |
| 102 | 49 if (config.exists("ariba.ip.addr")) ariba->setProperty("ip.addr", |
| 103 | 50 config.read<string>("ariba.ip.addr")); |
| 104 | 51 if (config.exists("ariba.tcp.port")) ariba->setProperty("tcp.port", |
| 105 | 52 config.read<string>("ariba.tcp.port")); |
| 106 | 53 if (config.exists("ariba.udp.port")) ariba->setProperty("udp.port", |
| 107 | 54 config.read<string>("ariba.udp.port")); |
| 108 | 55 if (config.exists("ariba.bootstrap.hints")) ariba->setProperty("bootstrap.hints", |
| 109 | 56 config.read<string>("ariba.bootstrap.hints")); |
| 110 | 57 |
| 111 | 58 // start ariba module |
| 112 | 59 ariba->start(); |
| 113 | 60 |
| 114 | 61 // create node and join |
| 115 | 62 node = new Node( *ariba, nodeName ); |
| 116 | 63 |
| 117 | 64 // bind communication and node listener |
| 118 | 65 node->bind( this ); /*NodeListener*/ |
| 119 | 66 node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/ |
| 120 | 67 |
| 121 | 68 // start node module |
| 122 | 69 node->start(); |
| 123 | 70 |
| 124 | 71 // initiate or join the spovnet |
| 125 | 72 if (!isInitiator) node->join(spovnetName); |
| 126 | 73 else node->initiate(spovnetName); |
| 127 | 74 |
| 128 | 75 // ping pong started up... |
| 129 | 76 logging_info( "pingpong starting up with" |
| 130 | 77 << " [spovnetid " << node->getSpoVNetId().toString() << "]" |
| 131 | 78 << " and [nodeid " << node->getNodeId().toString() << "]" ); |
| 132 | 79 } |