def discoverBroker()

in GreengrassAwareConnection.py [0:0]


    def discoverBroker(self):
        if self.hasDiscovered():
            return

        # Discover GGCs
        discoveryInfoProvider = DiscoveryInfoProvider()
        discoveryInfoProvider.configureEndpoint(self.host)
        discoveryInfoProvider.configureCredentials(self.rootCA, self.cert, self.key)
        discoveryInfoProvider.configureTimeout(10)  # 10 sec

        retryCount = self.max_discovery_retries
        self.groupCA = None
        coreInfo = None

        while retryCount != 0:
            try:
                discoveryInfo = discoveryInfoProvider.discover(self.thingName)
                caList = discoveryInfo.getAllCas()
                coreList = discoveryInfo.getAllCores()

                # We only pick the first ca and core info
                groupId, ca = caList[0]
                self.coreInfo = coreList[0]
                self.logger.info("Discovered GGC: %s from Group: %s" % (self.coreInfo.coreThingArn, groupId))

                self.groupCA = self.group_ca_path + groupId + "_CA_" + str(uuid.uuid4()) + ".crt"
                if not os.path.exists(self.group_ca_path):
                    os.makedirs(self.group_ca_path)
                groupCAFile = open(self.groupCA, "w")
                groupCAFile.write(ca)
                groupCAFile.close()

                self.discovered = True
                break
            except DiscoveryFailure as e:
                # device is not configured for greengrass, revert to IoT Core
                cl = Obj()
                cl.host = self.host
                cl.port = 8883

                self.coreInfo = Obj()
                self.coreInfo.connectivityInfoList = [cl]
                break
            except DiscoveryInvalidRequestException as e:
                print("Invalid discovery request detected!")
                print("Type: %s" % str(type(e)))
                print("Error message: %s" % e.message)
                print("Stopping...")
                break
            except BaseException as e:
                print("Error in discovery!")
                print("Type: %s" % str(type(e)))
                # print("Error message: %s" % e.message)
                retryCount -= 1
                print("\n%d/%d retries left\n" % (retryCount, self.max_discovery_retries))
                print("Backing off...\n")
                self.backOffCore.backOff()