Example #1
0
    def unwatch_dir(self, path, recursive=True):
        """Stop watching a directory.

        @type path: basestring
        @type cb: col.Callable
        """
        del self._path_to_callback[path]
        callInThread(self._unwatch_dir, path, recursive)
Example #2
0
    def watch_dir(self, path, cb):
        """Start watching a directory.

        @type path: basestring
        @type cb: col.Callable
        """
        self._path_to_callback[path] = cb
        callInThread(self._watch_dir, path, do_event_sending=False)
Example #3
0
    def watch_dir(self, path, cb):
        """Start watching a directory.

        @type path: basestring
        @type cb: col.Callable
        """
        self._path_to_callback[path] = cb
        callInThread(self._watch_dir, path, do_event_sending=False)
Example #4
0
    def unwatch_dir(self, path, recursive=True):
        """Stop watching a directory.

        @type path: basestring
        @type cb: col.Callable
        """
        del self._path_to_callback[path]
        callInThread(self._unwatch_dir, path, recursive)
Example #5
0
    def __heat_fs_path_in_reactor(self, base_dir, base_dir_id, old_path,
                                  new_path):
        """
        Similar to C{__heat_fs_path}, but runs definitely in reactor thread.
        """
        assert in_main_thread()

        with self.__cooling_down_to_store_lock:
            _call_to_store_fs_change = \
                self.__cooling_down_to_store.get(old_path)

            if _call_to_store_fs_change is not None:
                # Some file is being cooled down already.
                # Remove it from the collection,
                # and stop the associated callLater object.
                logger.debug('Reheating the file: %r (%r)', old_path, new_path)
                del self.__cooling_down_to_store[old_path]
                try:
                    _call_to_store_fs_change.cancel()
                except (internet_error.AlreadyCancelled,
                        internet_error.AlreadyCalled):
                    pass
            else:
                # Cooling down a completely new file..
                logger.verbose('Starting to cool down the file: %r', new_path)

            # pylint:disable=E1101,C0103
            _callLater = reactor.callLater
            # pylint:enable=E1101,C0103
            self.__cooling_down_to_store[new_path] = \
                _callLater(FILE_COOL_DOWN_TO_STORE.total_seconds(),
                           lambda: callInThread(
                                       self.__store_fs_change_after_cooling,
                                       base_dir, base_dir_id, new_path))
    def wait_for_message_for_peer(self,
                                  inh, prefer_msg_uuid, still_wait_checker,
                                  d=None):
        """Implementation of interface from C{AbstractTransactionManager}.

        @type inh: AbstractInhabitant
        @type prefer_msg_uuid: MessageUUID, NoneType
        @type still_wait_checker: col.Callable
        @type d: defer.Deferred, NoneType

        @returns: a deferred that fires with the message, or maybe
            a {error.ConnectionClosed()}.
        @rtype: defer.Deferred
        """
        assert not in_main_thread()

        if d is None:
            logger.debug('Checking for message for %r', inh)
        else:
            logger.debug('1 second passed, polling a message for %r', inh)

        d = defer.Deferred() if d is None else d

        if not still_wait_checker():
            logger.debug("Seems like we don't need to wait for %r anymore",
                         inh)
            d.errback(error.ConnectionClosed(u'No need to wait on {!r}'
                                                 .format(inh)))

        else:
            logger.debug("Let's deliver a message for %r, preferrably %r",
                         inh, prefer_msg_uuid)
            reply_msg = self.deliver_message_for_peer(inh, prefer_msg_uuid)
            assert isinstance(reply_msg, (AbstractMessage, NoneType)), \
                   repr(reply_msg)

            if reply_msg is not None:
                # We have an outgoing message for inh already!
                logger.verbose('Going to deliver a message for %r: %r',
                               inh, reply_msg)
                d.callback(reply_msg)
            else:
                # Unfortunately, we don't have a message yet.
                # Let's recall this function in, say, a second.
                logger.verbose('No messages for %r, retrying in %r',
                               inh, POLL_FOR_OUTGOING_MESSAGES_PERIOD)

                callFromThread(
                    task.deferLater,
                    reactor,
                    POLL_FOR_OUTGOING_MESSAGES_PERIOD.total_seconds(),
                    lambda: callInThread(
                                self.__wait_for_message_for_peer_ignore_result,
                                inh, prefer_msg_uuid, still_wait_checker, d))

        return d
Example #7
0
    def _when_starting_normal_mode(self):
        """Overrides method from C{HostApp}."""
        assert in_main_thread()

        super(UHostApp, self)._when_starting_normal_mode()

        _callLater = reactor.callLater  # pylint:disable=E1101,C0103

        self.__syncer_starter = \
            _callLater(START_SYNCING_AFTER.total_seconds(),
                       lambda: callInThread(self.__start_syncing))
Example #8
0
    def _when_starting_normal_mode(self):
        """Overrides method from C{HostApp}."""
        assert in_main_thread()

        super(UHostApp, self)._when_starting_normal_mode()

        _callLater = reactor.callLater  # pylint:disable=E1101,C0103

        self.__syncer_starter = \
            _callLater(START_SYNCING_AFTER.total_seconds(),
                       lambda: callInThread(self.__start_syncing))
