avahi.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2008, Willow Garage, Inc.
00004 # Copyright (c) 2014, Aldebaran Robotics (c)
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of the Willow Garage nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 import dbus, gobject, dbus.glib
00035 
00036 from python_qt_binding.QtGui import QComboBox
00037 
00038 import collections
00039 
00040 NetworkRobot = collections.namedtuple('NetworkRobot', ['name', 'address', 'port'])
00041 
00042 #import avahi
00043 class avahi:
00044     DBUS_NAME = "org.freedesktop.Avahi"
00045     DBUS_INTERFACE_SERVER = DBUS_NAME + ".Server"
00046     DBUS_PATH_SERVER = "/"
00047     DBUS_INTERFACE_ENTRY_GROUP = DBUS_NAME + ".EntryGroup"
00048     DBUS_INTERFACE_DOMAIN_BROWSER = DBUS_NAME + ".DomainBrowser"
00049     DBUS_INTERFACE_SERVICE_TYPE_BROWSER = DBUS_NAME + ".ServiceTypeBrowser"
00050     DBUS_INTERFACE_SERVICE_BROWSER = DBUS_NAME + ".ServiceBrowser"
00051     DBUS_INTERFACE_ADDRESS_RESOLVER = DBUS_NAME + ".AddressResolver"
00052     DBUS_INTERFACE_HOST_NAME_RESOLVER = DBUS_NAME + ".HostNameResolver"
00053     DBUS_INTERFACE_SERVICE_RESOLVER = DBUS_NAME + ".ServiceResolver"
00054     DBUS_INTERFACE_RECORD_BROWSER = DBUS_NAME + ".RecordBrowser"    
00055     PROTO_UNSPEC, PROTO_INET, PROTO_INET6  = -1, 0, 1
00056     IF_UNSPEC = -1
00057     LOOKUP_RESULT_CACHED = 1
00058     LOOKUP_RESULT_WIDE_AREA = 2
00059     LOOKUP_RESULT_MULTICAST = 4
00060     LOOKUP_RESULT_LOCAL = 8
00061     LOOKUP_RESULT_OUR_OWN = 16
00062     LOOKUP_RESULT_STATIC = 32
00063 
00064 
00065 class AvahiWidget(QComboBox):
00066     def __init__(self):
00067         super(AvahiWidget, self).__init__()
00068         self.setSizeAdjustPolicy(QComboBox.AdjustToContents)
00069         self.setInsertPolicy(QComboBox.InsertAlphabetically)
00070         self.setEditable(True)
00071 
00072         gobject.threads_init()
00073         dbus.glib.threads_init()
00074         self.robots = set()
00075         self.sys_bus = dbus.SystemBus()
00076         self.avahi_server = dbus.Interface(self.sys_bus.get_object(avahi.DBUS_NAME, '/'), avahi.DBUS_INTERFACE_SERVER)
00077 
00078         self.sbrowser = dbus.Interface(self.sys_bus.get_object(avahi.DBUS_NAME, self.avahi_server.ServiceBrowserNew(avahi.IF_UNSPEC,
00079             avahi.PROTO_INET, '_naoqi._tcp', 'local', dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
00080 
00081         self.sbrowser.connect_to_signal("ItemNew", self.avahiNewItem)
00082         self.sbrowser.connect_to_signal("ItemRemove", self.avahiItemRemove)
00083 
00084     def avahiNewItem(self, interface, protocol, name, stype, domain, flags):
00085         self.avahi_server.ResolveService(interface, protocol, name, stype, 
00086             domain, avahi.PROTO_INET, dbus.UInt32(0), 
00087             reply_handler=self.service_resolved, error_handler=self.print_error)
00088 
00089     def avahiItemRemove(self, interface, protocol, name, stype, domain, flags):
00090         tup = NetworkRobot(name, address, port)
00091         if tup in self.robots:
00092             self.robots.remove(tup)
00093         updateRobotCombobox();
00094 
00095     def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt, flags):
00096         self.robots.add(NetworkRobot(name, address, port))
00097         self.updateRobotCombobox()
00098 
00099     def updateRobotCombobox(self):
00100         selected = self.currentText()
00101         for i in range(self.count()):
00102             self.removeItem(0)
00103         id = -1
00104         texts = []
00105         for robot in self.robots:
00106             text = '%s:%s' % (robot.address, robot.port)
00107             text_full = "%s (%s)" % (robot.name, text)
00108             texts.append((text_full, text))
00109         texts = sorted(texts, key=lambda s: s[0].lower())
00110         for text in texts:
00111             self.addItem(text[0], text[1])
00112             if(text[0] == selected):
00113                 id = self.count()-1;
00114 
00115         if(self.count() == 1):
00116             self.setCurrentIndex(0)
00117         elif(id > -1):
00118             self.setCurrentIndex(id)
00119 
00120     def print_error(self, *args):
00121         print('error_handler')
00122         print(args)


nao_viz
Author(s): Stefan Osswald
autogenerated on Thu Aug 20 2015 14:54:04