summaryrefslogtreecommitdiffstats
path: root/Under-Testing/Server-Code-New/ClientClass.py
diff options
context:
space:
mode:
authorgsmselftest2011-11-16 18:06:02 +0100
committergsmselftest2011-11-16 18:06:02 +0100
commit8a7755d6f96e599fe75ac8e5e1c6994138f4b99e (patch)
tree9b320feb695cfbadd605808b2db5795e240e9399 /Under-Testing/Server-Code-New/ClientClass.py
parent delete dummy comment (diff)
downloadgsm-selftest-8a7755d6f96e599fe75ac8e5e1c6994138f4b99e.tar.gz
gsm-selftest-8a7755d6f96e599fe75ac8e5e1c6994138f4b99e.tar.xz
gsm-selftest-8a7755d6f96e599fe75ac8e5e1c6994138f4b99e.zip
under Testing folder
Diffstat (limited to 'Under-Testing/Server-Code-New/ClientClass.py')
-rw-r--r--Under-Testing/Server-Code-New/ClientClass.py123
1 files changed, 123 insertions, 0 deletions
diff --git a/Under-Testing/Server-Code-New/ClientClass.py b/Under-Testing/Server-Code-New/ClientClass.py
new file mode 100644
index 0000000..46c3b00
--- /dev/null
+++ b/Under-Testing/Server-Code-New/ClientClass.py
@@ -0,0 +1,123 @@
+import socket
+import sys
+import os
+import string
+import signal
+
+class TimeoutException(Exception):
+ pass
+
+class Connection:
+ def __init__(self, h, p):
+ self.host = h
+ self.port = p
+ self.s = None
+ self.connected = 0
+
+ self.debugMode = 0
+
+ def connect(self):
+ self.s = None
+
+ for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
+ af, socktype, proto, canonname, sa = res
+ try:
+ self.s = socket.socket(af, socktype, proto)
+ self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #this resolves the bug with live packets
+ except socket.error, msg:
+ self.s = None
+ self.connected = 0
+ continue
+ try:
+ self.s.connect(sa)
+ except socket.error, msg:
+ self.s.close()
+ self.connected = 0
+ self.s = None
+ continue
+ break
+ if self.s is None:
+ self.connected = 0
+ return 0 #couldn't connect to the server
+ else:
+ self.connected = 1
+ return 1 #successfully connected to the server
+
+ def sendData(self, data):
+ if self.connected == 1:
+ try:
+ self.s.send(data)
+ return 1
+ except Exception, e:
+ if self.debugMode == 1:
+ import traceback
+ print traceback.format_exc()
+ print e
+ self.connected = 0
+ return 2
+
+ else:
+ return 0
+
+ def receiveData(self, timeout):
+ if self.connected == 1:
+
+ def timeout_handler(signum, frame):
+ raise TimeoutException()
+
+ old_handler = signal.signal(signal.SIGALRM, timeout_handler)
+ signal.alarm(timeout) #start the timeout alarm, for timeout seconds
+ try:
+ data = self.s.recv(1024)
+
+ #stop the timeout function
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+
+ return data
+
+
+ except TimeoutException:
+ #timeout happened
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'TIMEOUT'
+
+ except Exception, e:
+
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+
+ error = str(e)
+ if self.debugMode == 1:
+ import traceback
+ print traceback.format_exc()
+ print e
+ self.connected = 0
+ if error[0:11] == '[Errno 104]':
+ return 3 #the other side reset the connection,[Errno 104] Connection reset by peer
+
+ return 2
+ else:
+ return 0
+
+ def closeConnection(self):
+ if self.connected == 1:
+ try:
+ self.connected = 0
+ SHUT_RDWR = 2
+ self.s.shutdown(SHUT_RDWR)
+ self.s.close()
+ return 1
+ except Exception, e:
+ self.connected = 0
+ error = str(e)
+ if self.debugMode == 1:
+ import traceback
+ print traceback.format_exc()
+ print e
+ if error[0:11] == '[Errno 107]':
+ return 3 #the other side closed the connection before us, [Errno 107] Transport endpoint is not connected
+ return 2
+ else:
+ return 0