Selaa lähdekoodia

Examples adapted

master
Dirk Alders 4 vuotta sitten
vanhempi
commit
945d728341
2 muutettua tiedostoa jossa 41 lisäystä ja 27 poistoa
  1. 8
    6
      _examples_/socket_protocol_client.py
  2. 33
    21
      _examples_/socket_protocol_server.py

+ 8
- 6
_examples_/socket_protocol_client.py Näytä tiedosto

@@ -1,14 +1,16 @@
1 1
 import sys
2 2
 sys.path.append('../..')
3 3
 
4
+
4 5
 import report
5 6
 import socket_protocol
7
+from socket_protocol_server import example_protocol, DID_ASC_TIME
6 8
 import tcp_socket
7 9
 import time
8 10
 
9
-
10
-report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
11
-c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
12
-sp = socket_protocol.pure_json_protocol(c, channel_name='example_client')
13
-sp.send(socket_protocol.SID_READ_REQUEST, 0, None)
14
-print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data()))
11
+if __name__ == '__main__':
12
+    report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
13
+    c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
14
+    sp = example_protocol(c, channel_name='example_client')
15
+    sp.send(socket_protocol.SID_READ_REQUEST, DID_ASC_TIME, None)
16
+    print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data()))

+ 33
- 21
_examples_/socket_protocol_server.py Näytä tiedosto

@@ -7,24 +7,36 @@ import tcp_socket
7 7
 import time
8 8
 
9 9
 
10
-def time_callback(msg):
11
-    if msg.get_status() == socket_protocol.STATUS_OKAY:
12
-        return socket_protocol.STATUS_OKAY, time.asctime()
13
-    else:
14
-        return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
15
-
16
-
17
-report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
18
-s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
19
-sp = socket_protocol.pure_json_protocol(s, channel_name='example_server')
20
-sp.register_callback(socket_protocol.SID_READ_REQUEST, 0, time_callback)
21
-
22
-i = 0
23
-while not s.is_connected() and i <= 20:
24
-    i += 1
25
-    time.sleep(.1)  # wait for a connection
26
-
27
-i = 0
28
-while s.is_connected() and i <= 20:
29
-    i += 1
30
-    time.sleep(.1)  # wait for disconnect
10
+DID_ASC_TIME = 0
11
+
12
+
13
+class example_protocol(socket_protocol.pure_json_protocol):
14
+    def __init__(self, *args, **kwargs):
15
+        socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
16
+        #
17
+        self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_ASC_TIME, 'asc_time')
18
+        #
19
+        if not self.__comm_inst__.IS_CLIENT:
20
+            self.register_callback(socket_protocol.SID_READ_REQUEST, 0, self.time_callback)
21
+
22
+    def time_callback(self, msg):
23
+        if msg.get_status() == socket_protocol.STATUS_OKAY:
24
+            return socket_protocol.STATUS_OKAY, time.asctime()
25
+        else:
26
+            return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
27
+
28
+
29
+if __name__ == '__main__':
30
+    report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
31
+    s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
32
+    sp = example_protocol(s, channel_name='example_server')
33
+
34
+    i = 0
35
+    while not s.is_connected() and i <= 20:
36
+        i += 1
37
+        time.sleep(.1)  # wait for a connection
38
+
39
+    i = 0
40
+    while s.is_connected() and i <= 20:
41
+        i += 1
42
+        time.sleep(.1)  # wait for disconnect

Loading…
Peruuta
Tallenna