Package: apt-proxy
Version: 1.9.36~svn
Severity: wishlist
Tags: patch

  Hello,

  I am working on adding avahi/zeroconf support to apt-proxy. Current
  patches only advertise the service if "avahi" option is set to 1 in
  apt-proxy.conf file.
  For this to be useful, apt (itself or through apt-zeroconf) should be
  able to understand that an apt-proxy instance is running in the
  network. This and publishing also which archive are being served are
  things that I hope I can go on adding soon.

  Cheers,


-- System Information:
Debian Release: 4.0
  APT prefers unstable
  APT policy: (500, 'unstable'), (101, 'experimental')
Architecture: i386 (i686)
Shell:  /bin/sh linked to /bin/bash
Kernel: Linux 2.6.18-1-686
Locale: [EMAIL PROTECTED], [EMAIL PROTECTED] (charmap=UTF-8)

Versions of packages apt-proxy depends on:
ii  adduser                       3.102      Add and remove users and groups
ii  bzip2                         1.0.3-6    high-quality block-sorting file co
ii  debconf [debconf-2.0]         1.5.11     Debian configuration management sy
ii  logrotate                     3.7.1-3    Log rotation utility
ii  python                        2.4.4-2    An interactive high-level object-o
ii  python-apt                    0.6.20     Python interface to libapt-pkg
ii  python-central                0.5.12     register and build utility for Pyt
ii  python-twisted-web            0.6.0-1    An HTTP protocol implementation to

apt-proxy recommends no packages.

-- debconf information excluded
>From 09f68dff0f6f8ab417a4b8316be936e926a4e679 Mon Sep 17 00:00:00 2001
From: Jose Carlos Garcia Sogo <[EMAIL PROTECTED]>
Date: Tue, 13 Feb 2007 00:15:09 +0100
Subject: [PATCH] Initial work for supporting avahi publising service in apt-proxy.

---
 apt_proxy/apt_proxy.py |    8 +++
 apt_proxy/avahisrv.py  |  113 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+), 0 deletions(-)

diff --git a/apt_proxy/apt_proxy.py b/apt_proxy/apt_proxy.py
index 5b454af..ed4c913 100644
--- a/apt_proxy/apt_proxy.py
+++ b/apt_proxy/apt_proxy.py
@@ -28,6 +28,9 @@ from misc import log, MirrorRecycler
 import twisted_compat
 from clients import HttpRequestClient
 
+from avahisrv import AvahiService
+import socket
+
 #from posixfile import SEEK_SET, SEEK_CUR, SEEK_END
 #since posixfile is considered obsolete I'll define the SEEK_* constants
 #myself.
@@ -280,6 +283,7 @@ class Factory(protocol.ServerFactory):
         self.periodicCallback = None
         self.databases = databaseManager(self)
         self.recycler = None
+	self.avahisrv = None
 
     def __del__(self):
         pass
@@ -319,6 +323,8 @@ class Factory(protocol.ServerFactory):
         self.dumpdbs()
         self.recycler = MirrorRecycler(self, 1)
         #self.recycler.start()
