Beispiel #1
0
class MQTTClientProcess():

    def __init__(self, clientid, broker="localhost", port=1884):
        self.queue = Queue()
        self.clientid = clientid
        self.broker = broker
        self.port = port
        self.started = False
        self.process = Process(target=self.startProcess, args=(self.queue, self.clientid, broker, port,))

    def send(self, channel, value):
        self.queue.put((channel, value))

    def start(self):
        self.process.start()
        self.started = True

    def stop(self):
        self.process.stop()
        self.started = False

    def startProcess(self, queue, clientid, host, port):
        setproctitle("MQTT Client")
        print("Connecting to broker")
        print("Host: %s" % host)
        print("Port: %s" % port)
        mqttc = paho.Client(clientid)
        mqttc.connect(host, port=port, keepalive=60)
        print("Connected Successfully.\n")
        while True:
            data = queue.get(block=True)
            print("Sending Data.\n")
            print(data[0])
            mqttc.publish("{}/{}".format(clientid, data[0]), data[1])
Beispiel #2
0
class Reloader(object):

    """Reload service when files change"""

    def __init__(self, sig='SIGHUP', files=[]):
        if not files:
            raise Exception('No file to watch for reload')

        self.log = Logs().log
        self.proc = None
        self._files = files
        sig_attr = getattr(signal, sig)
        try:
            assert int(sig_attr)
        except:
            raise Exception('Wrong signal provided for reload')
        self.observer = Observer()
        rel = Reload(sig=sig_attr, reloader=self)
        for dir in self.dirs:
            self.log.debug('Registering watcher for {dir}'.format(dir=dir))
            self.observer.schedule(rel, dir, recursive=False)

    def _get_files(self):
        """Return iterator of tuples (path, file)"""
        for f in self._files:
            for m in glob.iglob(f):
                if os.path.isdir(m):
                    yield (m, m)
                yield (os.path.dirname(m), m)
            if os.path.isdir(f):
                yield (f, f)
            yield (os.path.dirname(f), f)

    @property
    def files(self):
        """Return list of watched files"""
        return list(set([files[1] for files in self._get_files()]))

    @property
    def dirs(self):
        """Return list of watched directories"""
        return list(set([files[0] for files in self._get_files()]))

    def run(self, ret=False):
        while True:
            self.observer.start()
            if ret:
                return self.observer
            self.observer.join()

    def run_in_process(self):
        self.proc = Process(target=self.run)
        self.proc.start()

    def stop(self):
        if self.proc:
            self.proc.stop()
        else:
            self.observer.stop()
