-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchannel.py
99 lines (76 loc) · 3.09 KB
/
channel.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from hbpush.message import Message
import logging
import time
class Channel(object):
class DoesNotExist(Exception):
pass
class Duplicate(Exception):
pass
class Gone(Exception):
pass
class NotModified(Exception):
pass
def __init__(self, id, store):
self.store = store
self.id = id
self.sentinel = None
self.subscribers = {}
# Empty message, we just want to keep etag and lastmodified data
self.last_message = Message(0, -1)
def get_last_message(self):
return self.last_message
def send_to_subscribers(self, message):
# We work on a copy to deal with reentering subscribers
subs = self.subscribers.copy()
self.subscribers = {}
nb = 0
for (id_subscriber, (cb, eb)) in subs.items():
cb(message)
nb += 1
return nb
def post(self, content_type, body, callback, errback):
def _process_message(message):
nb_subscribers = self.send_to_subscribers(message)
# This piece assumes we will always get to that callback in the order
# we posted messages
assert self.last_message < message
self.last_message = Message(message.last_modified, message.etag)
# Give back control to the handler with the result of the store
callback((message, nb_subscribers))
message = self.make_message(content_type, body)
self.store.post(self.id, message, callback=_process_message, errback=errback)
def wait_for(self, last_modified, etag, id_subscriber, callback, errback):
request_msg = Message(last_modified, etag)
def _cb(message):
if request_msg >= message:
self.subscribe(id_subscriber, _cb, errback)
else:
callback(message)
self.subscribe(id_subscriber, _cb, errback)
def subscribe(self, id_subscriber, callback, errback):
self.subscribers[id_subscriber] = (callback, errback)
def unsubscribe(self, id_subscriber):
self.subscribers.pop(id_subscriber, None)
def get(self, last_modified, etag, callback, errback):
request_msg = Message(last_modified, etag)
if request_msg < self.last_message:
self.store.get(self.id, last_modified, etag, callback=callback, errback=errback)
else:
errback(Channel.NotModified())
def delete(self, callback, errback):
for id, (cb, eb) in self.subscribers.items():
eb(Channel.Gone())
# Just for the record
self.subscribers = {}
# Delete all messages from the store
self.store.flush(self.id, callback, errback)
def make_message(self, content_type, body):
if not self.sentinel:
self.sentinel = self.get_last_message()
last_modified = int(time.time())
if last_modified == self.sentinel.last_modified:
etag = self.sentinel.etag+1
else:
etag = 0
self.sentinel = Message(last_modified, etag, content_type, body)
return self.sentinel