Source code for gwa.eperf.server_tcp
#!/usr/bin/python
#-*-coding:iso-8859-15-*-
import SocketServer,threading,commands
from function import *
import sys,time,re
buf=1460
##############################################################################
[docs]class Connectionprocessing(SocketServer.BaseRequestHandler):
"""
This class allow to the connection with the class ServerThread. It define the handle function run by each thread created by each connection with the server.
"""
[docs] def handle(self):
"""
Function handle run by each thread or each connection
This function allow to determinate the connection information as the interval or duartion or ID for examples.
And, it's use to run the function for calculate the bandwidth in mode upload Or / And in mode Download.
"""
port =self.request.getsockname()[1]
print "--------- New Client ---------"
peer = self.request.getpeername()
#To determinate the ID of the client, we use the number of thread
ID=threading.currentThread().getName().split("Thread-")[1]
print "Client address "+str(peer[0])+" Port: "+str(peer[1])
print "[ID] Interval\t\tTransfert\t\tbandwidth"
# Juste after the connection the client send three arguments (interval, duration, and command)
data = self.request.recv(100)
interval=float(data.split(";")[0])
duration=float(data.split(";")[1])
command=data.split(";")[2]
print "["+ID+"] Interval = "+str(interval)+" Duration = "+str(duration)
# Server send ID at this client
self.request.sendall(ID+";")
# The server show command and compare this to start the fonction asked by the client.
if "upload" in command :
print "Measure Upload:"
bandwidthTCPUpload(self,interval,duration,ID)
else:
print "Measure Download:"
bandwidthTCPDownload(self,interval,duration,ID)
print "server TCP port : "+str(port)
print "server UDP port : "+str(port+1)
self.finish()
[docs]def bandwidthTCPDownload(self,interval,duration,ID):
"""
This function calculate the bandwidth in download between the client and this server.
:Argument:
interval (type int)
Determinate the interval in second to calculate the bandwidth.
duration (type float)
Determinate the number of second to run this function.
ID (type int)
Représent the identifaction of connection.
"""
curTime=Timer()
intTime=Timer()
curTime.restart()
intTime.restart()
sumNbBytes=0
intNbBytes=0
while curTime.get_value() < duration :
if intTime.get_value() > interval:
self.request.sendall("int")
print "["+ID+"] %.2f s\t\t\t"%curTime.get_value()+strNbData(intNbBytes)+"\t\t"+strbandwidth(intNbBytes,interval)
intNbBytes=0
intTime.restart()
nbBytes = self.request.send("0123456789"*148)
sumNbBytes=sumNbBytes+nbBytes
intNbBytes=intNbBytes+nbBytes
self.request.sendall("int")
self.request.send("quit")
print "["+ID+"] %.2f s\t\t\t"%curTime.get_value()+strNbData(intNbBytes)+"\t\t"+strbandwidth(intNbBytes,interval)
print "["+ID+"] [0-%.1f] s\t\t\t"%duration+strNbData(sumNbBytes)+"\t\t"+strbandwidth(sumNbBytes,duration)
[docs]def bandwidthTCPUpload(self,interval,duration,ID):
"""
This function calculate the bandwidth in upload between the client and this server.
:Argument:
interval (type int)
Determinate the interval in second to calculate the bandwidth.
duration (type float)
Determinate the number of second to run this function.
ID (type int)
Représent the identifaction of connection.
"""
curTime=Timer()
curTime.restart()
sumNbBytes=0
intNbBytes=0
while 1:
data = self.request.recv(buf)
sumNbBytes=sumNbBytes+len(data)
intNbBytes=intNbBytes+len(data)
if "quit" in data:
break
if "int" in data :
print "["+ID+"] %.2f s\t\t\t"%curTime.get_value()+strNbData(intNbBytes)+"\t\t"+strbandwidth(intNbBytes,interval)
intNbBytes=0
print "["+ID+"] [0-%.1f] s\t\t\t"%duration+strNbData(sumNbBytes)+"\t\t"+strbandwidth(sumNbBytes,duration)
##############################################################################
[docs]class ServerThread(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
"""
This class allow to link the class object with an handle function.
This handle function is define in the class Connectionprocessing.
At each connection with the server, this class create thread and run the same function written in the function handle in the Connectionprocessing.
**example**
>>> addrTCP=('',port)
>>> server = ServerThread(addrTCP, Connectionprocessing)
>>> SRV_TCP = threading.Thread(target=server.serve_forever)
>>> SRV_TCP.allow_reuse_address=True
>>> SRV_TCP.deamon=False
>>> SRV_TCP.start()
At this moment your server is on idle of connection.
"""
pass