aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZader Zheng <[email protected]>2015-04-17 23:29:58 +0800
committerZader Zheng <[email protected]>2015-04-17 23:43:40 +0800
commit42daf5aa3be3a659c94c9214ae63820c33876e32 (patch)
treeaf787d02d16cd2bb6cb9926785b8944aa161446e
parenta710f90d7de0efff19e0fd9ba2794ef276341eb0 (diff)
refactor lab_monitor_server a little
-rw-r--r--lab_monitor_server.py64
1 files changed, 32 insertions, 32 deletions
diff --git a/lab_monitor_server.py b/lab_monitor_server.py
index 4f1439b..50f9430 100644
--- a/lab_monitor_server.py
+++ b/lab_monitor_server.py
@@ -9,20 +9,6 @@ from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application, url, StaticFileHandler
from rwlock import RWLock
-logging.basicConfig(level=logging.DEBUG,
- format='[%(levelname)-7s] (%(threadName)-10s) %(message)s',)
-MAX_LENGTH = 65536
-MAX_RECORDS = 32
-PORT = 2333
-SOCKET_PORT = 2334
-HOST = ''
-
-lock = RWLock()
-stat_res = {}
-is_exiting = threading.Event()
-reclaimed_ids = []
-id_cnt = 0
-
class ActionError(Exception):
pass
@@ -82,12 +68,6 @@ def clear_records(mesg):
stat_res[mid]["records"] = []
return ""
-action_map = {"create": add_monitor,
- "drop": del_monitor,
- "add": add_record,
- "clear": clear_records,
- "alter": alter_records}
-
def command_server():
global c, cmd_socket, is_exiting
cmd_socket = socket.socket(AF_INET, SOCK_STREAM)
@@ -129,10 +109,6 @@ def command_server():
finally:
conn.close()
-cmd = threading.Thread(target=command_server, name="local")
-cmd.setDaemon(True)
-cmd.start()
-
def cmd_shutdown():
global is_exiting, cmd_socket, local_socket_address, cmd
is_exiting.set();
@@ -151,11 +127,35 @@ class AJAXHandler(RequestHandler):
lock.release()
callback()
yield gen.Task(grab_lock, self)
-try:
- app = Application([url(r"/ajax", AJAXHandler),
- url(r'/()', StaticFileHandler, {'path': "./static/index.html"}),
- url(r'/(.*)', StaticFileHandler, {'path': "./static/"})])
- app.listen(PORT)
- IOLoop.current().start()
-except KeyboardInterrupt:
- cmd_shutdown()
+
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG,
+ format='[%(levelname)-7s] (%(threadName)-10s) %(message)s',)
+ MAX_LENGTH = 65536
+ MAX_RECORDS = 32
+ PORT = 2333
+ SOCKET_PORT = 2334
+ HOST = ''
+
+ lock = RWLock()
+ stat_res = {}
+ is_exiting = threading.Event()
+ reclaimed_ids = []
+ id_cnt = 0
+ action_map = {"create": add_monitor,
+ "drop": del_monitor,
+ "add": add_record,
+ "clear": clear_records,
+ "alter": alter_records}
+
+ cmd = threading.Thread(target=command_server, name="local")
+ cmd.setDaemon(True)
+ cmd.start()
+ try:
+ app = Application([url(r"/ajax", AJAXHandler),
+ url(r'/()', StaticFileHandler, {'path': "./static/index.html"}),
+ url(r'/(.*)', StaticFileHandler, {'path': "./static/"})])
+ app.listen(PORT)
+ IOLoop.current().start()
+ except KeyboardInterrupt:
+ cmd_shutdown()