#simulate dictionary server with two modes. requires 3 seconds to
#switch between modes. first mode has 8 requests/s, second 5/s.
#server handles 15 requests/s.
#simulation stops when there is a request older than 30 seconds.

#parameters
nlent = 3	#time to spend in each mode
ennlt = 3
#state
t = 0		#time elapsed
nlen = []	#list of requests
ennl = []

def mode(t):
	if t % (nlent+3+ennlt+3) <= nlent:
		return 1
	if (nlent + 3) < t % (nlent+3+ennlt+3) <= (nlent + 3 + ennlt):
		return -1
	return 0

tot = nlent+3+ennlt+3 * 1.0
def f(x): return 8*x - 15*(nlent/tot)*x
def g(x): return 5*x - 15*(ennlt/tot)*x

#for a in range(100):
#	print a, f(a), g(a)
#print [g(a) for a in range(100)]
#exit()

while True:
	#place requests
	for a in range(8):
		nlen.append(t)
	for a in range(5):
		ennl.append(t)

	# handle requests
	for a in range(15):
		if mode(t) == 1:
			if not nlen: break
			nlen.pop(0)
		elif mode(t) == -1:
			if not ennl: break
			ennl.pop(0)

	t += 1		
	print t, len(nlen), len(ennl) 
	#stop if there are requests older than 30 seconds
	for a in (nlen, ennl):
		if a and min(a) + 30 < t:
			print 'there is a request older than 30 seconds'
			print a
			exit()
