diff options
author | Determinant <ted.sybil@gmail.com> | 2014-12-29 00:34:16 +0800 |
---|---|---|
committer | Determinant <ted.sybil@gmail.com> | 2014-12-29 00:34:16 +0800 |
commit | 4c7c9dcdfd05a2d9648756c0da5c68588929ba5d (patch) | |
tree | 2c1681ad60e1469b4dda7a2ab6a0d03637dbde1e /lab_monitor_client.py | |
parent | 5dc3a3b043a990fc58a4ead74c2c5c92e910727d (diff) |
restructured the project
Diffstat (limited to 'lab_monitor_client.py')
-rw-r--r-- | lab_monitor_client.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lab_monitor_client.py b/lab_monitor_client.py new file mode 100644 index 0000000..194b927 --- /dev/null +++ b/lab_monitor_client.py @@ -0,0 +1,51 @@ +from socket import socket, AF_UNIX +from struct import pack, unpack +from sys import stdout, stdin +import argparse +import json +parser = argparse.ArgumentParser() +parser.add_argument('--create') +parser.add_argument('--add') +parser.add_argument('--clear') +parser.add_argument('--drop') +parser.add_argument('--alter') +parser.add_argument('--type') +parser.add_argument('col', nargs='*') +args = parser.parse_args() + +def check_id(val): + try: + return int(val) + except ValueError: + print('invalid mid value') + exit(1) + +if __name__ == '__main__': + if args.create: + cmd = {'action': 'create'} + if len(args.create) >= 1: + cmd["name"] = args.create + if args.type is None: + print('please specify a type') + exit(1) + cmd["metadata"] = {'type' : args.type} + elif args.add: + cmd = {'action': 'add', 'jid': check_id(args.add), 'record' : args.col} + elif args.clear: + cmd = {'action': 'clear', 'jid': check_id(args.clear)} + elif args.drop: + cmd = {'action': 'drop', 'jid': check_id(args.drop)} + elif args.alter: + cmd = {'action': 'alter', 'jid': check_id(args.alter), + 'records' : [l[:-1].split() for l in stdin.readlines()]} + else: + print('please specify an action') + exit(1) + + s = socket(AF_UNIX) + s.connect("./lab_monitor.socket") + mesg = json.dumps(cmd) + mesg = pack("<i", len(mesg)) + mesg + s.send(mesg) + stdout.write(s.recv(1024)) + s.close() |