Beispiel #1
0
    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)
Beispiel #2
0
 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)
Beispiel #3
0
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]
Beispiel #4
0
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]
Beispiel #5
0
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')
Beispiel #6
0
class Lo(UDI):
    def get_name(self, device, child=None):
        return _get_name(device)

    def get_mode(self, device):
        return _get_mode(device)

    def setup(self):
        self._active = False
        self._loader = Loader(self.get_uid())
        Thread(target=create_server, args=(LO_ADDR, LO_PORT, LoServer)).start()

    def _get_device(self, name):
        mode = self._core.get_mode(name)
        prof = self._loader.get_profile(name)
        if prof:
            return device_name(prof['type'], name, mode)

    def scan(self):
        device_list = []
        if not self._active:
            self._active = True
            names = get_devices(self._uid, sort=True)
            if names:
                for name in names:
                    device = self._get_device(name)
                    if device and device not in _devices:
                        device_list.append(device)
        return device_list

    def connect(self, device):
        return (connect(device), True)

    @property
    def compute_unit(self):
        if not _devices:
            return
        keys = _devices.keys()
        length = len(keys)
        i = randint(0, length - 1)
        for _ in range(length):
            device = _devices[keys[i]]
            if device.get_mode() & MODE_CTRL:
                return device
            i += 1
            if i == length:
                i = 0
Beispiel #7
0
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]
Beispiel #8
0
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]
Beispiel #9
0
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
Beispiel #10
0
 def __init__(self, uid):
     self._timeout = {}
     self._loader = Loader(uid)
Beispiel #11
0
 def __init__(self, uid, addr=PROC_ADDR):
     self._addr = addr
     self._filters = {}
     self._loader = Loader(uid)
Beispiel #12
0
 def __init__(self, uid):
     self._freq = {}
     self._loader = Loader(uid)
Beispiel #13
0
 def __init__(self, uid):
     self._parent = {}
     self._loader = Loader(uid)
Beispiel #14
0
    def create(self, uid, name, mode, vrtx, parent, freq, prof, hndl, filt, disp, typ, timeout):
        if not typ:
            log_err(self, 'failed to create device, no type')
            raise Exception('Error: failed to create device')

        if mode & MODE_CLONE:
            if not parent:
                log_err(self, 'failed to create device, no parent')
                raise Exception('Error: failed to create device')
            prof = Loader(uid).get_profile(parent)
            typ = prof['type']

        if not mode & MODE_VIRT:
            timeout = None

        if not prof:
            if typ:
                driver = load_driver(typ)
                if not driver:
                    log_err(self, 'failed to create device, no %s driver' % str(typ))
                    raise Exception('Error: failed to create device')
                if mode & MODE_CLONE:
                    mode = driver.get_mode() | MODE_CLONE
                else:
                    mode = driver.get_mode()
                freq = driver.get_freq()
                prof = driver.get_profile()

        self._data.initialize(uid, name)
        self._attr.initialize(uid, name, ATTR_MODE, mode)

        if freq:
            self._attr.initialize(uid, name, ATTR_FREQ, freq)

        if filt:
            self._attr.initialize(uid, name, ATTR_FILTER, filt)

        if hndl:
            self._attr.initialize(uid, name, ATTR_HANDLER, hndl)

        if prof:
            self._attr.initialize(uid, name, ATTR_PROFILE, prof)

        if disp:
            self._attr.initialize(uid, name, ATTR_DISPATCHER, disp)

        if timeout:
            self._attr.initialize(uid, name, ATTR_TIMEOUT, timeout)

        if mode & MODE_CLONE:
            self._attr.initialize(uid, name, ATTR_PARENT, parent)

        if vrtx:
            if mode & MODE_CLONE:
                log_err(self, 'failed to create device, cannot set vertex for a cloned device')
                raise Exception('Error: failed to create device')
            self._vrtx.initialize(uid, name, vrtx)
            for v in vrtx:
                self._edge.initialize(uid, (v, name), hidden=True)

        self._manager.create(device_name(typ, name, mode), init=False)
Beispiel #15
0
 def setup(self):
     self._active = False
     self._loader = Loader(self.get_uid())
     Thread(target=create_server, args=(LO_ADDR, LO_PORT, LoServer)).start()
Beispiel #16
0
 def __init__(self, uid):
     self._mode = {}
     self._loader = Loader(uid)
Beispiel #17
0
 def __init__(self, uid, addr=PROC_ADDR):
     self._handlers = {}
     self._loader = Loader(uid)
     self._addr = addr