Beispiel #1
0
class RegistryPeerXMLRPC(RegistryBase):
    def __init__(self, port, peerListUrl):
        RegistryBase.__init__(self, port, peerListUrl)
        self.requestServer = AsyncAGXMLRPCServerThreaded(
            ("", self.port), intervalSecs=1, callback=self._UpdateCallback)
        self._RegisterFunctions()

    def _RegisterFunctions(self):
        self.requestServer.register_function(self.LookupBridge, "LookupBridge")
        self.requestServer.register_function(self.RegisterBridge,
                                             "RegisterBridge")
        self.requestServer.register_function(self.Ping, "Ping")

    def RegisterBridge(self, bridgeDescription):
        #print "Registering bridge:", bridgeDescription["host"], bridgeDescription["port"]
        expirationTime = time.time() + self.registrationLengthSecs
        self._StoreBridgeDescription(bridgeDescription, expirationTime)
        #print "bridges:", self.registeredBridges.keys()
        return self.registrationLengthSecs

    def Run(self):
        self.ConnectToRegistries()
        self.requestServer.run()

    def Stop(self):
        self.requestServer.stop()
Beispiel #2
0
class RegistryPeerXMLRPC(RegistryBase):
    def __init__(self, port, peerListUrl):
        RegistryBase.__init__(self, port, peerListUrl)
        self.requestServer = AsyncAGXMLRPCServerThreaded(("", self.port), intervalSecs=1, callback=self._UpdateCallback)
        self._RegisterFunctions()

    def _RegisterFunctions(self):
        self.requestServer.register_function(self.LookupBridge, "LookupBridge")
        self.requestServer.register_function(self.RegisterBridge, "RegisterBridge")
        self.requestServer.register_function(self.Ping, "Ping")

    def RegisterBridge(self, bridgeDescription):
        # print "Registering bridge:", bridgeDescription["host"], bridgeDescription["port"]
        expirationTime = time.time() + self.registrationLengthSecs
        self._StoreBridgeDescription(bridgeDescription, expirationTime)
        # print "bridges:", self.registeredBridges.keys()
        return self.registrationLengthSecs

    def Run(self):
        self.ConnectToRegistries()
        self.requestServer.run()

    def Stop(self):
        self.requestServer.stop()
Beispiel #3
0
class QuickBridgeServer:
    def __init__(self, name, location, listenPort, qbexec, registryUrlList, portRange=None):
        if not os.path.exists(qbexec):
            raise Exception("QuickBridge executable does not exist at this location:", qbexec)
        self.bridgeFactory = BridgeFactory(qbexec=qbexec, portRange=portRange, logger=log)
        self.providerProfile = ProviderProfile(name, location)
        self.listenPort = listenPort
        self.listeningServer = AsyncAGXMLRPCServerThreaded( ("", listenPort), intervalSecs=1, 
                                                    callback=self.MaintenanceCallback,
                                                    logRequests=0)
        self._RegisterRemoteFunctions()
        self.registryClients = []
        for registryUrl in registryUrlList:
            self.registryClients.append(RegistryClient(url=registryUrl))
        hostname = SystemConfig.instance().GetHostname()
        self.bridgeDescription = BridgeDescription(guid=GUID(), name=name, host=hostname, 
                                                   port=self.listenPort, serverType=QUICKBRIDGE_TYPE, 
                                                   description="", 
                                                   portMin=self.bridgeFactory.GetPortMin(),
                                                   portMax=self.bridgeFactory.GetPortMax())
        self._RegisterWithRegistry()
        self.running = False

    def _RegisterRemoteFunctions(self):
        self.listeningServer.register_function(self.JoinBridge, "JoinBridge")
        self.listeningServer.register_function(self.GetBridgeInfo, "GetBridgeInfo")
        self.listeningServer.register_function(self.Ping, "Ping")

    def JoinBridge(self,multicastNetworkLocation):
        mnl = multicastNetworkLocation
        log.info("Bridge request: mcast %s %s" % (mnl["host"],str(mnl["port"])))
        uaddr = SystemConfig.instance().GetHostname()
        retBridge = self.bridgeFactory.CreateBridge(id=mnl["id"], maddr=mnl["host"],
                    mport=mnl["port"], mttl=mnl["ttl"], uaddr=uaddr,uport=None)
        networkLocation = UnicastNetworkLocation(host=retBridge.uaddr, port=retBridge.uport)
        networkLocation.profile = self.providerProfile
        networkLocation.id = GUID()
        networkLocation.privateId = GUID()
        return networkLocation
        
    def GetBridgeInfo(self):
        ret = []
        bridges = self.bridgeFactory.GetBridges()
        print 'bridges = ', bridges
        for bridge in bridges:
            bridgedata = {'maddr': bridge.maddr,
                          'mport': bridge.mport,
                          'uaddr': bridge.uaddr,
                          'uport': bridge.uport 
                          }
            ret.append(bridgedata)
        return ret

    def Ping(self, data):
        return data

    def _RegisterWithRegistry(self):
        self.validSecs = -1
        for registry in self.registryClients:
            try:
                secs = registry.RegisterBridge(self.bridgeDescription)
                if self.validSecs == -1 or self.validSecs > secs:
                    self.validSecs = secs;
            except:
                print("Error connecting to bridge registry at " + registry.url)
                log.error("Error connecting to bridge registry at " + registry.url)
        now = time.time()
        if self.validSecs == True:  # only needed until registry code is updated
            self.validSecs = 120  # expires every 2 minutes
        self.registrationExpirationTime = now + self.validSecs
        self.nextRegistrationTime = now + (self.validSecs * .9 - 10)

    def _RegisterWithRegistryIfNeeded(self):
        if time.time() > self.nextRegistrationTime:
            self._RegisterWithRegistry()

    def MaintenanceCallback(self):
        try:
            self._RegisterWithRegistryIfNeeded()
        except:
            secsToSleep = random.randrange(5,60)
            log.error("Error reregistering, retry in " + str(secsToSleep) + " seconds.")
            time.sleep(secsToSleep)

    def Run(self):
        self.listeningServer.run()

    def SetName(self,name):
        self.providerProfile.name = name
            
    def SetLocation(self,location):
        self.providerProfile.location = location
        
    def SetBridgeExecutable(self,qbexec):
        self.bridgeFactory.SetBridgeExecutable(qbexec)

    def Shutdown(self):
        """
        Shutdown shuts down the BridgeServer. 
        - Stop bridges
        """
        log.info("Method BridgeServer.Shutdown called")
