1

我正在编写一个多播客户端服务器应用程序,它应该根据配置更改动态加入和离开它所侦听的多播组。

我如何从工厂中的循环调用中访问 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()
4

0 回答 0