Пример #1
0
    def __init__(self, robot_uid):
        self.logger = InfoLogger(__name__).get_logger()

        self.robot_uid = robot_uid

        self.host = ConfigMedia.host
        self.port = ConfigMedia.port
        self.path = ConfigMedia.path

        self.a_format = ConfigMedia.a_format
        self.analyze_duration = ConfigMedia.analyze_duration

        self.v_format = ConfigMedia.v_format
        self.pix_fmt = ConfigMedia.pix_fmt
        self.v_resolution = ConfigMedia.v_resolution

        self.audio_codec = ConfigMedia.audio_codec
        self.audio_bitrate = ConfigMedia.audio_bitrate
        self.strict_lvl = ConfigMedia.strict_lvl

        self.video_bitrate = ConfigMedia.video_bitrate
        self.output_format = ConfigMedia.output_format

        self.a_proc = None
        self.v_thread = None
        self.a_thread = None

        self.ff = None
        self.m_thread = None

        self.image_pipe = None
        self.audio_pipe = None
Пример #2
0
def run_audio_stream(robot_uid, sock, proc):
    logger = InfoLogger(__name__).get_logger()
    logger.info('[Audio] run_audio_stream start')

    try:
        while True:
            if proc.poll():
                break

            output = proc.stdout.read(ConfigAudio.buffer_size)

            try:
                select.select([], [sock], [])
            except socket.error:
                logger.info('[Audio] socket shutdown')
                break

            format_data = format_send_data(robot_uid, output)
            sock.sendall(format_data)

            # logger.info('[Audio] send output : %s' % format_data)

    except Exception as e:
        logger.info('[Audio] run_audio_stream error : %s' % e)

    if not proc.poll():
        proc.terminate()
    #     proc.kill()

    logger.info('[Audio] run_audio_stream exit done')
Пример #3
0
def stop_servers():
    logger = InfoLogger(__name__).get_logger()
    # logger.info('Servers to stop: %d' % len(Servers.server_threads))

    for ins_name, thread in Servers.server_threads.iteritems():
        thread.stop()
        logger.info('Stop server : %s' % ins_name)
Пример #4
0
def run_video_feed(robot_uid, sock, func, handle):
    logger = InfoLogger(__name__).get_logger()
    logger.info('[Video] run_video_feed start')

    try:
        while True:

            try:
                select.select([], [sock], [])
            except socket.error:
                logger.info('[Video] socket shutdown')
                break

            # image_container = yield func(handle)
            image_container = func(handle)
            image_data_lst = format_image_data(image_container)
            
            image_uid = uuid.uuid4().hex

            for image_dct in image_data_lst:
                send_data = format_send_data(robot_uid, image_uid, image_dct)
                sock.sendall(send_data)

            # logger.info('[Video] run_video_feed done')

    except Exception as e:
        logger.info('[Video] run_video_feed error : %s' % e)

    logger.info('[Video] run_video_feed exit done')
Пример #5
0
class BaseConnection(object):
    def __init__(self, stream, address, delimiter):
        self.logger = InfoLogger(__name__).get_logger()
        self.name = self.__class__.__name__

        self.delimiter = delimiter

        self._stream = stream
        self._address = address

        self._stream.set_close_callback(self.on_close)
        self.read_message()

    def read_message(self):
        # self.logger.info("In read_message, %r" % self.delimiter)
        self._stream.read_until(self.delimiter, self.on_message_read)

    def on_message_read(self, data):
        pass

    # def send_to_ws(self, data, robot_uid):
    #     """Send to broswer through websocket"""
    #     pass

    def on_close(self):
        self.logger.info('[%s] client close: %s' % (self.name, str(self._address)))
Пример #6
0
    def __init__(self, port, connection):
        super(BaseTCPServer, self).__init__()

        self.name = self.__class__.__name__
        self.logger = InfoLogger(self.name).get_logger()

        self.port = port
        self.connection = connection
