from socket import *
import sys, time
import pytia

# TODO tidy this up...

if __name__ == "__main__":
    c = pytia.TiAClient('localhost', 9000)
    c.connect()
    if not c.cmd_check_protocol_version():
        print('Protocol version check failed!')
        sys.exit(-1)
    status, dport = c.cmd_get_data_connection_tcp()
    if not c.start_streaming_data_tcp('localhost', dport):
        print('Failed to start streaming data')
        sys.exit(-1)

    time.sleep(0.1)
    for i in range(10):
        packets = c.get_data()
        print('')
        print('Retrieved %d packets' % (len(packets)))
        if len(packets) > 0:
            print('%d signals in packet' % (len(packets[0])))

            for i in range(len(packets[0])):
                print('Signal %d:' % (i+1), packets[0][i])

        print('')

    c.stop_streaming_data()
    c.disconnect()


@@ -30,16 +30,24 @@
 ys, time
+, random
 %0Aimport 
@@ -59,115 +59,791 @@
 %0A%0A# 
-TODO tidy this up...%0A%0Aif __name__ == %22__main__%22:%0A    c = pytia.TiAClient('localhost', 9000)%0A    c.connect()
+Simple demo of TiA client/server comms process%0A%0Adef data_callback(signalid):%0A    return %5Brandom.uniform(-1000, 1000) for x in range(3)%5D%0A%0Aif __name__ == %22__main__%22:%0A    # create a server listening on port 9000 and configured with a 3 channel,%0A    # 100Hz signal (containing random data)%0A    server = pytia.TiAServer(('', 9000), pytia.TiAConnectionHandler)%0A    server.start(%5Bpytia.TiASignalConfig(3, 100, 1, data_callback, 'signalid', pytia.TIA_SIG_USER_1)%5D)%0A%0A    # (server is now running in a background thread)%0A%0A    # create a client and connect to the server%0A    client = pytia.TiAClient('localhost', 9000)%0A    if not client.connect():%0A        print('Failed to connect to server')%0A        sys.exit(-1)%0A%0A    # TiA clients must start by checking server protocol version against theirs
 %0A   
@@ -851,16 +851,21 @@
 if not c
+lient
 .cmd_che
@@ -943,28 +943,519 @@
         
-sys.exit(-1)
+client.disconnect()%0A        sys.exit(-1)%0A%0A    # The client should now technically use the 'get metainfo' command to %0A    # retrieve the signal information the server  has been configured with.%0A    # TODO this doesn't actually return the actual metainfo data yet%0A    metainfo = client.cmd_get_metainfo()%0A%0A    # next, clients should request a TCP data connection (UDP not supported)%0A    # the server will respond with a port number that the client should connect%0A    # to in order to start streaming data.
 %0A    sta
@@ -1468,16 +1468,21 @@
 port = c
+lient
 .cmd_get
@@ -1519,169 +1519,657 @@
 not 
-c.start_streaming_data_tcp('localhost', dport):%0A        print('Failed to start streaming data')%0A        sys.exit(-1)%0A%0A    time.sleep(0.1)%0A    for i in range(10):
+status:%0A        print('Failed to get data connection')%0A        client.disconnect()%0A        sys.exit(-1)%0A%0A    # the client connects and requests that the server begin streaming data%0A    if not client.start_streaming_data_tcp('localhost', dport):%0A        print('Failed to start streaming data')%0A        client.disconnect()%0A        sys.exit(-1)%0A%0A    # once connected, the server continues streaming data until told to stop%0A    for i in range(10):%0A        # calling the get_data function returns the most recent data received%0A        # from the server. depending on the buffer size used (TODO?) there may%0A        # be multiple TiA packets returned here.
 %0A   
@@ -2184,16 +2184,21 @@
 kets = c
+lient
 .get_dat
@@ -2216,16 +2216,26 @@
  print('
+----------
 ')%0A     
@@ -2489,28 +2489,107 @@
     
-    print('')%0A
+# the client should tell the server to stop streaming before %0A    # disconnecting itself
 %0A    c
+lient
 .sto
@@ -2612,16 +2612,21 @@
 ()%0A    c
+lient
 .disconn

improve test script + comment