summaryrefslogtreecommitdiffstats
path: root/notFinishedCode
diff options
context:
space:
mode:
authorRefik Hadzialic2011-07-17 14:14:33 +0200
committerRefik Hadzialic2011-07-17 14:14:33 +0200
commit3136c97b242dc10061258731ae8ff88286996a44 (patch)
treea42288a211de7f069588765dfd639999df9a4ea7 /notFinishedCode
parentSince someone deleted the ping class I had to move it back here! (diff)
downloadgsm-selftest-3136c97b242dc10061258731ae8ff88286996a44.tar.gz
gsm-selftest-3136c97b242dc10061258731ae8ff88286996a44.tar.xz
gsm-selftest-3136c97b242dc10061258731ae8ff88286996a44.zip
I added timeout to the receiving data procedure!
Diffstat (limited to 'notFinishedCode')
-rw-r--r--notFinishedCode/ClientClass.py29
-rw-r--r--notFinishedCode/ServerClass.py43
2 files changed, 63 insertions, 9 deletions
diff --git a/notFinishedCode/ClientClass.py b/notFinishedCode/ClientClass.py
index 38ff475..46c3b00 100644
--- a/notFinishedCode/ClientClass.py
+++ b/notFinishedCode/ClientClass.py
@@ -2,6 +2,10 @@ import socket
import sys
import os
import string
+import signal
+
+class TimeoutException(Exception):
+ pass
class Connection:
def __init__(self, h, p):
@@ -55,12 +59,35 @@ class Connection:
else:
return 0
- def receiveData(self):
+ 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
diff --git a/notFinishedCode/ServerClass.py b/notFinishedCode/ServerClass.py
index b772f0a..93c2f8e 100644
--- a/notFinishedCode/ServerClass.py
+++ b/notFinishedCode/ServerClass.py
@@ -2,6 +2,10 @@ import socket
import sys
import os
import string
+import signal
+
+class TimeoutException(Exception):
+ pass
class ServerHandler:
@@ -54,17 +58,40 @@ class ServerHandler:
def connectedTo(self):
return self.address
- def receiveData(self):
+ def receiveData(self, timeout):
if self.connected == 1:
+
+ def timeout_handler(signum, frame):
+ raise TimeoutException()
+
try:
- while 1:
- data = self.connection.recv(1024)
- if not data:
- self.connected = 0
- return 'NO DATA'
- else:
- return data
+
+ old_handler = signal.signal(signal.SIGALRM, timeout_handler)
+ signal.alarm(timeout) #start the timeout alarm, for timeout seconds
+
+ data = self.connection.recv(1024)
+
+ #stop the timeout function
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+
+ if not data:
+ self.connected = 0
+ return 'NO DATA'
+ else:
+ return data
+
+ except TimeoutException:
+ #timeout happened
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'TIMEOUT'
+
except Exception, e:
+ #stop the timeout timer
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+
if self.debugMode == 1:
import traceback
print traceback.format_exc()