/
sse.py
141 lines (115 loc) · 4.44 KB
/
sse.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
import uuid
from buildbot.data.exceptions import InvalidPathError
from buildbot.util import json
from buildbot.util import toJson
from twisted.python import log
from twisted.web import resource
from twisted.web import server
class Consumer(object):
def __init__(self, request):
self.request = request
self.qrefs = {}
def stopConsuming(self, key=None):
if key is not None:
self.qrefs[key].stopConsuming()
else:
for qref in self.qrefs.values():
qref.stopConsuming()
self.qrefs = {}
def onMessage(self, event, data):
request = self.request
msg = dict(key=event, message=data)
request.write("event: " + "event" + "\n")
request.write("data: " + json.dumps(msg, default=toJson) + "\n")
request.write("\n")
def registerQref(self, path, qref):
self.qrefs[path] = qref
class EventResource(resource.Resource):
isLeaf = True
def __init__(self, master):
resource.Resource.__init__(self)
self.master = master
self.consumers = {}
def decodePath(self, path):
for i, p in enumerate(path):
if p == '*':
path[i] = None
return path
def finish(self, request, code, msg):
request.setResponseCode(code)
request.setHeader('content-type', 'text/plain; charset=utf-8')
request.write(msg)
return
def render(self, request):
command = "listen"
path = request.postpath
if path and path[-1] == '':
path = path[:-1]
if path and path[0] in ("listen", "add", "remove"):
command = path[0]
path = path[1:]
if command == "listen":
cid = str(uuid.uuid4())
consumer = Consumer(request)
elif command == "add" or command == "remove":
if path:
cid = path[0]
path = path[1:]
if cid not in self.consumers:
return self.finish(request, 400, "unknown uuid")
consumer = self.consumers[cid]
else:
return self.finish(request, 400, "need uuid")
pathref = "/".join(path)
path = self.decodePath(path)
if command == "add" or (command == "listen" and path):
options = request.args
for k in options:
if len(options[k]) == 1:
options[k] = options[k][1]
try:
d = self.master.mq.startConsuming(
consumer.onMessage,
tuple(path))
@d.addCallback
def register(qref):
consumer.registerQref(pathref, qref)
d.addErrback(log.err, "while calling startConsuming")
except NotImplementedError:
return self.finish(request, 404, "not implemented")
except InvalidPathError:
return self.finish(request, 404, "not implemented")
elif command == "remove":
try:
consumer.stopConsuming(pathref)
except KeyError:
return self.finish(request, 404, "consumer is not listening to this event")
if command == "listen":
self.consumers[cid] = consumer
request.setHeader("content-type", "text/event-stream")
request.write("")
request.write("event: handshake\n")
request.write("data: " + cid + "\n")
request.write("\n")
d = request.notifyFinish()
@d.addBoth
def onEndRequest(_):
consumer.stopConsuming()
del self.consumers[cid]
return server.NOT_DONE_YET
self.finish(request, 200, "ok")
return