-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScopePy_network.py
More file actions
1072 lines (721 loc) · 31.9 KB
/
ScopePy_network.py
File metadata and controls
1072 lines (721 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Sun May 25 20:38:12 2014
@author: john
Scope Py Server/Client interface
=================================
Contains the functions to perform the networking side of the Scope Py application.
Version
==============================================================================
$Revision:: 33 $
$Date:: 2015-04-25 09:08:23 -0400 (Sat, 2#$
$Author:: john $
==============================================================================
"""
#==============================================================================
#%% License
#==============================================================================
"""
Copyright 2015 John Bainbridge
This file is part of ScopePy.
ScopePy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ScopePy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ScopePy. If not, see <http://www.gnu.org/licenses/>.
"""
#=============================================================================
#%% Imports
#=============================================================================
import socket
import logging
from PyQt4.QtCore import *
from PyQt4.QtNetwork import *
import numpy as np
#==============================================================================
#%% Logger
#==============================================================================
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Add do nothing handler
logger.addHandler(logging.NullHandler())
# create console handler and set level to debug
con = logging.StreamHandler()
con.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('[%(asctime)s:%(name)s:%(levelname)s]: %(message)s')
# add formatter to ch
con.setFormatter(formatter)
# add ch to logger
logger.addHandler(con)
#=============================================================================
#%% Constants
#=============================================================================
# Magic number for identifying Scope Py packets
MAGIC_NUMBER = 7616893
# Packet type identifiers
DATA_PACKET = 4001
COMMAND_PACKET = 3456
# uint64 size in bytes
SIZEOF_UINT64 = 8
SOCKET_PORT = 63406
DEBUG = True
#=============================================================================
#%% Data Packet Constructor
#=============================================================================
#Functions for packaging arrays of numbers to be sent to the scope GUI
def makeDataPacket(channelLabel,xdata,ydata,columnNamesList = None):
"""
Convert a numpy array into a packet for sending to Scope Py GUI
Inputs
------
channelLabel = Label for scope channel [string]
xdata,ydata = input data may list or 1D arrays
columnNamesList = list of column names
Outputs
-----------
Packet in string form
The format:
* Packet type number [UINT16] 0-65536
* No. rows [UINT64]
* No. columns [UINT64]
* length of column name text [UINT64]
* Column name text comma delimited [Bytes]
* Data - 2D array float64 converted to byte string
"""
# Get shape of array
# ----------------------
# Check size of input data
assert len(xdata)==len(ydata), "x and y data must be the same length"
# Create numpy array from input data
# make sure the data is arranged in columns
array_in = np.array([xdata,ydata]).transpose()
# Get dimensions
nRows,nCols = array_in.shape
# Check Column names
# ----------------------
if columnNamesList == None:
# Assign default column names
# 'dataset1', 'dataset2' etc
columnNamesList = []
for iCol in range(nCols):
columnNamesList.append('dataset%d' % iCol+1)
# Check column names list is same length as number of columns
assert len(columnNamesList)==nCols, "Column names list does not have the same number of entries as there are columns"
# Add channel Label to column list
# want to put all strings to be transmitted in the same place
columnNamesList.insert(0,channelLabel)
# Construct the data packet
# --------------------------
# Make everything into a string and then pack it together
# initialise packet list
dataPacket = []
# Add Packet type as 64 bit unsigned integer
dataPacket.append( np.uint64(DATA_PACKET).tostring() )
# Add Number of rows as 64 bit unsigned integer
dataPacket.append( np.uint64(nRows).tostring() )
# Add Number of columns as 64 bit unsigned integer
dataPacket.append( np.uint64(nCols).tostring() )
# Add Column names
# Package the column names into a comma delimited list
# Add the length of the column name package first and then the list itself.
colNamePackage = ','.join(columnNamesList)
# Add length of package as 64 bit int
dataPacket.append(np.uint64(len(colNamePackage)).tostring() )
# Add column name package as bytes
dataPacket.append(colNamePackage.encode())
# Add data
dataPacket.append( np.float64(array_in).tostring())
# Return byte string packet by joining all the items in the list
return b''.join(dataPacket)
def getPacketType(packet):
""" packetType,remainingPacket = getPacketType(packet)
Read first unit64 from unwrapped packet string to identify which
packet has been sent. Then return the rest of the packet for extraction
by other functions
Input
--------
packet = byte string packet
Outputs
----------
packetType = numerical packet type as defined at top of this file
remainingPacket = packet with type stripped off
"""
packetType = np.fromstring(packet[0:SIZEOF_UINT64],dtype = 'uint64')
return int(packetType),packet[SIZEOF_UINT64:]
def extractDataPacket(packet):
"""
Extract 2D array data from packet
Inputs
--------
packet = byte string packet as returned by by getPacketType
Outputs
--------
(channelNumber,array_out) = tuple of outputs
Where
channelLabel : str
Scope channel to add data to
array_out = 2D numpy recarray with column names included
"""
# Extract integer data (No. Rows, columns and header string)
# -----------------------------------------------------------------------
# Get all 4 numbers in one shot
# Gives array with [Channel, No. Rows,No. Cols,Length of Column header string]
nH = 3 # Number of integers to extract first
numData = np.fromstring(packet[0:SIZEOF_UINT64*nH],dtype = 'uint64')
nRows = numData[0]
nCols = numData[1]
headerLength = int(numData[2])
# Extract header
# ----------------
# Slice out header, convert from bytes to string and separate by delimiters
headerList = packet[SIZEOF_UINT64*nH:SIZEOF_UINT64*nH+headerLength].decode().split(',')
# Extract channel label as the first item
channelLabel = headerList[0]
# Extract the numerical data
# ---------------------------
# extract the remaining string and convert to float64
# this produces a 1D array
array_out = np.fromstring(packet[SIZEOF_UINT64*nH+headerLength:],dtype='float64')
# resize to original dimensions
array_out = array_out.reshape((nRows,nCols))
# convert to recarray and add column headers
recarray_out = np.core.records.fromarrays(array_out.transpose(),
names = headerList[1:])
return (channelLabel,recarray_out)
#=============================================================================
# %% Client side for sending data
#=============================================================================
def wrapDataPacket(dataPacket):
""" Wrap the data packet for sending over TCIP connection
This just adds the length of the total packet to the front of
the data packet. The server can then read this first and will
then know how long to read data from the connection.
Input
------
dataPacket : str or bytes
output of makeDataPacket()
"""
# Convert to bytes if necessary
# -----------------------------
if not isinstance(dataPacket,bytes):
dataPacket = dataPacket.encode()
# Get length of packet
# ------------------------
pkLen = len(dataPacket)
# Check for empty packets
# return without error for now
if pkLen == 0:
return
# Add to packet
# ------------------------
# Add the total length of the packet including the size of the integer
# that is being added
wrappedPacket = b''.join([np.uint64(pkLen+SIZEOF_UINT64).tostring(),dataPacket])
# Return bytes packet
return wrappedPacket
def sendPacket(hostIP,dataPacket):
""" Send the packet over a TCIP link to the GUI server
Wrap data packet and send. Then close connection.
Inputs
-----------
hostIP = IP address of host
dataPacket = output of makeDataPacket()
TODO : May change this to array in future
Outputs
---------
successFlag = True if packet sent, otherwise False
"""
# Wrap the data packet
# ----------------------------
wrappedPacket = wrapDataPacket(dataPacket)
# Check it worked, otherwise return
if wrappedPacket == None:
return False
# Send packet
# ------------------------------
# Create socket object
s = socket.socket()
# Connect socket to port on server
s.connect((hostIP, SOCKET_PORT))
if DEBUG:
print("SendPacket:Connected to server")
print("SendPacket:Peername : ",s.getpeername())
print("SendPacket:Socket name : ",s.getsockname())
print("SendPacket: Packet length = %d" % len(wrappedPacket))
# Send command as bytes
s.send(wrappedPacket)
# Read reply
reply = readPacket(s)
logger.debug("SendPacket: Reply from server [%s]" % reply)
# Close the socket
s.close()
logger.debug("SendPacket: Connection is closed")
# Return if everything worked
# return reply?
return reply
def send2scope(channel_name,x_data,y_data,x_label,y_label,scope_IP=None):
"""
Send x,y data to a scope channel
This is the basic "plot" command
Inputs
--------
channel_name : str
The channel name under which the data will appear in on the scope
screen.
x_data : list or 1D numpy array
x axis data
y_data : list or 1D numpy array
y axis data
x_label : str
label for x axis
y_label : str
label for y axis
scope_IP : str
IP address of the computer where ScopePy is running.
If not specified then it is assumed that it is the same computer.
Outputs
--------
success : bool
returns True if a successful transfer
"""
# Get Scope IP address
# ----------------------
if not scope_IP:
scope_IP = socket.gethostname()
# Validate data TODO
# -------------------
assert len(x_data)==len(y_data), "ScopePy: x and y data are different lengths"
# Make data packet
# ------------------
dataPacket = makeDataPacket(channel_name,x_data,y_data,[x_label,y_label])
# Send data to scope
# -------------------
return sendPacket(scope_IP,dataPacket)
#=============================================================================
#%% Test server
#=============================================================================
def testServer():
"""
Simple non-QT4 server for testing the client functions
Prints output to console for debugging
TODO : Is out of date with packets now
"""
# Setup the socket
# -------------------------------------------------
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
s.bind((host, SOCKET_PORT)) # Bind to the port
print("Initialising ScopePy test server\n---------------------")
s.listen(5) # Now wait for client connection.
# Main Server loop
# -----------------------------------------------
while True: # Loop forever, listening for client connections
print("Listening\n")
c, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
# Read packet from connection
# TODO : this should be passed to a thread
readPacket(c)
# Close the connection
# TODO should this be done here or in readPackets ?
c.close()
print("Closing connection")
def testPacket(channel = "Test Packet"):
""" Generate a test packet for testing client/server connection
Input
------
channel = string for channel name
"""
# Make standard data array
x = np.arange(0,10)
y = (x**2)*1.27 + np.random.random_sample(x.shape)*5
columnNamesList = ['x axis text','y axis text']
return makeDataPacket(channel,x,y,columnNamesList)
#=============================================================================
#%% Server Packet reading
#=============================================================================
def readDataPacket(conn):
"""
Read packet from client connection.
First get the length of the data, then read the rest of the packet
Input
-----------
conn : socket
socket passed from server
Output
----------
"""
if DEBUG:
print("Reading packet ...")
# Read packet length
# ---------------------
# Keep reading until we have the first uint64 number from
# the packet
packet = b''
while len(packet) < SIZEOF_UINT64:
# Get data from socket
chunk = conn.recv(SIZEOF_UINT64-len(packet))
# If we received nothing then assume socket
# has been lost
if chunk == b'':
raise RuntimeError("socket connection broken : reading packet length")
# Data received - add it to the packet
packet = packet + chunk
# Packet should now contain the length of the total packet
# decode the length
packetLength = np.fromstring(packet[0:SIZEOF_UINT64],dtype = 'uint64')
if DEBUG:
print("\tPacket Length = %d" % packetLength)
print("\tReading rest of packet ...")
# Read the rest of the packet
# -------------------------------
while len(packet) < packetLength:
# Get data from socket
chunk = conn.recv(packetLength-len(packet))
# If we received nothing then assume socket
# has been lost
if chunk == b'':
raise RuntimeError("socket connection broken : reading packet")
# Data received - add it to the packet
packet = packet + chunk
# TODO : translate packet back into numbers
if DEBUG:
print("\tPacket received [%d bytes]:" % len(packet))
#print(packet)
# Extract data from packet
array_out = extractDataPacket(packet[SIZEOF_UINT64:])
if DEBUG:
print("Numerical data extracted:")
print(array_out)
def readPacket(conn):
"""
Read packet from socket.
First get the length of the data, then read the rest of the packet
Input
-----------
conn = socket passed from server
Output:
----------
packet : str
received packet in string form
"""
# # Read Magic number
# # ---------------------
# # Keep reading until we have the first uint64 number from
# # the packet
# packet_magic_number = b''
#
# while len(packet_magic_number) < SIZEOF_UINT64:
# # Get data from socket
# chunk = conn.recv(SIZEOF_UINT64-len(packet_magic_number))
#
# # If we received nothing then assume socket
# # has been lost
# if chunk == b'':
# raise RuntimeError("socket connection broken : reading magic number")
#
# # Data received - add it to the packet
# packet_magic_number = packet_magic_number + chunk
#
# # Check this is the correct number
# # if not then drop the connection
# magic_number = np.fromstring(packet_magic_number[0:SIZEOF_UINT64],dtype = 'uint64')
#
# if magic_number != MAGIC_NUMBER:
# if DEBUG:
# print("Unknown packet [%d] : dropping connection" % magic_number)
# return
#
#
# if DEBUG:
# print("Reading packet ...")
# Read packet length
# ---------------------
# Keep reading until we have the first uint64 number from
# the packet
packet = b''
while len(packet) < SIZEOF_UINT64:
# Get data from socket
chunk = conn.recv(SIZEOF_UINT64-len(packet))
# If we received nothing then assume socket
# has been lost
if chunk == b'':
raise RuntimeError("readPacket: socket connection broken : reading packet length")
# Data received - add it to the packet
packet = packet + chunk
# Packet should now contain the length of the total packet
# decode the length
packetLength = np.fromstring(packet[0:SIZEOF_UINT64],dtype = 'uint64')
logger.debug("readPacket: Packet Length = %d" % packetLength)
logger.debug("readPacket: Reading rest of packet ...")
# Read the rest of the packet
# -------------------------------
while len(packet) < packetLength:
# Get data from socket
chunk = conn.recv(packetLength-len(packet))
# If we received nothing then assume socket
# has been lost
if chunk == b'':
raise RuntimeError("socket connection broken : reading packet")
# Data received - add it to the packet
packet = packet + chunk
logger.debug("readPacket: Packet received [%d bytes]:" % len(packet))
if DEBUG:
logger.debug("Packet=\n%s" % packet[SIZEOF_UINT64:])
return packet[SIZEOF_UINT64:].decode('utf-8')
#=============================================================================
#%% QT4 Server
#=============================================================================
class TcpServer(QTcpServer):
""" Derived server class used to overload the 'incomingConnection' method
so that it points to a socket class that handles the reading of
custom packets
Ref : Mark Summerfield, "Rapid GUI Programming in Python and QT4"
"""
def __init__(self,parent=None):
# Initialise the base class
super(TcpServer,self).__init__(parent)
def incomingConnection(self,socketId):
""" Re-implementation of method.
This gets called when a connection is available at the server
"""
if DEBUG:
print("IncomingConnection detected")
# Straight from the book
socket = Socket(self,upLoadFunction = self.uploadDataArray)
socket.setSocketDescriptor(socketId)
def uploadDataArray(self,dataArray):
""" Emit a signal with the uploaded data array in order to
send the data back to the main GUI.
This is the method of getting the data out of the server and back
to the main GUI.
The basic flow is:
* Server receives connection
* Server passes connection to Socket class, plus a reference to this function
* Socket class reads the data and converts it to an output form
* Socket calls this function, which makes the server emit a signal
back to the main GUI.
It's a bit convoluted,but it was all I could think of for passing the
data back up!
"""
if DEBUG:
print("Sending UpLoad data array signal")
self.emit(SIGNAL("UpLoadChannelData"), dataArray)
class Socket(QTcpSocket):
"""
Custom socket handler for our packet format
"""
def __init__(self, parent=None,upLoadFunction=None):
super(Socket, self).__init__(parent)
# Connect socket to custom read response method
self.connect(self, SIGNAL("readyRead()"),self.readPacket)
self.connect(self, SIGNAL("disconnected()"), self.deleteLater)
self.nextBlockSize = 0
self.upLoadFunction = upLoadFunction
def readPacket(self):
""" Read wrapped packet using QT4 functions
Output
------
dataPacket = byte string packet ready for extractDataPacket()
"""
if DEBUG:
print("Reading packet")
# Create a stream object to read bytes into
# ------------------------------------------
stream = QDataStream(self)
stream.setVersion(QDataStream.Qt_4_2)
# Read first uint64 from packet to get the packet length
# -------------------------------------------------------
# Wait until we have enough bytes to make a 64bit Unsigned integer
if self.nextBlockSize == 0:
if self.bytesAvailable() < SIZEOF_UINT64:
print("\tBytes less than SIZEOF_UINT64")
# Not enough bytes accumulated, so return
return
# Read in enough bytes to make a uint64
plBytes = stream.readRawData(SIZEOF_UINT64)
print("Bytes read:")
print(plBytes)
# Convert bytes to uint64 using numpy
packetLength = int(np.fromstring(plBytes, dtype=np.uint64))
# Read the size of the rest of the packet
self.nextBlockSize = packetLength - SIZEOF_UINT64
print("Packet length = %i" % packetLength)
print("Reading rest of packet [%i]" % self.nextBlockSize)
# Read rest of packet
# --------------------------------------------
# Wait for all the bytes to arrive
if self.bytesAvailable() < self.nextBlockSize:
print("\tBytes less than packet size")
return
# Read the packet
# Read in the raw bytes
packet = stream.readRawData(self.nextBlockSize)
# Get the packet type
packetType,remainingPacket = getPacketType(packet)
if DEBUG:
print("\tPacket type = %d" % packetType)
# Select the action for each packet type
if packetType == DATA_PACKET:
# Extract numerical data and channel label
data4scope = extractDataPacket(remainingPacket)
if DEBUG:
print("Data packet received [size = %d bytes]" % len(remainingPacket))
print(remainingPacket)
print("\n\nNumerical data is:")
print(data4scope[1])
# Upload the array to calling function
self.upLoadFunction(data4scope)
#=============================================================================
#%% QT4 Threaded Server
#=============================================================================
# Threaded version of TcpServer
# intended as the main server when complete
#
# ref: Mark Summerfield, Rapid GUI programming with Python & QT4, Chap 19
class ThreadedTcpServer(QTcpServer):
def __init__(self, parent=None,channel_lock=None,
upload_function=None):
super(ThreadedTcpServer, self).__init__(parent)
#self.uploadFunction = None
# Thread locking variable for channel dictionary
self.channel_lock = channel_lock
self.upload_function = upload_function
def incomingConnection(self, socketId):
# Create a thread to process the incoming socket
thread = SocketThread(socketId, self,upLoadFunction=self.uploadDataArray,
lock=self.channel_lock,
commandUploadFunction=self.sendCmd2API)
self.connect(thread, SIGNAL("finished()"),
thread, SLOT("deleteLater()"))
# Start the thread - eventually executes the threads run method
thread.start()
def uploadDataArray(self,dataArray):
""" Emit a signal with the uploaded data array in order to
send the data back to the main GUI.
This is the method of getting the data out of the server and back
to the main GUI.
The basic flow is:
* Server receives connection
* Server passes connection to Socket class, plus a reference to this function
* Socket class reads the data and converts it to an output form
* Socket calls this function, which makes the server emit a signal
back to the main GUI.singlePacket(hostIP)
It's a bit convoluted,but it was all I could think of for passing the
data back up!
"""
logger.debug("Sending UpLoad data array signal")
self.emit(SIGNAL("UpLoadChannelData"), dataArray)
def sendCmd2API(self,socket,command_packet):
"""
Emit signal to ScopePy API
This is to pass a command packet to the main program
Basic flow:
* Command comes in to server
* Server receives packet on a thread and sends it to this function
* Signal is emitted to main program
* Ends the thread
Input
------
socket : QTcpSocket
connection to the client
command_packet : byte string
raw command packet
TODO : pass socket here
"""
logger.debug("Send command from server to main API")
self.emit(SIGNAL("UpLoadCommandPacket"),socket,command_packet)
class SocketThread(QThread):
"""
Class for handling incoming data in a separate thread
"""
# TODO : Could try a QMutex instead.
def __init__(self, socketId, parent,upLoadFunction=None,lock=None,
commandUploadFunction=None):
super(SocketThread, self).__init__(parent)
self.socketId = socketId
# Store upload function
self.upLoadFunction = upLoadFunction
# Channel dictionary thread locker
self.channel_lock = lock
# Command upload
self.commandUploadFunction = commandUploadFunction
def run(self):
"""
Process the data from the socket
"""
# Create a socket and check its ID
# --------------------------------------
socket = QTcpSocket()
if not socket.setSocketDescriptor(self.socketId):
self.emit(SIGNAL("error(int)"), socket.error())
return
# Read packet data from socket
# --------------------------------
while socket.state() == QAbstractSocket.ConnectedState:
nextBlockSize = 0
# Create a stream to read the packet
stream = QDataStream(socket)
stream.setVersion(QDataStream.Qt_4_2)
# Read packet length from first 64bit integer
if (socket.waitForReadyRead() and
socket.bytesAvailable() >= SIZEOF_UINT64):
# Read the 64 bits in byte form
plBytes = stream.readRawData(SIZEOF_UINT64)
# Convert bytes to uint64 using numpy
packetLength = int(np.fromstring(plBytes, dtype=np.uint64))
# Read the size of the rest of the packet
nextBlockSize = packetLength - SIZEOF_UINT64
else:
#self.sendError(socket, "Cannot read client request")
return
# Read in the rest of the packet