import logging
import asyncio

import aiohttp

DEFAULT_TIMEOUT = 10


class Pinger:
    _logger = logging.getLogger(__name__)

    def __init__(self, ping_handler, loop, timeout=DEFAULT_TIMEOUT):
        self._ping_handler = ping_handler
        self._loop = loop
        self._count = 0
        self._timeout = timeout
        self._timer = None
        self._tcp = None
        self._protocol = None
        self._node = None
        self._host = None
        self._port = None

    def register_tcp_service(self, protocol, node):
        self._tcp = True
        self._protocol = protocol
        self._node = node

    def register_http_service(self, host, port, node):
        self._tcp = False
        self._host = host
        self._port = port
        self._node = node

    @asyncio.coroutine
    def start_ping(self):
        if self._tcp:
            self._loop.call_later(self._timeout, self._send_timed_ping)
        else:
            url = 'http://{}:{}/ping'.format(self._host, self._port)
            yield from asyncio.sleep(self._timeout)
            self._logger.info('Pinging node {} at url: {}'.format(self._node, url))
            try:
                resp = yield from asyncio.wait_for(aiohttp.request('GET', url=url), self._timeout)
                resp.close()
                yield from self.start_ping()
            except:
                #self._ping_handler.handle_ping_timeout(self._node)
                pass

    def pong_received(self, count):
        if self._count == count:
            self._count += 1
            if self._timer is not None:
                self._timer.cancel()
                yield from self.start_ping()
        else:
            self._ping_timed_out()

    def _make_ping_packet(self):
        packet = {'type': 'ping', 'node_id': self._node, 'count': self._count}
        return packet

    def _send_timed_ping(self):
        packet = self._make_ping_packet()
        self._protocol.send(packet)
        self._timer = self._loop.call_later(self._timeout, self._ping_timed_out)

    def _ping_timed_out(self):
        if self._timer:
            self._timer.cancel()
        self._ping_handler.handle_ping_timeout(self._node)

@@ -57,16 +57,42 @@
 IMEOUT =
+ 15%0ADEFAULT_PING_INTERVAL=
  10%0A%0A%0Acl
@@ -339,16 +339,63 @@
 timeout%0A
+        self._interval = DEFAULT_PING_INTERVAL%0A
         
@@ -919,32 +919,46 @@
 tcp:%0A           
+ self._timer =
  self._loop.call
@@ -962,39 +962,40 @@
 all_later(self._
-timeout
+interval
 , self._send_tim
@@ -1125,31 +1125,32 @@
 sleep(self._
-timeout
+interval
 )%0A          
@@ -1330,23 +1330,24 @@
 , self._
-timeout
+interval
 )%0A      
@@ -1581,22 +1581,25 @@
 lf._
-count == count
+timer is not None
 :%0A  
@@ -1618,23 +1618,24 @@
 lf._
-count += 1%0A    
+timer.cancel()%0A%0A
     
@@ -1647,38 +1647,32 @@
 f self._
-timer is not None:%0A   
+count == count:%0A
         
@@ -1679,34 +1679,25 @@
     
- 
 self._
-timer.cancel()%0A    
+count += 1%0A
     
@@ -2145,65 +2145,8 @@
 f):%0A
-        if self._timer:%0A            self._timer.cancel()%0A
     

fix pinger logic