Пример #7
0
    def __init__(self, robot_uid):
        self.logger = InfoLogger(__name__).get_logger()

        self.host = ConfigVideo.host
        self.port = ConfigVideo.port
        self.robot_uid = robot_uid

        self.sock = None
Пример #8
0
    def __init__(self, stream, address, delimiter):
        self.logger = InfoLogger(__name__).get_logger()
        self.name = self.__class__.__name__

        self.delimiter = delimiter

        self._stream = stream
        self._address = address

        self._stream.set_close_callback(self.on_close)
        self.read_message()
Пример #9
0
    def __init__(self, robot_uid=None):
        self.logger = InfoLogger(__name__).get_logger()

        self.host = ConfigAudio.host
        self.port = ConfigAudio.port
        self.file_type = ConfigAudio.file_type
        self.rate = ConfigAudio.rate

        self.robot_uid = robot_uid

        self.sock = None
        self.proc = None
Пример #10
0
class BaseTCPServer(TCPServer):
    def __init__(self, port, connection):
        super(BaseTCPServer, self).__init__()

        self.name = self.__class__.__name__
        self.logger = InfoLogger(self.name).get_logger()

        self.port = port
        self.connection = connection

    def handle_stream(self, stream, address):
        self.logger.info("[%s] new connection: %s %s" % (self.name, address, stream))
        self.connection(stream, address)

    def start(self):
        try:
            self.logger.info("[%s] starting at port %s" % (self.name, self.port))

            self.listen(self.port)
            self.logger.info("[%s] socket setup done" % self.name)

            ioloop = IOLoop()
            ioloop.current().start()

        except Exception as e:
            self.logger.info("[%s] start error: %s" % (self.name, e))
Пример #11
0
    def __init__(self):
        self.server_host = ConfigServer.host
        self.server_port = ConfigServer.port
        self.delimiter = ConfigServer.delimiter
        self.timeout = ConfigServer.timeout
        self.reconnect_delta = ConfigServer.reconnect_delta

        self.collector_handle_delta = ConfigCollector.handle_delta

        self.logger = InfoLogger(__name__).get_logger()

        self.thread_stop = False

        self.collector = Collector()
        self.executor = Executor()
Пример #12
0
    def __init__(self):
        self.logger = InfoLogger(__name__).get_logger()
        # self.ioloop = IOLoop()

        self.shell = ConfigSSH.local_ws_shell
        self.port = ConfigSSH.local_ws_port

        self.height = ConfigSSH.height
        self.width = ConfigSSH.width
        self.winheight = ConfigSSH.winheight
        self.winwidth = ConfigSSH.winwidth

        self.max_terminals = ConfigSSH.max_terminals

        self.running = False
Пример #13
0
    def start_server_thread(cls, server_ins):
        ins_name = server_ins.__class__.__name__

        if ins_name in cls.server_threads:
            return

        thread = ServerThread(server_ins)

        thread.setName('Thread-%s' % ins_name)
        cls.server_threads[ins_name] = thread

        logger = InfoLogger(__name__).get_logger()
        logger.info('server_threads count : %d' % len(cls.server_threads))

        thread.start()
Пример #14
0
class WebsocketServer(object):
    def __init__(self):
        self.logger = InfoLogger(__name__).get_logger()
        # self.ioloop = IOLoop()

        self.shell = ConfigSSH.local_ws_shell
        self.port = ConfigSSH.local_ws_port

        self.height = ConfigSSH.height
        self.width = ConfigSSH.width
        self.winheight = ConfigSSH.winheight
        self.winwidth = ConfigSSH.winwidth

        self.max_terminals = ConfigSSH.max_terminals

        self.running = False

    def start(self):
        self.logger.info('Starting websocket server')

        term_settings = {
            'height': self.height,
            'width': self.width,
            'winheight': self.winheight,
            "winwidth": self.winwidth,
        }

        # term_manager = SingleTermManager(shell_command=[self.shell])
        term_manager = UniqueTermManager(shell_command = [self.shell], \
            max_terminals = self.max_terminals, term_settings = term_settings)

        handlers = [
            # (r"/websocket", MyTermSocket, {'term_manager': term_manager}),
            # (r"/websocket", TermSocket, {'term_manager': term_manager}),
            (r"/websocket/([^/]*)", TermSocket, {
                'term_manager': term_manager
            }),
        ]

        app = tornado.web.Application(handlers)

        app.listen(self.port)
        self.logger.info('Listen on port ' + str(self.port))

        self.running = True
        self.logger.info('Start websocket server done')

        # self.ioloop.current().start()
        IOLoop.current().start()

    def stop(self):
        # self.ioloop.current().stop()
        IOLoop.current().stop()
        self.running = False
        self.logger.info('Stop websocket server done')

    def is_running(self):
        return self.running
