def __init__(self, host=None, statusObj=None, transport='auto', delta=1.0, suppress_fetch_errors=False, suppress_conversion_errors=False): # Superclass initialization super(cachedStatusObj, self).__init__() # Dictionary holding all the cachedTable objects, indexed by table name self.cache = {} # Default cache expiration time for this table self.delta = delta # mutex used to arbitrate access to this object's state, particularly # self.cache self.lock = threading.RLock() # Thing we are going to use to get status. If we are not passed a # delegate, then try creating a rpcStatusObj with optional host param if statusObj != None: self.statusObj = statusObj else: if not host: raise common.statusError('Need to specify a host= or statusObj=') self.statusObj = rpcStatusObj(host, transport=transport) self.suppress_fetch_errors = suppress_fetch_errors self.suppress_conversion_errors = suppress_conversion_errors
def get_statusValuesDict(self, resDict): statusDict = {}.fromkeys(resDict.keys(), common.STATERROR) try: valDict = self.statusObj.fetch(statusDict) except ro.remoteObjectError, e: raise common.statusError(str(e))
def get_statusValuesList(self, aliasnames): statusDict = {}.fromkeys(aliasnames, common.STATERROR) try: valDict = self.statusObj.fetch(statusDict) except ro.remoteObjectError, e: raise common.statusError(str(e))
def get_statusValue(self, aliasname, allow_fail=True): try: val = self.statusObj.fetchOne(aliasname) except ro.remoteObjectError, e: if allow_fail: val = common.STATERROR else: raise common.statusError(str(e))
def set_statusValuesDict(self, statusDict): if common.ro_long_fix: valDict = common.ro_sanitize(statusDict) else: valDict = statusDict try: self.statusObj.store(valDict) except ro.remoteObjectError, e: raise common.statusError(str(e))
def get_subTable(self, tablename, offset, length): # Make sure there is a valid table present self.updateTable(tablename) cacheobj = self.get_tableCache(tablename) cacheobj.lock.acquire() try: if offset + length > cacheobj.tabledef.tablesize: raise common.statusError("Bad offset/length for table size") return cacheobj.table[offset:offset+length] finally: cacheobj.lock.release()
def put_subTable(self, tablename, data, offset): # Make sure there is a valid table present self.updateTable(tablename) cacheobj = self.get_tableCache(tablename) cacheobj.lock.acquire() try: newtbl = cacheobj.table[:offset] + data + \ cacheobj.table[offset+len(data):] if len(newtbl) != cacheobj.tabledef.tablesize: raise common.statusError("Bad data length for table size: %d != %d" % ( len(newtbl), cacheobj.tabledef.tablesize)) cacheobj.table = newtbl finally: cacheobj.lock.release()
def _mk_client(self): try: # Create the rpc client if self.transport == "auto": # Try TCP first, then UDP, according to RFC2224 try: cl = TCP_OSSC_SCREENGETClient(self.statushost) except socket.error: # print "TCP Connection refused, trying UDP" cl = UDP_OSSC_SCREENGETClient(self.statushost) elif transport == "tcp": cl = TCP_OSSC_SCREENGETClient(self.statushost) elif transport == "udp": cl = UDP_OSSC_SCREENGETClient(self.statushost) else: raise RuntimeError, "Invalid protocol" self.rpcclient = cl return except (rpc.PortMapError, socket.error), e: self.rpcclient = None raise common.statusError('Cannot create RPC client: %s' % (str(e)))
def store(self, statusDict): raise common.statusError("This is a read-only status object.")
try: res = self.rpcclient.call(tabledef.line, str(0), \ str(tabledef.tablesize)) except Exception, e: # Possibly stale RPC client, try resetting it... # this may raise a statusError self._mk_client() # Now try again, one more time... try: res = self.rpcclient.call(tabledef.line, str(0), \ str(tabledef.tablesize)) except Exception, e: self.rpcclient = None raise common.statusError("Exception raised invoking client rpc: %s" % str(e)) return res def get_statusTables(self, tablenames, res): for tablename in tablenames: res[tablename] = self.get_statusTable(tablename) def get_statusTablesByAliases(self, aliasnames, res): self.get_statusTables(self.info.aliasesToTables(aliasnames), res)