aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_router/moon_secrouter/messenger.py
blob: 52e5c3412927f932bded93d66ae0dd977b701e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

from oslo_config import cfg
import oslo_messaging
import time
from oslo_log import log as logging
from moon_secrouter.api.generic import Status, Logs
from moon_secrouter.api.route import Router
from moon_utilities.api import APIList

LOG = logging.getLogger(__name__)


class Server:

    TOPIC = "security_router"

    def __init__(self, add_master_cnx=False):
        if add_master_cnx and cfg.CONF.slave.master_url:
            self.transport = oslo_messaging.get_transport(cfg.CONF, cfg.CONF.slave.master_url)
            self.TOPIC = self.TOPIC + "_" + cfg.CONF.slave.slave_name
        else:
            self.transport = oslo_messaging.get_transport(cfg.CONF)
        self.target = oslo_messaging.Target(topic=self.TOPIC, server='server1')
        LOG.info("Starting MQ server with topic: {}".format(self.TOPIC))
        self.endpoints = [
            APIList((Status, Logs, Router)),
            Status(),
            Logs(),
            Router(add_master_cnx)
        ]
        self.server = oslo_messaging.get_rpc_server(self.transport, self.target, self.endpoints,
                                                    executor='threading',
                                                    access_policy=oslo_messaging.DefaultRPCAccessPolicy)
        self.__is_alive = False

    def stop(self):
        self.__is_alive = False
        self.endpoints[-1].delete()

    def run(self):
        try:
            self.__is_alive = True
            self.server.start()
            while True:
                if self.__is_alive:
                    time.sleep(1)
                else:
                    break
        except KeyboardInterrupt:
            print("Stopping server by crtl+c")
        except SystemExit:
            print("Stopping server with SystemExit")
        print("Stopping server")

        self.server.stop()
        self.server.wait()