Ejemplo n.º 1
0
    def test_dumps(self):
        s = parse.dumps({'ACTION': 'LIST-HASHES'})
        assert s == '(ACTION LIST-HASHES)'

        s = parse.dumps({'ACTION': 'SEHSAH-TSIL', 'HASHES': 'HEY!'})
        assert s == '(ACTION SEHSAH-TSIL) (HASHES HEY!)'

        s = parse.dumps({'ACTION': 'SEHSAH-TSIL',
                         'HASHES': {'K1': 'V1'}})
        assert s == '(ACTION SEHSAH-TSIL) (HASHES (K1 V1\\))'

        s = parse.dumps({'ACTION': 'SEHSAH-TSIL',
                         'HASHES': ['what', 'is', 'up']})
        assert s == ('(ACTION SEHSAH-TSIL) (HASHES (LENGTH 3\\) '
                     '(0 what\\) (1 is\\) (2 up\\))')
Ejemplo n.º 2
0
    def _handle_command(self, command):
        if isinstance(command, parse.ParseError):
            return parse.dumps({'ACTION': 'ERROR',
                                'REASON': 'Command is malformed'})

        in_sess = self._session.copy()

        try:
            resp, sess = self.delegate(command, in_sess)
        except parse.ParsedKeyError, e:
            resp = {'ACTION': 'ERROR',
                    'REASON': 'Missing key: `%s`' % e.missing_key}
Ejemplo n.º 3
0
    def pull_remote(self, file_id):
        """
        Grabs a file from the remote host and writes its contents to a 
        temporary file. Returns temp file path and file hash (from remote).
        """
        request = parse.dumps({'ACTION': 'PULL-FILE',
                               'ID': file_id})
        self._send(request + '\n')
        file_name = os.tmpnam() + '.mp3'

        digest = utils.pull_file(file_name, self._socket)
        return file_name, digest
Ejemplo n.º 4
0
    def communicate(self, message):
        """
        Takes a structure and sends it to the connected server, then waits for
        the server to send a response and returns the parsed response.
        """
        request = parse.dumps(message)

        logging.debug('GIVE -> %s' % request)

        self._send(request + '\n')
        response = next(self._responses)

        logging.debug('GOT  <- %s' % response)

        return response
Ejemplo n.º 5
0
    def push_command(self, command, session):
        cursor = self._conn.cursor()

        if command['TYPE'] == 'NEW':
            cursor.execute('INSERT INTO files (received) VALUES (?)', 
                           [time.time()])
            sid = cursor.lastrowid
            resp = parse.dumps({'ACTION': 'HSUP',
                                'ID': sid,
                                'DONE': 0})
            logging.debug('CONT -> %s' % resp)
            self._send(resp + '\n')
            file_path = os.path.join( 
                utils.read_settings(self._conn, 'storage_dir')['storage_dir'],
                '%d.mp3' % sid)

            digest = utils.pull_file(file_path, self._socket)
            our_digest = utils.hash_file(open(file_path, 'rb')).digest()

            if our_digest != digest:
                cursor.execute('DELETE FROM files WHERE id=?', [sid])
                resp = {'ACTION': 'ERROR',
                        'REASON': 'Hash mismatch, record revoked, retransmit'}
                self._conn.commit()
                return resp, session

            cursor.execute('UPDATE files SET path=?, hash=? WHERE id=?',
                           [file_path, digest.encode('hex'), sid])
            self._conn.commit()

            resp = {'ACTION': 'HSUP',
                    'DONE': 1}
            return resp, session

        elif command['TYPE'] == 'DELETE':
            sid = int(command['ID'])
            cursor.execute(
                'INSERT INTO deleted (file_id, del_time) VALUES (?, ?)',
                [sid, time.time()])
            cursor.execute('DELETE FROM files WHERE id=?', [sid])

            resp = {'ACTION': 'HSUP',
                    'DONE': 1}
            return resp, session
        else:
            resp = {'ACTION': 'ERROR',
                    'REASON': 'Unknown PUSH type: %s' % command['TYPE']}
            return resp, session