Beispiel #3
0
def execute(module,
            topology_module = None,
            topology=default_topology,
            function=default_function,
            args=default_args,
            cli=default_cli,
            timeout=default_timeout,
            nodes=default_nodes,
            nox_path=default_nox_path,
            verbose=default_verbose,
            experiment_mode=default_experiment_mode):

    nodes = int(nodes)
    topology_module = module + "_topo" if topology_module is None else topology_module

    dirs = fetch_subdirs()
    setup_env(module, topology_module, function, args, topology, dirs, experiment_mode, nodes)
    import_directories(dirs)

    q = Queue()
    listener = Listener(signal_address)
    wait = Process(target=receive_signal, args=(listener,q))
    wait.daemon = True
    wait.start()

    # if experiment_mode:
    #     update_application = get_function_by_name(module, function)
    #     initial_topology = get_function_by_name(topology_module, topology)
    #     setup = get_function_by_name("update_lib", "setup")
    #     inst = DummyComponent(args, update_application, setup, initial_topology)
    #     os._exit(0)
        
    if experiment_mode:
        # yappi.start()
        nox = Process(target=run_nox)
        nox = NOX("c0", "UpdateApp")
        nox.start()
        lg.setLogLevel('output')        
        output("*** Application started ***\n")
        wait.join(timeout)
        msg = ""
        status = ""
        if wait.is_alive():
            status = "killed"
            wait.terminate()
        else:
            status = "finished"
            msg = q.get()
        # yappi.stop()
        # stats = string.join([str(stat) for stat in yappi.get_stats(yappi.SORTTYPE_TTOTAL)],"\n")
        output("*** Application %s ***\n%s" % (status, msg))
        # output("*** Stats %s " % (stats))
        if verbose:
            output("\n*** Controller output: ***\n" + getControllerOutput() + "\n")
        nox.stop()
        os._exit(0)
    # elif nox_only:
    #     nox_command = os.path.expanduser(nox_path)
    #     nox_dir = os.path.dirname(nox_command)
    #     os.chdir(nox_dir)
    #     if verbose:
    #         nox_command += " -v"
 
    #     command = "%s -i ptcp:6633 %s" % (nox_command,"UpdateApp")
    #     os.system(command)        
    #     wait.join()
    #     os._exit(0)

    else:
        global mininet
        topo = get_function_by_name(topology_module, topology)(nodes).mininet_topo()
        mininet = Mininet( topo=topo, switch=UserSwitch,
                           controller=lambda name: NOX(name, "UpdateApp"),
                           xterms=False, autoSetMacs=True, autoStaticArp=True )
        mininet.start()
        lg.setLogLevel('output')
        output("*** Mininet Up ***\n")
        output("*** Application started ***\n")
        if cli:
            CLI(mininet)
        wait.join(timeout)
        msg = ""
        status = ""
        if wait.is_alive():
            status = "killed"
            listener.close()
            wait.terminate()
        else:
            status = "finished"
            msg = q.get()
        output("*** Application %s ***\n%s" % (status, msg))
        if verbose:
            output("*** Controller output: ***\n" + getControllerOutput() + "\n")
        mininet.stop()
        output("*** Mininet Down ***\n")
        os._exit(0)
