From 660321fb2e033b78231a9a998a130d4aa03fe3ba Mon Sep 17 00:00:00 2001 From: joshtch Date: Sun, 18 Dec 2016 20:10:20 -0500 Subject: [PATCH 1/2] Sun 8:10pm push --- cloud.py | 2 +- data.py | 15 ++++++++------- device.py | 2 +- location.py | 8 ++++---- network.py | 25 ++++++++++++------------- packet.py | 4 ++-- simulator.py | 24 ++++++++++++++++-------- vr_simulation.py | 18 +++++++++--------- 8 files changed, 53 insertions(+), 45 deletions(-) diff --git a/cloud.py b/cloud.py index cdd3013..919ecc5 100644 --- a/cloud.py +++ b/cloud.py @@ -12,7 +12,7 @@ def __init__(self, gameTraffic, location, timeout, num_players): self.requestList = Queue.Queue() - self.timeToProcess = TRAFFIC_DELAY(gameTraffic) + self.timeToProcess = TRAFFIC_DELAY[gameTraffic] self.num_players = num_players self.location = location diff --git a/data.py b/data.py index 8d5649c..9b64fcd 100644 --- a/data.py +++ b/data.py @@ -6,13 +6,14 @@ def __init__(self): def putSend(self, id, sendTime): if id not in self.d: - self.d[id] = { 'sendTime' : sendTime, 'receiveTime' : -1 } + self.d[id] = { 'sendTime' : sendTime, 'receiveTime' : None } else: self.d[id]['sendTime'] = sendTime def putReceive(self, id, receiveTime): + print "received: " + str(id) + ", " + str(receiveTime) if id not in self.d: - self.d[id] = { 'sendTime' : -1, 'receiveTime' : receiveTime } + self.d[id] = { 'sendTime' : None, 'receiveTime' : receiveTime } else: self.d[id]['receiveTime'] = receiveTime @@ -26,16 +27,16 @@ def receiveTime(self, id): return self.d[id]['receiveTime'] def latency(self, id): - if self.receiveTime(id) != -1: - return self.d[id]['receiveTime'] - self.d[id]['sendTime'] + if self.receiveTime(id) is not None and self.sendTime(id) is not None: + return self.d[id]['receiveTime'] - self.d[id]['sendTime'] else: - return None + return None def latencyList(self): - return [self.latency(x) for x in self.d if self.latency(x) is not None] + return [self.latency(x) for x in self.d.keys() if self.latency(x) is not None] def averageLatency(self): - return statistics.median(self.latencyList) + return statistics.median(self.latencyList()) def numDropped(self): return len([self.receiveTime(x) for x in self.d if self.receiveTime(x) is None]) diff --git a/device.py b/device.py index 9a29f7c..2587155 100644 --- a/device.py +++ b/device.py @@ -24,7 +24,7 @@ def responseAt(self, time): else: return [] - def receivePacket(packet): + def receivePacket(self, packet): id = packet.packet_id arrival = packet.arriveTime() self.data.putReceive(id, arrival) diff --git a/location.py b/location.py index 6a20b7f..afe16af 100644 --- a/location.py +++ b/location.py @@ -33,7 +33,7 @@ def getPropagation(self, c1, c2): return None - locations = { + locations = [ "Atlanta", "Austin", "Cambridge", @@ -45,7 +45,7 @@ def getPropagation(self, c1, c2): "San Francisco", "Seattle", "Washington" - } + ] def createPropagationMatrix(self): self.addPropagation("Atlanta", "Austin", 24) @@ -116,5 +116,5 @@ def createPropagationMatrix(self): if __name__ == "__main__": loc1 = Location("Washington") loc2 = Location("Chicago") - print "Delay from " + loc1.city + " to " + loc2.city + " is - " + str(loc1.propagationDelayFrom(loc2)) + " ms" + print "Delay from " + loc1.city + " to " + loc2.city + " is" + print str(loc1.propagationDelayFrom(loc2)) + " ms" diff --git a/network.py b/network.py index 2719c45..572f07f 100644 --- a/network.py +++ b/network.py @@ -10,7 +10,7 @@ def __init__(self, packet_loss_prob): def networkDelay(self, loc1, loc2): # Check for packet loss if random.random() < self.packet_loss_prob: - return -1 + return None # Otherwise, return propagation delay return loc1.propagationDelayFrom(loc2) @@ -21,30 +21,29 @@ class TCP(Network): HEADER_PROCESSING = 5 # ms def __init__(self, packet_loss_prob): - super(TuftsSecure, self).__init__(packet_loss_prob) + super(TCP, self).__init__(packet_loss_prob) def networkDelay(self, loc1, loc2): - propDelay = Network.networkDelay(loc1, loc2) - propDelay += TCP.HEADER_PROCESSING - if propDelay >= TCP.HEADER_PROCESSING: + propDelay = super(TCP, self).networkDelay(loc1, loc2) + if propDelay is not None: + propDelay += self.HEADER_PROCESSING return propDelay * 2 else: - return TCP.TIMEOUT_RATE + self.networkDelay(loc1, loc2) + return None class UDP(Network): HEADER_PROCESSING = 2 # ms def __init__(self, packet_loss_prob): - super(TuftsSecure, self).__init__(packet_loss_prob) + super(UDP, self).__init__(packet_loss_prob) def networkDelay(self, loc1, loc2): - propDelay = Network.networkDelay(loc1, loc2) - propDelay += UDP.HEADER_PROCESSING - if propDelay >= UDP.HEADER_PROCESSING: - return propDelay + propDelay = super(UDP, self).networkDelay(loc1, loc2) + if propDelay is not None: + return propDelay + self.HEADER_PROCESSING else: - return 0 + return None if __name__ == "__main__": @@ -54,6 +53,6 @@ def networkDelay(self, loc1, loc2): # Timeout test network = Network(2) # 2 percent chance to drop packet attempts = 0 - while network.networkDelay(loc1, loc2) != -1: + while network.networkDelay(loc1, loc2) != None: attempts += 1 print "Network dropped packet after " + str(attempts + 1) + " packets sent" diff --git a/packet.py b/packet.py index 48ce818..fa7a6f4 100644 --- a/packet.py +++ b/packet.py @@ -17,8 +17,8 @@ def addLatency(self, elapsedTime): self.elapsedTime += elapsedTime def isReady(self, time): - return timestamp + elapsedTime >= time + return self.timestamp + self.elapsedTime >= time def arriveTime(self): - return timestamp + elapsedTime + return self.timestamp + self.elapsedTime diff --git a/simulator.py b/simulator.py index ef713dd..097d1fa 100644 --- a/simulator.py +++ b/simulator.py @@ -1,4 +1,5 @@ import cloud, device, network, packet, data +from packet import Packet import heapq CLOUD = 0 @@ -16,7 +17,10 @@ def __init__(self, cloud, network, devices, simTime): self.activePackets = [] def run(self): + totalTimeStr = str(self.endTime) for timeStep in xrange(self.endTime): + if timeStep % 100 == 0: + print "\r" + "Computing time step: " + str(timeStep) + " out of " + totalTimeStr, self.runStep(timeStep) def getResults(self): @@ -36,12 +40,15 @@ def sendThruNetwork(self, packet): senderLocation = self.locationOf(packet.sender) receiverLocation = self.locationOf(packet.receiver) response = self.network.networkDelay(senderLocation, receiverLocation) - if notDropped(reponse): + if self.dropped(response): + return None + else: delay = response packet.addLatency(delay) return packet - else: - return None + + def dropped(self, response): + return response is None def addActivePacket(self, packet): toAdd = (packet.arriveTime(), packet) @@ -54,7 +61,7 @@ def hasActivePackets(self): return len(self.activePackets) > 0 def popActivePacket(self): - return heapq.heappop(self.activePackets) + return heapq.heappop(self.activePackets)[1] def deliverReadyPackets(self, step): self.resetDeliveryCounts() @@ -64,7 +71,7 @@ def deliverReadyPackets(self, step): def deliverPacket(self, packet): dest = packet.receiver if self.deliveryCounts[dest] <= MAX_PACKETS_PER_STEP: - self.endpoints[dest].receivePacket(packetToDeliver) + self.endpoints[dest].receivePacket(packet) self.incrementDeliveryCountFor(dest) def incrementDeliveryCountFor(self, deviceID): @@ -73,10 +80,11 @@ def incrementDeliveryCountFor(self, deviceID): def updateActivePackets(self, step): for endpoint in self.endpoints: response = endpoint.responseAt(step) - for packet in response: - self.queuePacket(packet) + if response is not None: + for packet in response: + self.queuePacket(packet) - def queuePacket(packet): + def queuePacket(self, packet): networkResponse = self.sendThruNetwork(packet) if isinstance(networkResponse, Packet): self.addActivePacket(networkResponse) diff --git a/vr_simulation.py b/vr_simulation.py index 9e3e621..993df73 100644 --- a/vr_simulation.py +++ b/vr_simulation.py @@ -1,15 +1,14 @@ from simulator import Simulator from visualizer import Visualizer from cloud import Cloud -from device import Device -from network import Network +import device +import network as net import random - -print networks -print "Tufts download speed is " + str(networks['tufts'].download) + " Mbps." +from location import Location # Initialize devices -DEVICE_OPTS = { OculusRift, HTCVive, PlayStationVR, LG360VR, GearVR, VisusVR } +DEVICE_OPTS = [ device.OculusRift, device.HTCVive, device.PlayStationVR, + device.LG360VR, device.GearVR, device.VisusVR ] devices = [] for i in xrange(1, 11): device = random.choice(DEVICE_OPTS) @@ -17,16 +16,17 @@ devices.append(device(i, loc)) # Initialize cloud -traffic_level = 0 +traffic_level = 'low' cloud_loc = Location() timeout = 2000 # 2 seconds num_players = len(devices) cloud = Cloud(traffic_level, cloud_loc, timeout, num_players) # Initialize network -network = TCP(); +packet_loss_probability = 0 # % chance of network-related dropped packet +network = net.TCP(packet_loss_probability); -sim_length = 30 * 60 * 1000 # 30 minutes +sim_length = 4 * 1000 # 4 seconds sim = Simulator(cloud, network, devices, sim_length) sim.run() From 7c0ce8b8f062f9a41a263892c3822ac31a8a1fc9 Mon Sep 17 00:00:00 2001 From: joshtch Date: Sun, 18 Dec 2016 20:12:18 -0500 Subject: [PATCH 2/2] Fix test case --- location.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/location.py b/location.py index c3e2f72..aed1188 100644 --- a/location.py +++ b/location.py @@ -117,4 +117,4 @@ def createPropagationMatrix(self): loc1 = Location("Washington") loc2 = Location("Chicago") print "Delay from " + loc1.city + " to " + loc2.city + " is" \ - , str(loc1.propagationDelayFrom(loc2)) + " ms" + + str(loc1.propagationDelayFrom(loc2)) + " ms"