Esempio n. 1
0
def run():
		
     # local heartbeat and Aggregator
     if options.aggregator:

          aggregator = Aggregator(options.agconfig)
          portal = create_portal(aggregator)
          
          heartbeat = Heartbeat(address=options.group, port=options.port)
          localip = socket.gethostbyname(socket.gethostname())
          heartbeat.setMessage(encode(options.id, localip, options.agport, aggregator.getPassword()))
          
          reactor.listenTCP(options.agport, pb.PBServerFactory(portal))
          aggregator.notifyOnMapped(heartbeat.start)

     # heartbeat monitor
     monitor = Monitor(address=options.group, port=options.port)

     # wx-twisted communication bridge (mediator)
     global twistedbridge
     twistedbridge = TwistedBridge(monitor)

     # monitor reports to the bridge via callbacks
     monitor.newHeartbeat(twistedbridge.newHeartbeat)
     monitor.lostHeartbeat(twistedbridge.lostHeartbeat)

     monitor.start()

     app = MyApp(redirect=False)
     app.MainLoop()
Esempio n. 2
0
class TestHeartbeat(unittest.TestCase):

    def setUpClass(self):
        self.localip = socket.gethostbyname(socket.gethostname())
    
    def tearDownClass(self):
        pass


    def setUp(self):
        self.failure = None

        # choose port that we likely aren't using ...
        self.port = ports.multicastport + 1001
        self.heartbeat = Heartbeat(port=self.port)
        self.monitor = Monitor(port=self.port, timeout=5)
        self.monitor.newHeartbeat(self._onNewHeartbeat)



    def tearDown(self):
        self.heartbeat.stop()
        self.monitor.stop()


    def succeeded(self, *args):
        """Allow reactor iteration loop in test proper to exit and pass test"""
        self.done = True
        self.timeout.cancel()  # safety timeout no longer necessary
        self.lastargs = args  # make persistent for later checks


    def failed(self, reason):
        """Allow reactor iteration loop in test proper to exit and fail test"""
        self.done = True
        self.failure = reason
        self.lastargs = None



    def _onNewHeartbeat(self, host, data):
        if host == self.localip:
            self.succeeded()
        else:
            print "WARNING: Received a heartbeat from %s with data '%s'" % (host,data)


    def _onLostHeartbeat(self, host):
        if host == self.localip:
            self.succeeded()
        else:
            print "WARNING: Received a dropped heartbeat from %s" % host



    def test_heartbeat(self):

        # safety timeout
        self.timeout = reactor.callLater(5, self.failed, "Timed out listening for heartbeat on default address/port... failing")

        self.monitor.start()
        self.heartbeat.start()
        
        # idle until code above triggers succeeded or timeout causes failure
        self.done = False
        while not self.done:
            reactor.iterate(0.1)
       
        # will arrive here eventually when either succeeded or failed method has fired
        if self.failure:
            self.fail(self.failure)


        # safety timeout
        self.timeout = reactor.callLater(10, self.failed, "Timed out waiting for expected dropped heartbeat ... failing")

        # stop heartbeat, expect notification ...
        self.monitor.lostHeartbeat(self._onLostHeartbeat)

        # idle until code above triggers succeeded or timeout causes failure
        self.done = False

        self.heartbeat.stop()
        while not self.done:
            reactor.iterate(0.1)

        # will arrive here eventually when either succeeded or failed method has fired
        if self.failure:
            self.fail(self.failure)