]> git.meshlink.io Git - catta/commitdiff
* add avahi-publish
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Jul 2005 20:32:07 +0000 (20:32 +0000)
committerLennart Poettering <lennart@poettering.net>
Fri, 29 Jul 2005 20:32:07 +0000 (20:32 +0000)
* avahi-dump-all: allow user to specify domain to browse
* DBUS: implement new methods Server.GetState(), Server.GetAlternativeHostName(), Server.GetAlternativeServiceName()
* Add dbust-test.py to EXTRA_DIST
* doc fix for alternative.h

git-svn-id: file:///home/lennart/svn/public/avahi/trunk@192 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-common/alternative.c
avahi-common/alternative.h
avahi-daemon/DBUS-API
avahi-daemon/Makefile.am
avahi-daemon/dbus-protocol.c
avahi-daemon/dbus-test.py
avahi-utils/avahi-dump-all
avahi-utils/avahi-publish [new file with mode: 0755]

index c9a0f48ac73a7c04fa245dbeceda6a73de35a6c2..b1b52de6d55a4d0ac0629cb18ccf1fdf8758186f 100644 (file)
@@ -50,7 +50,6 @@ gchar * avahi_alternative_host_name(const gchar *s) {
     g_free(c);
     
     return r;
-    
 }
 
 gchar *avahi_alternative_service_name(const gchar *s) {
index 7a2a4558494a38732b3f2c896f2973fd7f7b6721..c1bf8cb56635b42ce32dcdce7498a6c430a2d615 100644 (file)
@@ -33,13 +33,13 @@ AVAHI_C_DECL_BEGIN
 /** 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
index 5acee5f87b152d522ba9a6d43d6adc5120fc8b5b..c10a7e31c277ac266f481fe0e01ae3e5e4293eb3 100644 (file)
@@ -5,6 +5,9 @@ org.freedesktop.Avahi.Server               -- Accessible through /org/freedeskto
         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) 
index f746a491348b4bea39cb1f0f188130c7dd78ac4e..a1b2dcb8d9f0c5ea26dbfd4d0ef8b10d436fe1b0 100644 (file)
@@ -27,7 +27,6 @@ AM_CFLAGS= \
        -DAVAHI_SERVICE_DIRECTORY=\"$(servicedir)\" \
        -DAVAHI_CONFIG_FILE=\"$(pkgsysconfdir)/avahi-daemon.conf\"
 
-
 AM_LDADD=-lexpat
 
 # GLIB 2.0
@@ -64,7 +63,7 @@ service_DATA = \
 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
index f2d34d1b2dfa4254f9ecf2eb6baad4492deef845..4e67281da57c2a14f8e60eb1386184cd77793851 100644 (file)
@@ -882,7 +882,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
 
         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");
@@ -891,7 +891,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
 
         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");
@@ -900,7 +900,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
     
         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");
@@ -908,6 +908,52 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
         }
     
         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;
index 3cbe19c6aae846a53e45bd6b8bfa8ee62e90e820..baebf40a4a9a9a6a4d3f75f5d3838bb76c6fac5e 100755 (executable)
@@ -22,6 +22,10 @@ print server.ResolveAddress(0, 0, "192.168.50.4")
 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
index acd922d05afa93c674f24195c255e9358ef0e157..03efa85b7331fbf57d224ebfbaedec0b2d64fced 100755 (executable)
@@ -2,7 +2,7 @@
 # -*-python-*-
 # $Id$ 
 
-import avahi, dbus, gobject
+import avahi, dbus, gobject, sys
 
 try:
     import dbus.glib
@@ -20,6 +20,8 @@ def print_error(err):
     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
@@ -29,6 +31,7 @@ def remove_service(interface, protocol, name, type, domain):
     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)):
@@ -43,6 +46,7 @@ def new_service_type(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)):
@@ -60,16 +64,27 @@ def new_domain(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()
diff --git a/avahi-utils/avahi-publish b/avahi-utils/avahi-publish
new file mode 100755 (executable)
index 0000000..4375d1a
--- /dev/null
@@ -0,0 +1,99 @@
+#!/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()