2013-10-30 23:58:17 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-12-06 06:39:27 +02:00
|
|
|
"""
|
|
|
|
Set up and run the server
|
|
|
|
"""
|
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
import zmq
|
|
|
|
from zmq.eventloop import ioloop, zmqstream
|
|
|
|
ioloop.install()
|
|
|
|
|
|
|
|
import os
|
2013-12-07 02:52:16 +02:00
|
|
|
import signal
|
2013-12-06 06:39:27 +02:00
|
|
|
import errno
|
2013-12-05 09:21:06 +02:00
|
|
|
import functools
|
2013-10-30 23:58:17 +02:00
|
|
|
import socket
|
|
|
|
import tornado.ioloop
|
|
|
|
import tornado.web
|
2013-12-05 09:21:06 +02:00
|
|
|
import tornado.autoreload
|
2013-12-22 02:34:51 +02:00
|
|
|
from .handlers.jsonrpc_websocket import JSONRPCWebSocket
|
2013-12-06 06:39:27 +02:00
|
|
|
from .handlers.version_handler import VersionHandler
|
2013-12-05 09:21:06 +02:00
|
|
|
from .module_manager import ModuleManager
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2013-10-30 23:58:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Server(object):
|
|
|
|
|
|
|
|
# built-in handlers
|
2013-12-05 09:21:06 +02:00
|
|
|
handlers = [(r"/version", VersionHandler)]
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
def __init__(self, host, port, ipc=False):
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
if ipc:
|
|
|
|
self._zmq_port = 0 # this forces module to use IPC for communications
|
|
|
|
else:
|
|
|
|
self._zmq_port = port + 1 # this server port + 1
|
|
|
|
self._ipc = ipc
|
|
|
|
self._modules = []
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
def load_modules(self):
|
2013-12-06 06:39:27 +02:00
|
|
|
"""
|
2013-12-22 02:34:51 +02:00
|
|
|
Loads the modules.
|
2013-10-30 23:58:17 +02:00
|
|
|
"""
|
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
cwd = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
module_path = os.path.join(cwd, 'modules')
|
|
|
|
log.info("loading modules from {}".format(module_path))
|
|
|
|
module_manager = ModuleManager([module_path])
|
|
|
|
module_manager.load_modules()
|
|
|
|
for module in module_manager.get_all_modules():
|
|
|
|
instance = module_manager.activate_module(module, ("127.0.0.1", self._zmq_port))
|
|
|
|
self._modules.append(instance)
|
|
|
|
destinations = instance.destinations()
|
|
|
|
for destination in destinations:
|
2013-12-22 02:34:51 +02:00
|
|
|
JSONRPCWebSocket.register_destination(destination, module.name)
|
2013-12-05 09:21:06 +02:00
|
|
|
instance.start() # starts the new process
|
2013-10-30 23:58:17 +02:00
|
|
|
|
|
|
|
def run(self):
|
2013-12-05 09:21:06 +02:00
|
|
|
"""
|
2013-12-22 02:34:51 +02:00
|
|
|
Starts the Tornado web server and ZeroMQ server.
|
2013-10-30 23:58:17 +02:00
|
|
|
"""
|
|
|
|
|
2013-12-05 09:21:06 +02:00
|
|
|
router = self._create_zmq_router()
|
2013-12-22 02:34:51 +02:00
|
|
|
# Add our JSON-RPC Websocket handler to Tornado
|
|
|
|
self.handlers.extend([(r"/", JSONRPCWebSocket, dict(zmq_router=router))])
|
2013-12-05 09:21:06 +02:00
|
|
|
tornado_app = tornado.web.Application(self.handlers, debug=True) # FIXME: debug mode!
|
2013-10-30 23:58:17 +02:00
|
|
|
try:
|
2013-12-05 09:21:06 +02:00
|
|
|
print("Starting server on port {}".format(self._port))
|
|
|
|
tornado_app.listen(self._port)
|
2013-10-30 23:58:17 +02:00
|
|
|
except socket.error as e:
|
2013-12-06 06:39:27 +02:00
|
|
|
if e.errno == errno.EADDRINUSE: # socket already in use
|
2013-12-05 09:21:06 +02:00
|
|
|
logging.critical("socket in use for port {}".format(self._port))
|
2013-10-30 23:58:17 +02:00
|
|
|
raise SystemExit
|
2013-12-05 09:21:06 +02:00
|
|
|
|
|
|
|
ioloop = tornado.ioloop.IOLoop.instance()
|
|
|
|
stream = zmqstream.ZMQStream(router, ioloop)
|
2013-12-22 02:34:51 +02:00
|
|
|
stream.on_recv(JSONRPCWebSocket.dispatch_message)
|
2013-12-05 09:21:06 +02:00
|
|
|
tornado.autoreload.add_reload_hook(functools.partial(self._cleanup, stop=False))
|
|
|
|
|
2013-12-07 02:52:16 +02:00
|
|
|
def signal_handler(signum=None, frame=None):
|
|
|
|
log.warning("Got signal {}, exiting...".format(signum))
|
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
|
|
|
|
signal.signal(sig, signal_handler)
|
|
|
|
|
2013-10-30 23:58:17 +02:00
|
|
|
try:
|
2013-12-05 09:21:06 +02:00
|
|
|
ioloop.start()
|
2013-10-30 23:58:17 +02:00
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
print("\nExiting...")
|
2013-12-05 09:21:06 +02:00
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
def _create_zmq_router(self):
|
|
|
|
"""
|
|
|
|
Creates the ZeroMQ router socket to send
|
|
|
|
requests to modules.
|
|
|
|
|
2013-12-06 06:39:27 +02:00
|
|
|
:returns: ZeroMQ router socket
|
2013-12-05 09:21:06 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
context = zmq.Context()
|
|
|
|
context.linger = 0
|
|
|
|
router = context.socket(zmq.ROUTER)
|
|
|
|
if self._ipc:
|
|
|
|
try:
|
|
|
|
router.bind("ipc:///tmp/gns3.ipc")
|
|
|
|
except zmq.error.ZMQError as e:
|
|
|
|
log.critical("Could not start ZeroMQ server on ipc:///tmp/gns3.ipc, reason: {}".format(e))
|
|
|
|
self._cleanup()
|
|
|
|
raise SystemExit
|
|
|
|
log.info("ZeroMQ server listening to ipc:///tmp/gns3.ipc")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
router.bind("tcp://127.0.0.1:{}".format(self._zmq_port))
|
|
|
|
except zmq.error.ZMQError as e:
|
|
|
|
log.critical("Could not start ZeroMQ server on 127.0.0.1:{}, reason: {}".format(self._zmq_port, e))
|
|
|
|
self._cleanup()
|
|
|
|
raise SystemExit
|
|
|
|
log.info("ZeroMQ server listening to 127.0.0.1:{}".format(self._zmq_port))
|
|
|
|
return router
|
|
|
|
|
|
|
|
def _cleanup(self, stop=True):
|
|
|
|
"""
|
|
|
|
Shutdowns running module processes
|
|
|
|
and close remaining Tornado ioloop file descriptors
|
|
|
|
|
|
|
|
:param stop: Stop the ioloop if True (default)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# terminate all modules
|
|
|
|
for module in self._modules:
|
|
|
|
log.info("terminating {}".format(module.name))
|
|
|
|
module.terminate()
|
|
|
|
module.join(timeout=1)
|
|
|
|
|
|
|
|
ioloop = tornado.ioloop.IOLoop.instance()
|
|
|
|
# close any fd that would have remained open...
|
|
|
|
for fd in ioloop._handlers.keys():
|
|
|
|
try:
|
|
|
|
os.close(fd)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if stop:
|
|
|
|
ioloop.stop()
|