Polishing base server implementation

This commit is contained in:
grossmj committed 2013-12-05 21:39:27 -07:00
1 parent f4e51ea74f
commit 2f23a092e3
12 files changed
+191 -48

No files matched your search

+19 -7
View File
@@ -15,6 +15,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Base class (interface) for modules
"""
import multiprocessing
import zmq
@@ -44,16 +48,16 @@ class IModule(multiprocessing.Process):
self._current_session = None
self._current_destination = None
def setup(self):
def _setup(self):
"""
Sets up PyZMQ and creates the stream to handle requests
"""
self._context = zmq.Context()
self._ioloop = zmq.eventloop.ioloop.IOLoop.instance()
self._stream = self.create_stream(self._host, self._port, self.decode_request)
self._stream = self._create_stream(self._host, self._port, self._decode_request)
def create_stream(self, host=None, port=0, callback=None):
def _create_stream(self, host=None, port=0, callback=None):
"""
Creates a new ZMQ stream
"""
@@ -82,10 +86,10 @@ class IModule(multiprocessing.Process):
def run(self):
"""
Sets up everything and starts the event loop
Starts the event loop
"""
self.setup()
self._setup()
try:
self._ioloop.start()
except KeyboardInterrupt:
@@ -102,6 +106,8 @@ class IModule(multiprocessing.Process):
def send_response(self, response):
"""
Sends a response back to the requester
:param response:
"""
# add session and destination to the response
@@ -109,9 +115,11 @@ class IModule(multiprocessing.Process):
log.debug("ZeroMQ client ({}) sending: {}".format(self.name, response))
self._stream.send_json(response)
def decode_request(self, request):
def _decode_request(self, request):
"""
Decodes the request to JSON
:param request: request from ZeroMQ server
"""
try:
@@ -132,7 +140,9 @@ class IModule(multiprocessing.Process):
def destinations(self):
"""
Channels handled by this modules.
Destinations handled by this module.
:returns: list of destinations
"""
return self.destination.keys()
@@ -141,6 +151,8 @@ class IModule(multiprocessing.Process):
def route(cls, destination):
"""
Decorator to register a destination routed to a method
:param destination: destination to be routed
"""
def wrapper(method):
+50
View File
@@ -0,0 +1,50 @@
# -*- 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/>.
from gns3server.modules import IModule
from .hypervisor import Hypervisor
from .hypervisor_manager import HypervisorManager
from .dynamips_error import DynamipsError
from .nodes.router import Router
import logging
log = logging.getLogger(__name__)
class Dynamips(IModule):
def __init__(self, name=None, args=(), kwargs={}):
IModule.__init__(self, name=name, args=args, kwargs=kwargs)
#self._hypervisor_manager = HypervisorManager("/usr/bin/dynamips", "/tmp")
@IModule.route("dynamips/echo")
def echo(self, request):
print("Echo!")
log.debug("received request {}".format(request))
self.send_response(request)
@IModule.route("dynamips/create_vm")
def create_vm(self, request):
print("Create VM!")
log.debug("received request {}".format(request))
self.send_response(request)
@IModule.route("dynamips/start_vm")
def start_vm(self, request):
print("Start VM!")
log.debug("received request {}".format(request))
self.send_response(request)