Examples adapted

This commit is contained in:
Dirk Alders 2021-01-12 09:14:09 +01:00
parent e92601c6dd
commit 945d728341
2 changed files with 37 additions and 23 deletions

View File

@ -1,14 +1,16 @@
import sys import sys
sys.path.append('../..') sys.path.append('../..')
import report import report
import socket_protocol import socket_protocol
from socket_protocol_server import example_protocol, DID_ASC_TIME
import tcp_socket import tcp_socket
import time import time
if __name__ == '__main__':
report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
c = tcp_socket.tcp_client_stp('127.0.0.1', 17017) c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
sp = socket_protocol.pure_json_protocol(c, channel_name='example_client') sp = example_protocol(c, channel_name='example_client')
sp.send(socket_protocol.SID_READ_REQUEST, 0, None) 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())) print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data()))

View File

@ -7,24 +7,36 @@ import tcp_socket
import time import time
def time_callback(msg): DID_ASC_TIME = 0
if msg.get_status() == socket_protocol.STATUS_OKAY:
return socket_protocol.STATUS_OKAY, time.asctime()
else:
return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) class example_protocol(socket_protocol.pure_json_protocol):
s = tcp_socket.tcp_server_stp('127.0.0.1', 17017) def __init__(self, *args, **kwargs):
sp = socket_protocol.pure_json_protocol(s, channel_name='example_server') socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
sp.register_callback(socket_protocol.SID_READ_REQUEST, 0, time_callback) #
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 def time_callback(self, msg):
while not s.is_connected() and i <= 20: if msg.get_status() == socket_protocol.STATUS_OKAY:
i += 1 return socket_protocol.STATUS_OKAY, time.asctime()
time.sleep(.1) # wait for a connection else:
return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
i = 0
while s.is_connected() and i <= 20: if __name__ == '__main__':
i += 1 report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
time.sleep(.1) # wait for disconnect 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