1. Alessio Caiazza
  2. avahiserve

Commits

Alessio Caiazza  committed a4eaada

Verbosity aligned with mercurial state, more verbose on TXT data

  • Participants
  • Parent commits 2de4c35
  • Branches default

Comments (0)

Files changed (1)

File avahiserve.py

View file
 class AvahiReg:
 
 	def __init__(self, name, port):
-		self.serviceName = "Mercurial: " + name
+		self.verbose = False
+		self.serviceName = "Mercurial"
+		if name != '':
+			self.serviceName += ": " + name
 		self.serviceType = "_http._tcp" # See http://www.dns-sd.org/ServiceTypes.html
 		self.servicePort = port
 		self.serviceTXT = ["extension's author=Alessio Caiazza"] #TXT record for the service
 		self.rename_count = 12 # Counter so we only rename after collisions a sensible number of times
 
 	def add_service(self):
-	#global group, serviceName, serviceType, servicePort, serviceTXT, domain, host
 		if self.group is None:
 			self.group = dbus.Interface(
 				self.bus.get_object( avahi.DBUS_NAME, self.server.EntryGroupNew()),
 				avahi.DBUS_INTERFACE_ENTRY_GROUP)
 			self.group.connect_to_signal('StateChanged', self.entry_group_state_changed)
-		print "Adding service '%s' of type '%s' ..." % (self.serviceName, self.serviceType)
+		if self.verbose:
+			print "Adding service '%s' of type '%s' ..." % (self.serviceName, self.serviceType)
 		self.group.AddService(
 			avahi.IF_UNSPEC,    #interface
 			avahi.PROTO_UNSPEC, #protocol
 		self.group.Commit()
 
 	def remove_service(self):
-	   #global group
-	   if not group is None:
-		   group.Reset()
+		if not group is None:
+			group.Reset()
 
 	def server_state_changed(self, state):
 		if state == avahi.SERVER_COLLISION:
-			print "WARNING: Server name collision"
+			if self.verbose:
+				print "WARNING: Server name collision"
 			remove_service()
 		elif state == avahi.SERVER_RUNNING:
 			self.add_service()
 
 	def entry_group_state_changed(self,state, error):
-		#global serviceName, server, rename_count
 
-		print "state change: %i" % state
+		if self.verbose:
+			print "state change: %i" % state
 
 		if state == avahi.ENTRY_GROUP_ESTABLISHED:
 			print "Service established."
 			rename_count = rename_count - 1
 			if rename_count > 0:
 				name = server.GetAlternativeServiceName(name)
-				print "WARNING: Service name collision, changing name to '%s' ..." % name
+				if self.verbose:
+					print "WARNING: Service name collision, changing name to '%s' ..." % name
 				remove_service()
 				add_service()
 
 			else:
-				print "ERROR: No suitable service name found after %i retries, exiting." % n_rename
+				if self.verbose:
+					print "ERROR: No suitable service name found after %i retries, exiting." % n_rename
 				main_loop.quit()
 		elif state == avahi.ENTRY_GROUP_FAILURE:
-			print "Error in group state changed", error
+			if self.verbose:
+				print "Error in group state changed", error
 			main_loop.quit()
 			return
-    
+
 	def start(self):
 		DBusGMainLoop( set_as_default=True )
 		self.bus = dbus.SystemBus()
 
 		self.server.connect_to_signal( "StateChanged", self.server_state_changed )
 		self.server_state_changed( self.server.GetState() )
+	
+	def stop(self):
+		if self.verbose:
+			print "Stopping avahi-publish-service"
+		if not self.group is None:
+			self.group.Free()
 
 # every command must take a ui and and repo as arguments.
 # opts is a dict where you can find other command line flags
 	else:
 		port = 8000
 	a = AvahiReg(opts['name'], port)
+
+	if ui.cdata.has_option('web', 'description'):
+		a.serviceTXT += ["description=" + ui.cdata.get('web','description')]
+	if ui.cdata.has_option('ui', 'username'):
+		#remove < and > cose make problems on Avahi Discovery
+		a.serviceTXT += ["owner=" + 
+				ui.cdata.get('ui','username')
+				.replace('<','')
+				.replace('>','')]
+
+	a.verbose = ui.verbose
 	a.start()
-	commands.serve(ui,repo, **opts)
+	try:
+		commands.serve(ui,repo, **opts)
+	except KeyboardInterrupt:
+		pass
 	
-	#never reached, I think...
-	if not group is None:
-		group.Free()
+	a.stop()
 
 cmdtable = {
 	# cmd name        function call