-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
77 lines (60 loc) · 1.56 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import db
from os import listdir
from os.path import isfile, join
import netutil
from netutil import as_url
from time import sleep
from optparse import OptionParser
import traceback
class Server(object):
def __init__(self, parent):
self.db = db.DB()
self.parent = parent
def join(self, db):
self.db.addDb(db)
def route(self, uuid):
try:
my = self.db.find(uuid)
return ("DATA", my)
except KeyError:
if self.parent is not None:
return ("NEXT", self.parent)
else:
# download for ourself, for later?
return ("GM", None)
parser = OptionParser()
parser.add_option("-a", "--annonuce", dest="announce",action="store_true",
help="Lets this server parent other's", metavar="ANNOC")
(options, args) = parser.parse_args()
parent = netutil.connect_to_parent()
print 'Found a parent' , parent
s = Server(parent)
def get_local_files(path):
""" gets a { UUID : URL }
"""
return { f:as_url(f) for f in listdir(path) if isfile(join(path,f)) }
def local_update():
files = get_local_files("data")
if not files:
return
s.join(files)
if parent:
parent.join(files)
rpcs = netutil.LocalMasterServer()
rpcs.register(s.join)
rpcs.register(s.route)
rpcs.register(local_update)
rpcs.start()
try:
while True:
# remind the GM that we are a masta
if options.announce:
netutil.announce()
local_update()
sleep(10)
except (Exception, KeyboardInterrupt), e:
print e
traceback.print_exc()
print '-- Exiting.'
rpcs._Thread__stop()
raise SystemExit