audio_monitor.py
Go to the documentation of this file.
00001 #!/usr/bin/env/python
00002 # -*- coding: utf-8 -*-
00003 
00004 import os
00005 import time
00006 import logging
00007 import subprocess
00008 import traceback
00009 
00010 from rospeex_core.exceptions import WebAudioMonitorException
00011 
00012 logger = logging.getLogger(__name__)
00013 
00014 class WebAudioMonitor(object):
00015     """ AudioMonitor launcher class """
00016 
00017     def __init__(self, app_dir, app_name, send_port, recv_port, websocket_port):
00018         """
00019         initialize AudioMonitor class
00020         @param app_dir:
00021         @type  app_dir:
00022         @param app_name:
00023         @type  app_name:
00024         @param send_port:
00025         @type  send_port: int
00026         @param recv_port:
00027         @type  recv_port: int
00028         @param websocket_port:
00029         @type  websocket_port: int
00030         @raises WebAudioMonitorException
00031         """
00032         # set init setting
00033         self._process = None
00034         self._send_port = send_port
00035         self._recv_port = recv_port
00036         self._websocket_port = websocket_port
00037         self._app_dir = app_dir
00038 
00039         # get AudioMonitor path
00040         try:
00041             self._app_path = os.path.join(app_dir, app_name)
00042         except Exception, e:
00043             msg = 'monitorServer is not found. msg: %s'%str(e)
00044             raise WebAudioMonitorException(msg)
00045 
00046         # check rospack result
00047         if not os.path.exists(self._app_path):
00048             msg = 'rospeex is not found. msg: %s'%str(self._app_path)
00049             logger.warn(msg)
00050             raise WebAudioMonitorException(msg)
00051 
00052         # show args
00053         logger.info(
00054             'create audio monitor : \n\
00055             \t send_port        :%d\n\
00056             \t recv_port        :%d\n\
00057             \t websocket_port   :%d\n\
00058             \t application_path :%s'
00059             %(send_port, recv_port, websocket_port, self._app_path))
00060 
00061 
00062     def launch(self):
00063         """
00064         launch AudioMonitor
00065         @raises WebAudioMonitorException
00066         """
00067         if self._process is None:
00068             # launch process
00069             args = [self._app_path,
00070                     str(self._websocket_port),
00071                     str(self._recv_port),
00072                     str(self._send_port)]
00073             options = {'cwd': self._app_dir}
00074 
00075             try:
00076                 # launch subprocess
00077                 self._process = subprocess.Popen(args, **options)
00078             except OSError as err:
00079                 self._process = None
00080                 raise WebAudioMonitorException('Failed to launch. msg:', str(err))
00081             except Exception as err:
00082                 self._process = None
00083                 msg = 'Failed to launch. Unknown exception. msg: %s Traceback: %s' % (str(err), traceback.format_exc())
00084                 raise WebAudioMonitorException(msg)
00085             else:
00086                 logger.debug('AudioMonitor successfuly launched.')
00087         else:
00088             logger.info('AudioMonitor is aleady launched. pid:%d'%self._process.pid)
00089 
00090 
00091     def exit(self):
00092         """ exist AudioMonitor """
00093         if self.is_alive():
00094             # kill process
00095             self._process.kill()
00096             self._process.wait()
00097             self._process = None
00098         else:
00099             logger.info("AudioMonitor is aleady exit.")
00100             self._process = None
00101 
00102 
00103     def is_alive(self):
00104         """
00105         check audio monitor alive
00106         @return: True for audio monitor is alive
00107                  False for audio monitor is NOT alive
00108         @rtype : Bool
00109         """
00110         if self._process is not None:
00111             if self._process.poll() is None:
00112                 return True
00113         else:
00114             logger.info('AudioMonitor is not launched.')
00115 
00116         return False
00117 
00118 
00119 if __name__ == '__main__':
00120     send_port = 16001
00121     recv_port = 5002
00122     web_socket_port = 9000
00123 
00124     try:
00125         audio_monitor = WebAudioMonitor(send_port, recv_port, web_socket_port)
00126         audio_monitor.launch()
00127     except WebAudioMonitorException as err:
00128         print str(err)
00129     except Exception as err:
00130         print str(err)
00131 
00132     while audio_monitor.is_alive():
00133         time.sleep(0.1)
00134 
00135     audio_monitor.exit()
00136 


rospeex_core
Author(s): Komei Sugiura
autogenerated on Wed Aug 26 2015 16:10:30