Пример #15
0
def run_media_stream(robot_uid, ff_obj):
    logger = InfoLogger(__name__).get_logger()
    logger.info('[Media] run_media_stream start')

    try:
        ff_obj.run()

    except Exception as e:
        logger.info('[Media] run_media_stream error : %s' % e)

    logger.info('[Media] run_media_stream exit done')
Пример #16
0
    def __init__(self, ip='127.0.0.1', port=9559):
        self.qi_ip = ip
        self.qi_port = port
        self.full_path = 'tcp://%s:%s' % (self.qi_ip, self.qi_port)
        self.session = None

        self.logger = InfoLogger(__name__).get_logger()

        self.set_session()
        self.setup_qi_services()
Пример #17
0
class QiSession(object):
    session = None
    logger = InfoLogger('QiSession').get_logger()

    service_dct = {
        'ALAudioDevice': None,
        'ALBasicAwareness': None,
        'ALBattery': None,
        'ALBehaviorManager': None,
        'ALConnectionManager': None,
        'ALMemory': None,
        'ALMotion': None,
        'ALSystem': None,
        'ALTextToSpeech': None,
        'ALVideoDevice': None,
        'PackageManager': None,
    }

    @classmethod
    def log(cls, msg):
        cls.logger.info(msg)

    @classmethod
    def get_session(cls, qi_path):
        if not cls.session or not cls.session.isConnected():
            cls.log('QiSession is setting up new session')

            try:
                try:
                    # If no session is connecting, start an session
                    app = qi.Application(url=qi_path)
                    app.start()
                    s = app.session

                except:
                    # Another session is connecting(maybe AXMService), connect directly
                    s = qi.Session()
                    s.connect(qi_path)

                cls.session = s

            except Exception as e:
                cls.log('QiSession get_session error : %s' % e)

        return cls.session

    @classmethod
    def get_session_service(cls, qi_path):
        if None in cls.service_dct.values():
            s = cls.get_session(qi_path)

            for k, v in cls.service_dct.items():
                cls.service_dct[k] = s.service(k)

        return cls.service_dct
Пример #18
0
class ServerThread(threading.Thread):
    def __init__(self, server_ins):
        super(ServerThread, self).__init__()
        self.server_ins = server_ins
        self._stop = threading.Event()

        self.logger = InfoLogger(__name__).get_logger()

    def stop(self):
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

    def run(self):
        thread_name = threading.currentThread().getName()
        server_name = self.server_ins.__class__.__name__
        self.logger.info('[%s] Run server=%s' % (thread_name, server_name))

        self.server_ins.start()
Пример #19
0
def run_video_feed(func, handle, image_pipe):
    logger = InfoLogger(__name__).get_logger()
    logger.info('[Media] run_video_feed start')

    try:
        while not image_pipe.is_closed():
            image_container = func(handle)
            image_pipe.write(image_container[6])

    except Exception as e:
        logger.info('[Media] run_video_feed error : %s' % e)

    logger.info('[Media] run_video_feed exit done')
