Try to find master on relay startup

portnov [2009-03-02 20:07:44]
Try to find master on relay startup
Filename
centrixd/centrixd.ini
centrixd/cxcore.py
centrixd/machines.py
centrixd/zeroconf.py
machines.py
diff --git a/centrixd/centrixd.ini b/centrixd/centrixd.ini
index e0cc02c..2aa0cf0 100644
--- a/centrixd/centrixd.ini
+++ b/centrixd/centrixd.ini
@@ -2,8 +2,8 @@
 Network Name = Test Centrix Network
 Node Name = Main Centrix Server

-Mode = Master
-# Mode = Relay
+# Mode = Master
+Mode = Relay
 # Master = centrix.local

 [Network]
diff --git a/centrixd/cxcore.py b/centrixd/cxcore.py
index 82a1921..3dfd835 100644
--- a/centrixd/cxcore.py
+++ b/centrixd/cxcore.py
@@ -3,15 +3,16 @@
 # jid: rigid_mgn@jabber.ru

 import signal
-
-from cxutils import log
-from cxlogger import CXLogger
-from cxconfig import config
 from sys import exit

 from louie import dispatcher
 from time import sleep

+from cxutils import log
+from cxlogger import CXLogger
+from cxconfig import config
+import zeroconf
+
 def on_sigint(signum, frame):
     print "Got SIGINT"
     log("Ctrl-C pressed at console - exiting")
@@ -27,12 +28,22 @@ mode = config['Centrix']['Mode']
 master = (mode == 'Master')
 log("Entering %s mode" % mode)

+if config['Network']['ZC Discover'] != False:
+    lock = zeroconf.discover(timeout=3000)
+    print "Waiting for services discovery..."
+    lock.acquire()
+    print zeroconf.services
+
 if not master:
     if 'Master' in config['Centrix']:
         smarthost = config['Centrix']
-        log("Will use %s as master" % smarthost)
     else:
-        log("Master not found.")
-        exit()
+        if 'centrix_master' in zeroconf.services:
+            smarthost = zeroconf.services['centrix_master'].ip
+        else:
+            print "Could not find master"
+            log("Master not found.")
+            exit()
+    log("Will use %s as master" % smarthost)

 import cxproto
diff --git a/centrixd/machines.py b/centrixd/machines.py
new file mode 100644
index 0000000..92996ff
--- /dev/null
+++ b/centrixd/machines.py
@@ -0,0 +1,64 @@
+
+lastpriority = 0
+groups = []
+
+class Host(object):
+    def __init__(self):
+        self.ip = ''
+        self.hostname = ''
+
+    def __repr__(self):
+        return "<Host %s>" % self.hostname
+
+class PriorityList(list):
+    """Something like python list,
+    but automatically exntends when assigning non-existing items."""
+    def __setitem__(self,idx,val):
+        if idx >= len(self):
+            self.extend([None]*(idx-len(self)))
+            self.append(val)
+        else:
+            list.__setitem__(self,idx,val)
+
+    def swap_up(self,idx):
+        if idx==len(self)-1:
+            return
+        self[idx+1],self[idx] = self[idx],self[idx+1]
+
+    def swap_down(self,idx):
+        if not idx:
+            return
+        self[idx-1],self[idx] = self[idx],self[idx-1]
+
+bypriority = PriorityList()
+
+class MachinesGroup(object):
+    "Group of machines"
+    def __init__(self,name,load=False,*args):
+        global lastpriority
+        global groups
+
+        self.name = name
+        self.machines = args
+        groups.append(self)
+        if not load:
+            lastpriority += 1
+            self.set_priority(lastpriority)
+
+    def set_priority(self,priority):
+        global bypriority
+        self.priority = priority
+        bypriority[priority] = self
+
+    def inc_priority(self):
+        global bypriority
+        bypriority.swap_up(self.priority)
+
+    def dec_priority(self):
+        global bypriority
+        bypriority.swap_down(self.priority)
+
+    def __repr__(self):
+        return "<Group %s (%d machines)>" % (self.name, len(self.machines))
+
+
diff --git a/centrixd/zeroconf.py b/centrixd/zeroconf.py
index 94eafaa..3c3fc5b 100644
--- a/centrixd/zeroconf.py
+++ b/centrixd/zeroconf.py
@@ -7,8 +7,8 @@ from dbus.mainloop.glib import DBusGMainLoop
 import avahi
 import socket

-from centrixd import data
-from centrixd.cxconfig import config
+import data
+from cxconfig import config
 import machines

 hosts = []
@@ -23,6 +23,8 @@ servicenames = {'_ssh._tcp':  'ssh',
                 '_ftp._tcp':  'ftp',
                 '_workstation._tcp': 'machine',
                 '_pulse-server._tcp':  'pulseaudio',
+                '_centrix_master._tcp': 'centrix_master',
+                '_centrix_relay._tcp': 'centrix_relay',
                 '_nameserver._udp': 'dns'}

 class ZeroconfThread(threading.Thread):
@@ -151,7 +153,7 @@ class ZeroconfListener(object):
         services[sname] = svc
 #         print "services[%s] = %s" % (sname, svc)

-def discover(services=('_ssh._tcp','_workstation._tcp','_nameserver._udp'),timeout=None):
+def discover(services=('_ssh._tcp','_workstation._tcp','_nameserver._udp', '_centrix_master._tcp'),timeout=None):
     """Discover services of given types via zeroconf.
     Returns (blocked) lock, which is released when all services are discovered."""
     global browser
diff --git a/machines.py b/machines.py
deleted file mode 100644
index 92996ff..0000000
--- a/machines.py
+++ /dev/null
@@ -1,64 +0,0 @@
-
-lastpriority = 0
-groups = []
-
-class Host(object):
-    def __init__(self):
-        self.ip = ''
-        self.hostname = ''
-
-    def __repr__(self):
-        return "<Host %s>" % self.hostname
-
-class PriorityList(list):
-    """Something like python list,
-    but automatically exntends when assigning non-existing items."""
-    def __setitem__(self,idx,val):
-        if idx >= len(self):
-            self.extend([None]*(idx-len(self)))
-            self.append(val)
-        else:
-            list.__setitem__(self,idx,val)
-
-    def swap_up(self,idx):
-        if idx==len(self)-1:
-            return
-        self[idx+1],self[idx] = self[idx],self[idx+1]
-
-    def swap_down(self,idx):
-        if not idx:
-            return
-        self[idx-1],self[idx] = self[idx],self[idx-1]
-
-bypriority = PriorityList()
-
-class MachinesGroup(object):
-    "Group of machines"
-    def __init__(self,name,load=False,*args):
-        global lastpriority
-        global groups
-
-        self.name = name
-        self.machines = args
-        groups.append(self)
-        if not load:
-            lastpriority += 1
-            self.set_priority(lastpriority)
-
-    def set_priority(self,priority):
-        global bypriority
-        self.priority = priority
-        bypriority[priority] = self
-
-    def inc_priority(self):
-        global bypriority
-        bypriority.swap_up(self.priority)
-
-    def dec_priority(self):
-        global bypriority
-        bypriority.swap_down(self.priority)
-
-    def __repr__(self):
-        return "<Group %s (%d machines)>" % (self.name, len(self.machines))
-
-
ViewGit