Skip to content

Commit

Permalink
Pep8 reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeb16157 committed Feb 18, 2021
1 parent 887e6d7 commit 5519ebd
Showing 1 changed file with 71 additions and 53 deletions.
124 changes: 71 additions & 53 deletions Python/server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#Server: This class serves as the functionality for the server devices in our network
# Server: This class serves as the functionality for the server devices in our network
from . import packet
import random

class Server:
def __init__(self, routerConnection, ipAddress, maxPendingConnections, resendSynackTime, maxSynAck, fileGen, packetSize, queueLimit, bandwidthToRouter, packet_ID_prefix = "S-"):

class Server:
def __init__(self, routerConnection, ipAddress,
maxPendingConnections, resendSynackTime,
maxSynAck, fileGen, packetSize, queueLimit,
bandwidthToRouter, packet_ID_prefix="S-"):
self.ipAddress = ipAddress
self.routerConnection = routerConnection
self.queue = [] #(packet, time when packet is ready to be sent)
self.queue = [] # (packet, time when packet is ready to be sent)
self.curTime = 0
self.receiveBuffer = []
self.sendingPacket = None
Expand All @@ -16,131 +19,146 @@ def __init__(self, routerConnection, ipAddress, maxPendingConnections, resendSyn
self.maxSynAck = maxSynAck
self.packetInQueue = 0
self.packetsInReceiveBuffer = 0
self.halfOpenConnections = [] #[(Number of synacks sent, time to next synack, the syn packet, seqnumber of syn-ack before it)]
self.halfOpenConnections = [] # [(Number of synacks sent, time to next synack, the syn packet, seqnumber of syn-ack before it)]

#self.sendRate = sendRate #MB per sec
self.bandwidthToRouter = bandwidthToRouter
self.packetSize = packetSize #Size of packets in MB
self.queueLimit = queueLimit #MB
self.packetSize = packetSize # Size of packets in MB
self.queueLimit = queueLimit # MB
self.currentQueueLoad = 0

self.packetsSent = 0 #counter for logging purposes
self.packetsReceived = 0 #counter for logging purposes (currently unused but it makes it easier for the log file to operate)
self.packetsDropped = 0 #counter for packets dropped (currently unused but it makes it easier for the log file to operate)
self.packetsGenerated = 0 #Counter for the number of new packets uniquely created. Used for the packet ID
self.packetsInQueue = 0 #Counter for logging purposes
self.packetsDroppedOnConnection = 0 #specifies the number of packets that dropped on the connectionObject due to bandwidth after being sent by the device
# Counter for logging purposes
self.packetsSent = 0
self.packetsReceived = 0
self.packetsDropped = 0
self.packetsGenerated = 0
self.packetsInQueue = 0
self.packetsDroppedOnConnection = 0

self.fileGen = fileGen #File management object
self.packetIDPrefix = packet_ID_prefix #Used for packet IDs as a prefix so that we're not using the IPs
self.fileGen = fileGen # File management object
self.packetIDPrefix = packet_ID_prefix # Used for packet IDs as a prefix so that we're not using the IPs
self.timeToNextIteration = 1

#Takes packets in the queue and determines how to respond to those packages.
# Takes packets in the queue and determines how to respond to those packages.
def curateResponses(self):
for i in self.receiveBuffer:
curPacket = i
self.receiveBuffer.remove(i)
#If it is a syn packet, curate a syn-ack packet and add entry to half-open connections
# If it is a syn packet, curate a syn-ack packet and add entry to half-open connections
if(curPacket.packetType == "SYN"):
seqNumber = random.randint(0, 500) #generate a random port number
#packetID = random.randint(0, 20000) #generate a random port number
seqNumber = random.randint(0, 500) # generate a random port number
self.packetsGenerated += 1
packetID = self.packetIDPrefix + str(self.packetsGenerated)
newPacket = packet.packet(self.ipAddress, curPacket.dstPortNumber, curPacket.srcIP, curPacket.srcPortNumber, "SYN-ACK", seqNumber, curPacket.sequenceNumber + 1, packetID,self.packetSize, self.ipAddress)
self.fileGen.addToLog(newPacket.packetID, "Generated at " + self.packetIDPrefix[0] + "Server", newPacket.sequenceNumber, newPacket.ackNumber, newPacket.packetType, self.curTime)
newPacket = packet.packet(self.ipAddress, curPacket.dstPortNumber,
curPacket.srcIP, curPacket.srcPortNumber,
"SYN-ACK", seqNumber,
curPacket.sequenceNumber + 1,
packetID, self.packetSize, self.ipAddress)
self.fileGen.addToLog(newPacket.packetID,
"Generated at " + self.packetIDPrefix[0] + "Server",
newPacket.sequenceNumber, newPacket.ackNumber,
newPacket.packetType, self.curTime)
self.currentQueueLoad += newPacket.packetSize
if(self.currentQueueLoad <= self.queueLimit):
self.queue.append(newPacket)
self.packetsInQueue += 1
self.packetsInReceiveBuffer -= 1
#add entry to half-open connections, if full pop the last item and insert new in beginning
# add entry to half-open connections, if full pop the last item and insert new in beginning
if(len(self.halfOpenConnections) >= self.maxPendingConnections):
self.halfOpenConnections.pop()
self.halfOpenConnections.insert(0, [0, self.curTime + self.resendSynackTime, curPacket, seqNumber])
else:
self.halfOpenConnections.insert(0, [0, self.curTime + self.resendSynackTime, curPacket, seqNumber])
else:
self.currentQueueLoad -= newPacket.packetSize
#Drop Packet
#if packet isn't syn, then it is a ack or rst, delete corresponding half-open connection
# Drop Packet
# if packet isn't syn, then it is a ack or rst, delete corresponding half-open connection
else:
for j in self.halfOpenConnections:
if(j[2].dstPortNumber == curPacket.dstPortNumber and j[2].srcPortNumber == curPacket.srcPortNumber):
if(j[2].dstPortNumber == curPacket.dstPortNumber
and j[2].srcPortNumber == curPacket.srcPortNumber):
self.halfOpenConnections.remove(j)
self.packetsInReceiveBuffer -= 1


#Resend synack's after the alloted resent timer has expired
# Resend synack's after the alloted resent timer has expired
def resendSynAcks(self):
for i in self.halfOpenConnections:
#If a half-open connection has sent max syn-acks, then delete that half-open connection
if(i[0] >= self.maxSynAck): #>= originally before 28JAN2021
# If a half-open connection has sent max syn-acks, then delete that half-open connection
if(i[0] >= self.maxSynAck):
self.halfOpenConnections.remove(i)
#If not maxed out, send another syn-ack
# If not maxed out, send another syn-ack
else:
if(i[1] >= self.curTime):
i[1] = self.curTime + self.resendSynackTime
i[0] += 1
self.packetsGenerated += 1
packetID = self.packetIDPrefix + str(self.packetsGenerated)
i[3] = i[3] + 1
newPacket = packet.packet(self.ipAddress, i[2].dstPortNumber, i[2].srcIP, i[2].srcPortNumber, "SYN-ACK", i[3], i[2].sequenceNumber + 1, packetID,self.packetSize, self.ipAddress)
self.fileGen.addToLog(newPacket.packetID, "Generated at " + self.packetIDPrefix[0] + "Server", newPacket.sequenceNumber, newPacket.ackNumber, newPacket.packetType, self.curTime)
i[3] = i[3] + 1
newPacket = packet.packet(self.ipAddress, i[2].dstPortNumber,
i[2].srcIP, i[2].srcPortNumber, "SYN-ACK",
i[3], i[2].sequenceNumber + 1, packetID,
self.packetSize, self.ipAddress)
self.fileGen.addToLog(newPacket.packetID, "Generated at "
+ self.packetIDPrefix[0] + "Server",
newPacket.sequenceNumber, newPacket.ackNumber,
newPacket.packetType, self.curTime)
self.currentQueueLoad += newPacket.packetSize
if(self.currentQueueLoad <= self.queueLimit):
self.packetsGenerated += 1
self.queue.append(newPacket)
self.packetInQueue += 1
else:
self.currentQueueLoad -= newPacket.packetSize
#Drop Packet

# Drop Packet


#If no packet is being sent, it will pop a packet from the queue and start sending it
# If no packet is being sent, it will pop a packet from the queue and start sending it
def processPackets(self):
if(len(self.queue) > 0 and self.sendingPacket == None):
if(len(self.queue) > 0 and self.sendingPacket is None):
curPacket = self.queue.pop(0)
### DW AUDIT 31JAN2021 transmissionDelay = (curPacket.packetSize / self.sendRate) * 1000
transmissionDelay = (curPacket.packetSize / self.bandwidthToRouter) * 1000 #DW AUDIT 31JAN2021
transmissionDelay = (curPacket.packetSize / self.bandwidthToRouter) * 1000
self.timeToNextIteration -= transmissionDelay
if(self.timeToNextIteration >= 0):
self.packetsGenerated += 1
self.sendingPacket = [curPacket, self.curTime + transmissionDelay] #Packet will be put onto connection after transmission delay has occured
self.sendingPacket = [curPacket, self.curTime
+ transmissionDelay] # Trans delay
self.currentQueueLoad -= curPacket.packetSize

else:
self.timeToNextIteration += transmissionDelay
self.packetsGenerated += 1
self.sendingPacket = [curPacket, (self.curTime + transmissionDelay) - (self.timeToNextIteration)]
self.sendingPacket = [curPacket, (self.curTime
+ transmissionDelay)
- (self.timeToNextIteration)]
self.timeToNextIteration -= transmissionDelay
self.currentQueueLoad -= curPacket.packetSize
elif(self.sendingPacket != None):
elif(self.sendingPacket is not None):
leftoverTime = self.sendingPacket[1] - self.curTime
self.timeToNextIteration -= leftoverTime


#If packet is being sent, the packet is placed onto the connection after the packet's transmission delay is satifisfied
# If packet is being sent, the packet is placed onto the connection after the packet's transmission delay is satifisfied
def sendPackets(self):
if((self.sendingPacket[1] - self.curTime) < 1):
self.packetsSent += 1 #increment counter
self.routerConnection.transferPacketOut(self.sendingPacket[0]) #place packet on connection
self.packetsSent += 1 # increment counter
self.routerConnection.transferPacketOut(self.sendingPacket[0]) # place packet on connection
self.sendingPacket = None

#Places packets into the receive buffer
# Places packets into the receive buffer
def receivePacket(self, packet):
self.fileGen.addToLog(packet.packetID, "Received at " + self.packetIDPrefix[0] + "Server", packet.sequenceNumber, packet.ackNumber, packet.packetType, self.curTime)
self.fileGen.addToLog(packet.packetID, "Received at "
+ self.packetIDPrefix[0] + "Server",
packet.sequenceNumber, packet.ackNumber,
packet.packetType, self.curTime)
self.packetsReceived += 1
self.receiveBuffer.append(packet)
self.packetsInReceiveBuffer += 1


#Updates the current time and runs the functions
# Updates the current time and runs the functions
def updateTime(self):
self.curTime = self.curTime + 1
self.timeToNextIteration = 1
self.resendSynAcks()
self.curateResponses()
while(self.timeToNextIteration > 0 and (self.sendingPacket is not None or len(self.queue) > 0)):
while(self.timeToNextIteration > 0
and (self.sendingPacket is not None
or len(self.queue) > 0)):
self.processPackets()
self.sendPackets()

0 comments on commit 5519ebd

Please sign in to comment.