Example #9
0
    def __init__(self, fsnotify_manager):
        """Constructor.

        @type fsnotify_manager: AbstractFSNotifyManager
        """
        self.__inner_fsnotify_manager = fsnotify_manager
        self.__watched_dir_to_cb = {}  # mapping from path to callback
        self.__implicitly_watched_dirs = set()
        self.__poll_sync_dir_timer = \
            TimerService(POLL_SYNC_DIR_FOR_SYMLINKS_PERIOD.total_seconds(),
                         lambda: callInThread(
                                     self.__on_poll_sync_dir_timer))
        self.__links_map_pair = LinksMapPair(links={}, reverse_links={})
Example #10
0
    def __init__(self, fsnotify_manager):
        """Constructor.

        @type fsnotify_manager: AbstractFSNotifyManager
        """
        self.__inner_fsnotify_manager = fsnotify_manager
        self.__watched_dir_to_cb = {}  # mapping from path to callback
        self.__implicitly_watched_dirs = set()
        self.__poll_sync_dir_timer = \
            TimerService(POLL_SYNC_DIR_FOR_SYMLINKS_PERIOD.total_seconds(),
                         lambda: callInThread(
                                     self.__on_poll_sync_dir_timer))
        self.__links_map_pair = LinksMapPair(links={}, reverse_links={})
    def wait_for_message_for_peer(self, inh, prefer_msg_uuid, still_wait_checker, d=None):
        """Implementation of interface from C{AbstractTransactionManager}.

        @type inh: AbstractInhabitant
        @type prefer_msg_uuid: MessageUUID, NoneType
        @type still_wait_checker: col.Callable
        @type d: defer.Deferred, NoneType

        @returns: a deferred that fires with the message, or maybe
            a {error.ConnectionClosed()}.
        @rtype: defer.Deferred
        """
        assert not in_main_thread()

        if d is None:
            logger.debug("Checking for message for %r", inh)
        else:
            logger.debug("1 second passed, polling a message for %r", inh)

        d = defer.Deferred() if d is None else d

        if not still_wait_checker():
            logger.debug("Seems like we don't need to wait for %r anymore", inh)
            d.errback(error.ConnectionClosed(u"No need to wait on {!r}".format(inh)))

        else:
            logger.debug("Let's deliver a message for %r, preferrably %r", inh, prefer_msg_uuid)
            reply_msg = self.deliver_message_for_peer(inh, prefer_msg_uuid)
            assert isinstance(reply_msg, (AbstractMessage, NoneType)), repr(reply_msg)

            if reply_msg is not None:
                # We have an outgoing message for inh already!
                logger.verbose("Going to deliver a message for %r: %r", inh, reply_msg)
                d.callback(reply_msg)
            else:
                # Unfortunately, we don't have a message yet.
                # Let's recall this function in, say, a second.
                logger.verbose("No messages for %r, retrying in %r", inh, POLL_FOR_OUTGOING_MESSAGES_PERIOD)

                callFromThread(
                    task.deferLater,
                    reactor,
                    POLL_FOR_OUTGOING_MESSAGES_PERIOD.total_seconds(),
                    lambda: callInThread(
                        self.__wait_for_message_for_peer_ignore_result, inh, prefer_msg_uuid, still_wait_checker, d
                    ),
                )

        return d
Example #12
0
 def _on_path_changed(self, path):
     try:
         mode = os.stat(path).st_mode
     except OSError:
         logger.debug('Path does not exist, %r', path)
         # try to unwatch this path as if it was dir..
         callInThread(self._unwatch_dir, path, True)
     else:
         logger.debug('Received event about path %r', path)
         if stat.S_ISDIR(mode):
             callInThread(self._handle_changes_in_dir, path)
         elif stat.S_ISREG(mode):
             callInThread(self._handle_changes_in_file, path)
         else:
             logger.debug('I do not know what to do with fh with mode %r',
                          mode)
Example #13
0
 def _on_path_changed(self, path):
     try:
         mode = os.stat(path).st_mode
     except OSError:
         logger.debug('Path does not exist, %r', path)
         # try to unwatch this path as if it was dir..
         callInThread(self._unwatch_dir, path, True)
     else:
         logger.debug('Received event about path %r', path)
         if stat.S_ISDIR(mode):
             callInThread(self._handle_changes_in_dir, path)
         elif stat.S_ISREG(mode):
             callInThread(self._handle_changes_in_file, path)
         else:
             logger.debug('I do not know what to do with fh with mode %r',
                          mode)
Example #14
0
    def __heat_fs_path_in_reactor(self,
                                  base_dir, base_dir_id, old_path, new_path):
        """
        Similar to C{__heat_fs_path}, but runs definitely in reactor thread.
        """
        assert in_main_thread()

        with self.__cooling_down_to_store_lock:
            _call_to_store_fs_change = \
                self.__cooling_down_to_store.get(old_path)

            if _call_to_store_fs_change is not None:
                # Some file is being cooled down already.
                # Remove it from the collection,
                # and stop the associated callLater object.
                logger.debug('Reheating the file: %r (%r)',
                             old_path, new_path)
                del self.__cooling_down_to_store[old_path]
                try:
                    _call_to_store_fs_change.cancel()
                except (internet_error.AlreadyCancelled,
                        internet_error.AlreadyCalled):
                    pass
            else:
                # Cooling down a completely new file..
                logger.verbose('Starting to cool down the file: %r',
                               new_path)

            # pylint:disable=E1101,C0103
            _callLater = reactor.callLater
            # pylint:enable=E1101,C0103
            self.__cooling_down_to_store[new_path] = \
                _callLater(FILE_COOL_DOWN_TO_STORE.total_seconds(),
                           lambda: callInThread(
                                       self.__store_fs_change_after_cooling,
                                       base_dir, base_dir_id, new_path))