from twisted.web import resource, server, http
from twisted.internet import reactor
import sseframework
import thread
import time

class ExampleService(sseframework.Service):
	def __init__(self):
		#the constructor
		
		#call the base class constructor to set a name for this service
		sseframework.Service.__init__(self,'ExampleService')

	def parse(self,request):
		#see if we already have a client
		client = self.findClient(request)
		
		if client != None:
			#as we already have a session open to the client,
			#we don't need to open a new one. 
			
			#see if the client wants to run a command		
			if request.args.has_key('c'):
				c = request.args['c'][0]
				
				#He's sending a message
				if c == 'say':
					self.broadcast('chat',request.args['msg'][0])
					
			#we don't need to do anything if the client is already online
			#and didn't send any commands or such.
			return ''
		
		else:		
			#if there is no client yet start a new session
			client = sseframework.SSEClient(self,request)
			return self.startSession(client)


if __name__ == '__main__':
	port = 3080
		
	#define service paths and classes
	services = {
						'/': ExampleService()						
						}
	
	#create a new SSEServer
	cc = sseframework.SSEServer(services,True,True)
	
	#start Twisted
	reactor.listenTCP(port, server.Site(cc))	
	reactor.run()
