annotate web/settings.py @ 586:87c20b8c5472 default master

port to python3
author Matt Johnston <matt@ucc.asn.au>
date Mon, 09 Sep 2019 22:24:10 +0800
parents 03e540c3ec24
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
586
87c20b8c5472 port to python3
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
1 import gevent.event
253
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
258
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
4 import binascii
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
5 import os
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
6
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 class Settings(object):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 RAND_SIZE = 15 # 120 bits
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 """ 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
11 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
12 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
13 updates that have the same epoch.
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 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
16 its current known tag and waits for it to change.
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 content is opaque, presently a dictionary of decoded json
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 def __init__(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 self.event = gevent.event.Event()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.contents = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 self.epoch = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 self.tag = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
258
03e540c3ec24 fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
27 self.update(None, 'startepoch')
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 def wait(self, epoch_tag = None, timeout = None):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 """ returns false if the timeout was hit """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 if self.epoch_tag() != epoch_tag:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 # has alredy changed
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 return True
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 return self.event.wait(timeout)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 def epoch_tag(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 return '%s-%s' % (self.epoch, self.tag)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 def random(self):
586
87c20b8c5472 port to python3
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
40 return os.urandom(self.RAND_SIZE).hex()
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 def update(self, contents, epoch = None):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 """ replaces settings contents and updates waiters if changed """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 if epoch:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if self.epoch == epoch:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 else:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 self.epoch = epoch
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 self.tag = self.random()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 self.contents = contents
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 self.event.set()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 self.event.clear()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 def get(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 """ Returns (contents, epoch-tag) """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 return self.contents, self.epoch_tag()
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
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63