File: streamhandler.py

package info (click to toggle)
pushpin 1.41.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,056 kB
  • sloc: cpp: 44,382; python: 1,305; sh: 139; makefile: 73; javascript: 34; php: 27
file content (90 lines) | stat: -rw-r--r-- 2,627 bytes parent folder | download
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
# this handler responds to every request with "hello world"

import os
import tnetstring
import zmq

instance_id = "streamhandler.{}".format(os.getpid()).encode("utf-8")

ctx = zmq.Context()
in_sock = ctx.socket(zmq.PULL)
in_sock.connect("ipc://client-out")
in_stream_sock = ctx.socket(zmq.ROUTER)
in_stream_sock.identity = instance_id
in_stream_sock.connect("ipc://client-out-stream")
out_sock = ctx.socket(zmq.PUB)
out_sock.connect("ipc://client-in")

poller = zmq.Poller()
poller.register(in_sock, zmq.POLLIN)
poller.register(in_stream_sock, zmq.POLLIN)

while True:
    socks = dict(poller.poll(None))

    if socks.get(in_sock) == zmq.POLLIN:
        m_raw = in_sock.recv()
    elif socks.get(in_stream_sock) == zmq.POLLIN:
        m_list = in_stream_sock.recv_multipart()
        m_raw = m_list[2]
    else:
        continue

    req = tnetstring.loads(m_raw[1:])
    print("IN {}".format(req))

    if req.get(b"type"):
        # skip all non-data messages
        continue

    if req.get(b"uri", b"").startswith(b"ws"):
        resp = {}
        resp[b"from"] = instance_id
        resp[b"id"] = req[b"id"]
        resp[b"seq"] = 0
        resp[b"code"] = 101
        resp[b"reason"] = b"Switching Protocols"
        resp[b"credits"] = 1024

        print("OUT {} {}".format(req[b"from"], resp))
        out_sock.send(req[b"from"] + b" T" + tnetstring.dumps(resp))

        resp = {}
        resp[b"from"] = instance_id
        resp[b"id"] = req[b"id"]
        resp[b"seq"] = 1
        resp[b"body"] = b"hello world"

        print("OUT {} {}".format(req[b"from"], resp))
        out_sock.send(req[b"from"] + b" T" + tnetstring.dumps(resp))

        resp = {}
        resp[b"from"] = instance_id
        resp[b"id"] = req[b"id"]
        resp[b"seq"] = 2
        resp[b"type"] = b"close"

        print("OUT {} {}".format(req[b"from"], resp))
        out_sock.send(req[b"from"] + b" T" + tnetstring.dumps(resp))
    else:
        resp = {}
        resp[b"from"] = instance_id
        resp[b"id"] = req[b"id"]
        resp[b"seq"] = 0
        resp[b"code"] = 200
        resp[b"reason"] = b"OK"
        resp[b"headers"] = [[b"Content-Type", b"text/plain"]]
        resp[b"more"] = True
        resp[b"credits"] = 1024

        print("OUT {} {}".format(req[b"from"], resp))
        out_sock.send(req[b"from"] + b" T" + tnetstring.dumps(resp))

        resp = {}
        resp[b"from"] = instance_id
        resp[b"id"] = req[b"id"]
        resp[b"seq"] = 1
        resp[b"body"] = b"hello world\n"

        print("OUT {} {}".format(req[b"from"], resp))
        out_sock.send(req[b"from"] + b" T" + tnetstring.dumps(resp))