Beispiel #1
0
class service:
    def __init__(self):
        self.ioloop = ZMQIOLoop()
        self.ioloop.install()
        return

    def process_message_two(self, msg):
        print "get thread two message"
        print "processing .....", msg
        return

    def process_message_three(self, msg):
        print "get thread three message"
        print "processing......", msg
        return

    def timeout(self):
        print "thread one timeout"
        data = {}
        data['thread'] = 'one'
        self.socket_to_others.send(zmqconfig.one_to_two_subject, zmq.SNDMORE)
        self.socket_to_others.send(json.dumps(data))
        self.socket_to_others.send(zmqconfig.one_to_three_subject, zmq.SNDMORE)
        self.socket_to_others.send(json.dumps(data))
        self.ioloop.add_timeout(time.time() + 3, self.timeout)
        return

    def run(self):
        self.socket_to_others = zmqconfig.context.socket(zmq.PUB)
        self.socket_to_others.bind(zmqconfig.one_zmq_addr)
        self.socket_from_two = zmqconfig.context.socket(zmq.SUB)
        self.socket_from_two.connect(zmqconfig.two_zmq_addr)
        self.socket_from_two.setsockopt(zmq.SUBSCRIBE,
                                        zmqconfig.two_to_one_subject)
        self.stream_from_two_sub = zmqstream.ZMQStream(self.socket_from_two)
        self.stream_from_two_sub.on_recv(self.process_message_two)
        self.socket_from_three = zmqconfig.context.socket(zmq.SUB)
        self.socket_from_three.connect(zmqconfig.three_zmq_addr)
        self.socket_from_three.setsockopt(zmq.SUBSCRIBE,
                                          zmqconfig.three_to_one_subject)
        self.socket_from_three_sub = zmqstream.ZMQStream(
            self.socket_from_three)
        self.socket_from_three_sub.on_recv(self.process_message_three)
        self.ioloop.add_timeout(time.time(), self.timeout)
        application = tornado.web.Application(urls)
        application.listen(8887)
        self.ioloop.start()
        return
class service:

    def __init__(self):
        self.ioloop = ZMQIOLoop()
        self.ioloop.install()
        return

    def process_message_two(self, msg):
        print "get thread two message"
        print "processing .....", msg
        return

    def process_message_three(self, msg):
        print "get thread three message"
        print "processing......", msg
        return

    def timeout(self):
        print "thread one timeout"
        data = {}
        data['thread'] = 'one'
        self.socket_to_others.send(zmqconfig.one_to_two_subject, zmq.SNDMORE)
        self.socket_to_others.send(json.dumps(data))
        self.socket_to_others.send(zmqconfig.one_to_three_subject, zmq.SNDMORE)
        self.socket_to_others.send(json.dumps(data))
        self.ioloop.add_timeout(time.time() + 3, self.timeout)
        return

    def run(self):
        self.socket_to_others = zmqconfig.context.socket(zmq.PUB)
        self.socket_to_others.bind(zmqconfig.one_zmq_addr)
        self.socket_from_two = zmqconfig.context.socket(zmq.SUB)
        self.socket_from_two.connect(zmqconfig.two_zmq_addr)
        self.socket_from_two.setsockopt(zmq.SUBSCRIBE, zmqconfig.two_to_one_subject)
        self.stream_from_two_sub = zmqstream.ZMQStream(self.socket_from_two)
        self.stream_from_two_sub.on_recv(self.process_message_two)
        self.socket_from_three = zmqconfig.context.socket(zmq.SUB)
        self.socket_from_three.connect(zmqconfig.three_zmq_addr)
        self.socket_from_three.setsockopt(zmq.SUBSCRIBE, zmqconfig.three_to_one_subject)
        self.socket_from_three_sub = zmqstream.ZMQStream(self.socket_from_three)
        self.socket_from_three_sub.on_recv(self.process_message_three)
        self.ioloop.add_timeout(time.time(), self.timeout)
        application = tornado.web.Application(urls)
        application.listen(8887)
        self.ioloop.start()
        return
