Project

General

Profile

Files ยป starpy-manager_AMI-auto-connect.patch

Patch for starpy/manager (AMI auto-connect) - Denis Zhuravlyov, 20/03/2016 04:24 PM

View differences:

examples/priexhaustionbare.py
21 21
	)
22 22
	def main( self ):
23 23
		"""Main operation for the channel-tracking demo"""
24
		amiDF = APPLICATION.amiSpecifier.login( 
25
		).addCallback( self.onAMIConnect )
24
		deferred = APPLICATION.amiSpecifier.login( on_reconnect=self.onAMIReconnect )
25
		self.addCallbacks( deferred )
26 26
	def onAMIConnect( self, ami ):
27
		"""Connection handler"""
27 28
		ami.status().addCallback( self.onStatus, ami=ami )
28 29
		ami.registerEvent( 'Hangup', self.onChannelHangup )
29 30
		ami.registerEvent( 'Newchannel', self.onChannelNew )
31
	def onAMIReconnect( self, deferred=None ):
32
		"""Callback for AMIFactory's reconnect event"""
33
		log.debug("""Reconnecting""")
34
		self.addCallbacks( deferred )
35
	def onAMIFailed(self, reason):
36
		"""Failed connection handler"""
37
		log.info("""Connection failed. Trying to reconnect...""")
38
		log.debug("""Reason: %s""", reason)
39
	def addCallbacks( self, deferred=None ):
40
		"""Callbacks setting helper"""
41
		deferred.addCallback( self.onAMIConnect )
42
		deferred.addErrback( self.onAMIFailed )
30 43
	def onStatus( self, events, ami=None ):
31 44
		"""Integrate the current status into our set of channels"""
32 45
		log.debug( """Initial channel status retrieved""" )
......
35 48
	def onChannelNew( self, ami, event ):
36 49
		"""Handle creation of a new channel"""
37 50
		log.debug( """Start on channel %s""", event )
38
		opening = not self.channels.has_key( event['uniqueid'] )
39
		self.channels[ event['uniqueid'] ] = event 
40
		if opening:
41
			self.onChannelChange( ami, event, opening = opening )
51
		if 'uniqueid' in event:
52
			opening = not event['uniqueid'] in self.channels
53
			self.channels[ event['uniqueid'] ] = event
54
			if opening:
55
				self.onChannelChange( ami, event, opening = opening )
42 56
	def onChannelHangup( self, ami, event ):
43 57
		"""Handle hangup of an existing channel"""
44 58
		try:
45
-- a/examples/utilapplication.py
59
++ b/examples/utilapplication.py
......
173 173
        "timeout", """Timeout in seconds for an AMI connection timeout""",
174 174
        defaultValue = 5.0,
175 175
    )
176
    def login( self ):
176
    def login( self, on_reconnect=None ):
177 177
        """Login to the specified manager via the AMI"""
178
        theManager = manager.AMIFactory(self.username, self.secret)
178
        theManager = manager.AMIFactory(self.username, self.secret, on_reconnect=on_reconnect)
179 179
        return theManager.login(self.server, self.port, timeout=self.timeout)
180 180

  
181 181
class AGISpecifier( propertied.Propertied ):
182
-- a/starpy/manager.py
182
++ b/starpy/manager.py
......
1111 1111
    def clientConnectionFailed(self, connector, reason):
1112 1112
        """Connection failed, report to our callers"""
1113 1113
        self.loginDefer.errback(reason)
1114
        self.reconnect(connector)
1114 1115

  
1115 1116
    def clientConnectionLost(self, connector, unused_reason):
1116 1117
        """Connection lost, re-build the login connection"""
1117 1118
        log.info('connection lost, reconnecting...')
1119
        log.debug(self.on_reconnect)
1120
        self.reconnect(connector)
1121

  
1122
    def reconnect(self, connector):
1118 1123
        self.retry(connector)
1119 1124
        self.loginDefer = defer.Deferred()
1120
        log.info(self.on_reconnect)
1121 1125
        if self.on_reconnect:
1122 1126
            self.on_reconnect(self.loginDefer)
    (1-1/1)