First implementation of client/server

portnov [2009-02-25 11:39:02]
First implementation of client/server
Filename
client_test.py
data.py
discovery.py
test.py
diff --git a/client_test.py b/client_test.py
new file mode 100755
index 0000000..d8718ec
--- /dev/null
+++ b/client_test.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+
+from twisted.internet import reactor
+from twisted.protocols.basic import LineReceiver
+from twisted.internet.protocol import ClientFactory
+
+import data
+
+class Centrixc(LineReceiver):
+    def connectionMade(self):
+        self.sendLine(data.Message('register_machine').to_xml())
+
+    def lineReceived(self,line):
+        print data.Message.parse(line)
+        self.transport.loseConnection()
+
+class CClientFactory(ClientFactory):
+    protocol = Centrixc
+
+    def clientConnectionLost(self, connector, reason):
+        print 'Lost connection.  Reason:', reason
+        reactor.stop()
+
+    def clientConnectionFailed(self, connector, reason):
+        print 'Connection failed. Reason:', reason
+        reactor.stop()
+
+factory = CClientFactory()
+reactor.connectTCP('localhost',2009,factory)
+reactor.run()
+
diff --git a/data.py b/data.py
index 0cc5087..0951197 100644
--- a/data.py
+++ b/data.py
@@ -21,13 +21,16 @@ class Service(object):
         return "<Service %s on %s:%s>" % (self.name,self.ip,self.port)

 class Message(object):
-    def __init__(self,type,**kwargs):
+    def __init__(self,type=None,**kwargs):
         self.type = type
         self.__dict__.update(kwargs)

     def __repr__(self):
         return "<Message of type %s>" % self.type

+    def to_xml(self):
+        return xml.serialize(self)
+
     @classmethod
     def parse(cls, text):
         return xml.deserialize(text,cls=cls)
diff --git a/discovery.py b/discovery.py
index 004b0ec..c1bdef0 100644
--- a/discovery.py
+++ b/discovery.py
@@ -1,3 +1,4 @@
+import sys
 import signal
 import threading
 import gobject
@@ -17,15 +18,14 @@ servicenames = {'_ssh._tcp':  'ssh',

 def on_sigint(signum, frame):
     print "Got SIGINT"
-    browser.ml.stop()
-    browser.stop()
+    sys.exit()


 class Browser(threading.Thread):
     def __init__(self,group=None,target=None,name=None,args=(), kwargs=None,verbose=None):
         threading.Thread.__init__(self)
         self.types = args
-        signal.signal(signal.SIGINT, on_sigint)
+#         signal.signal(signal.SIGINT, on_sigint)

     def _interface_for_type(self,type):
         return dbus.Interface(self.bus.get_object(avahi.DBUS_NAME,
diff --git a/test.py b/test.py
index 1d8f55d..a0bf4c9 100755
--- a/test.py
+++ b/test.py
@@ -1,24 +1,31 @@
 #!/usr/bin/python

+import sys
 import time
 import signal
 from copy import copy

+from twisted.internet.protocol import Factory
+from twisted.protocols.basic import LineReceiver
+from twisted.internet import reactor
+
 import discovery
 import templates
 import configs
 import configsets
 import data
-
+import machines
 # import xmlserialize as XML

 #####################################
 # Internal section

 def on_sigint(signum, frame):
+    global browser, reactor
     print "Got SIGINT"
-    browser.ml.stop()
-    browser.stop()
+    browser.ml.quit()
+    reactor.stop()
+    sys.exit()

 signal.signal(signal.SIGINT, on_sigint)

@@ -28,7 +35,7 @@ class ZeroconfListener(object):
         global hosts

         if stype == '_workstation._tcp':
-            host = data.Host()
+            host = machines.Host()
             host.hostname = name.split()[0]
             host.ip = addr
             hosts.append(host)
@@ -42,6 +49,17 @@ class ZeroconfListener(object):
         services[sname] = svc
 #         print "services[%s] = %s" % (sname, svc)

+class Centrixd(LineReceiver):
+    def lineReceived(self,line):
+        msg = data.Message.parse(line)
+        print "Message received:", msg
+        if msg.type == 'register_machine':
+            self.sendLine(data.Message('OK').to_xml())
+        elif msg.type == 'quit':
+            self.transport.loseConnection()
+        else:
+            self.sendLine(data.Message('unknown').to_xml())
+
 #####################################
 # Setup

@@ -59,7 +77,7 @@ services['proxy'].port = 3128
 services['gateway'] = data.Service()
 services['gateway'].ip = '192.168.32.1'

-machine = data.Host()
+machine = machines.Host()
 machine.ip = '192.168.1.2'
 machine.hostname = 'portnov'

@@ -98,3 +116,9 @@ print
 nset = configsets.read_configset('configs','first.xml')
 print "Read nset:"
 print nset
+
+factory = Factory()
+factory.protocol = Centrixd
+reactor.listenTCP(2009, factory)
+print "Reactor run."
+reactor.run()
ViewGit