Пример #20
0
def run_audio_feed(proc, audio_pipe):
    logger = InfoLogger(__name__).get_logger()
    logger.info('[Media] run_audio_feed start')

    try:
        while not audio_pipe.is_closed():
            # audio_pipe.write(proc.stdout.read(80))
            audio_pipe.write(proc.stdout.readline())

    except Exception as e:
        logger.info('[Media] run_audio_feed error : %s' % e)

    logger.info('[Media] run_audio_feed exit done')
Пример #21
0
class PuppetWebSocketServer(object):
    def __init__(self):
        self.logger = InfoLogger(__name__).get_logger()
        self.host = ConfigWebSocket.host
        self.port = ConfigWebSocket.port

    def start(self):
        try:
            app = Application([
                (r'/puppet', PuppetWebSocketHandler),
                (r'/puppet/ssh/([^/]*)', SSHWebSocketHandler),
                (r'/puppet/video/([^/]*)', VideoWebSocketHandler),
                (r'/puppet/audio/([^/]*)', AudioWebSocketHandler),
            ])
            self.logger.info("PuppetWebSocket Handler setup done")

            if ConfigSSL.enable:
                app.listen(address=self.host,
                           port=self.port,
                           ssl_options={
                               "certfile": ConfigSSL.certfile,
                               "keyfile": ConfigSSL.keyfile,
                           })
            else:
                app.listen(address=self.host, port=self.port)
            self.logger.info('Listening ws at ' + str(self.host) + ':' +
                             str(self.port))

            ioloop = IOLoop()
            ioloop.current().start()

        except Exception as e:
            self.logger.info("PuppetWebSocketServer start error: %s" % e)
Пример #22
0
    def __init__(self):
        self.logger = InfoLogger(__name__).get_logger()

        self.sys_command = SystemCommand()
        self.behavior_command = BehaviorCommand()
        self.package_command = PackageCommand()
        self.video_command = VideoCommand()
        self.demo_command = DemoCommand()
        self.ssh_command = SSHCommand()
        self.audio_command = AudioCommand()
        self.media_command = MediaCommand()

        self.command_maps = (
            self.sys_command,
            self.behavior_command,
            self.package_command,
            self.video_command,
            self.demo_command,
            self.ssh_command,
            self.audio_command,
            self.media_command,
        )
Пример #23
0
class Manager(object):
    def __init__(self):
        self.pid_file = ConfigManager.pid_file
        self.logger = InfoLogger(__name__).get_logger()

    def set_pid(self, pid):
        with open(self.pid_file, 'a') as f:
            f.write(str(pid) + '\n')

    def get_pid(self):
        if not os.path.exists(self.pid_file):
            return []

        pids = [
            int(pid.rstrip('\n'))
            for pid in open(self.pid_file, 'r').readlines()
        ]
        return pids

    def kill_process(self, pid=None):
        if pid:
            pids = [pid]
        else:
            pids = self.get_pid()

        try:
            for pid in pids:
                self.logger.info("Stopping pid %s" % pid)

                try:
                    os.kill(pid, signal.SIGTERM)

                except OSError, err:
                    if err.errno == errno.ESRCH:
                        self.logger.info("pid %s not running" % pid)
                        continue

                self.logger.info("Stop pid %s done" % pid)

            # clear file
            with open(self.pid_file, 'w') as f:
                f.write('')

            return "Done"

        except OSError, err:
            # if err.errno == errno.ESRCH:
            #     return "Not running"
            if err.errno == errno.EPERM:
                return "No permission to signal this process!"
            else:
                return "Unknown error"
Пример #24
0
class ClientManager(object):
    logger = InfoLogger("ClientManager").get_logger()

    @classmethod
    def clean_defunct_client(cls):
        for uid,iostream_obj in ConnectionMap.clients.items():

            cls.logger.info('Cleanning %s' % uid)
            cls.logger.info('closed %s' % str(iostream_obj._stream.closed()))

            if iostream_obj._stream.closed():
                cls.logger.info('%s is shutdown, clean' % uid)

                if ConnectionMap.clients.has_key(uid):
                    cls.logger.info('ConnectionMap clients pop: %s' % uid)
                    ConnectionMap.clients.pop(uid)

                if ConnectionMap.client_status.has_key(uid):
                    cls.logger.info('ConnectionMap client_status pop: %s' % uid)
                    ConnectionMap.client_status.pop(uid)

        for conn in ConnectionMap.ws_conn.values():
            conn.refresh_client_status()
