34 from .
import remotecontrolportapi_pb2
35 from .
import ControlPortException
44 if any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_INT8:
45 return (any.i32Value,ControlPortClient.TYPE_INT8)
46 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_UINT8:
47 return (any.u32Value,ControlPortClient.TYPE_UINT8)
48 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_INT16:
49 return (any.i32Value,ControlPortClient.TYPE_INT16)
50 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_UINT16:
51 return (any.u32Value,ControlPortClient.TYPE_UINT16)
52 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_INT32:
53 return (any.i32Value,ControlPortClient.TYPE_INT32)
54 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_UINT32:
55 return (any.u32Value,ControlPortClient.TYPE_UINT32)
56 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_INT64:
57 return (any.i64Value,ControlPortClient.TYPE_INT64)
58 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_UINT64:
59 return (any.u64Value,ControlPortClient.TYPE_UINT64)
60 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_FLOAT:
61 return (any.fValue,ControlPortClient.TYPE_FLOAT)
62 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_DOUBLE:
63 return (any.dValue,ControlPortClient.TYPE_DOUBLE)
64 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_STRING:
65 return (any.sValue,ControlPortClient.TYPE_STRING)
66 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_BOOLEAN:
67 return (any.bValue,ControlPortClient.TYPE_BOOLEAN)
68 elif any.type == remotecontrolportapi_pb2.Any.TYPE_ANY_INETADDR:
69 return (any.sValue,ControlPortClient.TYPE_INETADDR)
71 def toAny(any,value,valueType):
73 if valueType == ControlPortClient.TYPE_INT8:
74 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_INT8
77 elif valueType == ControlPortClient.TYPE_UINT8:
78 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_UINT8
81 elif valueType == ControlPortClient.TYPE_INT16:
82 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_INT16
85 elif valueType == ControlPortClient.TYPE_UINT16:
86 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_UINT16
89 elif valueType == ControlPortClient.TYPE_INT32:
90 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_INT32
93 elif valueType == ControlPortClient.TYPE_UINT32:
94 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_UINT32
97 elif valueType == ControlPortClient.TYPE_INT64:
98 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_INT64
101 elif valueType == ControlPortClient.TYPE_UINT64:
102 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_UINT64
105 elif valueType == ControlPortClient.TYPE_FLOAT:
106 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_FLOAT
109 elif valueType == ControlPortClient.TYPE_DOUBLE:
110 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_DOUBLE
113 elif valueType == ControlPortClient.TYPE_STRING:
114 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_STRING
117 elif valueType == ControlPortClient.TYPE_BOOLEAN:
118 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_BOOLEAN
121 elif valueType == ControlPortClient.TYPE_INETADDR:
122 any.type = remotecontrolportapi_pb2.Any.TYPE_ANY_INETADDR
127 class ControlPortClient:
146 self.
_sock = socket.socket()
150 self.
_sock.connect((hostname,port))
152 raise ControlPortException(
"unable to connect to %s:%d" % (hostname,port),
True)
157 self.
_lock = threading.Lock()
158 self._read,self.
_write = os.pipe()
166 os.write(self.
_write, str.encode(
"\n")
if sys.version_info >= (3,0)
else "\n")
171 request = remotecontrolportapi_pb2.Request()
172 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY
173 request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_MANIFEST
177 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
178 if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_MANIFEST:
180 for nem
in response.query.manifest.nems:
182 for component
in nem.components:
183 if component.type == remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_PHY:
185 elif component.type == remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_MAC:
187 elif component.type == remotecontrolportapi_pb2.Response.Query.Manifest.NEM.Component.TYPE_COMPONENT_SHIM:
192 layers.append((component.buildId,name,component.plugin))
194 manifest[nem.id] = tuple(layers)
198 raise ControlPortException(
'malformed manifest query response')
200 raise ControlPortException(
'malformed query response')
204 request = remotecontrolportapi_pb2.Request()
205 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY
206 request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_CONFIGURATION
207 request.query.configuration.buildId = buildId
208 for parameter
in parameters:
209 request.query.configuration.names.append(parameter)
213 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
214 if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_CONFIGURATION:
216 for parameter
in response.query.configuration.parameters:
219 for value
in parameter.values:
222 configuration[parameter.name] = tuple(values)
226 raise ControlPortException(
'malformed configuration query response')
228 raise ControlPortException(
'malformed query response')
232 request = remotecontrolportapi_pb2.Request()
233 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY
234 request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_STATISTIC
235 request.query.statistic.buildId = buildId
236 for element
in elements:
237 request.query.statistic.names.append(element)
241 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
242 if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_STATISTIC:
244 for element
in response.query.statistic.elements:
245 statistics[element.name] =
fromAny(element.value)
250 raise ControlPortException(
'malformed statistic query response')
252 raise ControlPortException(
'malformed query response')
256 request = remotecontrolportapi_pb2.Request()
257 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_QUERY
258 request.query.type = remotecontrolportapi_pb2.TYPE_QUERY_STATISTICTABLE
259 request.query.statisticTable.buildId = buildId
261 request.query.statisticTable.names.append(table)
265 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_QUERY:
266 if response.query.type == remotecontrolportapi_pb2.TYPE_QUERY_STATISTICTABLE:
269 for table
in response.query.statisticTable.tables:
271 for row
in table.rows:
273 for value
in row.values:
275 tableData.append(tuple(rowData))
276 tables[table.name] = (tuple(table.labels),tuple(tableData))
280 raise ControlPortException(
'malformed statisticTable query response')
282 raise ControlPortException(
'malformed query response')
286 request = remotecontrolportapi_pb2.Request()
287 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_UPDATE
288 request.update.type = remotecontrolportapi_pb2.TYPE_UPDATE_STATISTICCLEAR
289 request.update.statisticClear.buildId = buildId
290 for element
in elements:
291 request.update.statisticClear.names.append(element)
295 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_UPDATE:
296 if response.update.type == remotecontrolportapi_pb2.TYPE_UPDATE_STATISTICCLEAR:
299 raise ControlPortException(
'malformed statistic clear update response')
301 raise ControlPortException(
'malformed update response')
305 request = remotecontrolportapi_pb2.Request()
306 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_UPDATE
307 request.update.type = remotecontrolportapi_pb2.TYPE_UPDATE_STATISTICTABLECLEAR
308 request.update.statisticTableClear.buildId = buildId
310 request.update.statisticTableClear.names.append(table)
314 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_UPDATE:
315 if response.update.type == remotecontrolportapi_pb2.TYPE_UPDATE_STATISTICTABLECLEAR:
318 raise ControlPortException(
'malformed statistic table clear update response')
320 raise ControlPortException(
'malformed update response')
324 request = remotecontrolportapi_pb2.Request()
325 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_UPDATE
326 request.update.type = remotecontrolportapi_pb2.TYPE_UPDATE_CONFIGURATION
327 request.update.configuration.buildId = buildId
329 for (name,dataType,values)
in updates:
330 parameter = request.update.configuration.parameters.add()
331 parameter.name = name
333 any = parameter.values.add()
334 toAny(any,value,dataType)
338 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_UPDATE:
339 if response.update.type == remotecontrolportapi_pb2.TYPE_UPDATE_CONFIGURATION:
342 raise ControlPortException(
'malformed configuration update response')
344 raise ControlPortException(
'malformed update response')
348 request = remotecontrolportapi_pb2.Request()
349 request.type = remotecontrolportapi_pb2.Request.TYPE_REQUEST_UPDATE
350 request.update.type = remotecontrolportapi_pb2.TYPE_UPDATE_LOGLEVEL
351 request.update.logLevel.level = level
355 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_UPDATE:
356 if response.update.type == remotecontrolportapi_pb2.TYPE_UPDATE_LOGLEVEL:
359 raise ControlPortException(
'malformed log level update response')
361 raise ControlPortException(
'malformed update response')
365 def _sendMessage(self,request):
370 raise ControlPortException(
'connection terminated by server',
True)
374 request.sequence = sequence
376 msg = request.SerializeToString()
378 self.
_sock.send(struct.pack(
"!L%ds" % len(msg),len(msg),msg))
380 event = threading.Event()
399 if response.type == remotecontrolportapi_pb2.Response.TYPE_RESPONSE_ERROR:
400 raise ControlPortException(response.error.description);
405 raise ControlPortException(
'connection terminated by server',
True)
409 buffer =
bytes()
if sys.version_info >= (3,0)
else "" 410 messageLengthBytes = 0
414 readable,_,_ = select.select([self.
_sock,self._read],[],[])
418 if not messageLengthBytes:
419 data = self.
_sock.recv(4-len(buffer))
427 if(len(buffer) == 4):
428 (messageLengthBytes,) = struct.unpack(
'!I',buffer);
429 buffer =
bytes()
if sys.version_info >= (3,0)
else "" 432 data = self.
_sock.recv(messageLengthBytes-len(buffer))
440 if(len(buffer) == messageLengthBytes):
441 response = remotecontrolportapi_pb2.Response()
443 response.ParseFromString(buffer)
453 messageLengthBytes = 0
454 buffer =
bytes()
if sys.version_info >= (3,0)
else "" 456 elif fd
is self._read:
464 for event
in list(self.
_eventMap.values()):
def getStatistic(self, buildId, elements=())
def setLogLevel(self, level)
def getStatisticTable(self, buildId, tables=())
def _sendMessage(self, request)
struct EtherAddrBytes bytes
def getConfiguration(self, buildId, parameters=())
def clearStatistic(self, buildId, elements=())
def updateConfiguration(self, buildId, updates)
def toAny(any, value, valueType)
def __init__(self, hostname, port, disconnect=None)
def clearTable(self, buildId, tables=())