-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
171 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ __pycache__/ | |
*.py[cod] | ||
*$py.class | ||
|
||
# ctags tagfile | ||
tags | ||
|
||
# C extensions | ||
*.so | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,36 @@ | ||
import Queue | ||
if __name__ == "__main__": import packet | ||
from packet import Packet,PacketQueue | ||
|
||
# these delays can be seen as the time response time of the server for each game | ||
# being played | ||
|
||
MAX_PACKETS_PER_STEP = 350 | ||
TRAFFIC_DELAY = { 'high' : 10, 'med' : 5, 'low' : 3 } | ||
|
||
class Cloud(object): | ||
|
||
def __init__(self, gameTraffic, location, timeout, num_players): | ||
|
||
self.requestList = Queue.Queue() | ||
|
||
self.timeToProcess = TRAFFIC_DELAY[gameTraffic] | ||
|
||
self.num_players = num_players | ||
self.location = location | ||
self.timeout = timeout | ||
|
||
def responseAt(self, time): | ||
if time % self.timeToProcess == 0 and not self.requestList.empty() : | ||
headPacket = self.requestList.get(); | ||
if (time - headPacket.timestamp) > self.timeout: | ||
return None | ||
else: | ||
responsePackets = [] | ||
for i in range(1,self.num_players): | ||
newPacket = headPacket | ||
|
||
# update packet | ||
newPacket.sender = 0 | ||
newPacket.receiver = i | ||
|
||
responsePackets.append(newPacket) | ||
return responsePackets | ||
else: | ||
return None | ||
self.requestList = PacketQueue() | ||
self.time = 0 | ||
|
||
def step(self): | ||
self.time += 1 | ||
responsePackets = [] | ||
while not self.requestList.empty() \ | ||
and self.requestList.next().isReady(self.time) \ | ||
and len(responsePackets) <= MAX_PACKETS_PER_STEP: | ||
headPacket = self.requestList.pop() | ||
#if headPacket.arriveTime() + self.timeout <= self.time: | ||
for i in xrange(1, self.num_players + 1): | ||
newPacket = headPacket.deepcopy() | ||
newPacket.sender = 0 | ||
newPacket.receiver = i | ||
responsePackets.append(newPacket) | ||
return responsePackets | ||
|
||
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" | ||
|
||
packet.addLatency(self.timeToProcess) | ||
self.requestList.push(packet) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,43 @@ | ||
import uuid | ||
import heapq | ||
|
||
class Packet(object): | ||
|
||
def __init__(self, timestamp, receiver, sender): | ||
self._init_packet_id() | ||
def __init__(self, timestamp, receiver, sender, packet_id = None): | ||
self.packet_id = packet_id if packet_id is not None else uuid.uuid4() | ||
self.timestamp = timestamp | ||
self.receiver = receiver | ||
self.sender = sender | ||
self.elapsedTime = 0 | ||
self.phase = 'network->cloud' | ||
|
||
def _init_packet_id(self): | ||
self.packet_id = uuid.uuid4() # generate a random UUID | ||
|
||
def addLatency(self, elapsedTime): | ||
self.elapsedTime += elapsedTime | ||
|
||
def isReady(self, time): | ||
return self.timestamp + self.elapsedTime >= time | ||
return self.arriveTime() >= time | ||
|
||
def arriveTime(self): | ||
return self.timestamp + self.elapsedTime | ||
|
||
def deepcopy(self): | ||
return Packet(self.timestamp, self.receiver, self.sender, self.packet_id) | ||
|
||
# Packet priority queue, with packets ordered by arrival time | ||
class PacketQueue(object): | ||
|
||
def __init__(self): | ||
self._packets = [] | ||
|
||
def push(self, packet): | ||
toAdd = (packet.arriveTime(), packet) | ||
heapq.heappush(self._packets, toAdd) | ||
|
||
def pop(self): | ||
return (heapq.heappop(self._packets))[1] | ||
|
||
def next(self): | ||
return self._packets[0][1] | ||
|
||
def empty(self): | ||
return len(self._packets) == 0 | ||
|
Oops, something went wrong.