Merge branch 'pm_phase2' into v2.0. Amend change for NSD based on package_type rpc...
[osm/SO.git] / examples / ping_pong_ns / ping_pong_ns / start_pong.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import argparse
19 import signal
20 import logging
21
22 import tornado
23 import tornado.httpserver
24
25 from pong import (
26 Pong,
27 PongAdminStatusHandler,
28 PongServerHandler,
29 PongStatsHandler,
30 )
31 from util.util import (
32 VersionHandler,
33 )
34
35 logging.basicConfig(level=logging.DEBUG,
36 format='(%(threadName)-10s) %(name)-8s :: %(message)s',
37 )
38
39 def main():
40 log = logging.getLogger("main")
41
42 # parse arguments
43 parser = argparse.ArgumentParser()
44 parser.add_argument(
45 "--pong-manager-port",
46 required=False,
47 default="18889",
48 help="port number for pong")
49 parser.add_argument(
50 "--worker-count",
51 required=False,
52 default=5,
53 help="ip address of pong")
54
55 arguments = parser.parse_args()
56
57 # setup application
58 log.debug("setup application")
59 pong_instance = Pong(arguments.worker_count)
60 pong_application_arguments = {'pong_instance': pong_instance}
61 pong_application = tornado.web.Application([
62 (r"/version", VersionHandler, pong_application_arguments),
63 (r"/api/v1/pong/stats", PongStatsHandler, pong_application_arguments),
64 (r"/api/v1/pong/server/?([0-9a-z\.]*)", PongServerHandler, pong_application_arguments),
65 (r"/api/v1/pong/adminstatus/([a-z]+)", PongAdminStatusHandler, pong_application_arguments)
66 ])
67 pong_server = tornado.httpserver.HTTPServer(
68 pong_application)
69
70 # setup SIGINT handler
71 log.debug("setup SIGINT handler")
72 def signal_handler(signal, frame):
73 print("") # print newline to clear user input
74 log.info("Exiting")
75 pong_instance.stop()
76 pong_server.stop()
77 log.info("Sayonara!")
78 quit()
79
80 signal.signal(signal.SIGINT, signal_handler)
81
82 # start
83 log.debug("pong application listening on %s" % arguments.pong_manager_port)
84 try:
85 pong_server.listen(arguments.pong_manager_port)
86 except OSError:
87 print("port %s is already is use, exiting" % arguments.ping_manager_port)
88 return
89 tornado.ioloop.IOLoop.instance().start()
90
91 if __name__ == "__main__":
92 main()
93
94