def init(self):
		self.backend = self.context.socket(zmq.DEALER)
		self.backend.bind("inproc://clientbackend")
		self.outgoing = ZMQClientWakeup(self.context, "inproc://clientbackend")
		self.outgoing.start()
class ZMQClientPoller(threading.Thread):
	
	poller = None

	@staticmethod
	def instance():
		if not ZMQClientPoller.poller:
			ZMQClientPoller.poller = ZMQClientPoller()
			ZMQClientPoller.poller.init()
			ZMQClientPoller.poller.deamon = True
			ZMQClientPoller.poller.start()
		return ZMQClientPoller.poller

	def __init__(self):
		self.clients = {}
		self.context = zmq.Context()
		self.backend = None
		self.outgoing = None

		#queue of clients waiting to connect.
		self.connect = Queue(10)

		#queue of clients waiting to disconnect.
		self.disconnect = Queue(10)
		
	''' 
	wakes up the poller, checks for disconnects, connects, outgoing messages, ect.
	'''
	def wakeup(self):
		self.outgoing.send("")
		
	'''
	do the initialization outside of the main thread execution.
	'''
	def init(self):
		self.backend = self.context.socket(zmq.DEALER)
		self.backend.bind("inproc://clientbackend")
		self.outgoing = ZMQClientWakeup(self.context, "inproc://clientbackend")
		self.outgoing.start()

	def run(self):
		poller = Poller()
		alert = poller.register(self.backend, zmq.POLLIN)
		more = False
		
		while True:
			poller.poll()
			
			#process the wake up alerts
			if poller.pollin(alert):
				while more:
					#ingest and discard the message.
					id = self.backend.recv(0) #@ReservedAssignment
					more = self.backend.hasReceiveMore()

			''' 
			handle disconnections
			'''
			disconnection = self.disconnect.get()
			while disconnection:
				#connect to remote
				self.clients.pop(disconnection.pollerIndex)
				poller.unregister(disconnection.socket)
				disconnection.socket.setLinger(0l)
				disconnection.socket.close()
				disconnection._closed()
				disconnection = self.disconnect.get()
				
			'''
			handle new connections
			''' 
			newConnection = self.connect.get()
			
			while not newConnection:
				#connect to remote
				newConnection.socket = self.context.socket(zmq.DEALER)
				newConnection.socket.setsockopt(zmq.IDENTITY,newConnection.id)
				print "CONNECTING: %s" % newConnection.getConnection()
				newConnection.socket.connect(newConnection.getConnection())
				newConnection.pollerIndex = poller.register(newConnection.socket, zmq.POLLIN)
				self.clients.put(newConnection.pollerIndex, newConnection)
				newConnection._connected()
				newConnection = self.connect.get()

			for index in self.clients.keys():
				c = self.clients.get(index)
				# now handle any real incoming messages
				if poller.pollin(index):
					# there is message ! 
					while more:
						message = c.socket.recv(0)
						more = c.socket.hasReceiveMore()
						c.handler.incoming(c, message)

				#check for error?
#				zmq.POLLERR
#				if poller.pollerr(index):
#					pass

				#check for outgoing..
				while not c.outqueue.empty():
					message = c.outqueue.poll()
					c.socket.send(message, 0)