Source code for gwa.eperf.server_udp
#!/usr/bin/python
#-*-coding:iso-8859-15-*-
import socket,random
from function import *
[docs]class UDPServer:
def __init__(self,address):
"""
The function "__init__" allows to initialize parameters for UDP server.
Most important parameters (initialized by this function) are : interval, duration, bandwidth, upload and download.
This function initialize sockets for TCP and UDP communications with the function "createSocket".
The argument "address" taken by this function is a tuple of the server's IP address and the port.
"""
self.buf = 65507
self.interval=0
self.duration=0
self.listFinal = []
self.data_size=0
self.total_data_size=0
self.curTime1=0
self.packets_number=0
self.total_packets_number=0
self.i=0
self.curTime = Timer()
self.address=address
self.TCP_socket=self.createSocket(address, socket.SOCK_STREAM)
self.UDP_socket=self.createSocket(address, socket.SOCK_DGRAM)
self.parameters()
[docs] def createSocket(self, address, protocol):
"""
The function "createSocket" allows the creation of a socket UDP or TCP and returns the socket created.
It takes 2 arguments :
- "addr" : a tuple of the server's IP address and the port
- "protocol" : type of the socket ("SOCK_DGRAM" or "SOCK_STREAM")
"""
print "Creation of the server..."
Sock = socket.socket(socket.AF_INET, protocol)
Sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if protocol == socket.SOCK_STREAM:
Sock.bind(address)
Sock.listen(5)
sock, address = Sock.accept()
return sock
else :
Sock.bind(address)
Sock.settimeout(2)
return Sock
[docs] def parameters(self):
"""
The function "parameters" allows to recover parameters from the client ("interval", "duration", "bandwidth", "upload" and "download").
Before recovering parameters, the function does a synchronization with the client sending a start message.
This function tests also if the input bandwidth exceeds the maximum buffer of the function "sendto()".
"""
self.TCP_socket.send("start")
#~ self.infos=None
#~ while self.infos == None :
data, self.infos = self.UDP_socket.recvfrom(self.buf)
#~ print data
#~ self.TCP_socket.send("OK")
#reception of interval
data = self.TCP_socket.recv(self.buf)
#~ self.interval = int(data)
self.interval=int(data.split(";")[0])
self.duration=int(data.split(";")[1])
self.bw=int(data.split(";")[2])
self.upload=data.split(";")[3]
self.download=data.split(";")[4]
# check if the imput bandwidth exceeds the buffer of sendto()
if self.bw <= self.buf:
self.limit = self.bw
else :
self.limit = self.buf
return self.interval,self.duration,self.bw
[docs] def uploadUDP(self):
"""
This function "uploadUDP" allows to count the size of received data during every interval and calculate the bandwidth for each interval
It adds every measure of the bandwidth in a list called "listFinal".
"""
################################################################################################################################################
#UpLoad
################################################################################################################################################
#initialization of the time
self.TCP_socket.send("start")
self.curTime.restart()
self.intTime = self.interval
while (1):
try:
#data reception
data, self.infos = self.UDP_socket.recvfrom(self.buf)
self.curTime1 = self.curTime.get_value()
#size of received data
self.data_size = self.data_size + len(data)
self.total_data_size = self.total_data_size +len(data)
#change of interval
if self.curTime1 >= self.intTime:
self.intTime = self.intTime+ self.interval
self.listFinal.append(strbandwidth(self.data_size,(self.curTime1-(self.i*self.interval))))
self.packets_number = 0
self.data_size = 0
self.i = self.i+1
self.packets_number = self.packets_number + 1
self.total_packets_number = self.total_packets_number + 1
except socket.timeout:
print "\n\n"
break
self.totalTime = self.curTime1
self.listFinal.append((strbandwidth(self.total_data_size, self.totalTime)))
self.TCP_socket.send(";".join(self.listFinal))
[docs] def downloadUDP(self):
"""
The function "downloadUDP" allows to send data with a random size during the "duration" informed by the user.
It sends data with a rate forced every second to the bandwidth given by the user.
"""
################################################################################################################################################
#DownLoad
################################################################################################################################################
#envoi du signal pret au serveur
self.TCP_socket.send("pret")
#initialization of parameters for sending data
packet_number=0
total_packet_number=0
second = 1
total_bw=0
data_sent=0
boolean=1
#reception of the starting message to initialize the curTime
data = self.TCP_socket.recv(self.buf)
if "start" in data :
self.curTime.restart()
else:
print "nimporte quoi"
exit(0)
#while the duration is not reached
while self.curTime.get_value() <= self.duration+0.1:
#if it has reached one second
if self.curTime.get_value() >= second :
second = second + 1
total_bw=0
packet_number=0
boolean=1
#while it doesn't exceeded the bandwidth
if total_bw < self.bw :
#generation of random packet or final packet
#final packet
if self.bw - total_bw <= self.limit:
datas = "a"*(self.bw-total_bw)
#random packet
else:
datas = "a"*(random.randint(1,self.limit))
#sending of the generated packet
size_sent = self.UDP_socket.sendto(datas,self.infos)
packet_number = packet_number+1
total_packet_number = total_packet_number + 1
total_bw = total_bw + size_sent
data_sent = data_sent + size_sent
else:
if boolean == 1 :
boolean=0
[docs] def closeSocket(self):
"""
This function "closeSocket" allows to close TCP and UDP sockets.
"""
#closing the UDP socket
self.UDP_socket.close()
#closing the TCP socket
self.TCP_socket.close()