Beispiel #3
0
    def _start_ipython(self):
        from IPython import get_ipython
        if get_ipython() is not None:
            raise RuntimeError("Cannot start IPython, it's already running.")

        from zmq.eventloop.ioloop import ZMQIOLoop
        from ipykernel.kernelapp import IPKernelApp
        # save the global IOLoop instance
        # since IPython relies on it, but we are going to put it in a thread.
        save_inst = IOLoop.instance()
        IOLoop.clear_instance()
        zmq_loop = ZMQIOLoop()
        zmq_loop.install()

        # start IPython, disabling its signal handlers that won't work due to running in a thread:
        app = self._ipython_kernel = IPKernelApp.instance(log=logger)
        # Don't connect to the history database
        app.config.HistoryManager.hist_file = ':memory:'
        # listen on all interfaces, so remote clients can connect:
        app.ip = self.ip
        app.init_signal = lambda : None
        app.initialize([])
        app.kernel.pre_handler_hook = lambda : None
        app.kernel.post_handler_hook = lambda : None
        app.kernel.start()

        # save self in the IPython namespace as 'worker'
        app.kernel.shell.user_ns['worker'] = self

        # start IPython's IOLoop in a thread
        zmq_loop_thread = Thread(target=zmq_loop.start)
        zmq_loop_thread.start()

        # put the global IOLoop instance back:
        IOLoop.clear_instance()
        save_inst.install()
        return app
Beispiel #4
0
def start_ipython(ip=None, ns=None, log=None):
    """Start an IPython kernel in a thread

    Parameters
    ----------

    ip: str
        The IP address to listen on (likely the parent object's ip).
    ns: dict
        Any names that should be injected into the IPython namespace.
    log: logger instance
        Hook up IPython's logging to an existing logger instead of the default.
    """
    from IPython import get_ipython

    if get_ipython() is not None:
        raise RuntimeError("Cannot start IPython, it's already running.")

    from zmq.eventloop.ioloop import ZMQIOLoop
    from ipykernel.kernelapp import IPKernelApp

    # save the global IOLoop instance
    # since IPython relies on it, but we are going to put it in a thread.
    save_inst = IOLoop.instance()
    IOLoop.clear_instance()
    zmq_loop = ZMQIOLoop()
    zmq_loop.install()

    # start IPython, disabling its signal handlers that won't work due to running in a thread:
    app = IPKernelApp.instance(log=log)
    # Don't connect to the history database
    app.config.HistoryManager.hist_file = ":memory:"
    # listen on all interfaces, so remote clients can connect:
    if ip:
        app.ip = ip
    # disable some signal handling, logging

    def noop():
        return None

    app.init_signal = noop
    app.log_connection_info = noop

    # start IPython in a thread
    # initialization happens in the thread to avoid threading problems
    # with the sqlite history
    evt = Event()

    def _start():
        app.initialize([])
        app.kernel.pre_handler_hook = noop
        app.kernel.post_handler_hook = noop
        app.kernel.start()
        app.kernel.loop = IOLoop.instance()
        # save self in the IPython namespace as 'worker'
        # inject things into the IPython namespace
        if ns:
            app.kernel.shell.user_ns.update(ns)
        evt.set()
        zmq_loop.start()

    zmq_loop_thread = Thread(target=_start)
    zmq_loop_thread.daemon = True
    zmq_loop_thread.start()
    assert evt.wait(timeout=5), "IPython didn't start in a reasonable amount of time."

    # put the global IOLoop instance back:
    IOLoop.clear_instance()
    save_inst.install()
    return app
Beispiel #5
0
# coding: utf8

import getopt
import tornado.ioloop
import tornado.web
from tornado.log import app_log
from zmq.eventloop.zmqstream import ZMQStream
from zmq.eventloop.ioloop import ZMQIOLoop

loop = ZMQIOLoop()
loop.install()

from lib.autoconf import *
from lib.path import *
from lib.log import *
from biz.core import *

'''
    conf_drawer是一个配置管理器的实例,类型Config定义在lib.autoconf中,register_my_setup
    装饰器会在加载文件的时候执行,把各个文件的setup函数append到其setups列表中去,所以理论上它
    先于main函数的执行,而main函数会调用conf_drawer的setup函数,其中就是遍历注册的setups列
    执行注册过来的各种setup函数
'''
@conf_drawer.register_my_setup(look='push')
def all_start(pcc):
    files_list = os.listdir(BIZ_PATH)
    files_list = set(['biz.' + x[:x.rfind(".")] for x in files_list if x.endswith(".py")])
    map(__import__, files_list)
    Hubber(pcc['ihq'])  # subscirbe

