Esempio n. 1
0
    def handle_read(self):
        """
        Stream incoming messages into MessagePack, and action them when ready.
        """

        try:
            self.unpacker.feed(self.recv(1))

            for message in self.unpacker:
                log.info("%s received: %s", self.name, message)

                # Attempt to call the requested method on the proxy.
                #
                try:
                    method = message.keys()[0]

                    try:
                        getattr(self.proxy, method)(message[method])

                    except AttributeError as excp:
                        log.error("Failed to call method %s: %s", method, excp)

                except AttributeError as excp:
                    log.error("Failed to call a method: %s", excp)

        except socket.error as excp:
            log.error("Failed to receive: %s", excp)
Esempio n. 2
0
    def select_new(self):
        log.info("Perfoming new media select")

        with self.database:
            data = self.database.select_media_by_modified()

        return data
Esempio n. 3
0
    def select_like_media(self, term):
        log.info("Perfoming search with term: %s", term)

        with self.database:
            data = self.database.select_media_like_term(term)

        return data
Esempio n. 4
0
    def select_new(self):
        log.info("Perfoming new media select")

        with self.database:
            data = self.database.select_media_by_modified()

        return data
Esempio n. 5
0
    def _open_connection(self):
        log.info("Opening database connection")

        self.connection = sqlite3.connect(self.database)
        self.connection.text_factory = str
        self.connection.row_factory = self._dictionary_factory
        self.cursor = self.connection.cursor()
Esempio n. 6
0
    def select_all_media(self):
        log.info("Perfoming all media select")

        with self.database:
            data = self.database.select_media()

        return data
Esempio n. 7
0
    def select_all_media(self):
        log.info("Perfoming all media select")

        with self.database:
            data = self.database.select_media()

        return data
Esempio n. 8
0
    def select_like_media(self, term):
        log.info("Perfoming search with term: %s", term)

        with self.database:
            data = self.database.select_media_like_term(term)

        return data
Esempio n. 9
0
    def _open_connection(self):
        log.info("Opening database connection")

        self.connection = sqlite3.connect(self.database)
        self.connection.text_factory = str
        self.connection.row_factory = self._dictionary_factory
        self.cursor = self.connection.cursor()
Esempio n. 10
0
    def handle_read(self):
        """
        Stream incoming messages into MessagePack and action them when ready.
        """

        self.unpacker.feed(self.recv(1))

        for message in self.unpacker:
            if not self.name:
                # The first message received from a new connection will be the
                # name of the client, which we can store and use in future to
                # access this connection.
                #
                try:
                    Communicate.connections[message] = self
                    self.name = message

                    log.info("%s connected", self.name)

                except TypeError as excp:
                    log.error("Failed to set name: %s", excp)

            else:
                log.info("Received from %s: %s", self.name, message)

                # Attempt to call the requested method on the proxy.
                #
                method = message.keys()[0]

                try:
                    getattr(CommunicationServer.proxy, method)(message[method])

                except AttributeError as excp:
                    log.error("Failed to call method %s: %s", method, excp)
Esempio n. 11
0
    def handle_close(self):
        self.close()
        self.connected = False
        log.info("CommunicationClient closed")

        time.sleep(5)
        log.warn("Attempting to reconnect to server")
        self._connect()
Esempio n. 12
0
    def select_latest_viewed_by_show(self, show):
        log.info("Perfoming latest viewed select for show: %s", show)

        with self.database:
            data = self.database.select_latest_viewed_by_show(show)

        if data:
            return data["id"]
Esempio n. 13
0
    def select_latest_viewed_by_show(self, show):
        log.info("Perfoming latest viewed select for show: %s", show)

        with self.database:
            data = self.database.select_latest_viewed_by_show(show)

        if data:
            return data["id"]
Esempio n. 14
0
    def __init__(self, client, proxy, host, port, name):
        super(CommunicationThread, self).__init__()

        log.info("CommunicationThread initialised")

        self.handler = CommunicationHandler(client, proxy, host, port, name)
        CommunicationThread.packer = self.handler.packer
        self.run = self.handler.run
        self._send_client = self.handler._send_client
        self._send_server = self.handler._send_server
Esempio n. 15
0
    def __init__(self, proxy, host, port, name, qthread=False):
        asyncore.dispatcher.__init__(self)

        log.info("CommunicationClient initialised")

        self.unpacker = msgpack.Unpacker()
        self.proxy = proxy()
        self.host = host
        self.port = port
        self.name = name
        self.qthread = qthread
        self._connect()
Esempio n. 16
0
    def select_viewed(self, media_id=None):
        log.info("Perfoming viewed media select")

        with self.database:
            if media_id:
                data = self.database.select_viewed_by_id(media_id)

                if data:
                    data = data[0]

            else:
                data = self.database.select_viewed()

        return data
