Line data Source code
1 : //
2 : // Author: Vladimir Migashko <migashko@gmail.com>, (C) 2013-2015
3 : //
4 : // Copyright: See COPYING file that comes with this distribution
5 : //
6 :
7 : #include "ponger.hpp"
8 : #include <pingpong/iponger.hpp>
9 : #include <wfc/logger.hpp>
10 : #include <wfc/memory.hpp>
11 : #include <iostream>
12 : #include <atomic>
13 : #include <memory>
14 : #include <chrono>
15 : #include <iomanip>
16 :
17 :
18 : // #define PONGER_LOG_MESSAGE(message) WFC_LOG_MESSAGE("ponger", message)
19 : // #define PONGER_LOG_DEBUG(message) WFC_LOG_DEBUG("ponger", message)
20 :
21 : namespace demo{ namespace pingpong{
22 :
23 0 : void ponger::reconfigure()
24 : {
25 0 : _pong_count = this->options().pong_count;
26 0 : }
27 :
28 0 : void ponger::ping(ball::ptr req, ball::handler cb, io_id_t /*io_id*/, std::weak_ptr<ipinger> wp )
29 : {
30 0 : if ( this->notify_ban(req, cb ) )
31 0 : return;
32 :
33 0 : std::cout << "ponger::ping power=" << req->power << std::endl;
34 0 : auto pcount = std::make_shared< std::atomic<size_t> >();
35 0 : auto ptotal = std::make_shared< std::atomic<size_t> >();
36 :
37 0 : size_t pong_count = _pong_count;
38 0 : if ( pong_count == 0 )
39 : {
40 0 : cb( std::move(req) );
41 : }
42 : else
43 : {
44 0 : *pcount = pong_count;
45 0 : for ( size_t i =0; i < pong_count; ++i )
46 : {
47 : using namespace std::placeholders;
48 0 : if ( auto p = wp.lock() )
49 : {
50 0 : auto rereq = std::make_unique<ball>( *req );
51 0 : ++rereq->count;
52 :
53 0 : p->pong(
54 0 : std::move(rereq),
55 0 : [this, pcount, ptotal, cb](ball::ptr res)
56 : {
57 0 : if ( this->system_is_stopped() )
58 0 : return;
59 :
60 0 : if ( res==nullptr )
61 : {
62 0 : DOMAIN_LOG_FATAL("Bad Gateway");
63 0 : return;
64 : }
65 :
66 0 : auto& ref_count = *pcount;
67 0 : if ( ref_count == 0 )
68 0 : return;
69 :
70 0 : *ptotal+=res->count;
71 0 : --ref_count;
72 0 : if ( ref_count == 0 )
73 : {
74 0 : res->count = *ptotal;
75 0 : cb( std::move(res) );
76 : }
77 : },
78 : 0,
79 : nullptr
80 0 : );
81 0 : }
82 : }
83 0 : }
84 : }
85 :
86 3 : }}
|