from time import sleep
from sys import exit
from Queue import Queue
import threading

from PdServer import PdServer
from PdChannel import PdChannel
from PdLANPoller import PdLANPoller

class PdLANParty(PdServer):
	def __init__(self):
		""" Creates the lanparty server. Call the Run() method to start it. Use GetMessages() and PostMessage(data) to send stuff to and from connected clients. """
		# two way Pd style connection working
		# run server first
		# emulate ip address broadcast catcher = nc -lu 10314
		# emulate Pd listener = nc -l 192.168.2.113 10315
		# emulate Pd sender = nc localhost 10314 -s 192.168.2.113
		
		# launch a thread which broadcasts to all devices on the network every two seconds
		
		poller = PdLANPoller()
		threadSrv = threading.Thread(target=poller.Launch)
		threadSrv.start()
		
		PdServer.__init__(self, Queue(), Queue(), channelClass=PdChannel, localaddr=("0.0.0.0", 10314), listeners=20)
	
	def Launch(self):
		threadSrv = threading.Thread(target=self.ServerLoop)
		threadSrv.start()
	
	def ServerLoop(self):
		try:
			while 1:
				self.Pump()
				sleep(0.001)
		except:
			exit(0)

