2009-10-07 15 views
5

Sto provando a scrivere un programma che emetta dati che possono essere serviti su una rete con avahi. La documentazione che ho visto sembra dire che devo registrare il servizio con dbus e quindi collegarlo ad avahi, ma la documentazione per farlo è piuttosto scarsa. Qualcuno sa di una buona documentazione per questo? Ho cercato questi:Creazione di un programma che deve essere trasmesso da avahi

python-dbus: http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html#exporting-objects

python-avahi: http://www.amk.ca/diary/2007/04/rough_notes_python_and_dbus.html

sono davvero familiarità con il modo avahi funziona a tutti, quindi tutti i puntatori sarebbe utile .

risposta

0

Avahi è "solo" un'implementazione client di ZeroConfig che fondamentalmente è un protocollo "DNS basato su multicast". Puoi utilizzare Avahi per pubblicare la disponibilità dei tuoi "dati" attraverso gli endpoint. I dati effettivi devono essere recuperati attraverso altri mezzi, ma normalmente si registra un end-point che può essere "invocato" attraverso un metodo a proprio piacimento.

-3

Se il tuo programma è scritto in Java, puoi usare avahi4j che fornisce un'API facile da usare per registrare (e navigare) i servizi Bonjour sulla tua rete locale. http://avahi4j.googlecode.com

10

Mi rendo conto che questa risposta è abbastanza tardi, considerando che la tua domanda è stata posta quattro anni fa. Tuttavia, potrebbe aiutare gli altri.

Il seguente annuncia un servizio utilizzando avahi/dbus:

import avahi 
import dbus 
from time import sleep 


class ServiceAnnouncer: 
    def __init__(self, name, service, port, txt): 
     bus = dbus.SystemBus() 
     server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) 
     group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), 
           avahi.DBUS_INTERFACE_ENTRY_GROUP) 

     self._service_name = name 
     index = 1 
     while True: 
      try: 
       group.AddService(avahi.IF_UNSPEC, avahi.PROTO_INET, 0, self._service_name, service, '', '', port, avahi.string_array_to_txt_array(txt)) 
      except dbus.DBusException: # name collision -> rename 
       index += 1 
       self._service_name = '%s #%s' % (name, str(index)) 
      else: 
       break 

     group.Commit() 

    def get_service_name(self): 
     return self._service_name 


if __name__ == '__main__': 
    announcer = ServiceAnnouncer('Test Service', '_test._tcp', 12345, ['foo=bar', '42=true']) 
    print announcer.get_service_name() 

    sleep(42) 

Utilizzando avahi-passi in rassegna per verificare che sia effettivamente pubblicato:

[email protected]:~$ avahi-browse -a -v -t -r 
Server version: avahi 0.6.30; Host name: els-mifr-03.local 
E Ifce Prot Name           Type     Domain 
+ eth0 IPv4 Test Service         _test._tcp   local 
= eth0 IPv4 Test Service         _test._tcp   local 
    hostname = [els-mifr-03.local] 
    address = [10.9.0.153] 
    port = [12345] 
    txt = ["42=true" "foo=bar"]