g_free(c);
return r;
-
}
gchar *avahi_alternative_service_name(const gchar *s) {
/** Find an alternative for the specified host name. If called with an
* original host name, "2" is appended, Afterwards the number is
* increased on each call. (i.e. "foo" becomes "foo2" becomes "foo3"
- * and so on.)*/
+ * and so on.) g_free() the result. */
gchar *avahi_alternative_host_name(const gchar *s);
/** Find an alternative for the specified service name. If called with
an original service name, " #2" is appended. Afterwards the number
is increased on each call (i.e. "foo" becomes "foo #2" becomes
- "foo #3" and so on.)*/
+ "foo #3" and so on.) g_free() the result. */
gchar *avahi_alternative_service_name(const gchar *s);
AVAHI_C_DECL_END
string GetHostNameFqdn()
string GetDomainName()
string GetVersionString()
+ int32 GetState()
+ string GetAlternativeHostName(string name)
+ string GetAlternativeServiceName(string name)
[int32 interface, int32 protocol, string host_name, int32 aprotocol, string address] ResolveHostName(int32 interface, int32 protocol, string name, int32 aprotocol)
[int32 interface, int32 protocol, int32 aprotocol, string address, string host_name] ResolveAddress(int32 interface, int32 protocol, string address)
[int32 interface, int32 protocol, string name, string type, string domain, string host, int32 aprotocol, string address, uint16 port, string txt[]] ResolveService(int32 interface, int32 protocol, string name, string type, string domain, int32 aprotocol)
-DAVAHI_SERVICE_DIRECTORY=\"$(servicedir)\" \
-DAVAHI_CONFIG_FILE=\"$(pkgsysconfdir)/avahi-daemon.conf\"
-
AM_LDADD=-lexpat
# GLIB 2.0
pkgdata_DATA = \
avahi-service.dtd
-EXTRA_DIST = avahi-service.dtd avahi-daemon.conf example.service
+EXTRA_DIST = avahi-service.dtd avahi-daemon.conf example.service dbus-test.py
if ENABLE_DBUS
avahi_daemon_SOURCES += dbus-protocol.c dbus-protocol.h
return respond_string(c, m, avahi_server_get_host_name(avahi_server));
- } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
avahi_log_warn("Error parsing Server::GetDomainName message");
return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
- } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
- } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
avahi_log_warn("Error parsing Server::GetVersionString message");
}
return respond_string(c, m, PACKAGE_STRING);
+
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
+ DBusMessage *reply;
+ gint32 s;
+
+ if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
+ avahi_log_warn("Error parsing Server::GetState message");
+ goto fail;
+ }
+
+ s = (gint32) avahi_server_get_state(avahi_server);
+
+ reply = dbus_message_new_method_return(m);
+ dbus_message_append_args(reply, DBUS_TYPE_INT32, &s, DBUS_TYPE_INVALID);
+ dbus_connection_send(c, reply, NULL);
+ dbus_message_unref(reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
+ gchar *n, * t;
+
+ if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) {
+ avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
+ goto fail;
+ }
+
+ t = avahi_alternative_host_name(n);
+ respond_string(c, m, t);
+ g_free(t);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
+ gchar *n, *t;
+
+ if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) {
+ avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
+ goto fail;
+ }
+
+ t = avahi_alternative_service_name(n);
+ respond_string(c, m, t);
+ g_free(t);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
Client *client;
print "Host name: %s" % server.GetHostName()
print "Domain name: %s" % server.GetDomainName()
print "FQDN: %s" % server.GetHostNameFqdn()
+print "State: %i" % server.GetState()
+
+print server.GetAlternativeHostName("gurkiman10")
+print server.GetAlternativeServiceName("Ahuga Service")
def entry_group_state_changed_callback(t):
print "EntryGroup::StateChanged: ", t
# -*-python-*-
# $Id$
-import avahi, dbus, gobject
+import avahi, dbus, gobject, sys
try:
import dbus.glib
print "Error:", str(err)
def new_service(interface, protocol, name, type, domain):
+ global server
+
print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
# Asynchronous resolving
print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
def new_service_type(interface, protocol, type, domain):
+ global server, service_browsers
# Are we already browsing this domain for this type?
if service_browsers.has_key((interface, protocol, type, domain)):
service_browsers[(interface, protocol, type, domain)] = b
def browse_domain(interface, protocol, domain):
+ global server, service_type_browsers
# Are we already browsing this domain?
if service_type_browsers.has_key((interface, protocol, domain)):
# We browse for .local anyway...
if domain != "local":
browse_domain(interface, protocol, domain)
+
+
+domain = None
+
+if len(sys.argv) > 1:
+ domain = sys.argv[1]
bus = dbus.SystemBus()
server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", '/org/freedesktop/Avahi/Server'), 'org.freedesktop.Avahi.Server')
-# Explicitly browse .local
-browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
+if domain is None:
+ # Explicitly browse .local
+ browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
+
+ # Browse for other browsable domains
+ db = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), 'org.freedesktop.Avahi.DomainBrowser')
+ db.connect_to_signal('ItemNew', new_domain)
-# Browse for other browsable domains
-db = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), 'org.freedesktop.Avahi.DomainBrowser')
-db.connect_to_signal('ItemNew', new_domain)
+else:
+ # Just browse the domain the user wants us to browse
+ browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
try:
gobject.MainLoop().run()
--- /dev/null
+#!/usr/bin/python2.4
+# -*-python-*-
+# $Id$
+
+import avahi, dbus, gobject, sys, getopt
+
+try:
+ import dbus.glib
+except ImportError, e:
+ pass
+
+def usage(retval = 0):
+ print "%s [options] <name> <type> <port> [<txt> ...]\n" % sys.argv[0]
+ print " -h --help Show this help"
+ print " -d --domain Domain where to register this service"
+ print " -H --host Host where this service resides"
+ sys.exit(retval)
+
+try:
+ opts, args = getopt.getopt(sys.argv[1:], "d:H:", ["help", "domain=", "host="])
+except getopt.GetoptError:
+ usage(2)
+
+domain = ""
+host = ""
+
+for o, a in opts:
+ if o in ("-h", "--help"):
+ usage()
+
+ if o in ("-d", "--domain"):
+ domain = a
+
+ if o in ("-H", "--host"):
+ host = a
+
+if len(args) < 3:
+ sys.stderr.write("Invalid number of arguments\n")
+ sys.exit(1)
+
+name = args[0]
+stype = args[1]
+port = int(args[2])
+txt = args[3:]
+
+# python-dbus doesn't allow transmission of empty arrays, therefore we "fix" it with a bogus entry
+if len(txt) == 0:
+ txt.append("python-dbus=brain-damage")
+
+group = None
+
+def remove_service():
+ global group
+
+ if not (group is None):
+ group.Free()
+ group = None
+
+def add_service():
+ global group, name, stype, domain, host, port, txt
+
+ assert group is None
+
+ print "Adding service '%s' of type '%s' ..." % (name, args[1])
+
+ group = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.EntryGroupNew()), 'org.freedesktop.Avahi.EntryGroup')
+ group.connect_to_signal('StateChanged', entry_group_state_changed)
+ group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, name, stype, domain, host, dbus.UInt16(port), txt)
+ group.Commit()
+
+def entry_group_state_changed(state):
+ global name, server
+
+ if state == avahi.ENTRY_GROUP_ESTABLISHED:
+ print "Service established."
+ elif state == avahi.ENTRY_GROUP_COLLISION:
+ name = server.GetAlternativeServiceName(name)
+ print "WARNING: Service name collision, changing name to '%s' ..." % name
+ remove_service()
+ add_service()
+
+def server_state_changed(state):
+ if state == avahi.SERVER_COLLISION:
+ print "WARNING: Server name collision"
+ remove_service()
+ elif state == avahi.SERVER_RUNNING:
+ add_service()
+
+bus = dbus.SystemBus()
+server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", '/org/freedesktop/Avahi/Server'), 'org.freedesktop.Avahi.Server')
+server.connect_to_signal("StateChanged", server_state_changed)
+server_state_changed(server.GetState())
+
+try:
+ gobject.MainLoop().run()
+except KeyboardInterrupt, k:
+ pass
+
+remove_service()