diff options
author | cathook <b01902109@csie.ntu.edu.tw> | 2014-11-09 02:40:08 +0800 |
---|---|---|
committer | cathook <b01902109@csie.ntu.edu.tw> | 2014-11-09 02:40:08 +0800 |
commit | fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1 (patch) | |
tree | 252f682763ec8356bc01ebf8c611dcaa0a07515f /server/src/shared_vim_server.py | |
parent | 3f84cb8b7c2aa24e739ae1a02c883e1e4cdb4004 (diff) | |
download | vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.tar vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.tar.gz vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.tar.bz2 vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.tar.lz vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.tar.xz vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.tar.zst vim-shrvim-fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1.zip |
big change
Diffstat (limited to 'server/src/shared_vim_server.py')
-rwxr-xr-x | server/src/shared_vim_server.py | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/server/src/shared_vim_server.py b/server/src/shared_vim_server.py new file mode 100755 index 0000000..a7f5201 --- /dev/null +++ b/server/src/shared_vim_server.py @@ -0,0 +1,119 @@ +#! /usr/bin/env python3 + +"""Main thread.""" + +import signal +import sys +import threading +import log + +from cmd_ui import CmdUI +from tcp_server import TCPServer +from users_text_manager import UsersTextManager + + +class _ArgsError(Exception): + """Exception raised by _Args.""" + pass + +class _Args(object): + """Arguments of this program. + + Attributes: + port: Port number. + user_list_filename: Default user list. + save_filename: Name of the file to save the text. + """ + DOCUMENT = '[usage] <port_number> <user_list_filename> <save_filename>\n' + def __init__(self): + if len(sys.argv) != 4: + raise _ArgsError('Wrong length of arguments.') + try: + self.port = int(sys.argv[1]) + except ValueError as e: + raise _ArgsError(e) + self.user_list_filename = sys.argv[2] + self.saved_filename = sys.argv[3] + + +class _SharedVimServerError(Exception): + """Error raised by SharedVimServer.""" + pass + +class SharedVimServer(threading.Thread): + """Main class. + + Attributes: + _users_text_manager: Instance of UsersTextManager. + _tcp_server: Instance of TCPServer. + _cmd_ui: Instance of CmdUI. + """ + def __init__(self): + """Constructor.""" + super(SharedVimServer, self).__init__() + try: + self._args = _Args() + except _ArgsError as e: + raise _SharedVimServerError(str(e) + '\n' + _Args.DOCUMENT) + self._users_text_manager = UsersTextManager(self._args.saved_filename) + self._tcp_server = TCPServer(self._args.port, self._users_text_manager) + self._cmd_ui = CmdUI(self._users_text_manager, self._tcp_server, self) + log.info.interface = self._cmd_ui + log.error.interface = self._cmd_ui + + def run(self): + """Starts the program.""" + self._tcp_server.start() + self._cmd_ui.start(['load %s' % self._args.user_list_filename]) + self._cmd_ui.join() + self._tcp_server.join() + + def stop(self): + """Exits the program.""" + self._cmd_ui.stop() + self._tcp_server.stop() + + +class _SignalHandler(object): + """Single handler. + + It will handle below the signals: + SIGTERM, SIGINT - Exit the program. + + Attributes: + _shared_vim_server: Instance of SharedVimServer. + """ + def __init__(self, shared_vim_server): + """Constructor. + + Args: + shared_vim_server: Instance of SharedVimServer. + """ + self._shared_vim_server = shared_vim_server + signal.signal(signal.SIGTERM, self._handler) + signal.signal(signal.SIGINT, self._handler) + + def _handler(self, number, unused_frame): + """Signal handler function. + + Args: + number: The signal number to be handle. + """ + if number in (signal.SIGTERM, signal.SIGINT): + self._shared_vim_server.stop() + + +def main(): + """Program entry point.""" + try: + shared_vim_server = SharedVimServer() + _SignalHandler(shared_vim_server) + shared_vim_server.start() + shared_vim_server.join() + except _SharedVimServerError as e: + print(e) + sys.exit(1) + + +if __name__ == '__main__': + main() |