class Hubber(object):
Beispiel #6
0
                    dict(status='error',
                         message='Session error,please refresh')))

        self.finish()


if __name__ == '__main__':
    options.define("p", default=7777, help="run on the given port", type=int)
    options.parse_command_line()
    config = Configurations()
    port = options.options.p

    logger = get_logger("server", logging.DEBUG)

    loop = ZMQIOLoop()
    loop.install()
    context = zmq.Context()
    zmq_publish = context.socket(zmq.PUB)
    zmq_publish.bind("tcp://127.0.0.1:%s" %
                     str(config.get_configuration("zmqPublish")))
    zmq_dispatch = context.socket(zmq.REP)
    zmq_dispatch.bind("tcp://127.0.0.1:%s" %
                      str(config.get_configuration("zmqDispatch")))
    zmq_result = context.socket(zmq.PULL)
    zmq_result.bind("tcp://127.0.0.1:%s" %
                    str(config.get_configuration("zmqResult")))
    receiver = ZMQStream(zmq_result)
    receiver.on_recv(on_worker_data_in)

    cli_device_dict = {}
    snmp_device_dict = {}
Beispiel #7
0
def start_ipython(ip=None, ns=None, log=None):
    """Start an IPython kernel in a thread

    Parameters
    ----------

    ip: str
        The IP address to listen on (likely the parent object's ip).
    ns: dict
        Any names that should be injected into the IPython namespace.
    log: logger instance
        Hook up IPython's logging to an existing logger instead of the default.
    """
    from IPython import get_ipython
    if get_ipython() is not None:
        raise RuntimeError("Cannot start IPython, it's already running.")

    from zmq.eventloop.ioloop import ZMQIOLoop
    from ipykernel.kernelapp import IPKernelApp
    # save the global IOLoop instance
    # since IPython relies on it, but we are going to put it in a thread.
    save_inst = IOLoop.instance()
    IOLoop.clear_instance()
    zmq_loop = ZMQIOLoop()
    zmq_loop.install()

    # start IPython, disabling its signal handlers that won't work due to running in a thread:
    app = IPKernelApp.instance(log=log)
    # Don't connect to the history database
    app.config.HistoryManager.hist_file = ':memory:'
    # listen on all interfaces, so remote clients can connect:
    if ip:
        app.ip = ip
    # disable some signal handling, logging
    noop = lambda : None
    app.init_signal = noop
    app.log_connection_info = noop

    # start IPython in a thread
    # initialization happens in the thread to avoid threading problems
    # with the sqlite history
    evt = Event()
    def _start():
        app.initialize([])
        app.kernel.pre_handler_hook = noop
        app.kernel.post_handler_hook = noop
        app.kernel.start()
        app.kernel.loop = IOLoop.instance()
        # save self in the IPython namespace as 'worker'
        # inject things into the IPython namespace
        if ns:
            app.kernel.shell.user_ns.update(ns)
        evt.set()
        zmq_loop.start()

    zmq_loop_thread = Thread(target=_start)
    zmq_loop_thread.daemon = True
    zmq_loop_thread.start()
    assert evt.wait(timeout=5), "IPython didn't start in a reasonable amount of time."

    # put the global IOLoop instance back:
    IOLoop.clear_instance()
    save_inst.install()
    return app
