~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-25 22:33:16 UTC
  • Revision ID: git-v1:72df4f3fdd1f779adf7358468417dbcd7f9f59db
Every block we get is now stored.

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