我正在编写一个多播客户端服务器应用程序,动态地根据配置更改应该加入并离开它侦听的多播组。
我如何从工厂的循环调用中获取对protocoll.transport的访问权限。这样我可以动态调用.joinGroup()
多播代码按预期工作,但我的想法是在工厂中有一个定时循环,查找更改并调用protocol.transport并加入正确的通道。
正如您在代码中看到的那样,我试图覆盖buildProtocol以获得对协议的访问权。
class MultiCastListenerProtocol(DatagramProtocol):
def startProtocol(self):
print 'stating protocol'
pass
def datagramReceived(self, datagram, (host, port)):
self.factory.datagramRecieved(self)
class MultiCastListenerFactory(protocol.Factory):
protocol = MultiCastListenerProtocol
def __init__(self):
print 'starting multicast listener'
self.loop = LoopingCall(self.timedLoop)
self.port = 4444
self.groupHandler = GroupHandler()
deferred = self.loop.start(.020 )
deferred.addErrback(self.dummyCallError)
self.ip = self.getLocalIp()
def buildProtocol(self, address):
print "creating protocol"
proto = protocol.Factory.buildProtocol(self, address)
self.protocol = proto
return proto
def datagramReceived(self, datagram, (host, port)):
# if host != self.ip:
print "recieved data from %s: %s" %(host, str(datagram))
def sendData(self, data, targets):
for ip in targets:
#print ip
self.socket.sendto(data,(ip, self.port))
def timedLoop(self):
targets = ['227.0.0.1'] + self.groupHandler.getGroupIps()
for group in targets:
self.protocol.
def dummyCallError(self, reason):
print "ERROR %s" %reason
def makeConnection(self, transport):
logging.info("connection:%s" %transport)
pass
def joinGroup(self, group):
self.connectedProtocol.transport.joinGroup(group)
def main():
"""
Da main program start
"""
reactor.listenMulticast(4444, MultiCastListenerFactory(), listenMultiple=True)
reactor.run()
if __name__ == "__main__":
main()