Beispiel #8
0
class service:
    def __init__(self):
        self.ioloop = ZMQIOLoop()
        self.ioloop.install()
        self.clients = {}
        self.in_client = "server"
        return

    def process_message_client(self, msg):
        body = json.loads(bytes.decode(msg[1]))
        # from client heart
        if body["type"] == "heart":
            clients = json.loads(bytes.decode(msg[0]))
            ip = clients["ip"]
            if ip not in self.clients:
                self.clients[ip] = {}
                self.clients[ip]["time"] = time.time()
                self.clients[ip]["tag"] = bytes.decode(msg[0])
            else:
                self.clients[ip]["time"] = time.time()

            pass
        # from input t
        elif body["type"] == "cmd":
            # a = os.system(body["cmd"])
            ip_cmd = body["ip_cmd"]
            if self.in_client == "server":  # input to server
                temp_ip_cmd = body["ip_cmd"].split(" ", 1)
                if temp_ip_cmd[0] == "ls":
                    message = {}
                    message["type"] = "server_message"
                    if self.clients != {}:
                        # del overdate
                        for ip in self.clients:
                            if time.time() - self.clients[ip]["time"] > float(
                                    server_config.delay):
                                self.clients[ip]["time"] = 0
                        message["clients"] = self.clients
                    else:
                        message["clients"] = {}
                    self.socket_to_others.send_string(
                        server_config.server_to_input_subject, zmq.SNDMORE)
                    self.socket_to_others.send_string(json.dumps(message))
                elif temp_ip_cmd[0] == "ssh":
                    if temp_ip_cmd[1] in self.clients:
                        self.in_client = self.clients[temp_ip_cmd[1]]['tag']
                        message = {}
                        message["type"] = "state"
                        message["state"] = temp_ip_cmd[1]
                        self.socket_to_others.send_string(
                            server_config.server_to_input_subject, zmq.SNDMORE)
                        self.socket_to_others.send_string(json.dumps(message))
                    else:
                        message = {}
                        message["type"] = "error"
                        message["error"] = "no client is %s" % temp_ip_cmd[1]
                        self.socket_to_others.send_string(
                            server_config.server_to_input_subject, zmq.SNDMORE)
                        self.socket_to_others.send_string(json.dumps(message))

                else:
                    message = {}
                    message["type"] = "error"
                    message["error"] = "sorry, server no this cmd"
                    self.socket_to_others.send_string(
                        server_config.server_to_input_subject, zmq.SNDMORE)
                    self.socket_to_others.send_string(json.dumps(message))
                    return

            else:  # input to client
                temp_ip_cmd = body["ip_cmd"].split(" ", 1)
                if temp_ip_cmd[0] == "exit":
                    self.in_client = "server"
                    message = {}
                    message["type"] = "state"
                    message["state"] = "server"
                    self.socket_to_others.send_string(
                        server_config.server_to_input_subject, zmq.SNDMORE)
                    self.socket_to_others.send_string(json.dumps(message))
                else:
                    try:
                        tag = self.in_client
                        self.socket_to_others.send_string(tag, zmq.SNDMORE)
                        self.socket_to_others.send_string(
                            json.dumps({
                                "type": "cmd",
                                "cmd": ip_cmd
                            }))
                    except Exception as err:
                        message = {}
                        message["type"] = "error"
                        message["error"] = "unexcept error is occur!"
                        self.socket_to_others.send_string(
                            server_config.server_to_input_subject, zmq.SNDMORE)
                        self.socket_to_others.send_string(json.dumps(message))
        # from client cmd_result
        elif body["type"] == "cmd_result":
            self.socket_to_others.send_string(
                server_config.server_to_input_subject, zmq.SNDMORE)
            self.socket_to_others.send_string(json.dumps(body))
        return

    def timeout(self):

        # self.socket_to_others.send_string(zmqconfig.server_to_client_subject, zmq.SNDMORE)
        # self.socket_to_others.send_string(json.dumps(""))
        self.ioloop.add_timeout(time.time() + 3, self.timeout)
        return

    def run(self):
        self.socket_to_others = server_config.context.socket(zmq.PUB)
        self.socket_to_others.bind(server_config.server_zmq_addr)

        # 服务端收信息,不同机子
        self.socket_from_others = server_config.context.socket(zmq.SUB)
        self.socket_from_others.setsockopt_string(zmq.SUBSCRIBE, "")
        self.socket_from_others.bind(server_config.server_zmq_addr_accept)
        self.stream_from_others_sub = zmqstream.ZMQStream(
            self.socket_from_others)
        self.stream_from_others_sub.on_recv(self.process_message_client)

        self.ioloop.add_timeout(time.time(), self.timeout)
        # application = tornado.web.Application(urls)
        # application.listen(8887)
        self.ioloop.start()
        return