socket_protocol/_examples_/socket_protocol_server.py
2021-01-12 09:14:09 +01:00

43 lines
1.2 KiB
Python

import sys
sys.path.append('../..')
import report
import socket_protocol
import tcp_socket
import time
DID_ASC_TIME = 0
class example_protocol(socket_protocol.pure_json_protocol):
def __init__(self, *args, **kwargs):
socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
#
self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_ASC_TIME, 'asc_time')
#
if not self.__comm_inst__.IS_CLIENT:
self.register_callback(socket_protocol.SID_READ_REQUEST, 0, self.time_callback)
def time_callback(self, msg):
if msg.get_status() == socket_protocol.STATUS_OKAY:
return socket_protocol.STATUS_OKAY, time.asctime()
else:
return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
if __name__ == '__main__':
report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
sp = example_protocol(s, channel_name='example_server')
i = 0
while not s.is_connected() and i <= 20:
i += 1
time.sleep(.1) # wait for a connection
i = 0
while s.is_connected() and i <= 20:
i += 1
time.sleep(.1) # wait for disconnect