Esempio n. 17
0
    def select_viewed(self, media_id=None):
        log.info("Perfoming viewed media select")

        with self.database:
            if media_id:
                data = self.database.select_viewed_by_id(media_id)

                if data:
                    data = data[0]

            else:
                data = self.database.select_viewed()

        return data
Esempio n. 18
0
    def _send_server(self, names, message):
        """
        Send a message to the named connection(s).
        """

        try:
            for name in names:
                connection = Communicate.connections[name]
                connection.send(self.packer.pack(message))

                log.info("Sent to %s: %s", name, message)

        except (KeyError, RuntimeError) as excp:
            log.error("Send failed: %s", excp)

            return False

        return True
Esempio n. 19
0
    def __init__(self, proxy, host, port):
        asyncore.dispatcher.__init__(self)

        log.info("CommunicationServer initialised")

        CommunicationServer.proxy = proxy()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()

        try:
            self.bind((host, port))

        except socket.error as excp:
            log.error("Failed to bind socket: %s", excp)

            raise

        log.warn("Listening on port: %s", port)

        self.listen(5)
Esempio n. 20
0
    def select_media(self, media_id):
        log.info("Perfoming media ID select: %s", media_id)

        data = {}

        for key in self._cache.keys():
            data = self._cache[key].get(media_id, {})

            if data:
                break

        if data:
            log.info("Returning media select from cache")

            return data

        with self.database:
            data = self.database.select_media_by_id(media_id)

        if data.get(media_id):
            category = data[media_id]["category"]

            if not self._cache.get(category):
                self._cache[category] = {}

            self._cache[category][media_id] = data[media_id]

        log.info("Returning media select from database")

        return data.get(media_id)
Esempio n. 21
0
    def select_media(self, media_id):
        log.info("Perfoming media ID select: %s", media_id)

        data = {}

        for key in self._cache.keys():
            data = self._cache[key].get(media_id, {})

            if data:
                break

        if data:
            log.info("Returning media select from cache")

            return data

        with self.database:
            data = self.database.select_media_by_id(media_id)

        if data.get(media_id):
            category = data[media_id]["category"]

            if not self._cache.get(category):
                self._cache[category] = {}

            self._cache[category][media_id] = data[media_id]

        log.info("Returning media select from database")

        return data.get(media_id)
Esempio n. 22
0
    def select_next_tracks(self, media_id):
        log.info("Perfoming select next tracks for media: %s", media_id)

        try:
            media_id = int(media_id)

            with self.database:
                data = self.database.select_media_by_id(media_id)[media_id]

        except Exception as excp:
            log.error("Select next tracks failed: %s", excp)

            return []

        if data["category"] != "Music":
            return []

        artist = data["name_one"]
        album = data["name_two"]

        with self.database:
            data = self.database.select_tracks(artist, album)

        return data
Esempio n. 23
0
    def select_next_tracks(self, media_id):
        log.info("Perfoming select next tracks for media: %s", media_id)

        try:
            media_id = int(media_id)

            with self.database:
                data = self.database.select_media_by_id(media_id)[media_id]

        except Exception as excp:
            log.error("Select next tracks failed: %s", excp)

            return []

        if data["category"] != "Music":
            return []

        artist = data["name_one"]
        album = data["name_two"]

        with self.database:
            data = self.database.select_tracks(artist, album)

        return data
Esempio n. 24
0
    def _send_client(self, message):
        """
        Check for an active connection and then send a message to the server.
        """

        try:
            # Check that we are connected.
            if self.connection.connected:
                # Write the message to the buffer so that it will be sent.
                self.connection.buffer_ = self.packer.pack(message)

                log.info("%s buffered: %s", self.name, message)

            else:
                log.error("Send failed: No active connection found")

                return False

        except AttributeError as excp:
            log.error("Send failed: %s", excp)

            return False

        return True
Esempio n. 25
0
    def select_category(self, category):
        log.info("Perfoming category select: %s", category)

        data = self._cache.get(category, {})

        if data:
            log.info("Returning category select from cache")

            return data

        with self.database:
            data = self.database.select_media_by_category(category)

        self._cache[category] = data

        log.info("Returning category select from database")

        return data
Esempio n. 26
0
    def select_category(self, category):
        log.info("Perfoming category select: %s", category)

        data = self._cache.get(category, {})

        if data:
            log.info("Returning category select from cache")

            return data

        with self.database:
            data = self.database.select_media_by_category(category)

        self._cache[category] = data

        log.info("Returning category select from database")

        return data
Esempio n. 27
0
    def empty_queue(self):
        log.info("Emptying queue")

        self._queue = []
Esempio n. 28
0
    def _close_connection(self):
        log.info("Closing database connection")

        self.connection.commit()
        self.cursor.close()
        self.connection.close()
Esempio n. 29
0
    def _close_connection(self):
        log.info("Closing database connection")

        self.connection.commit()
        self.cursor.close()
        self.connection.close()