annotate lib/twisted_sse_demo/sse_server.py @ 351:7716b1810d6c

reasoning & collector move into docker images Ignore-this: 67e97d307eba96791cbe77e57c57ad57
author drewp@bigasterisk.com
date Mon, 03 Sep 2018 00:45:34 -0700
parents service/reasoning/twisted_sse_demo/sse_server.py@14ac4a210dbc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
294
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
1 import sys
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
2
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
3 from twisted.web import server, resource
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
4 from twisted.internet import reactor
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
5 from twisted.python import log
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
6
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
7
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
8 class Root(resource.Resource):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
9 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
10 Root resource; serves JavaScript
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
11 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
12 def getChild(self, name, request):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
13 if name == '':
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
14 return self
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
15 return resource.Resource.getChild(self, name, request)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
16
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
17 def render_GET(self, request):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
18 return r"""
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
19 <html>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
20 <head>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
21 <script language="JavaScript">
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
22 eventSource = new EventSource("http://localhost:12000/subscribe");
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
23 eventSource.onmessage = function(event) {
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
24 element = document.getElementById("event-data");
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
25 element.innerHTML = event.data;
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
26 };
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
27 </script>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
28 </head>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
29 <body>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
30 <h1> Welcome to the SSE demo </h1>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
31 <h3> Event data: </h3>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
32 <p id="event-data"></p>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
33 </body>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
34 </html>
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
35 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
36
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
37
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
38 class Subscribe(resource.Resource):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
39 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
40 Implements the subscribe resource
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
41 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
42 isLeaf = True
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
43
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
44 def __init__(self):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
45 self.subscribers = set()
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
46
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
47 def render_GET(self, request):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
48 request.setHeader('Content-Type', 'text/event-stream; charset=utf-8')
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
49 request.setResponseCode(200)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
50 self.subscribers.add(request)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
51 d = request.notifyFinish()
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
52 d.addBoth(self.removeSubscriber)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
53 log.msg("Adding subscriber...")
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
54 request.write("")
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
55 return server.NOT_DONE_YET
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
56
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
57 def publishToAll(self, data):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
58 for subscriber in self.subscribers:
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
59 for line in data:
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
60 subscriber.write("data: %s\r\n" % line)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
61 # NOTE: the last CRLF is required to dispatch the event at the client
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
62 subscriber.write("\r\n")
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
63
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
64 def removeSubscriber(self, subscriber):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
65 if subscriber in self.subscribers:
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
66 log.msg("Removing subscriber..")
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
67 self.subscribers.remove(subscriber)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
68
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
69
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
70 class Publish(resource.Resource):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
71 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
72 Implements the publish resource
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
73 """
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
74 isLeaf = True
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
75
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
76 def __init__(self, subscriber):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
77 self.subscriber = subscriber
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
78
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
79 def render_POST(self, request):
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
80 if 'data' not in request.args:
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
81 request.setResponseCode(400)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
82 return "The parameter 'data' must be set\n"
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
83 data = request.args.get('data')
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
84 self.subscriber.publishToAll(data)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
85 return 'Thank you for publishing data %s\n' % '\n'.join(data)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
86
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
87
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
88 root = Root()
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
89 subscribe = Subscribe()
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
90 root.putChild('subscribe', subscribe)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
91 root.putChild('publish', Publish(subscribe))
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
92 site = server.Site(root)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
93 reactor.listenTCP(12000, site)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
94 log.startLogging(sys.stdout)
14ac4a210dbc Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff changeset
95 reactor.run()