Skip to content

Commit

Permalink
Minor code optimizations and formatting edits to web server
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestaylr committed Aug 27, 2015
1 parent 591bd26 commit c3f5eeb
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/modules/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@

logger = logging.getLogger('log')

wss = []

target_locations = []
boundary_locations = []
http_server = None
main_loop = None
wss = target_locations = boundary_locations = []
http_server = main_loop = None

class WSHandler(tornado.websocket.WebSocketHandler):

""" Creates the web socket server
"""

def check_origin(self, origin):
return True

Expand All @@ -38,7 +31,7 @@ def on_message(self, message):
logger.info('Received message: %s' % message)

def on_close(self):
logger.info('Connection closed.')
logger.info('Connection closed')
if self in wss:
wss.remove(self)

Expand All @@ -51,9 +44,11 @@ def get(self):
class Application(tornado.web.Application):
def __init__(self):
handlers = [(r'/ws', WSHandler), (r'/', IndexHandler)]
settings = {'debug': True,
'static_path': os.path.join(os.path.dirname(__file__),
'../web')}
settings = {
'debug': True,
'static_path': os.path.join(os.path.dirname(__file__),
'../web')
}
tornado.web.Application.__init__(self, handlers, **settings)

application = Application()
Expand All @@ -69,14 +64,14 @@ def send_data(self, message):
logger.error('Tried to send invalid value.')

def close_sockets(self):
logger.info('Closing all connections....')
logger.info('Closing all connections...')
for ws in wss:
ws.close()

def run(self):
global target_locations, boundary_locations

logger.info('Starting server.')
logger.info('Starting server')

# Defining the locations array
target_locations = self._kwargs['target_locations']
Expand All @@ -87,7 +82,6 @@ def run(self):

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(int(self._kwargs['port']))

main_loop = tornado.ioloop.IOLoop.instance()

def shutdown():
Expand All @@ -97,13 +91,9 @@ def shutdown():
http_server.stop()
main_loop.stop()

scheduler = tornado.ioloop.PeriodicCallback(shutdown, 1000, io_loop = main_loop)
scheduler = tornado.ioloop.PeriodicCallback(shutdown, 500, io_loop = main_loop)
scheduler.start()

logger.info('The web server successfully bound to port %d'
% self._kwargs['port'])

# Starts the main IO loop
logger.info('The web server successfully bound to port %d' % self._kwargs['port'])
main_loop.start()

except OSError:
Expand Down

0 comments on commit c3f5eeb

Please sign in to comment.