Skip to content

Commit

Permalink
Print better results with Data class
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 ac83073 + 900edeb commit 7bd6890
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 204 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,30 @@ ENV/

# Rope project settings
.ropeproject

# Mac OS
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
77 changes: 53 additions & 24 deletions cloud.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,81 @@
import Queue
if __name__ == "__main__": import packet

# these delays can be seen as the time response time of the server for each game
# being played
# being played

HI_P_CLOUD_DELAY = 10
MED_P_CLOUD_DELAY = 5
LOW_P_CLOUD_DELAY = 3
TRAFFIC_DELAY = { 'high' : 10, 'med' : 5, 'low' : 3 }

class Cloud(object):

def __init__(self, gameTraffic, location, timeout, num_players):

self.requestList = Queue.Queue()

if gameTraffic == 0:
self.timeToProcess = LOW_P_CLOUD_DELAY
elif gameTraffic == 1:
self.timeToProcess = MED_P_CLOUD_DELAY
else:
self.timeToProcess = HIGH_P_CLOUD_DELAY
self.timeToProcess = TRAFFIC_DELAY(gameTraffic)

self.num_players = num_players
self.location = location
self.timeMS = 0
self.timeout = timeout

def updateTime(self, time):
self.timeMS = time
return self.processResponse(time)

def processResponse(self, time):
if self.timeMS % self.timeToProcess == 0 and not self.requestList.empty() :
def responseAt(self, time):
if time % self.timeToProcess == 0 and not self.requestList.empty() :
headPacket = self.requestList.get();
if (time - headPacket.timestamp) > self.timeout:
if (time - headPacket.timestamp) > self.timeout:
return None
else:
else:
responsePackets = []
for i in range(1,self.num_players):
newPacket = headPacket

# update packet
newPacket.sendAddress = 0
newPacket.receiveAddress = i
# update packet
newPacket.sender = 0
newPacket.receiver = i

responsePackets.append(newPacket)
return responsePackets
else:
return None

def receiveRequest(self, packet):
def receivePacket(self, packet):
self.requestList.put(packet)


if __name__ == "__main__":
packet1 = packet.Packet(100, 0, 0, 1)
packet2 = packet.Packet( 0, 0, 0, 2)
packet3 = packet.Packet(100, 0, 0, 1)
packet4 = packet.Packet( 100, 0, 0, 2)

cloud = cloud.Cloud(0, 0, 10, 10)

cloud.receivePacket(packet1)
cloud.receivePacket(packet4)
cloud.receivePacket(packet3)
cloud.receivePacket(packet2)

time = 100
print "\ntest 1: should return 3 packets"
for i in range(0,20):
#print time
packetList = cloud.updateTime(time)
if packetList != None:
print packetList[0].packet_id
time = time + 1

cloud.receivePacket(packet1)
cloud.receivePacket(packet2)
cloud.receivePacket(packet3)
cloud.receivePacket(packet4)

time = 100
print "\ntest 2: should return 2 packets"

for i in range(0,20):
#print time
packetList = cloud.updateTime(time)
if packetList != None:
print packetList[0].packet_id
time = time + 1
print "\n"

41 changes: 41 additions & 0 deletions data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import statistics
class Data(object):

def __init__(self):
self.d = {}

def putSend(self, id, sendTime):
if id not in self.d:
self.d[id] = { 'sendTime' : sendTime, 'receiveTime' : -1 }
else:
self.d[id]['sendTime'] = sendTime

def putReceive(self, id, receiveTime):
if id not in self.d:
self.d[id] = { 'sendTime' : -1, 'receiveTime' : receiveTime }
else:
self.d[id]['receiveTime'] = receiveTime

def rawData(self):
return self.d

def sendTime(self, id):
return self.d[id]['sendTime']

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']
else:
return None

def latencyList(self):
return [self.latency(x) for x in self.d if self.latency(x) is not None]

def averageLatency(self):
return statistics.median(self.latencyList)

def numDropped(self):
return len([self.receiveTime(x) for x in self.d if self.receiveTime(x) is None])
44 changes: 15 additions & 29 deletions device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from packet import Packet
from random import randint
from data import Data

TICKS_PER_EVENT = 20

Expand All @@ -9,40 +10,25 @@ def __init__(self, deviceID, fps, ticksPerEvent, location):
self.deviceID = deviceID
self.fps = fps
self.ticksPerEvent = ticksPerEvent
self.firstEventOffset = randint(0, ticksPerEvent - 1)
self.location = location

self.position = (randint(0, 10), randint(5, 20))
self.timeMS = 0
self.packetDict = {}

def updateTime(self, time, packet=None):
if packet is None:
return self.sendPacket(time)
else:
self.receivePacket(time, packet)

self.data = Data()

def sendPacket(self, time):
if time % self.ticksPerEvent == 0:
self.movePlayer(time)
packet = Packet(time, self.position, 0, self.deviceID)
def responseAt(self, time):
if (time + self.firstEventOffset) % self.ticksPerEvent == 0:
packet = Packet(time, 0, self.deviceID)
id = packet.packet_id
self.packetDict[id] = {'sendTime' : time, 'receiveTime' : -1}
self.data.putSend(id, time)
print 'Device: %d. Location: %s. Packet sent: %s. Timestamp: %s' % (self.deviceID, self.location, packet.packet_id, packet.timestamp)
return packet
return None

def receivePacket(self, time, packet):
print 'Device: %d. Location: %s. Packet received: %s. Timestamp: %s' % (self.deviceID, self.location, packet.packet_id, packet.timestamp)
id = packet.packet_id
if id in self.packetDict.keys():
self.packetDict[id]['receiveTime'] = time
return [packet]
else:
# stray packet received
self.packetDict[id] = {'sendTime' : -1, 'receiveTime' : time}
return []

def movePlayer(self, time):
self.position = (randint(0, 10), randint(5, 50))
def receivePacket(packet):
id = packet.packet_id
arrival = packet.arriveTime()
self.data.putReceive(id, arrival)
print 'Device: %d. Location: %s. Packet received: %s. Timestamp: %s' % (self.deviceID, self.location, packet.packet_id, packet.timestamp)


class OculusRift(Device):
Expand All @@ -67,4 +53,4 @@ def __init__(self, deviceID, location):

class VisusVR(Device):
def __init__(self, deviceID, location):
super(VisusVR, self).__init__(deviceID, 60, TICKS_PER_EVENT, location)
super(VisusVR, self).__init__(deviceID, 60, TICKS_PER_EVENT, location)
Loading

0 comments on commit 7bd6890

Please sign in to comment.