
# This is a simple script that shows how too use TCPServer to build
# a synchronously multiplexed server that clients can connect to 
# and that will print all data sent to it in the standard output as
# well as sending it back to the client.

from Selecting import SelectManager, TCPServer

# Handler for incoming connections. receive() is called upon reception
# of terminator-delimited data. (\r\n)

class MyHandler( SelectManager.ChannelHandler ):
   def __init__( s, server ):
	pass
   def receive( s, data ):
	print "Got data: %s" % data
	s.write( data )

# instantiate the manager
mgr = SelectManager.ProactiveSelectManager() 

# add the server socket as a channel to the select manager
port = input( 'Port to bind to: ' )
serv = TCPServer.ServerSocket(( '', port ), MyHandler )
mgr.add_channel( serv )

# serve new connections forever
mgr.loop()

