| 1 | import tornado.ioloop
|
|---|
| 2 | import tornado.web
|
|---|
| 3 | import sys
|
|---|
| 4 |
|
|---|
| 5 | indices = set()
|
|---|
| 6 | min_index = sys.maxsize
|
|---|
| 7 | max_index = -1
|
|---|
| 8 |
|
|---|
| 9 | class StatusHandler(tornado.web.RequestHandler):
|
|---|
| 10 | def get(self):
|
|---|
| 11 | global indices
|
|---|
| 12 | global min_index
|
|---|
| 13 | global max_index
|
|---|
| 14 | if not indices:
|
|---|
| 15 | self.write('Not started yet')
|
|---|
| 16 | return
|
|---|
| 17 | expected = set(range(min_index, max_index + 1))
|
|---|
| 18 | diff = sorted(expected - indices)
|
|---|
| 19 | if diff:
|
|---|
| 20 | self.write(str(diff))
|
|---|
| 21 | else:
|
|---|
| 22 | self.write('OK')
|
|---|
| 23 | self.write(f'\r\nLatest received segment: {max_index}')
|
|---|
| 24 |
|
|---|
| 25 | class PutHandler(tornado.web.RequestHandler):
|
|---|
| 26 | def put(self, *args, **kwargs):
|
|---|
| 27 | global indices
|
|---|
| 28 | global min_index
|
|---|
| 29 | global max_index
|
|---|
| 30 | name = args[0]
|
|---|
| 31 | index = int(name)
|
|---|
| 32 | indices.add(index)
|
|---|
| 33 | min_index = min(min_index, index)
|
|---|
| 34 | max_index = max(max_index, index)
|
|---|
| 35 | self.write('OK')
|
|---|
| 36 |
|
|---|
| 37 | def make_app():
|
|---|
| 38 | return tornado.web.Application([
|
|---|
| 39 | (r"/status", StatusHandler),
|
|---|
| 40 | (r"/(\d+).ts$", PutHandler),
|
|---|
| 41 | ])
|
|---|
| 42 |
|
|---|
| 43 | if __name__ == "__main__":
|
|---|
| 44 | port = 45000
|
|---|
| 45 | if len(sys.argv) > 1:
|
|---|
| 46 | try:
|
|---|
| 47 | port = int(sys.argv[1])
|
|---|
| 48 | except:
|
|---|
| 49 | print("Invalid port, using default 45000")
|
|---|
| 50 | app = make_app()
|
|---|
| 51 | app.listen(port)
|
|---|
| 52 | tornado.ioloop.IOLoop.current().start()
|
|---|