diff -ur v1/chat_conn.py v2/chat_conn.py --- v1/chat_conn.py 2019-08-10 00:25:07.000000000 +0900 +++ v2/chat_conn.py 2019-08-11 23:59:01.000000000 +0900 @@ -220,7 +220,7 @@ elif s == 'names': r = ' '.join( conn_lst.names() ) elif s == 'other_names': - r = ' '.join( other_names(conn) ) + r = ' '.join( conn_lst.other_names(conn) ) elif is_lock_unlock(s): return conn.send_from('srv', r) @@ -377,9 +377,65 @@ return e.to_attr( locals() ) +def cli(host, port, name0): + cli = cli_new(host, port, name0) + th_quit = threading.Event() + + def th_func(): + rest = rest_new() + while not th_quit.wait(0): + if not readable(sys.stdin): + continue + s = sys.stdin.readline() + if not s: + break + cli.send_to('', s) + + th = threading.Thread( target=th_func ) + th.daemon = True + th.start() + + while True: + r = cli.recv_split() + if r == False: + break + if not r: + continue + (f, s) = r + if f: + s = '{}> {}'.format(f, s) + print(s) + + th_quit.set() + th.join() + cli.dis_conn() + +def help(v): + ss = [ + 'Usage: {} [conn [] []]'.format(sys.argv[0]), + ' conn: client mode', + ' user name: default "guest"', + ' hostname: default "localhost"', + ] + print('\n'.join(ss)) + sys.exit(v) + if __name__ == "__main__": - if sys.argv[1:]: - srv( int(sys.argv[1]) ) - else: - print('Usage: {} '.format(sys.argv[0])) + arg = lambda i, def_v=None: sys.argv[i] if sys.argv[i:] else def_v + + port = int( arg(1, '-1') ) + if port < 0: + help(1) + + kind = arg(2) + if not kind: + srv(port) + sys.exit(0) + if kind != 'conn': + help(2) + + name0 = arg(3, 'guest') + host = arg(4, 'localhost') + + cli(host, port, name0) # EOF