def _mount(self): if self._children: self._mode |= MODE_VIRT if self._mode & MODE_VIRT or self._index == None: mode = None freq = None prof = None else: mode = self._mode freq = self._freq prof = self.d_profile path = get_mnt_path(self._uid, self._name) if os.path.exists(path): loader = Loader(self._uid) curr_prof = loader.get_profile(self._name) if curr_prof['type'] == prof['type']: curr_mode = loader.get_attr(self._name, ATTR_MODE, int) if ((curr_mode | MODE_SYNC) == (mode | MODE_SYNC) ) and curr_prof.get('spec') == prof.get('spec'): mode = None freq = None prof = None else: if not (curr_mode & MODE_SYNC): mode &= ~MODE_SYNC else: mode |= MODE_SYNC freq = loader.get_attr(self._name, ATTR_FREQ, float)
class Mode(object): def __init__(self, uid): self._mode = {} self._loader = Loader(uid) def _log(self, text): if LOG_MODE: log_debug(self, text) def _get(self, name): mode = self._loader.get_attr(name, ATTR_MODE, int) if mode != None: self._mode[name] = mode self._log('name=%s, mode=%s' % (str(name), str(mode))) return mode def get(self, name): if self._mode.has_key(name): ret = self._mode.get(name) if ret != None: return ret return self._get(name) def remove(self, name): if self._mode.has_key(name): del self._mode[name]
class Freq(object): def __init__(self, uid): self._freq = {} self._loader = Loader(uid) def _log(self, text): if LOG_FREQ: log_debug(self, text) def _get(self, name): freq = self._loader.get_attr(name, ATTR_FREQ, float) if freq != None: self._freq[name] = freq self._log('name=%s, freq=%s' % (str(name), str(freq))) return freq def get(self, name): if self._freq.has_key(name): ret = self._freq.get(name) if ret != None: return ret return self._get(name) def remove(self, name): if self._freq.has_key(name): del self._freq[name]
class Filter(object): def __init__(self, uid, addr=PROC_ADDR): self._addr = addr self._filters = {} self._loader = Loader(uid) def _get_code(self, name): buf = self._filters.get(name) if not buf: buf = self._loader.get_attr(name, ATTR_FILTER, str) self._filters.update({name:buf}) return buf def check(self, name): if self._filters.get(name): return True else: buf = self._loader.get_attr(name, ATTR_FILTER, str) if buf: self._filters.update({name:buf}) return True def remove(self, name): if self._filters.has_key(name): del self._filters[name] def put(self, name, buf): try: code = self._get_code(name) if code == None: code = self._get_code(name) if not code: return return proc.put(self._addr, FILTER_PORT, code, buf) except: log_err(self, 'failed to put')
class Timeout(object): def __init__(self, uid): self._timeout = {} self._loader = Loader(uid) def _get(self, name): timeout = self._loader.get_attr(name, ATTR_TIMEOUT, float) if timeout != None: self._timeout[name] = timeout return timeout def get(self, name): if self._timeout.has_key(name): ret = self._timeout.get(name) if ret != None: return ret return self._get(name) def remove(self, name): if self._timeout.has_key(name): del self._timeout[name]
class Parent(object): def __init__(self, uid): self._parent = {} self._loader = Loader(uid) def _get(self, name): parent = self._loader.get_attr(name, ATTR_PARENT, str) if parent != None: self._parent[name] = parent return parent def get(self, name): if self._parent.has_key(name): ret = self._parent.get(name) if ret != None: return ret return self._get(name) def remove(self, name): if self._parent.has_key(name): del self._parent[name]
class Dispatcher(object): def __init__(self, uid, channel, core, addr=PROC_ADDR): self._uid = uid self._queue = [] self._paths = {} self._hidden = {} self._core = core self._addr = addr self._visible = {} self._scheduler = None self._dispatchers = {} self._channel = channel self._lock = NamedLock() self._loader = Loader(self._uid) if ASYNC and QUEUE_LEN: self._scheduler = DispatcherScheduler(core) def _log(self, text): if LOG_DISPATCHER: log_debug(self, text) def _get_code(self, name): buf = self._dispatchers.get(name) if not buf: buf = self._loader.get_attr(name, ATTR_DISPATCHER, str) self._dispatchers.update({name: buf}) return buf def _deliver(self, dest, src, buf, flags): try: if ASYNC: if QUEUE_LEN: while True: ret = self._scheduler.select(dest, src, buf, flags) if ret == None: self._scheduler.wait() else: if not ret: self._scheduler.put(dest, src, buf, flags) break else: Thread(target=self._core.put, args=(dest, src, buf, flags)).start() else: self._core.put(dest, src, buf, flags) except: log_err(self, 'failed to deliver, dest=%s, src=%s' % (dest, src)) def _send(self, dest, src, buf, flags): self._channel.put(dest, src, buf=buf, flags=flags) @edge_lock def add_edge(self, edge, hidden=False): src = edge[0] dest = edge[1] if hidden: paths = self._hidden else: paths = self._visible if paths.has_key(src) and paths[src].has_key(dest): return if not paths.has_key(src): paths[src] = {} if not self._paths.has_key(src): self._paths[src] = {} local = is_local(self._uid, dest) paths[src].update({dest: local}) if not self._paths[src].has_key(dest): if not local: self._channel.allocate(dest) self._paths[src].update({dest: 1}) else: self._paths[src][dest] += 1 self._log('add edge, dest=%s, src=%s, local=%s' % (dest, src, str(local))) @edge_lock def remove_edge(self, edge, hidden=False): src = edge[0] dest = edge[1] if hidden: paths = self._hidden else: paths = self._visible if not paths.has_key(src) or not paths[src].has_key(dest): return local = paths[src][dest] del paths[src][dest] self._paths[src][dest] -= 1 if 0 == self._paths[src][dest]: del self._paths[src][dest] if not local: self._channel.free(dest) self._log('remove edge, dest=%s, src=%s, local=%s' % (dest, src, str(local))) @named_lock def has_edge(self, name): return self._visible.has_key(name) def update_edges(self, name, edges): if not edges: return for dest in edges: if dest.startswith('.'): dest = dest[1:] if dest != name: self.add_edge((name, dest)) def remove_edges(self, name): paths = self._visible.get(name) for i in paths: self.remove_edge((name, i)) paths = self._hidden.get(name) for i in paths: self.remove_edge((name, i), hidden=True) if self._dispatchers.has_key(name): del self._dispatchers[name] def remove(self, name): if self._dispatchers.has_key(name): del self._dispatchers[name] def sendto(self, dest, src, buf, hidden=False, flags=0): if not buf: return self.add_edge((src, dest), hidden=hidden) if self._hidden: local = self._hidden[src][dest] else: local = self._visible[src][dest] if not local: self._send(dest, src, buf, flags) else: self._deliver(dest, src, buf, flags) self._log('sendto, dest=%s, src=%s' % (dest, src)) def send(self, name, buf, flags=0): if not buf: return dest = self._visible.get(name) if not dest: return for i in dest: if not dest[i]: self._send(i, name, buf, flags) else: self._deliver(i, name, buf, flags) self._log('send, dest=%s, src=%s' % (i, name)) def send_blocks(self, name, blocks): if not blocks: return dest = self._visible.get(name) if not dest: return cnt = 0 keys = dest.keys() len_keys = len(keys) len_blks = len(blocks) window = (len_blks + len_keys - 1) / len_keys start = randint(0, len_keys - 1) for _ in range(len_keys): i = keys[start] for _ in range(window): if blocks[cnt]: if not dest[i]: self._send(i, name, blocks[cnt], 0) else: self._deliver(i, name, blocks[cnt], 0) self._log('send a block, dest=%s, src=%s' % (i, name)) cnt += 1 if cnt == len_blks: return start += 1 if start == len_keys: start = 0 def put(self, name, buf): try: code = self._get_code(name) if code == None: code = self._get_code(name) if not code: return return proc.put(self._addr, DISPATCHER_PORT, code, buf) except: log_err(self, 'failed to put') def check(self, name): if self._dispatchers.get(name): return True else: buf = self._loader.get_attr(name, ATTR_DISPATCHER, str) if buf: self._dispatchers.update({name: buf}) return True