Source code for gwa.eperf.client_udp

#!/usr/bin/python
#-*-coding:iso-8859-15-*-

import socket,random
from function import *

[docs]class UDPClient: def __init__(self,address,interval,duration,bw,upload,download): """ The function "__init__" allows to initialize parameters for UDP client. 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" """ self.curTime = Timer() self.datas = "" self.BUFMAX = 65507 self.addr = address self.listFinalDown = [] self.listFinalUp = [] 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.UDPSock = self.createSocket( self.addr, socket.SOCK_DGRAM) self.TCPSock = self.createSocket( self.addr, socket.SOCK_STREAM) self.parameters(interval,duration,bw,upload,download)
[docs] def createSocket(self, addr, 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") """ Sock = socket.socket(socket.AF_INET, protocol) Sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if protocol == socket.SOCK_STREAM: Sock.connect(addr) return Sock else : #creation of an UDP client Sock.settimeout(2) return Sock
[docs] def parameters(self, interval, duration, bw, upload, download ): """ The function "parameters" recovers parameters entered by the user ("interval", "duration", "bandwidth", "upload" and "download") for the server. This function tests also if the input bandwidth exceeds the maximum buffer of the function "sendto()" and initializes the chronometer for the measures. """ data = self.TCPSock.recv(self.BUFMAX) if data == "start": self.curTime.restart() else: print "nimporte quoi :"+data exit(0) #~ while not "OK" in data: self.UDPSock.sendto("0123456789",self.addr) self.interval=interval self.duration=duration self.bw=bw params=str(interval)+";"+str(duration)+";"+str(self.bw)+";"+str(upload)+";"+str(download)+";" self.TCPSock.send(params) #tests if the input bandwidth exceeds the maximum buffer of sendto() if self.bw <= self.BUFMAX: self.limit = self.bw else : self.limit = self.BUFMAX
[docs] def uploadUDP(self): """ This function "uploadUDP" 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. """ ################################################################################################################################################ #UpLoad ################################################################################################################################################ #~ print "\n\t\t *** UpLoad ***" #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.TCPSock.recv(self.BUFMAX) if data == "start": #~ print "\n\t\t sending data ...\n" self.curTime.restart() else: print "nimporte quoi :"+data 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: self.datas = "a"*(self.bw - total_bw) #random packet else: self.datas = "a"*(random.randint(1,self.limit)) #sending of the generated packet size_sent = self.UDPSock.sendto(self.datas,self.addr) 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 #measure of rate self.data = self.TCPSock.recv(self.BUFMAX) self.listFinalUp = self.data.split(";") #~ print self.data
[docs] def downloadUDP(self): """ The function "downloadUDP" allows to count the size of data received during every interval and calculate the bandwidth for each interval. It adds every measure of the bandwidth in a list called "listFinalDown". """ ################################################################################################################################################ #DownLoad ################################################################################################################################################ #wait for the client to be ready data = self.TCPSock.recv(self.BUFMAX) #~ print "\n\n\t\t ***DownLoad***" #initialization of the time self.TCPSock.send("start") #~ print "\n\t\t sending data ...\n" self.intTime = self.interval self.curTime.restart() while (1): try: #data reception data, infos = self.UDPSock.recvfrom(self.BUFMAX) 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.listFinalDown.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: break totalTime = self.curTime1 self.listFinalDown.append((strbandwidth(self.total_data_size, totalTime))) #~ print self.listFinalDown
[docs] def closeSocket(self): """ This function "closeSocket" allows to close TCP and UDP sockets. """ self.TCPSock.close() self.UDPSock.close()