1 | #include "PingPong.h"
|
---|
2 | #include "ariba/utility/configuration/Configuration.h"
|
---|
3 | #include "ariba/utility/visual/DddVis.h"
|
---|
4 |
|
---|
5 | using ariba::utility::Configuration;
|
---|
6 | using namespace ariba;
|
---|
7 |
|
---|
8 | namespace ariba {
|
---|
9 | namespace application {
|
---|
10 | namespace pingpong {
|
---|
11 |
|
---|
12 | // logging
|
---|
13 | use_logging_cpp( PingPong );
|
---|
14 |
|
---|
15 | // the service that the pingpong wants to use
|
---|
16 | ServiceID PingPong::PINGPONG_SERVICEID = ServiceID( 111 );
|
---|
17 |
|
---|
18 | // construction
|
---|
19 | PingPong::PingPong() : pingId( 0 ) {
|
---|
20 | Timer::setInterval( 1000 );
|
---|
21 | }
|
---|
22 |
|
---|
23 | // destruction
|
---|
24 | PingPong::~PingPong() {
|
---|
25 | }
|
---|
26 |
|
---|
27 | // implementation of the startup interface
|
---|
28 | void PingPong::startup() {
|
---|
29 |
|
---|
30 | // set up logging
|
---|
31 | logging_rootlevel_warn();
|
---|
32 | logging_classlevel_debug(PingPong);
|
---|
33 |
|
---|
34 | logging_info( "starting up PingPong service ... " );
|
---|
35 |
|
---|
36 | // create ariba module
|
---|
37 | logging_debug( "creating ariba underlay module ... " );
|
---|
38 | ariba = new AribaModule();
|
---|
39 |
|
---|
40 | Name spovnetName("pingpong");
|
---|
41 | Name nodeName = Name::UNSPECIFIED;
|
---|
42 | this->name = string("<ping>");
|
---|
43 |
|
---|
44 | // get settings from configuration object
|
---|
45 | if( Configuration::haveConfig() ){
|
---|
46 | Configuration& config = Configuration::instance();
|
---|
47 |
|
---|
48 | // get node name
|
---|
49 | if (config.exists("node.name"))
|
---|
50 | nodeName = config.read<string> ("node.name");
|
---|
51 |
|
---|
52 | // configure ariba module
|
---|
53 | if (config.exists("ariba.endpoints"))
|
---|
54 | ariba->setProperty("endpoints", config.read<string>("ariba.endpoints"));
|
---|
55 | if (config.exists("ariba.bootstrap.hints"))
|
---|
56 | ariba->setProperty("bootstrap.hints", config.read<string>("ariba.bootstrap.hints"));
|
---|
57 | if (config.exists("pingpong.name"))
|
---|
58 | name = config.read<string>("pingpong.name");
|
---|
59 |
|
---|
60 | // get visualization
|
---|
61 | if( config.exists("ariba.visual3d.ip") && config.exists("ariba.visual3d.port")){
|
---|
62 | string ip = config.read<string>("ariba.visual3d.ip");
|
---|
63 | unsigned int port = config.read<unsigned int>("ariba.visual3d.port");
|
---|
64 | unsigned int color = config.exists("node.color") ?
|
---|
65 | config.read<unsigned int>("node.color") : 0;
|
---|
66 | ariba::utility::DddVis::instance().configure(ip, port, color);
|
---|
67 | }
|
---|
68 |
|
---|
69 | } // if( Configuration::haveConfig() )
|
---|
70 |
|
---|
71 | // start ariba module
|
---|
72 | ariba->start();
|
---|
73 |
|
---|
74 | // create node and join
|
---|
75 | node = new Node( *ariba, nodeName );
|
---|
76 |
|
---|
77 | // bind communication and node listener
|
---|
78 | node->bind( this ); /*NodeListener*/
|
---|
79 | node->bind( this, PingPong::PINGPONG_SERVICEID); /*CommunicationListener*/
|
---|
80 |
|
---|
81 | // start node module
|
---|
82 | node->start();
|
---|
83 |
|
---|
84 | // when initiating, you can define the overlay type, default is Chord [CHORD_OVERLAY]
|
---|
85 | SpoVNetProperties params;
|
---|
86 | //params.setBaseOverlayType( SpoVNetProperties::ONE_HOP_OVERLAY ); // alternative: OneHop
|
---|
87 |
|
---|
88 | // initiate the spovnet
|
---|
89 | logging_info("initiating spovnet");
|
---|
90 | node->initiate(spovnetName, params);
|
---|
91 |
|
---|
92 | // join the spovnet
|
---|
93 | logging_info("joining spovnet");
|
---|
94 | node->join(spovnetName);
|
---|
95 |
|
---|
96 | // ping pong started up...
|
---|
97 | logging_info( "pingpong starting up with"
|
---|
98 | << " [spovnetid " << node->getSpoVNetId().toString() << "]"
|
---|
99 | << " and [nodeid " << node->getNodeId().toString() << "]" );
|
---|
100 | }
|
---|
101 |
|
---|
102 | // implementation of the startup interface
|
---|
103 | void PingPong::shutdown() {
|
---|
104 |
|
---|
105 | logging_info( "pingpong service starting shutdown sequence ..." );
|
---|
106 |
|
---|
107 | // stop timer
|
---|
108 | Timer::stop();
|
---|
109 |
|
---|
110 | // leave spovnet
|
---|
111 | node->leave();
|
---|
112 |
|
---|
113 | // unbind communication and node listener
|
---|
114 | node->unbind( this ); /*NodeListener*/
|
---|
115 | node->unbind( this, PingPong::PINGPONG_SERVICEID ); /*CommunicationListener*/
|
---|
116 |
|
---|
117 | // stop the ariba module
|
---|
118 | ariba->stop();
|
---|
119 |
|
---|
120 | // delete node and ariba module
|
---|
121 | delete node;
|
---|
122 | delete ariba;
|
---|
123 |
|
---|
124 | // now we are completely shut down
|
---|
125 | logging_info( "pingpong service shut down" );
|
---|
126 | }
|
---|
127 |
|
---|
128 | // timer event
|
---|
129 | void PingPong::eventFunction() {
|
---|
130 |
|
---|
131 | // we ping all nodes that are known in the overlay structure
|
---|
132 | // this can be all nodes (OneHop) overlay or just some neighbors
|
---|
133 | // in case of a Chord or Kademlia structure
|
---|
134 |
|
---|
135 | // in this sample we use auto-links: we just send out our message
|
---|
136 | // to the node and the link is established automatically. for more
|
---|
137 | // control we would use the node->establishLink function to create
|
---|
138 | // a link and start using the link in the CommunicationListener::onLinkUp
|
---|
139 | // function that is implemented further down in PingPong::onLinkUp
|
---|
140 |
|
---|
141 | logging_info( "pinging overlay neighbors with ping id " << ++pingId );
|
---|
142 | PingPongMessage pingmsg( pingId, name );
|
---|
143 |
|
---|
144 | //-----------------------------------------------------------------------
|
---|
145 | // Option 1: get all neighboring nodes and send the message to each
|
---|
146 | //-----------------------------------------------------------------------
|
---|
147 | counter++;
|
---|
148 | if (counter<0 || counter>4) {
|
---|
149 | counter = 0;
|
---|
150 | string s;
|
---|
151 | for (int i=0; i<names.size();i++) {
|
---|
152 | if (i!=0) s+= ", ";
|
---|
153 | s = s+names[i];
|
---|
154 | }
|
---|
155 | logging_info("----> I am " << name << " and I know " << s);
|
---|
156 | names.clear();
|
---|
157 | }
|
---|
158 |
|
---|
159 | vector<NodeID> nodes = node->getNeighborNodes();
|
---|
160 | BOOST_FOREACH( NodeID nid, nodes ){
|
---|
161 | logging_info( "sending ping message to " << nid.toString() );
|
---|
162 | node->sendMessage( pingmsg, nid, PingPong::PINGPONG_SERVICEID );
|
---|
163 | }
|
---|
164 |
|
---|
165 | //-----------------------------------------------------------------------
|
---|
166 | // Option 2: send a "broadcast message" that actually does the same thing
|
---|
167 | // internally, gets all neighboring nodes and sends the message
|
---|
168 | //-----------------------------------------------------------------------
|
---|
169 | // node->sendBroadcastMessage( pingmsg, PingPong::PINGPONG_SERVICEID );
|
---|
170 | }
|
---|
171 |
|
---|
172 | void PingPong::onJoinCompleted( const SpoVNetID& vid ) {
|
---|
173 | logging_info( "pingpong node join completed, spovnetid=" << vid.toString() );
|
---|
174 |
|
---|
175 | // start the timer to ping every second
|
---|
176 | Timer::start();
|
---|
177 | }
|
---|
178 |
|
---|
179 | void PingPong::onJoinFailed( const SpoVNetID& vid ) {
|
---|
180 | logging_error("pingpong node join failed, spovnetid=" << vid.toString() );
|
---|
181 | }
|
---|
182 |
|
---|
183 | void PingPong::onLeaveCompleted( const SpoVNetID& vid ){
|
---|
184 | logging_info("pingpong node leave completed, spovnetid=" << vid.toString() );
|
---|
185 | }
|
---|
186 |
|
---|
187 | void PingPong::onLeaveFailed( const SpoVNetID& vid ){
|
---|
188 | logging_error("pingpong node leave failed, spovnetid=" << vid.toString() );
|
---|
189 | }
|
---|
190 |
|
---|
191 | void PingPong::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk) {
|
---|
192 | PingPongMessage* pingmsg = msg.getMessage()->convert<PingPongMessage> ();
|
---|
193 | bool found=false;
|
---|
194 | for (int i=0;i<names.size(); i++) if (names[i]==pingmsg->getName()) found=true;
|
---|
195 | if (!found) names.push_back(pingmsg->getName());
|
---|
196 | logging_info( "received ping message on link " << lnk.toString()
|
---|
197 | << " from node " << remote.toString()
|
---|
198 | << ": " << pingmsg->info() );
|
---|
199 | delete pingmsg;
|
---|
200 | }
|
---|
201 |
|
---|
202 | void PingPong::onLinkUp(const LinkID& lnk, const NodeID& remote){
|
---|
203 | logging_info( "received link-up event for link " << lnk.toString()
|
---|
204 | << " and node " << remote.toString() );
|
---|
205 | }
|
---|
206 |
|
---|
207 | void PingPong::onLinkDown(const LinkID& lnk, const NodeID& remote){
|
---|
208 | logging_info( "received link-down event for link " << lnk.toString()
|
---|
209 | << " and node " << remote.toString() );
|
---|
210 | }
|
---|
211 |
|
---|
212 | void PingPong::onLinkChanged(const LinkID& lnk, const NodeID& remote){
|
---|
213 | logging_info( "link-changed event for link " << lnk.toString()
|
---|
214 | << " and node " << remote.toString() );
|
---|
215 | }
|
---|
216 |
|
---|
217 | bool PingPong::onLinkRequest(const NodeID& remote) {
|
---|
218 | logging_info( "node " << remote.toString() << " wants to build up a link with us ... allowing" );
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | void PingPong::onLinkFail(const LinkID& lnk, const NodeID& remote){
|
---|
223 | logging_info( "received link-failed event for link " << lnk.toString()
|
---|
224 | << " and node " << remote.toString() );
|
---|
225 | }
|
---|
226 |
|
---|
227 | }}} // namespace ariba, application, pingpong
|
---|