File size: 1,099 Bytes
d4035c1 |
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 |
#!/usr/bin/python
# Server program
#!/usr/bin/python
from socket import *
import sys
host = sys.argv[1]
port = int(sys.argv[2])
addr = (host,port)
buf = 1024
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
numCollections=5
maxCollectionLen=300000
lockCollections = []
for i in range(numCollections): lockCollections.append({})
currentLockCollection = 0
# Receive messages
while 1:
data,addr = UDPSock.recvfrom(buf)
keyFound = False
for i in range(numCollections):
if lockCollections[i].has_key(data):
keyFound = True
if keyFound:
UDPSock.sendto('stop', addr)
else:
UDPSock.sendto('go', addr)
print "lock '%s' granted to %s" % (data, addr)
if len(lockCollections[currentLockCollection]) == maxCollectionLen:
currentLockCollection = (currentLockCollection + 1) % numCollections
lockCollections [currentLockCollection] = {}
print "** flushing lock collection %d" % currentLockCollection
lockCollections [currentLockCollection] [data] = 1
# Close socket
UDPSock.close()
|