import os, sys
import traceback, socket
import time

globals()['_GLOBAL_DONE'] = False

from pyZeroconf import Zeroconf
from server import ZeroConfServer as ZeroConfServerBase

class ZeroConfServer(ZeroConfServerBase):
	def check():
		return True
	check = staticmethod(check)

	def __init__(self):
		ZeroConfServerBase.__init__(self)

		self.services = {}
		self.server = Zeroconf.Zeroconf("0.0.0.0")

	def ServiceRemove(self, name, type, addr):
		print "ServiceRemove", name, type, addr
		key = (name, type, addr)
		if key in self.services:
			service = self.services[key]
			del self.services[key]
			self.server.unregisterService(service)

	def ServiceAdd(self, name, type, addr, required, optional):
		print "ServiceAdd", name, type, addr

		prop =  {}
		prop.update(optional)
		prop.update(required)
		stype = "_%s._tcp.local." % type
		sname = "%s.%s" % (name, stype)

		svc = Zeroconf.ServiceInfo(stype, sname, server=addr[0], address=socket.inet_aton(addr[1]), port=addr[2], properties=prop)

		self.server.registerService(svc, 30)
		self.services[(name, type, addr)] = svc

	######################################
	# Callback functions
	######################################

	def run(self):
		while not globals()['_GLOBAL_DONE']:
			try:
				self.server.run()
			except Exception, e:
				print e
				traceback.print_exc()
				globals()['_GLOBAL_DONE'] = True

	def exit(self):
		globals()['_GLOBAL_DONE'] = True

def main():
	from game import Game

	game1 = Game("testing 1")
	game1.updateRequired({'tp': '0.3', 'server': 'None', 'sertype':'Avahi Testing Script', 'rule': "Testing Avahi!", 'rulever': 'None'})
	game1.addLocation("tp", ("mithro.local", "10.1.1.1", 80))

	game2 = Game("testing 2")
	game2.updateRequired({'tp': '0.3', 'server': 'None', 'sertype':'Avahi Testing Script', 'rule': "Testing Avahi!", 'rulever': 'None'})
	game2.addLocation("tp",  ("mithro.local", "10.1.1.1", 8080))
#	game2.addLocation("tp",  ("mithro.local", "10.1.1.1", 443)) -- Can't have to services on the same name and type
	game2.addLocation("tps", ("mithro.local", "10.1.1.1", 90))

	a = ZeroConfServer()
	a.GameAdd(game1)
	a.GameAdd(game2)
	a.run()

if __name__ == "__main__":
	main()


@@ -328,16 +328,35 @@
 (self)%0A%0A
+%09%09self.tocall = %5B%5D%0A
 %09%09self.s
@@ -456,31 +456,88 @@
 e, addr):%0A%09%09
-print %22
+self.post(self.__ServiceRemove, (name, type, addr), %7B%7D)%0A%0A%09def __
 ServiceRemov
@@ -537,17 +537,21 @@
 ceRemove
-%22
+(self
 , name, 
@@ -560,16 +560,18 @@
 pe, addr
+):
 %0A%09%09key =
@@ -785,15 +785,89 @@
 :%0A%09%09
-print %22
+self.post(self.__ServiceAdd, (name, type, addr, required, optional), %7B%7D)%0A%0A%09def __
 Serv
@@ -872,17 +872,21 @@
 rviceAdd
-%22
+(self
 , name, 
@@ -895,17 +895,38 @@
 pe, addr
-%0A
+, required, optional):
 %0A%09%09prop 
@@ -1369,65 +1369,240 @@
 def 
-run(self):%0A%09%09while not globals()%5B'_GLOBAL_DONE'%5D:%0A%09%09%09try:
+post(self, method, args, kw):%0A%09%09self.tocall.append((method, args, kw))%0A%0A%09def run(self):%0A%09%09while not globals()%5B'_GLOBAL_DONE'%5D:%0A%09%09%09try:%0A%09%09%09%09if len(self.tocall) %3E 0:%0A%09%09%09%09%09method, args, kw = self.tocall.pop(0)%0A%09%09%09%09%09method(*args, **kw)%0A
 %0A%09%09%09

Make the ServiceAdd/ServiceRemove on the pyzeroconf_server non-blocking.