Skip to content

Commit

Permalink
Adds timeout capability and fixes multi-device bug
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasr committed Jun 3, 2014
1 parent 589710e commit 072a6b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
35 changes: 35 additions & 0 deletions server/device.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from collections import deque
from plistlib import *
from operator import itemgetter
import time

class device:
TIMEOUT = 60 # Number of seconds before command times out
def __init__(self, newUDID, tuple):
self.UDID = newUDID
self.IP = tuple[0]
Expand Down Expand Up @@ -48,6 +50,7 @@ def getQueueInfo(self):
return self.pushMagic, self.deviceToken

def getResponse(self, cmdUUID):
print self.cmdList.keys()
return self.cmdList[cmdUUID]['response']

def sortCommands(self):
Expand Down Expand Up @@ -98,6 +101,9 @@ def addCommand(self, cmd):
# Update status to show command pending
self.status = 1

#print cmd
cmd['TimeStamp'] = time.time()

# Update command with unlockToken if necessary
if cmd['Command']['RequestType'] == 'ClearPasscode':
cmd['Command']['UnlockToken'] = Data(self.unlockToken)
Expand Down Expand Up @@ -132,3 +138,32 @@ def addResponse(self, cmdUUID, response):
elif response['Status'] == 'Error':
self.cmdList[cmdUUID]['status'] = 'danger'
self.status = 2

def checkTimeout(self):
# Checks for command timeout
now = time.time()

# If we have no commands waiting, we're good
if self.status != 1:
return

# Check queue for timed out commands
if len(self.queue) > 0:
for cmd in self.queue:
if now - cmd['TimeStamp'] > self.TIMEOUT:
# Command has time out, add it to cmd list with an error
self.status = 2
self.queue.remove(cmd)
self.cmdList[cmd['CommandUUID']] = {}
self.cmdList[cmd['CommandUUID']]['cmd'] = cmd
self.cmdList[cmd['CommandUUID']]['response'] = {'Status':'TimeoutError'}
self.cmdList[cmd['CommandUUID']]['status'] = 'danger'
self.cmdList[cmd['CommandUUID']]['order'] = len(self.cmdList.keys())
return

# Check command list for timed out commands
for commandUUID in self.cmdList:
if self.cmdList[commandUUID]['response'] == "" and now-self.cmdList[commandUUID]['cmd']['TimeStamp'] > self.TIMEOUT:
self.status = 2
self.cmdList[command['cmd']['CommandUUID']]['status'] = 'danger'
self.cmdList[command['cmd']['CommandUUID']]['response'] = {'Status':'TimeoutError'}
3 changes: 2 additions & 1 deletion server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def POST(self):

# Update page - currently not using update()
#return update()
return
return

class do_mdm:
def PUT(self):
Expand Down Expand Up @@ -476,6 +476,7 @@ def POST(self):
devices = []

for key in device_list:
device_list[key].checkTimeout()
devices.append(device_list[key].populate())

# A device-sorting functionality could happen here
Expand Down

0 comments on commit 072a6b9

Please sign in to comment.