Пример #25
0
class BaseHTTPServer(object):
    def __init__(self, port):
        self.name = self.__class__.__name__
        self.logger = InfoLogger(self.name).get_logger()

        self.port = port

    def set_app(self, app_tup_lst):
        app = Application(app_tup_lst)
        self.logger.info("[%s] app setup done" % self.name)
        return app

    def set_server(self, app):
        server = HTTPServer(app)

        if ConfigSSL.enable:
            server = HTTPServer(app, 
                ssl_options = {
                    "certfile" : ConfigSSL.certfile,
                    "keyfile" : ConfigSSL.keyfile,
                }
            )
        else:
            server = HTTPServer(app)

        server.listen(self.port)
        return server

    def start(self, app_tup_lst):
        try:
            app = self.set_app(app_tup_lst)
            server = self.set_server(app)

            self.logger.info("[%s] listen at port: %s" % (self.name, str(self.port)))

            ioloop = IOLoop()
            ioloop.current().start()

        except Exception as e:
            self.logger.info("[%s] start error: %s" % (self.name, e))
Пример #26
0
    def __init__(self, server_ins):
        super(ServerThread, self).__init__()
        self.server_ins = server_ins
        self._stop = threading.Event()

        self.logger = InfoLogger(__name__).get_logger()
Пример #27
0
def run_write_client(sock, ws_connect):
    logger = InfoLogger(__name__).get_logger()
    logger.info('[writer] start')

    ws = yield ws_connect

    while True:
        try:
            recv_sock = sock.recv(1024)
            # logger.info('[writer] recv_sock : %r' % recv_sock)

            if recv_sock:
                msg_list = recv_sock.split(ConfigSSH.delimiter)

                for msg_str in msg_list:
                    # msg_str may be '', if json loads failed, continue to next one
                    try:
                        msg_dict = json.loads(msg_str)
                    except:
                        continue

                    msg = msg_dict.get('msg', None)

                    if msg:
                        # yield ws.write_message(msg)
                        ws.write_message(msg)
                        # logger.info('[writer] write to ws : %r' % msg)
                    else:
                        logger.info('[writer] read no msg from sock : %r' %
                                    msg)

            else:
                # sock.shutdown(socket.SHUT_RDWR) is called
                logger.info('[writer] socket shutdown')

                # send to ws, let run_read_client know and exit
                ws.write_message(r'["stdin", "\r"]')

                break

        except Exception as e:
            logger.info('[writer] error: %s' % e)
            break

    logger.info('[writer] exit done')
Пример #28
0
 def __init__(self):
     self.pid_file = ConfigManager.pid_file
     self.logger = InfoLogger(__name__).get_logger()
Пример #29
0
            with open(self.pid_file, 'w') as f:
                f.write('')

            return "Done"

        except OSError, err:
            # if err.errno == errno.ESRCH:
            #     return "Not running"
            if err.errno == errno.EPERM:
                return "No permission to signal this process!"
            else:
                return "Unknown error"


if args.run:
    logger = InfoLogger(__name__).get_logger()
    start_servers()

    pid = os.getpid()

    manager = Manager()
    result = manager.kill_process()
    logger.info('Stop clients : %s' % result)
    
    manager.set_pid(pid)
    
    client = Client()
    client.connect()

elif args.restart:
    logger = InfoLogger(__name__).get_logger()
Пример #30
0
def stop_servers():
    logger = InfoLogger(__name__).get_logger()

    for server_name, thread in Servers.server_threads.items():
        thread.stop()
        logger.info('Stop server : %s' % server_name)