worker.py
author Eugen Sawin <sawine@me73.com>
Thu, 17 Jun 2010 23:24:41 +0200
changeset 6 9147c845cecb
parent 5 5ab3cc2e7851
permissions -rw-r--r--
Client skeleton added.
sawine@2
     1
#!/usr/bin/python
sawine@2
     2
sawine@2
     3
import com
sawine@2
     4
import model
sawine@2
     5
import optparse
sawine@5
     6
import time
sawine@2
     7
import pickle
sawine@2
     8
import SocketServer
sawine@2
     9
sawine@2
    10
option_list = [
sawine@2
    11
	optparse.make_option("-q", "--quiet", action="store_true", dest="quiet", 
sawine@2
    12
		help="quiet mode", default=False)]
sawine@2
    13
sawine@2
    14
def spawn(host, port):
sawine@2
    15
	return Worker(host, port, Handler, ())
sawine@2
    16
		
sawine@2
    17
class Handler(SocketServer.BaseRequestHandler):
sawine@2
    18
	"""
sawine@2
    19
	"""	
sawine@2
    20
	def handle(self):
sawine@2
    21
		data = self.request[0].strip()
sawine@2
    22
		socket = self.request[1]
sawine@2
    23
		model_size = int(data[:10])
sawine@2
    24
		model = pickle.loads(data[10:10+model_size])
sawine@2
    25
		max_iter = int(data[10+model_size:20+model_size])
sawine@2
    26
		model.update(max_iter)
sawine@4
    27
		for cell in model.cells.iteritems():
sawine@6
    28
			socket.sendto(" ".join(("data", model.name, str(cell[0].real), 
sawine@6
    29
			str(cell[0].imag), str(cell[1].real), str(cell[1].imag))),
sawine@5
    30
				self.client_address)
sawine@2
    31
		
sawine@2
    32
class Worker(SocketServer.UDPServer):
sawine@2
    33
	def __init__(self, host, port, handler, options):
sawine@2
    34
		SocketServer.UDPServer.__init__(self, (host, port), handler)
sawine@2
    35
		self.host = host
sawine@2
    36
		self.port = port
sawine@2
    37
		self.handler = handler
sawine@2
    38
		self.options = options
sawine@2
    39
	def run(self):
sawine@2
    40
		print "worker running at port %s" % self.port
sawine@2
    41
		self.serve_forever()
sawine@2
    42
sawine@2
    43
if __name__ == "__main__":
sawine@2
    44
	parser = optparse.OptionParser(usage="usage: %prog [options] port",
sawine@2
    45
		option_list=option_list)
sawine@2
    46
	options, args = parser.parse_args()
sawine@2
    47
	
sawine@2
    48
	if len(args) > 0:
sawine@2
    49
		port = int(args[0])
sawine@2
    50
		worker = Worker("localhost", port, Handler, options)
sawine@2
    51
		worker.run()