Beispiel #4
0
class QuickBridgeServer:
    def __init__(self,
                 name,
                 location,
                 listenPort,
                 qbexec,
                 registryUrlList,
                 portRange=None):
        if not os.path.exists(qbexec):
            raise Exception(
                "QuickBridge executable does not exist at this location:",
                qbexec)
        self.bridgeFactory = BridgeFactory(qbexec=qbexec,
                                           portRange=portRange,
                                           logger=log)
        self.providerProfile = ProviderProfile(name, location)
        self.listenPort = listenPort
        self.listeningServer = AsyncAGXMLRPCServerThreaded(
            ("", listenPort),
            intervalSecs=1,
            callback=self.MaintenanceCallback,
            logRequests=0)
        self._RegisterRemoteFunctions()
        self.registryClients = []
        for registryUrl in registryUrlList:
            self.registryClients.append(RegistryClient(url=registryUrl))
        hostname = SystemConfig.instance().GetHostname()
        self.bridgeDescription = BridgeDescription(
            guid=GUID(),
            name=name,
            host=hostname,
            port=self.listenPort,
            serverType=QUICKBRIDGE_TYPE,
            description="",
            portMin=self.bridgeFactory.GetPortMin(),
            portMax=self.bridgeFactory.GetPortMax())
        self._RegisterWithRegistry()
        self.running = False

    def _RegisterRemoteFunctions(self):
        self.listeningServer.register_function(self.JoinBridge, "JoinBridge")
        self.listeningServer.register_function(self.GetBridgeInfo,
                                               "GetBridgeInfo")
        self.listeningServer.register_function(self.Ping, "Ping")

    def JoinBridge(self, multicastNetworkLocation):
        mnl = multicastNetworkLocation
        log.info("Bridge request: mcast %s %s" %
                 (mnl["host"], str(mnl["port"])))
        uaddr = SystemConfig.instance().GetHostname()
        retBridge = self.bridgeFactory.CreateBridge(id=mnl["id"],
                                                    maddr=mnl["host"],
                                                    mport=mnl["port"],
                                                    mttl=mnl["ttl"],
                                                    uaddr=uaddr,
                                                    uport=None)
        networkLocation = UnicastNetworkLocation(host=retBridge.uaddr,
                                                 port=retBridge.uport)
        networkLocation.profile = self.providerProfile
        networkLocation.id = GUID()
        networkLocation.privateId = GUID()
        return networkLocation

    def GetBridgeInfo(self):
        ret = []
        bridges = self.bridgeFactory.GetBridges()
        print 'bridges = ', bridges
        for bridge in bridges:
            bridgedata = {
                'maddr': bridge.maddr,
                'mport': bridge.mport,
                'uaddr': bridge.uaddr,
                'uport': bridge.uport
            }
            ret.append(bridgedata)
        return ret

    def Ping(self, data):
        return data

    def _RegisterWithRegistry(self):
        self.validSecs = -1
        for registry in self.registryClients:
            try:
                secs = registry.RegisterBridge(self.bridgeDescription)
                if self.validSecs == -1 or self.validSecs > secs:
                    self.validSecs = secs
            except:
                print("Error connecting to bridge registry at " + registry.url)
                log.error("Error connecting to bridge registry at " +
                          registry.url)
        now = time.time()
        if self.validSecs == True:  # only needed until registry code is updated
            self.validSecs = 120  # expires every 2 minutes
        self.registrationExpirationTime = now + self.validSecs
        self.nextRegistrationTime = now + (self.validSecs * .9 - 10)

    def _RegisterWithRegistryIfNeeded(self):
        if time.time() > self.nextRegistrationTime:
            self._RegisterWithRegistry()

    def MaintenanceCallback(self):
        try:
            self._RegisterWithRegistryIfNeeded()
        except:
            secsToSleep = random.randrange(5, 60)
            log.error("Error reregistering, retry in " + str(secsToSleep) +
                      " seconds.")
            time.sleep(secsToSleep)

    def Run(self):
        self.listeningServer.run()

    def SetName(self, name):
        self.providerProfile.name = name

    def SetLocation(self, location):
        self.providerProfile.location = location

    def SetBridgeExecutable(self, qbexec):
        self.bridgeFactory.SetBridgeExecutable(qbexec)

    def Shutdown(self):
        """
        Shutdown shuts down the BridgeServer. 
        - Stop bridges
        """
        log.info("Method BridgeServer.Shutdown called")