Beispiel #4
0
class Choco(object):
    def __init__(self, config):
        self.config = config
        self.queue = Queue(10000)
        self.count = 0
        self.pool = [Thread(target=self.start) for i in range(config.THREAD_COUNT)]
        self.working_count = Value(c_int)
        self.working_count_lock = Lock()
        if os.name is 'nt':
            self.watch_process = Thread(target=self.watch)
            self.ping_process = Thread(target=self.auto_ping)
        else:
            self.watch_process = Process(target=self.watch)
            self.ping_process = Process(target=self.auto_ping)
        self.exit = Value(c_bool)
        self.kakao = None

        redis_pool = redis.ConnectionPool(host=config.REDIS_HOST,
            port=config.REDIS_PORT,
            db=config.REDIS_DB,
            password=config.REDIS_PASSWORD)
        self.cache = ChocoCache.adapter = redis.Redis(connection_pool=redis_pool)
        self.module = Endpoint()
        self.module.set_prefix(config.COMMAND_PREFIX)

        self.cli = ChocoCLI(self)

        auth_mail = self.cache.hget('choco_auth', 'mail')
        auth_pass = self.cache.hget('choco_auth', 'password')
        auth_client = self.cache.hget('choco_auth', 'client')
        auth_x_vc = self.cache.hget('choco_auth', 'x_vc')
        if self.cache.hexists('choco_auth', 'uuid_base64'):
            auth_uuid = self.cache.hget('choco_auth', 'uuid_base64')
        else:
            auth_uuid = base64.b64encode(self.cache.hget('choco_auth', 'uuid'))

        if not auth_client:
            print >> sys.stderr, "Authenticate failed: client name not found\n" + \
                "Please check config.py and set 'choco_auth' to redis server"
            sys.exit(1)
        elif not auth_uuid:
            print >> sys.stderr, "Authenticate failed: uuid not found\n" + \
                "Please check config.py and set 'choco_auth' to redis server"
            sys.exit(1)

        self.load_module()
        if self.auth_kakao(auth_mail, auth_pass, auth_client, auth_uuid, auth_x_vc):
            print >> sys.stdout, 'Successfully connected to KakaoTalk server'

    def load_module(self):
        from modules import init_module, module_loader
        init_module(self, self.module)
        module_loader(home, self.config)

    def auth_kakao(self, mail, password, client, uuid, x_vc):
        user_session = self.cache.hget('choco_session', 'key')
        user_id = self.cache.hget('choco_session', 'id')

        if not user_session or not user_id:
            if not mail:
                print >> sys.stderr, "Authenticate failed: email address not found\n" + \
                    "Please check config.py and set 'choco_auth' to redis server"
                sys.exit(1)
            elif not password:
                print >> sys.stderr, "Authenticate failed: password not found\n" + \
                    "Please check config.py and set 'choco_auth' to redis server"
                sys.exit(1)
            elif not x_vc:
                print >> sys.stderr, "Authenticate failed: X-VC token not found\n" + \
                    "Please check config.py and set 'choco_auth' to redis server"
                sys.exit(1)

            self.kakao = kakaotalk(debug=self.config.DEBUG)
            auth_result = self.kakao.auth(mail, password, client, uuid, x_vc)
            if not auth_result:
                print >> sys.stderr, "KakaoTalk auth failed"
                sys.exit(1)
            else:
                login_result = self.kakao.login()
                if not login_result:
                    print >> sys.stderr, "KakaoTalk login failed"
                    sys.exit(1)
                else:
                    self.cache.hset('choco_session', 'key', self.kakao.session_key)
                    self.cache.hset('choco_session', 'id', self.kakao.user_id)
        else:
            self.kakao = kakaotalk(user_session, uuid, user_id,
                debug=self.config.DEBUG)
            login_result = self.kakao.login()
            if not login_result:
                print >> sys.stderr, "KakaoTalk login failed"
                sys.exit(1)

        return True

    def reload_kakao(self):
        print >> sys.stdout, 'Trying to reconnect..'
        self.ping_process.stop()
        for p in self.pool:
            p.stop()
        while not self.queue.empty():
            self.queue.get()
        self.kakao.s.close()
        user_session = self.kakao.session_key
        uuid = self.kakao.device_uuid
        user_id = self.kakao.user_id
        del self.kakao
        self.kakao = kakaotalk(user_session, uuid, user_id, debug=self.config.DEBUG)
        login = self.kakao.login()
        if login:
            print >> sys.stdout, 'Reconnected'
            self.exit.value = False
            self.ping_process.start()
            for p in self.pool:
                p.start()
            self.watch()
        else:
            print >> sys.stderr, 'ERROR: failed to re-authorize to KakaoTalk'

    @staticmethod
    def run(config):
        bot = Choco(config)
        bot.ping_process.start()
        for p in bot.pool:
            p.start()
        bot.watch_process.start()
        bot.cli.open()

    def watch(self):
        while not self.exit.value:
            try:
                data = self.kakao.translate_response()
                if not data:
                    print >> sys.stderr, \
                        'WARNING: data is None. probably socket is disconnected?'
                    self.reload_kakao()
                    break
                elif data['command'] == 'MSG':
                    body = data['body']
                    if 'chatId' in body:
                        chatId = str(body['chatId'])
                        exists = self.cache.hexists('choco:rooms', chatId)
                        if not exists:
                            data['command'] = 'NEW'

                    self.queue.put(data)
                    self.cache.incr('choco:count:recv')
                elif data['command'] == 'DECUNREAD' or data['command'] == 'WELCOME':
                    body = data['body']
                    if 'chatId' in body:
                        chatId = str(body['chatId'])
                        exists = self.cache.hexists('choco:rooms', chatId)
                        if not exists:
                            data['command'] = 'NEW'
                            self.queue.put(data)
            except socket.timeout, e:
                print >> sys.stderr, 'ERROR: socket timeout'
            except KeyboardInterrupt, e:
                self.send_exit()
                self.exit.value = True
            except Exception, e:
                print >> sys.stderr, e
                self.send_exit()
                self.exit.value = True