respond ok to add and remove commands

This commit is contained in:
clowwindy 2015-08-06 19:44:59 +08:00
parent 77f9979b2e
commit c8b3f71e1b

View file

@ -114,14 +114,17 @@ class Manager(object):
command, config = parsed command, config = parsed
a_config = self._config.copy() a_config = self._config.copy()
if config: if config:
# let the command override the configuration file
a_config.update(config) a_config.update(config)
if 'server_port' not in a_config: if 'server_port' not in a_config:
logging.error('can not find server_port in config') logging.error('can not find server_port in config')
else: else:
if command == 'add': if command == 'add':
self.add_port(a_config) self.add_port(a_config)
self._send_control_data(b'ok')
elif command == 'remove': elif command == 'remove':
self.remove_port(a_config) self.remove_port(a_config)
self._send_control_data(b'ok')
elif command == 'ping': elif command == 'ping':
self._send_control_data(b'pong') self._send_control_data(b'pong')
else: else:
@ -152,6 +155,7 @@ class Manager(object):
def send_data(data_dict): def send_data(data_dict):
if data_dict: if data_dict:
# use compact JSON format (without space)
data = common.to_bytes(json.dumps(data_dict, data = common.to_bytes(json.dumps(data_dict,
separators=(',', ':'))) separators=(',', ':')))
self._send_control_data(b'stat: ' + data) self._send_control_data(b'stat: ' + data)
@ -159,6 +163,7 @@ class Manager(object):
for k, v in self._statistics.items(): for k, v in self._statistics.items():
r[k] = v r[k] = v
i += 1 i += 1
# split the data into segments that fit in UDP packets
if i >= STAT_SEND_LIMIT: if i >= STAT_SEND_LIMIT:
send_data(r) send_data(r)
r.clear() r.clear()
@ -229,9 +234,14 @@ def test():
cli.send(b'add: {"server_port":7001, "password":"1234"}') cli.send(b'add: {"server_port":7001, "password":"1234"}')
time.sleep(1) time.sleep(1)
assert 7001 in manager._relays assert 7001 in manager._relays
data, addr = cli.recvfrom(1506)
assert b'ok' in data
cli.send(b'remove: {"server_port":8381}') cli.send(b'remove: {"server_port":8381}')
time.sleep(1) time.sleep(1)
assert 8381 not in manager._relays assert 8381 not in manager._relays
data, addr = cli.recvfrom(1506)
assert b'ok' in data
logging.info('add and remove test passed') logging.info('add and remove test passed')
# test statistics for TCP # test statistics for TCP