def getConfigurationTypes(sock, sequence, buildId):
    # create a Request message
    request = remotecontrolportapi_pb2.Request()

    # set the Request sequence number
    request.sequence = sequence

    # set the Request type
    request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY

    # set the Request Query type
    request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_CONFIGURATION

    # set target build id
    request.query.configuration.buildId = buildId

    response = transaction(sock, request)

    mapping = {}

    for parameter in response.query.configuration.parameters:
        if parameter.values:
            mapping[parameter.name] = fromAny(parameter.values[0]).type

    return mapping
    def start(self):
        self._logger.log(Logger.DEBUG_LEVEL, "/%s start" % self._name)

        self._sock = socket.socket()

        try:
            self._sock.connect((self._address, self._port))
        except:
            raise ProbeException("unable to connect to %s:%d" %
                                 (self._address, self._port))

        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s control port connected" % self._name)

        request = remotecontrolportapi_pb2.Request()

        request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY

        request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_MANIFEST

        self._logger.log(Logger.DEBUG_LEVEL, "/%s T1" % self._name)
        response = self._transaction(request)
        self._logger.log(Logger.DEBUG_LEVEL, "/%s T2" % self._name)

        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s control port getting manifest" % (self._name))

        if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_MANIFEST:
                manifest = {}
                for nem in response.query.manifest.nems:
                    if self._nemId != None and self._nemId != nem.id:
                        continue
                    layers = []
                    for component in nem.components:
                        if component.plugin == self._plugin:
                            self._buildId = component.buildId
                            break

        if not self._buildId:
            raise ProbeException("unable to determine build id")
        else:
            self._logger.log(
                Logger.DEBUG_LEVEL,
                "/%s component build id %d" % (self._name, self._buildId))
def getManifest(sock, sequence):
    # create a Request message
    request = remotecontrolportapi_pb2.Request()

    # set the Request sequence number
    request.sequence = sequence

    # set the Request type
    request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY

    # set the Request Query type
    request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_MANIFEST

    response = transaction(sock, request)

    manifest = {}
    ManifestEntry = collections.namedtuple('ManifestEntry',
                                           'buildId type plugin')

    dataType = {
        remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_PHY:
        'PHY',
        remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_MAC:
        'MAC',
        remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_SHIM:
        'SHIM',
        remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_TRANSPORT:
        'TRANSPORT'
    }

    if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
        if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_MANIFEST:
            for nem in response.query.manifest.nems:
                manifest[nem.id] = []
                for component in nem.components:
                    manifest[nem.id].append(
                        ManifestEntry(component.buildId,
                                      dataType[component.type],
                                      component.plugin))

    return manifest
    import emanesh.remotecontrolportapi_pb2 as remotecontrolportapi_pb2

# create a socket
sock = socket.socket()

# connect to a running emulator instance
sock.connect(('node-1', 47000))

# set the Request sequence number
sequence = 1

# retrieve the manifest from the running emulator instance
manifest = getManifest(sock, sequence)

# create a Request message
request = remotecontrolportapi_pb2.Request()

# set the Request sequence number
sequence += 1
request.sequence = sequence

# set the Request type
request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY

# set the Request Query type
request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_STATISTIC

# for this example, set the target build id to the smallest build id
# component contained in the smallest NEM id running in the emulator
request.query.statistic.buildId = max(
    [x.buildId for x in manifest[max(manifest)]])
    def probe(self):
        self._logger.log(Logger.DEBUG_LEVEL, "/%s probe" % self._name)
        request = remotecontrolportapi_pb2.Request()
        request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY
        request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_STATISTIC
        request.query.statistic.buildId = self._buildId
        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s probe getting statistics" % self._name)
        response = self._transaction(request)

        stats = {}

        if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_STATISTIC:
                for element in response.query.statistic.elements:
                    stats[element.name] = fromAny(element.value)[0]

        request = remotecontrolportapi_pb2.Request()
        request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY
        request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_STATISTICTABLE
        request.query.statisticTable.buildId = self._buildId
        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s probe getting tables" % self._name)
        response = self._transaction(request)

        tables = {}

        if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
            if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_STATISTICTABLE:
                for table in response.query.statisticTable.tables:
                    tableData = []
                    for row in table.rows:
                        rowData = []
                        for value in row.values:
                            rowData.append(fromAny(value))
                        tableData.append(tuple(rowData))
                    tables[table.name] = (tuple(table.labels),
                                          tuple(tableData))

        self._logger.log(Logger.DEBUG_LEVEL,
                         "/%s probe processing" % self._name)
        lstats = {}

        for name, entry in list(stats.items()):
            lstats[name.lower()] = entry

        ltables = {}

        for name, entry in list(tables.items()):
            ltables[name.lower()] = entry

        probeData = []

        for name, probe in self._probes:
            probe.Clear()
            for member in list(probe.DESCRIPTOR.fields_by_name.keys()):
                if member != "description":
                    setattr(probe, member, lstats[member.replace('_', '.')])

            probeData.append(
                (name, probe.SerializeToString(), probe.description.name,
                 probe.description.module, probe.description.version))

        for name, probe in self._tables:
            probe.Clear()

            for member in list(probe.DESCRIPTOR.fields_by_name.keys()):

                if member != "description":
                    table = getattr(probe, member)

                    labels, rows = ltables[member.replace('_', '.')]

                    if not self.loadTable(member, table, rows):
                        for label in labels:
                            table.labels.append(label)

                        for row in rows:
                            pass
                            entries = []
                            r = table.rows.add()
                            for entry in row:
                                e = r.values.add()
                                loadMeasurement(e, *entry)

            probeData.append(
                (name, probe.SerializeToString(), probe.description.name,
                 probe.description.module, probe.description.version))

        return probeData