-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandQueue.py
More file actions
75 lines (60 loc) · 2.09 KB
/
Copy pathCommandQueue.py
File metadata and controls
75 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
'''
Created on Aug 30, 2015
@author: derp
'''
from Queue import Queue
from random import randint
import math
import time
import threading
def log(string):
print string
def random_from_set(listValue):
index = randint(0, len(listValue)-1)
return listValue[index]
#current strategy is to wait +/- 10% of the expected wait time
def wait_randomization_strategy_result(waitTime):
boundDeterminer = int(round(math.ceil(int(waitTime)/5)))
if(boundDeterminer < 1):
boundDeterminer = 1
lowerBound = waitTime - boundDeterminer
if lowerBound < 1:
lowerBound = 2
upperBound = waitTime + boundDeterminer
if(upperBound < 4):
upperBound = 4
listValue = range(int(lowerBound), int(upperBound))
return random_from_set(listValue)
class CommandQueueObject:
waitTime = 0
command = ''
def __init__(self, command, waitTime):
self.waitTime = waitTime
self.command = command
class CommandQueue:
commandQueue = Queue()
ircRef = None
quieteMode = False
def __init__(self, ircRef):
self.ircRef = ircRef
self.commandThread = threading.Thread(target=self.command_worker)
self.commandThread.start()
def __del__(self):
self.commandThread.join()
def sendCommandToQueue(self, commandValue, waitTime):
self.commandQueue.put(CommandQueueObject(commandValue, waitTime))
def join(self):
self.commandThread.join()
def command_worker(self):
while True:
commandValue = self.commandQueue.get(block=True)
if not (self.quieteMode and commandValue.command != 'PONG'):
self.send_timed_command(commandValue)
else:
print 'command to irc ommitted due to quiet mode: ' + commandValue.command
commandValue = None
def send_timed_command(self, commandValue):
time.sleep(wait_randomization_strategy_result(commandValue.waitTime))
print 'sending command: ' + commandValue.command
self.ircRef.send(commandValue.command)