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