Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Oghenefego Ahia authored and Oghenefego Ahia committed Dec 19, 2016
2 parents e661170 + 7c0ce8b commit e6cfa04
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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])
2 changes: 1 addition & 1 deletion device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions location.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def getPropagation(self, c1, c2):

return None

locations = {
locations = [
"Atlanta",
"Austin",
"Cambridge",
Expand All @@ -45,7 +45,7 @@ def getPropagation(self, c1, c2):
"San Francisco",
"Seattle",
"Washington"
}
]

def createPropagationMatrix(self):
self.addPropagation("Atlanta", "Austin", 24)
Expand Down Expand Up @@ -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"
25 changes: 12 additions & 13 deletions network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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__":

Expand All @@ -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"
4 changes: 2 additions & 2 deletions packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

24 changes: 16 additions & 8 deletions simulator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cloud, device, network, packet, data
from packet import Packet
import heapq

CLOUD = 0
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions vr_simulation.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
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)
loc = Location() # Select at random
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()
Expand Down

0 comments on commit e6cfa04

Please sign in to comment.