annotate web/settings.py @ 253:0a1b642e3086

long polling config updates
author Matt Johnston <matt@ucc.asn.au>
date Mon, 08 Jun 2015 22:29:46 +0800
parents
children 03e540c3ec24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import gevent
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import fcntl
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import hashlib
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 class Settings(object):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 RAND_SIZE = 15 # 120 bits
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 """ Handles state updates from both the web UI and from the fridge client.
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 The fridge client is canonical. It provides the epoch (apart from 'startepoch'), that
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 is changed any time the fridge reloads its local config. The fridge only accepts
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 updates that have the same epoch.
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 When the web UI changes it keeps the same epoch but generates a new tag. The fridge sends
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 its current known tag and waits for it to change.
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 content is opaque, presently a dictionary of decoded json
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 def __init__(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 self.event = gevent.event.Event()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 self.contents = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 self.epoch = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.tag = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 self.update(self, None, 'startepoch')
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def wait(self, epoch_tag = None, timeout = None):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 """ returns false if the timeout was hit """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 if self.epoch_tag() != epoch_tag:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 # has alredy changed
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 return True
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return self.event.wait(timeout)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def epoch_tag(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 return '%s-%s' % (self.epoch, self.tag)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 def random(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return binascii.hexlify(os.urandom(self.RAND_SIZE))
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 def update(self, contents, epoch = None):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 """ replaces settings contents and updates waiters if changed """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 if epoch:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if self.epoch == epoch:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 return
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 else:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 self.epoch = epoch
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 self.tag = self.random()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 self.contents = contents
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 self.event.set()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 self.event.clear()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 def get(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 """ Returns (contents, epoch-tag) """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 return self.contents, self.epoch_tag()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61