~vomun-developers/anonplus/vomun-trunk

« back to all changes in this revision

Viewing changes to src/libs/storage/manager.py

  • Committer: AJ00200
  • Date: 2011-11-26 00:03:57 UTC
  • Revision ID: git-v1:f4f4352eb338a5f57a5a92e4bf23bec36d298d13
Blocks we receive are now stored in ~/.vomun/blocks.json. libs.globals and libs.config are now used directly after importing libs; they are set in libs/__init__.py now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
'''
5
5
import json
6
6
import hashlib
 
7
 
 
8
import libs
7
9
import libs.errors
8
10
import libs.events
9
11
 
15
17
        self.data = data
16
18
        self.hash = hashlib.sha256(data).hexdigest()
17
19
        # TODO: Decide on using sha256 or sha1 (or even sha512)^
18
 
        
 
20
 
19
21
class StorageDB(libs.events.Handler):
20
22
    '''An database type object to store and sort blocks.'''
21
23
    def __init__(self):
22
24
        self.usks = {}
23
25
        self.uuks = {}
24
 
        
 
26
 
25
27
    # Database methods
26
28
    def add_usk(self, block):
27
29
        self.usks[block.hash] = block
28
 
        
 
30
 
29
31
    def add_uuk(self, block):
30
32
        self.uuks[block.hash] = block
31
 
        
 
33
 
32
34
    def search(self, query):
33
35
        if query.type == 'UUK':
34
36
            if query.id in self.uuks:
36
38
        elif query.type == 'USK':
37
39
            if query.id in self.usks:
38
40
                return self.usks[query.id]
39
 
            
 
41
 
40
42
    def save(self, path):
41
43
        '''Save the database from a storage file.'''
42
44
        out_json = {}
43
45
        for block in self.uuks.values():
44
46
            out_json[block.hash] = block.data
45
 
            
 
47
 
46
48
        db_file = open(path, 'w') # TODO: possibly make a backup?
47
49
        db_file.write(json.dumps(out_json, indent = 2))
48
50
        db_file.close()
49
 
        
 
51
 
50
52
    def load(self, path):
51
53
        '''Load the database to a storage file.'''
52
54
        db_file = open(path, 'r')
53
55
        db_json = json.loads(db_file.read())
54
56
        db_file.close
55
 
        
 
57
 
56
58
        for block in db_json:
57
59
            self.uuks[block] = db_json[block]
58
 
        
 
60
 
59
61
    # Event methods
60
62
    def got_message(self, packet):
61
63
        message = packet.message
62
64
        self.add_uuk(Block(message))
63
 
        out_json = {}
64
 
        for block in self.uuks.values():
65
 
            out_json[block.hash] = block.data
66
 
        print(json.dumps(out_json, indent = 2))        
 
65
 
67
66
    def got_request(self, query):
68
67
        self.search(query)
69
 
        
 
68
 
 
69
    def shutdown(self):
 
70
        self.save(self.path)
 
71
 
70
72
class Query(object):
71
73
    def __init__(self, blocktype, id):
72
74
        self.type = blocktype
73
75
        self.id = id
74
 
        
 
76
 
75
77
 
76
78
def start():
77
79
    '''Create the storage database.'''
78
80
    database = StorageDB()
 
81
    database.path = libs.config['vomundir'] + 'blocks.json'
 
82
    print('Path is: ' + database.path)
79
83
    libs.events.register_handler(database)