comparison sat.tac @ 6:5799493fa548

connection and disconnection management
author Goffi <goffi@goffi.org>
date Fri, 23 Oct 2009 17:04:22 +0200
parents c0c92129a54b
children ef8060d365cb
comparison
equal deleted inserted replaced
5:c0c92129a54b 6:5799493fa548
96 plug_info = mod.PLUGIN_INFO 96 plug_info = mod.PLUGIN_INFO
97 info ("importing plugin: %s", plug_info['name']) 97 info ("importing plugin: %s", plug_info['name'])
98 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self) 98 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self)
99 99
100 def connect(self): 100 def connect(self):
101 if (self.connected):
102 info("already connected !")
103 return
101 print "connecting..." 104 print "connecting..."
102 reactor.connectTCP(self.memory.getParamV("Server", "Connection"), 5222, self.factory) 105 reactor.connectTCP(self.memory.getParamV("Server", "Connection"), 5222, self.factory)
103 self.connectionStatus="online" #TODO: check if connection is OK
104 self.connected=True #TODO: use startedConnecting and clientConnectionLost of XMPPClientFactory
105 106
107 def disconnect(self):
108 if (not self.connected):
109 info("not connected !")
110 return
111 info("Disconnecting...")
112 self.factory.stopTrying()
113 if self.xmlstream:
114 self.xmlstream.sendFooter()
115
106 def getService(self): 116 def getService(self):
107 print "GetService !" 117 print "GetService !"
108 """if (self.connected): 118 """if (self.connected):
109 info("already connected !") 119 info("already connected !")
110 return""" 120 return"""
111 info("Getting service...") 121 info("Getting service...")
112 self.me = jid.JID(self.memory.getParamV("JabberID", "Connection")) 122 self.me = jid.JID(self.memory.getParamV("JabberID", "Connection"))
113 self.factory = client.XMPPClientFactory(self.me, self.memory.getParamV("Password", "Connection")) 123 self.factory = client.XMPPClientFactory(self.me, self.memory.getParamV("Password", "Connection"))
124 self.factory.clientConnectionLost = self.connectionLost
114 self.factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,self.authd) 125 self.factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,self.authd)
115 self.factory.addBootstrap(xmlstream.INIT_FAILED_EVENT,self.failed) 126 self.factory.addBootstrap(xmlstream.INIT_FAILED_EVENT,self.failed)
116 self.connectionStatus="online" #TODO: check if connection is OK
117 self.connected=True #TODO: use startedConnecting and clientConnectionLost of XMPPClientFactory
118 return internet.TCPClient(self.memory.getParamV("Server", "Connection"), 5222, self.factory) 127 return internet.TCPClient(self.memory.getParamV("Server", "Connection"), 5222, self.factory)
119
120 def disconnect(self):
121 if (not self.connected):
122 info("not connected !")
123 return
124 info("Disconnecting...")
125 self.factory.stopTrying()
126 if self.xmlstream:
127 self.xmlstream.sendFooter()
128 self.connectionStatus="offline" #TODO: check if connection is OK
129 self.connected=False #TODO: use startedConnecting and clientConnectionLost of XMPPClientFactory
130 128
131 def run(self): 129 def run(self):
132 debug("running app") 130 debug("running app")
133 reactor.run() 131 reactor.run()
134 132
135 def stop(self): 133 def stop(self):
136 debug("stopping app") 134 debug("stopping app")
137 reactor.stop() 135 reactor.stop()
138 136
139 def authd(self,xmlstream): 137 def authd(self,xmlstream):
140 self.xmlstream=xmlstream 138 self.xmlstream=xmlstream
141 roster=client.IQ(xmlstream,'get') 139 roster=client.IQ(xmlstream,'get')
142 roster.addElement(('jabber:iq:roster', 'query')) 140 roster.addElement(('jabber:iq:roster', 'query'))
143 roster.addCallback(self.rosterCb) 141 roster.addCallback(self.rosterCb)
144 roster.send() 142 roster.send()
145 debug("server = %s",self.memory.getParamV("Server", "Connection")) 143 debug("server = %s",self.memory.getParamV("Server", "Connection"))
146 144
147 ###FIXME: tmp disco ### 145 ###FIXME: tmp disco ###
148 self.memory.registerFeature("http://jabber.org/protocol/disco#info") 146 self.memory.registerFeature("http://jabber.org/protocol/disco#info")
149 self.disco(self.memory.getParamV("Server", "Connection"), self.serverDisco) 147 self.disco(self.memory.getParamV("Server", "Connection"), self.serverDisco)
150 148
151 149
152 #we now send our presence status 150 #we now send our presence status
153 self.setPresence(status="Online") 151 self.setPresence(status="Online")
154 152
155 # add a callback for the messages 153 # add a callback for the messages
156 xmlstream.addObserver('/message', self.gotMessage) 154 xmlstream.addObserver('/message', self.gotMessage)
157 xmlstream.addObserver('/presence', self.presenceCb) 155 xmlstream.addObserver('/presence', self.presenceCb)
158 xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb) 156 xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
159 #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile") 157 print "********** CONNECTED **********"
158 self.connected=True
159 #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
160
161 def connectionLost(self, connector, unused_reason):
162 print "********** DISCONNECTED **********"
163 self.connected=False
164
160 165
161 def sendMessage(self,to,msg,type='chat'): 166 def sendMessage(self,to,msg,type='chat'):
162 #FIXME: check validity of recipient 167 #FIXME: check validity of recipient
163 debug("Sending jabber message to %s...", to) 168 debug("Sending jabber message to %s...", to)
164 message = domish.Element(('jabber:client','message')) 169 message = domish.Element(('jabber:client','message'))