From 945d7283413fd5739b49d161f85c37184ae9c5c7 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Tue, 12 Jan 2021 09:14:09 +0100 Subject: [PATCH] Examples adapted --- _examples_/socket_protocol_client.py | 14 +++++---- _examples_/socket_protocol_server.py | 46 ++++++++++++++++++---------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/_examples_/socket_protocol_client.py b/_examples_/socket_protocol_client.py index c6b80f8..38246e9 100644 --- a/_examples_/socket_protocol_client.py +++ b/_examples_/socket_protocol_client.py @@ -1,14 +1,16 @@ import sys sys.path.append('../..') + import report import socket_protocol +from socket_protocol_server import example_protocol, DID_ASC_TIME import tcp_socket import time - -report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) -c = tcp_socket.tcp_client_stp('127.0.0.1', 17017) -sp = socket_protocol.pure_json_protocol(c, channel_name='example_client') -sp.send(socket_protocol.SID_READ_REQUEST, 0, None) -print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data())) +if __name__ == '__main__': + report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) + c = tcp_socket.tcp_client_stp('127.0.0.1', 17017) + sp = example_protocol(c, channel_name='example_client') + sp.send(socket_protocol.SID_READ_REQUEST, DID_ASC_TIME, None) + print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data())) diff --git a/_examples_/socket_protocol_server.py b/_examples_/socket_protocol_server.py index 391bf69..ed73471 100644 --- a/_examples_/socket_protocol_server.py +++ b/_examples_/socket_protocol_server.py @@ -7,24 +7,36 @@ import tcp_socket import time -def time_callback(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 +DID_ASC_TIME = 0 -report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) -s = tcp_socket.tcp_server_stp('127.0.0.1', 17017) -sp = socket_protocol.pure_json_protocol(s, channel_name='example_server') -sp.register_callback(socket_protocol.SID_READ_REQUEST, 0, time_callback) +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) -i = 0 -while not s.is_connected() and i <= 20: - i += 1 - time.sleep(.1) # wait for a connection + 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 -i = 0 -while s.is_connected() and i <= 20: - i += 1 - time.sleep(.1) # wait for disconnect + +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