diff -urN rt_v27/Makefile rt_v28/Makefile --- rt_v27/Makefile 2018-04-20 23:52:43.000000000 +0900 +++ rt_v28/Makefile 2018-04-24 01:06:42.000000000 +0900 @@ -7,5 +7,5 @@ rt_sock: $(OBJS) gcc -o $@ $(OBJS) -lm clean: - rm -f *.o *~ + rm -f *.o *~ rt_sock # EOF diff -urN rt_v27/bin.py rt_v28/bin.py --- rt_v27/bin.py 2018-04-20 23:52:43.000000000 +0900 +++ rt_v28/bin.py 2018-04-23 22:02:00.000000000 +0900 @@ -165,10 +165,14 @@ return (ltd, s) nms = [ - 'i4', 'col', 'np64', 'f8', 'p', 'line', 'vs', + 'i4', 'col', 'str', 'np64', 'f8', 'p', 'line', 'vs', 'ax', 'cx', 'fx', 'xx', 'lx', 'rtd', 'ltd', ] -nm_dic_pack = dict( map( lambda nm: ( nm, eval('pack_' + nm) ), nms ) ) -nm_dic_unpack = dict( map( lambda nm: ( nm, eval('unpack_' + nm) ), nms ) ) + +packs = ut.map_lst( lambda nm: eval('pack_' + nm), nms ) +unpacks = ut.map_lst( lambda nm: eval('unpack_' + nm), nms ) + +nm_dic_pack = dict( zip(nms, packs) ) +nm_dic_unpack = dict( zip(nms, unpacks) ) # EOF diff -urN rt_v27/cg.py rt_v28/cg.py --- rt_v27/cg.py 2018-04-21 00:30:33.000000000 +0900 +++ rt_v28/cg.py 2018-04-23 17:41:32.000000000 +0900 @@ -259,16 +259,14 @@ if __name__ == "__main__": if 'use_img_srv' in sys.argv: - img_sock.srv_boot() - img_sock.client.conn() + img_sock.client.boot_server() data = sum( map( d_setup, dat.data ), [] ) lights = dat.lights if 'use_srv' in sys.argv: - rt_sock.server_boot( len(data) ) - rt_sock.client.conn() + rt_sock.client.boot_server( len(data) ) rt_sock.client.add_data(data) rt_sock.client.add_lights(lights) diff -urN rt_v27/img_sock.py rt_v28/img_sock.py --- rt_v27/img_sock.py 2018-04-20 23:52:43.000000000 +0900 +++ rt_v28/img_sock.py 2018-04-23 22:13:00.000000000 +0900 @@ -1,163 +1,50 @@ #!/usr/bin/env python -import sys -import socket -import six import ut import img -import bin +import sock -HOST = 'localhost' PORT = ut.arg_i('port', 23456) + 1 FIFO = 'fifo_img' -cmds = [ 'get_idx', 'wh', 'col', 'quit' ] -cmdi = lambda s: bin.pack_i4( cmds.index(s) ) -cmdi_ck = lambda i: 0 <= i and i < len(cmds) - -k_nm_lst = [ - ('fn', 'i4'), # idx - ('dmy', 'i4'), - ('sec', 'f8'), - ('x', 'i4'), - ('y', 'i4'), - ('def_col', 'col'), - ('dmy2', 'i4'), - ('rep_x', 'i4'), - ('rep_y', 'i4'), -] +def srv_col(fn, sec, x, y, def_col, rep_x, rep_y): + rep = ( bool(rep_x), bool(rep_y) ) + if rep == (False, False): + rep = None + return img.col(fn, sec, x, y, def_col, rep) -nm_lst = ut.map_up_lst( lambda k, nm: nm, k_nm_lst ) +cmd_infs = [ + { 'cmd': 'get_idx', 'func': img.get_idx, 'args': '(str)', 'rets': 'i4' }, + { 'cmd': 'wh', 'func': img.wh, 'args': '(i4)', 'rets': '(i4,i4)' }, + { 'cmd': 'col', 'func': srv_col, 'args': '(i4,f8,i4,i4,col,i4,i4)', 'rets': 'col' }, + { 'cmd': 'quit' }, +] def new_client(): - e = ut.Empty() - e.proc = None - e.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - e.conn = lambda : e.sock.connect((HOST, PORT)) - - recv = lambda n: e.sock.recv(n) if n > 0 else '' - - def send_recv(s, rn): - e.sock.sendall(s) - return recv(rn) - - def quit(): - send_recv( cmdi('quit'), 1 ) - ut.kill_proc(e.proc) - ut.cmd_exec('rm ' + FIFO) - e.quit = quit - - def get_idx(fn): # fn: str - s = bin.pack_str(fn) - s = cmdi('get_idx') + bin.pack_i4( len(s) ) + s + cli = sock.new_client(cmd_infs) - s = send_recv(s, 4) - - (idx, s) = bin.unpack_i4(s) - return idx - - e.get_idx = get_idx + e = ut.Empty() - def wh(fn): # fn: idx - s = cmdi('wh') + bin.pack_i4(fn) + e.boot_server = lambda : cli.boot_server( './img_sock.py', PORT, FIFO ) - s = send_recv(s, 4*2) + e.get_idx = lambda fn: cli.call( 'get_idx', [fn] ) # fn : str - (w, s) = bin.unpack_i4(s) - (h, s) = bin.unpack_i4(s) - return (w, h) - e.wh = wh + e.wh = lambda fn: cli.call( 'wh', [fn] ) # fn : int def col(fn, sec, x, y, def_col=[128,128,128], rep=None): - (rep_x, rep_y) = rep if rep else (False, False) - lst = [ fn, 0, sec, x, y, def_col, 0, int(rep_x), int(rep_y) ] - s = bin.pack_lst(lst, nm_lst) - s = cmdi('col') + bin.pack_i4( len(s) ) + s - - s = send_recv(s, 4*3) - - (col_, s) = bin.unpack_col(s) - return col_ + if not rep: + rep = (False, False) + (rep_x, rep_y) = map(int, rep) + return cli.call( 'col', [ fn, sec, x, y, def_col, rep_x, rep_y ] ) e.col = col + e.quit = lambda : cli.call('quit') + return e client = new_client() -### - -def srv_boot(): - cmd1 = 'nc -l {}'.format(PORT) - cmd2 = './img_sock.py' - cmd = 'rm -f @ ; mkfifo @ ; cat @ | {} | {} > @'.replace('@', FIFO) - cmd = cmd.format(cmd1, cmd2) - client.proc = ut.cmd_proc(cmd) - ut.sleep(3.0) # - -recv = lambda n: sys.stdin.buffer.read(n) if six.PY3 else sys.stdin.read(n) - -def send(s): - if six.PY3: - sys.stdout.buffer.write(s) - else: - sys.stdout.write(s) - sys.stdout.flush() - -err_msg = lambda s: sys.stderr.write(s + '\n') - -def get_idx(): - n = bin.unpack_i4( recv(4) )[0] - s = recv(n) - (fn, s) = bin.unpack_str(s) - - idx = img.get_idx(fn) - - send( bin.pack_i4(idx) ) - -def wh(): - fn = bin.unpack_i4( recv(4) )[0] - - (w, h) = img.wh(fn) - - s = bin.pack_i4(w) + bin.pack_i4(h) - send(s) - -def col(): - n = bin.unpack_i4( recv(4) )[0] - s = recv(n) - - (d, s) = bin.unpack_dic(s, k_nm_lst) - - rep = tuple( map( lambda k: bool( d.get(k) ), ['rep_x', 'rep_y' ] ) ) - if rep == (False, False): - rep = None - - lst = [ d.get('fn'), d.get('sec'), d.get('x'), d.get('y'), d.get('def_col'), rep ] - - col = img.col(*lst) - - send( bin.pack_col(col) ) - -def quit(): - send(b'r') - sys.exit(0) - if __name__ == "__main__": - - dic = { - 'get_idx': get_idx, - 'wh': wh, - 'col': col, - 'quit': quit, - } - while True: - cmd_idx = bin.unpack_i4( recv(4) )[0] - if cmdi_ck(cmd_idx): - cmd = cmds[ cmd_idx ] - f = dic.get(cmd) - f() - else: - msg = '?' + ( ' cmd_idx={:02x}' if cmd_idx else '' ) - err_msg(msg) - sys.exit(1) + srv = sock.new_server(cmd_infs) + srv.main_loop() # EOF diff -urN rt_v27/rt_sock.c rt_v28/rt_sock.c --- rt_v27/rt_sock.c 2018-04-20 23:52:43.000000000 +0900 +++ rt_v28/rt_sock.c 2018-04-24 00:23:49.000000000 +0900 @@ -19,6 +19,14 @@ struct lights lights = { 0, NULL }; void +ret0(void) +{ + int n = 0; + fwrite(&n, sizeof(n), 1, stdout); + fflush(stdout); +} + +void alloc(int n) { int i, sz = sizeof(*data) * n; @@ -45,6 +53,7 @@ data[i].ki = -1; } } + ret0(); } char * @@ -108,7 +117,7 @@ int n, i, ki; char *buf, *bp; - fread(&n, sizeof(int), 1, stdin); + fread(&n, sizeof(n), 1, stdin); bp = buf = malloc(n); fread(buf, 1, n, stdin); @@ -126,6 +135,7 @@ if(buf){ free(buf); } + ret0(); /* fprintf(dbg, "a %d %d/%f %f %f/%f %f %f,%f %f %f,%f %f %f\n", i, data[i].ki, @@ -166,6 +176,7 @@ if(buf){ free(buf); } + ret0(); } void @@ -178,7 +189,8 @@ struct cross_ret ret, ret_; char sbuf[ sizeof(int) * 2 + sizeof(ret) ]; - n = sizeof(buf); + fread(&n, sizeof(n), 1, stdin); + /*n = sizeof(buf);*/ fread(buf, 1, n, stdin); n = sizeof(dbuf); @@ -237,8 +249,7 @@ n = (i 0 else '' - - def send_recv(s, rn): - e.sock.sendall(s) - return recv(rn) - - def quit(): - send_recv( cmdi('quit'), 1 ) - ut.kill_proc(e.proc) - ut.cmd_exec('rm ' + FIFO) - e.quit = quit - - e.clear = lambda : send_recv( cmdi('clear'), 0 ) - - def add(i, d): - dic = { - 'idx': i, - 'ki': kinds.index( d.get('kind') ), - 'l2g': d.get('l2g'), - 'rtd': d.get('rtd'), - } - s = bin.pack_dic(dic, add_k_nm_lst) - s = cmdi('add') + bin.pack_i4( len(s) ) + s - send_recv(s, 0) - e.add = add + + def boot_server(n=100): + cmd_stdio = './rt_sock.py n={} xname={}'.format( n, ut.arg_s('xname', 'xdat') ) + if 'srv_c' in sys.argv: + cmd_stdio = './rt_sock n={}'.format(n) + cli.boot_server(cmd_stdio, PORT, FIFO) + e.boot_server = boot_server + + e.quit = lambda : cli.call('quit') + + e.clear = lambda : cli.call('clear') + + add = lambda i, d: cli.call( 'add', [ i, kinds.index( d.get('kind') ), d.get('l2g'), d.get('rtd') ] ) def add_data(data): for (i, d) in enumerate(data): add(i, d) e.add_data = add_data - def add_lights(lights): - s = bin.pack_i4( len(lights) ) - for ltd in lights: - s += bin.pack_ltd(ltd) - s = cmdi('add_lights') + bin.pack_i4( len(s) ) + s - send_recv(s, 0) - e.add_lights = add_lights + e.add_lights = lambda lights: cli.call( 'add_lights', [lights] ) def cross(l_g, prev_idx=-1): - s = cmdi('cross_') - s += bin.pack_line(l_g) - s += bin.pack_i4(prev_idx) - - s = send_recv(s, 4) - s = recv( bin.unpack_i4(s)[0] ) - - (crs, s) = bin.unpack_dic(s, cross_ret_k_nm_lst) - return crs + rets = cli.call( 'cross_', [l_g, prev_idx] ) + ks = [ 'idx', 't', 'l', 'p', 'l_nv_g', 'nv', 'eyev', 'ang_nv_eyev', 'diff' ] + return dict( zip(ks, rets) ) e.cross = cross return e client = new_client() -### - -def server_boot(n=100): - cmd1 = 'nc -l {}'.format(PORT) - cmd2 = './rt_sock.py n={} xname={}'.format( n, ut.arg_s('xname', 'xdat') ) - if 'srv_c' in sys.argv: - cmd2 = './rt_sock n={}'.format(n) - cmd = 'rm -f @ ; mkfifo @ ; cat @ | {} | {} > @'.replace('@', FIFO) - cmd = cmd.format(cmd1, cmd2) - client.proc = ut.cmd_proc(cmd) - ut.sleep(3.0) # - def new_server(): e = ut.Empty() e.data = [] @@ -123,47 +68,16 @@ for i in range(n): e.data.append({}) - clear = lambda : alloc( len(e.data) ) - - recv = lambda n: sys.stdin.buffer.read(n) if six.PY3 else sys.stdin.read(n) - - def send(s): - if six.PY3: - sys.stdout.buffer.write(s) - else: - sys.stdout.write(s) - sys.stdout.flush() - - err_msg = lambda s: sys.stderr.write(s + '\n') - - def add(): - s = recv( bin.unpack_i4( recv(4) )[0] ) - (dic, s) = bin.unpack_dic(s, add_k_nm_lst) - d = e.data[ dic.get('idx') ] - d['kind'] = kinds[ dic.get('ki') ] - d['l2g'] = dic.get('l2g') - d['rtd'] = dic.get('rtd') + def add(idx, ki, l2g, rtd): + d = e.data[idx] + d['kind'] = kinds[ki] + d['l2g'] = l2g + d['rtd'] = rtd - def add_lights(): - s = recv( bin.unpack_i4( recv(4) )[0] ) - (n, s) = bin.unpack_i4(s) - for i in range(n): - (ltd, s) = bin.unpack_ltd(s) - e.lights.append(ltd) - - e.in_ = [] - e.out = [] - - def cross_(): - s = recv(8*3*2 + 4) - - if e.in_: - send( e.in_.pop(0) ) - return - - (l_g, s) = bin.unpack_line(s) - (prev_idx, s) = bin.unpack_i4(s) + def add_lights(lights): + e.lights = lights + def cross_(l_g, prev_idx): lst = [] for (i, d) in enumerate(e.data): if not d: @@ -192,57 +106,27 @@ d = e.data[ crs.get('idx') ] crs['diff'] = rt.calc_diff(d, e.lights, crs) - s = bin.pack_dic(crs, cross_ret_k_nm_lst) - s = bin.pack_i4( len(s) ) + s - send(s) - e.out.append(s) + ks = [ 'idx', 't', 'l', 'p', 'l_nv_g', 'nv', 'eyev', 'ang_nv_eyev', 'diff' ] + return list( map( lambda k: crs.get(k), ks ) ) e.fn = ut.arg_s('xname', 'xdat') - def save(): - if not e.out or ut.exists(e.fn): - return - with open(e.fn, 'wb') as f: - s = b''.join(e.out) - f.write(s) - - def load(): - if not ut.exists(e.fn): - return - with open(e.fn, 'rb') as f: - s = f.read() - e.in_ = [] - while s: - (n, _) = bin.unpack_i4(s) - sz = 4 + n - e.in_.append( s[:sz] ) - s = s[sz:] - - def quit(): - save() - send(b'r') - sys.exit(0) - def main_loop(): alloc( ut.arg_i('n', 100) ) - load() - dic = { - 'clear': clear, - 'add': add, - 'add_lights': add_lights, - 'cross_': cross_, - 'quit': quit, - } - while True: - cmd_idx = bin.unpack_i4( recv(4) )[0] - if cmdi_ck(cmd_idx): - cmd = cmds[ cmd_idx ] - f = dic.get(cmd) - f() - else: - msg = '?' + ( ' cmd_idx={:02x}' if cmd_idx else '' ) - err_msg(msg) - sys.exit(1) + + funcs = [ + lambda : alloc( len(d.data) ), + add, + add_lights, + cross_, + None, + ] + for (d, func) in zip( cmd_infs, funcs ): + if func: + d['func'] = func + + srv = sock.new_server(cmd_infs) + srv.main_loop() e.main_loop = main_loop return e diff -urN rt_v27/sock.py rt_v28/sock.py --- rt_v27/sock.py 1970-01-01 09:00:00.000000000 +0900 +++ rt_v28/sock.py 2018-04-23 22:15:27.000000000 +0900 @@ -0,0 +1,170 @@ +#!/usr/bin/env python + +import sys +import socket +import six +import ut +import bin + +# Ex. psk = '(i4,(f8,f8,f8),[i4],[(f8,(i4,i4))])' + +pks_lst = lambda pks: pks[1:-1].split(',') + +def pks_to_funcs_pack(pks): + if pks[0] == '(': + return tuple( map( pks_to_funcs_pack, pks_lst(pks) ) ) + if pks[0] == '[': + return [ pks_to_funcs_pack( pks[1:-1] ) ] # len == 1 + return bin.nm_dic_pack.get(pks) + +def pks_to_funcs_unpack(pks): + if pks[0] == '(': + return tuple( map( pks_to_funcs_unpack, pks_lst(pks) ) ) + if pks[0] == '[': + return [ pks_to_funcs_unpack( pks[1:-1] ) ] # len == 1 + return bin.nm_dic_unpack.get(pks) + +def pack_pks(pks, v): + if not pks: + return b'' + typ = type(pks) + if typ == tuple: + return b''.join( ut.map_up( pack_pks, zip(pks, v) ) ) + if typ == list: + return bin.pack_i4( len(v) ) + b''.join( map( lambda v_: pack_pks( pks[0], v_ ), v ) ) + return pks(v) + +def unpack_pks(pks, s): + if not pks: + return (None, s) + typ = type(pks) + if typ == tuple: + lst = [] + for pks_ in pks: + (v, s) = unpack_pks(pks_, s) + lst.append(v) + return (lst, s) + if typ == list: + (n, s) = bin.unpack_i4(s) + pks_ = pks[0] + lst = [] + for i in range(n): + (v, s) = unpack_pks(pks_, s) + lst.append(v) + return (lst, s) + return pks(s) + +def new_client(cmd_infs): + # cmd_infs == list of { 'cmd': cmd, 'args': pks, 'rets': pks } + + cmd_infs_ = cmd_infs + cmd_infs = [] + for d in cmd_infs_: + d = d.copy() + if 'args' in d: + d['args'] = pks_to_funcs_pack( d.get('args') ) + if 'rets' in d: + d['rets'] = pks_to_funcs_unpack( d.get('rets') ) + cmd_infs.append(d) + + e = ut.Empty() + + e.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + e.proc = None + e.fifo_name = '' + + def boot_server(cmd_stdio, port, fifo_name): + cmd_nc = 'nc -l {}'.format(port) + cmd = 'rm -f @ ; mkfifo @ ; cat @ | {} | {} > @'.replace('@', fifo_name) + cmd = cmd.format(cmd_nc, cmd_stdio) + e.proc = ut.cmd_proc(cmd) + ut.sleep(3) # ! + e.sock.connect(('localhost', port)) + e.fifo_name = fifo_name + e.boot_server = boot_server + + read = lambda n: e.sock.recv(n) if n > 0 else '' + write = lambda s: e.sock.sendall(s) + + recv = lambda : read( bin.unpack_i4( read(4) )[0] ) + send = lambda s: write( bin.pack_i4( len(s) ) + s ) + + recv_pks = lambda pks: unpack_pks( pks, recv() ) + send_pks = lambda pks, ret: send( pack_pks( pks, ret ) ) + + cmds = list( map( lambda d: d.get('cmd'), cmd_infs ) ) + + get_cmd_idx = lambda cmd: cmds.index(cmd) if cmd in cmds else -1 + + def call(cmd, args=None): + i = get_cmd_idx(cmd) + if i < 0: + err_msg( '? cmd={}'.format(i) ) + return None + cmd_inf = cmd_infs[i] + write( bin.pack_i4(i) ) + send_pks( cmd_inf.get('args'), args ) + (rets, _) = recv_pks( cmd_inf.get('rets') ) + + if cmd == 'quit': + if e.proc: + ut.kill_proc(e.proc) + if e.fifo_name: + ut.cmd_exec('rm ' + e.fifo_name) + return rets + e.call = call + + return e + +def new_server(cmd_infs): + # cmd_infs == list of { 'cmd': cmd, 'func': f, 'args': pks, 'rets': pks } + + cmd_infs_ = cmd_infs + cmd_infs = [] + for d in cmd_infs_: + d = d.copy() + if 'args' in d: + d['args'] = pks_to_funcs_unpack( d.get('args') ) + if 'rets' in d: + d['rets'] = pks_to_funcs_pack( d.get('rets') ) + cmd_infs.append(d) + + e = ut.Empty() + + err_msg = lambda s: sys.stderr.write(s + '\n') + + read = lambda n: ( sys.stdin.buffer.read(n) if six.PY3 else sys.stdin.read(n) ) if n > 0 else b'' + + def write(s): + if six.PY3: + sys.stdout.buffer.write(s) + else: + sys.stdout.write(s) + sys.stdout.flush() + + recv = lambda : read( bin.unpack_i4( read(4) )[0] ) + send = lambda s: write( bin.pack_i4( len(s) ) + s ) + + recv_pks = lambda pks: unpack_pks( pks, recv() ) + send_pks = lambda pks, ret: send( pack_pks( pks, ret ) ) + + get_cmd_inf = lambda i: cmd_infs[i] if i in range( len(cmd_infs) ) else {} + + def main_loop(): + while True: + (i, _) = bin.unpack_i4( read(4) ) + cmd_inf = get_cmd_inf(i) + if not cmd_inf: + err_msg( '? cmd_idx={:02x}'.format(i) ) + break + (args, _) = recv_pks( cmd_inf.get('args') ) + func = cmd_inf.get('func') + rets = ( func(*args) if args != None else func() ) if func else None + send_pks( cmd_inf.get('rets'), rets ) + if cmd_inf.get('cmd') == 'quit': + break + e.main_loop = main_loop + + return e +# EOF diff -urN rt_v27/sock_test.py rt_v28/sock_test.py --- rt_v27/sock_test.py 1970-01-01 09:00:00.000000000 +0900 +++ rt_v28/sock_test.py 2018-04-23 21:50:00.000000000 +0900 @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +import sys +import ut +import sock + +add = lambda a, b: a+b +add_sub = lambda a, b: (a+b, a-b) +add1 = lambda v: v+1 +quit = lambda : None + +cmd_infs = [ + { 'cmd': 'add', 'func': add, 'args': '(f8,f8)', 'rets': 'f8' }, + { 'cmd': 'add_sub', 'func': add_sub, 'args': '(f8,f8)', 'rets': '(f8,f8)' }, + { 'cmd': 'add1', 'func': add1, 'args': '(i4)', 'rets': 'i4' }, + { 'cmd': 'quit', 'func': quit }, +] + +if __name__ == "__main__": + port = ut.arg_i('port', 34567) + + if 'srv' in sys.argv: + server = sock.new_server(cmd_infs) + server.main_loop() + else: + proc = sock.boot_server( './sock_test.py srv', port, 'sock_test_fifo' ) + ut.sleep(3) + + client = sock.new_client(cmd_infs) + client.conn(port) + + rets = client.call('add1', [123]); + print('rets=', rets) + rets = client.call('add1', [999]); + print('rets=', rets) + rets = client.call('add', [123.45, 111.11]) + print('rets=', rets) + rets = client.call('add_sub', [123.45, 111.11]) + print('rets=', rets) + client.call('quit'); + + ut.kill_proc(proc) +# EOF