41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import sys # nopep8
|
|
sys.path.append('../..') # nopep8
|
|
|
|
import time
|
|
import tcp_socket
|
|
import socket_protocol
|
|
import report
|
|
|
|
|
|
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.default_logging_config()
|
|
|
|
# Start server
|
|
s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
|
|
sps = example_protocol(s, channel_name='example_server')
|
|
|
|
# Start client
|
|
c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
|
|
spc = example_protocol(c, channel_name='example_client')
|
|
spc.send(socket_protocol.SID_READ_REQUEST, DID_ASC_TIME, None)
|
|
print('The Client received: %s' % repr(spc.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data()))
|