+	self.avahisrv = AvahiService('apt-proxy@'+socket.gethostname(), '_apt-proxy._tcp', '', '', 9999, ['apt-proxy service'])
+	self.avahisrv.run()
 
     def configurationChanged(self, oldconfig = None):
         """
@@ -508,6 +514,8 @@ class Factory(protocol.ServerFactory):
             self.recycler = None
         self.stopPeriodic()
         #self.closeDatabases()
+	self.avahiservice.stop()
+
 
     def dumpdbs (self):
         def dump_update(key, value):
diff --git a/apt_proxy/avahisrv.py b/apt_proxy/avahisrv.py
new file mode 100644
index 0000000..074aab5
--- /dev/null
+++ b/apt_proxy/avahisrv.py
@@ -0,0 +1,113 @@
+# This file is part of apt-proxy.
+# It is heavily based on similar file from apt-zeroconf.
+# 
+# Copytight (C) 2006 - Jose Carlos Garcia Sogo <[EMAIL PROTECTED]>
+#
+# Copyright (C) 2006 - Florian Ludwig <[EMAIL PROTECTED]>
+#                      Franz Pletz <[EMAIL PROTECTED]>
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with apt-zeroconf; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import avahi
+import dbus, dbus.glib
+import gobject
+
+import threading
+from misc import log
+
+
+class AvahiService(threading.Thread):
+    """
+    This class takes care of publishing services in Avahi.
+    When instantiating you have to pass a service name, a type, 
+    current domain and host, port used to contact the service and
+    a descriptive txt.
+
+    It will run in its own thread looping for different Avahi services.
+
+    """
+    def  __init__(self, name, stype, domain, host, port, txt):
+        threading.Thread.__init__(self)
+        self.group = None
+        self.n_rename = 0
+        
+        self.stype = stype
+        self.name = name
+        self.domain = domain
+        self.host = host
+        self.port = port
+        self.txt = txt
+
+
+    def _server_state_changed(self, state):
+        if state == avahi.SERVER_COLLISION:
+            log.warning('Server name collision')
+            self._remove_servivce()
+        elif state == avahi.SERVER_RUNNING:
+            self._add_service()
+
+    def _remove_service(self):
+        if not self.group is None:
+            self.group.Reset()
+
+    def _add_service(self):
+        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)
+
+        assert self.group.IsEmpty()
+        log.debug('Creating service \'%s\' of type \'%s\'' %
+                (self.name, self.stype))
+
+        self.group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, 0,
+                self.name, self.stype, self.domain, self.host,
+                dbus.UInt16(self.port),
+                avahi.string_array_to_txt_array(self.txt))
+        self.group.Commit()
+        log.debug('Service commited')
+
+    def _entry_group_state_changed(self, state, wtf):
+        if state == avahi.ENTRY_GROUP_ESTABLISHED:
+            log.debug('Service established.')
+        elif state == avahi.ENTRY_GROUP_COLLISION:
+            self.n_rename += 1
+            if self.n_rename >= 12:
+                log.error('No suitable service name found after %i '
+                        'retries, exiting.' % n_rename)
+                self.stop()
+            else:
+                self.name = self.server.GetAlternativeServiceName(name)
+                log.warning('Service name collision, changing name to '
+                            '\'%s\'.' % name)
+                self._remove_service()
+                self._add_service()
+
+    def run(self):
+        self.bus = dbus.SystemBus()
+        self.server = self.bus.get_object ('org.freedesktop.Avahi', avahi.DBUS_PATH_SERVER)
+        self.interface = dbus.Interface (self.server, avahi.DBUS_INTERFACE_SERVER)
+        log.debug('Avahi service created')
+        self.server.connect_to_signal('StateChanged', self._server_state_changed)
+        self._server_state_changed(self.server.GetState())
+
+        self.ml = gobject.MainLoop()
+        gobject.threads_init()
+        self.ml.run()
+
+    def stop(self):
+        self.ml.quit()
-- 
1.4.4.4

>From 520007c8de5be652f4d792cf8eeef73bde74d0f5 Mon Sep 17 00:00:00 2001
From: Jose Carlos Garcia Sogo <[EMAIL PROTECTED]>
Date: Thu, 15 Feb 2007 00:09:55 +0100
Subject: [PATCH]  - avahi publishing will be only run if 'avahi' var is set in config file.
 - use 'port' setting from configuration, instead of hardcoding it to 9999
---
 apt_proxy/apt_proxy.py      |    5 +++--
 apt_proxy/apt_proxy_conf.py |    3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/apt_proxy/apt_proxy.py b/apt_proxy/apt_proxy.py
index ed4c913..8c8766e 100644
--- a/apt_proxy/apt_proxy.py
+++ b/apt_proxy/apt_proxy.py
@@ -323,8 +323,9 @@ class Factory(protocol.ServerFactory):
         self.dumpdbs()
         self.recycler = MirrorRecycler(self, 1)
         #self.recycler.start()
-	self.avahisrv = AvahiService('apt-proxy@'+socket.gethostname(), '_apt-proxy._tcp', '', '', 9999, ['apt-proxy service'])
-	self.avahisrv.run()
+	if getattr(self.config, 'avahi'):
+            self.avahisrv = AvahiService('apt-proxy@'+socket.gethostname(), '_apt-proxy._tcp', '', '', getattr(self.config, 'port'), ['apt-proxy service'])
+            self.avahisrv.run()
 
     def configurationChanged(self, oldconfig = None):
         """
diff --git a/apt_proxy/apt_proxy_conf.py b/apt_proxy/apt_proxy_conf.py
index d99df6a..8d38027 100644
--- a/apt_proxy/apt_proxy_conf.py
+++ b/apt_proxy/apt_proxy_conf.py
@@ -101,7 +101,8 @@ class apConfig:
         ['http_proxy', None , 'proxyspec'],
         ['username', 'aptproxy', 'string'],
         ['bandwidth_limit', None, '*int'],
-        ['use_experimental_decompressors', False, 'boolean']
+        ['use_experimental_decompressors', False, 'boolean'],
+        ['avahi', False, 'boolean']
         ]
 
     """
-- 
1.4.4.4

Reply via email to