changeset 6:5799493fa548

connection and disconnection management
author Goffi <goffi@goffi.org>
date Fri, 23 Oct 2009 17:04:22 +0200
parents c0c92129a54b
children c14a3a7018a5
files sat.tac
diffstat 1 files changed, 37 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/sat.tac	Mon Oct 19 23:49:31 2009 +0200
+++ b/sat.tac	Fri Oct 23 17:04:22 2009 +0200
@@ -98,11 +98,21 @@
             self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self)
 
     def connect(self):
+        if (self.connected):
+            info("already connected !")
+            return
         print "connecting..."
         reactor.connectTCP(self.memory.getParamV("Server", "Connection"), 5222, self.factory)
-        self.connectionStatus="online"  #TODO: check if connection is OK
-        self.connected=True         #TODO: use startedConnecting  and clientConnectionLost of XMPPClientFactory
     
+    def disconnect(self):
+        if (not self.connected):
+            info("not connected !")
+            return
+        info("Disconnecting...")
+        self.factory.stopTrying()
+        if self.xmlstream:
+            self.xmlstream.sendFooter()
+
     def getService(self):
         print "GetService !"
         """if (self.connected):
@@ -111,23 +121,11 @@
         info("Getting service...")
         self.me = jid.JID(self.memory.getParamV("JabberID", "Connection"))
         self.factory = client.XMPPClientFactory(self.me, self.memory.getParamV("Password", "Connection"))
+        self.factory.clientConnectionLost = self.connectionLost
         self.factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,self.authd)
         self.factory.addBootstrap(xmlstream.INIT_FAILED_EVENT,self.failed)
-        self.connectionStatus="online"  #TODO: check if connection is OK
-        self.connected=True         #TODO: use startedConnecting  and clientConnectionLost of XMPPClientFactory
         return internet.TCPClient(self.memory.getParamV("Server", "Connection"), 5222, self.factory)
 
-    def disconnect(self):
-        if (not self.connected):
-            info("not connected !")
-            return
-        info("Disconnecting...")
-        self.factory.stopTrying()
-        if self.xmlstream:
-			self.xmlstream.sendFooter()
-        self.connectionStatus="offline"  #TODO: check if connection is OK
-        self.connected=False         #TODO: use startedConnecting  and clientConnectionLost of XMPPClientFactory
-
     def run(self):
         debug("running app")
         reactor.run()
@@ -137,26 +135,33 @@
         reactor.stop()
         
     def authd(self,xmlstream):
-      self.xmlstream=xmlstream
-      roster=client.IQ(xmlstream,'get')
-      roster.addElement(('jabber:iq:roster', 'query'))
-      roster.addCallback(self.rosterCb)
-      roster.send()
-      debug("server = %s",self.memory.getParamV("Server", "Connection"))
+        self.xmlstream=xmlstream
+        roster=client.IQ(xmlstream,'get')
+        roster.addElement(('jabber:iq:roster', 'query'))
+        roster.addCallback(self.rosterCb)
+        roster.send()
+        debug("server = %s",self.memory.getParamV("Server", "Connection"))
 
-      ###FIXME: tmp disco ###
-      self.memory.registerFeature("http://jabber.org/protocol/disco#info")
-      self.disco(self.memory.getParamV("Server", "Connection"), self.serverDisco)
+        ###FIXME: tmp disco ###
+        self.memory.registerFeature("http://jabber.org/protocol/disco#info")
+        self.disco(self.memory.getParamV("Server", "Connection"), self.serverDisco)
 
 
-      #we now send our presence status
-      self.setPresence(status="Online")
-      
-      # add a callback for the messages
-      xmlstream.addObserver('/message',  self.gotMessage)
-      xmlstream.addObserver('/presence', self.presenceCb)
-      xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
-      #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
+        #we now send our presence status
+        self.setPresence(status="Online")
+
+        # add a callback for the messages
+        xmlstream.addObserver('/message',  self.gotMessage)
+        xmlstream.addObserver('/presence', self.presenceCb)
+        xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
+        print "********** CONNECTED **********"
+        self.connected=True      
+        #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
+
+    def connectionLost(self, connector, unused_reason):
+        print "********** DISCONNECTED **********"
+        self.connected=False      
+        
 
     def sendMessage(self,to,msg,type='chat'):
         #FIXME: check validity of recipient