fixed zeroconf to work with refactored dispatcher
This commit is contained in:
		
							parent
							
								
									acdf4ff143
								
							
						
					
					
						commit
						ed7dd84cfe
					
				
					 3 changed files with 20 additions and 26 deletions
				
			
		|  | @ -23,6 +23,7 @@ from common.xmpp.idlequeue import IdleObject | |||
| from common.xmpp import dispatcher_nb, simplexml | ||||
| from common.xmpp.client import * | ||||
| from common.xmpp.simplexml import ustr | ||||
| from common.xmpp.transports_nb import DATA_RECEIVED, DATA_SENT | ||||
| from common.zeroconf import zeroconf | ||||
| 
 | ||||
| from common.xmpp.protocol import * | ||||
|  | @ -36,8 +37,6 @@ log = logging.getLogger('gajim.c.z.client_zeroconf') | |||
| from common.zeroconf import roster_zeroconf | ||||
| 
 | ||||
| MAX_BUFF_LEN = 65536 | ||||
| DATA_RECEIVED = 'DATA RECEIVED' | ||||
| DATA_SENT = 'DATA SENT' | ||||
| TYPE_SERVER, TYPE_CLIENT = range(2) | ||||
| 
 | ||||
| # wait XX sec to establish a connection | ||||
|  | @ -120,6 +119,7 @@ class P2PClient(IdleObject): | |||
| 	on_ok=None, on_not_ok=None): | ||||
| 		self._owner = self | ||||
| 		self.Namespace = 'jabber:client' | ||||
| 		self.protocol_type = 'XMPP' | ||||
| 		self.defaultNamespace = self.Namespace | ||||
| 		self._component = 0 | ||||
| 		self._registered_name = None | ||||
|  | @ -130,16 +130,7 @@ class P2PClient(IdleObject): | |||
| 		self.Server = host | ||||
| 		self.on_ok = on_ok | ||||
| 		self.on_not_ok = on_not_ok | ||||
| 		self.DBG = 'client' | ||||
| 		self.Connection = None | ||||
| 		if gajim.verbose: | ||||
| 			debug = ['always', 'nodebuilder'] | ||||
| 		else: | ||||
| 			debug = [] | ||||
| 		self._DEBUG = Debug.Debug(debug) | ||||
| 		self.DEBUG = self._DEBUG.Show | ||||
| 		self.debug_flags = self._DEBUG.debug_flags | ||||
| 		self.debug_flags.append(self.DBG) | ||||
| 		self.sock_hash = None | ||||
| 		if _sock: | ||||
| 			self.sock_type = TYPE_SERVER | ||||
|  | @ -202,8 +193,6 @@ class P2PClient(IdleObject): | |||
| 		self.Dispatcher.Stream._dispatch_depth = 2 | ||||
| 		self.Dispatcher.Stream.dispatch = self.Dispatcher.dispatch | ||||
| 		self.Dispatcher.Stream.stream_header_received = self._check_stream_start | ||||
| 		self.debug_flags.append(simplexml.DBG_NODEBUILDER) | ||||
| 		self.Dispatcher.Stream.DEBUG = self.DEBUG | ||||
| 		self.Dispatcher.Stream.features = None | ||||
| 		if self.sock_type == TYPE_CLIENT: | ||||
| 			self.send_stream_header() | ||||
|  | @ -221,8 +210,7 @@ class P2PClient(IdleObject): | |||
| 
 | ||||
| 	def _check_stream_start(self, ns, tag, attrs): | ||||
| 		if ns<>NS_STREAMS or tag<>'stream': | ||||
| 			self.Connection.DEBUG('Incorrect stream start: (%s,%s).Terminating! ' \ | ||||
| 				% (tag, ns), 'error') | ||||
| 			log.error('Incorrect stream start: (%s,%s).Terminating!' % (tag, ns), 'error') | ||||
| 			self.Connection.disconnect() | ||||
| 			if self.on_not_ok: | ||||
| 				self.on_not_ok('Connection to host could not be established: Incorrect answer from server.') | ||||
|  | @ -472,13 +460,13 @@ class P2PConnection(IdleObject, PlugIn): | |||
| 			if self._owner.sock_type == TYPE_CLIENT: | ||||
| 				self.set_timeout(ACTIVITY_TIMEOUT_SECONDS) | ||||
| 			if received.strip(): | ||||
| 				self.DEBUG(received, 'got') | ||||
| 				log.debug('received: %s', received) | ||||
| 			if hasattr(self._owner, 'Dispatcher'): | ||||
| 				self._owner.Dispatcher.Event('', DATA_RECEIVED, received) | ||||
| 			self.on_receive(received) | ||||
| 		else: | ||||
| 			# This should never happed, so we need the debug | ||||
| 			self.DEBUG('Unhandled data received: %s' % received,'error') | ||||
| 			log.error('Unhandled data received: %s' % received) | ||||
| 			self.disconnect() | ||||
| 		return True | ||||
| 
 | ||||
|  | @ -553,7 +541,7 @@ class P2PConnection(IdleObject, PlugIn): | |||
| 
 | ||||
| 	def _on_send(self): | ||||
| 		if self.sent_data and self.sent_data.strip(): | ||||
| 			self.DEBUG(self.sent_data,'sent') | ||||
| 			log.debug('sent: %s' % self.sent_data) | ||||
| 			if hasattr(self._owner, 'Dispatcher'): | ||||
| 				self._owner.Dispatcher.Event('', DATA_SENT, self.sent_data) | ||||
| 		self.sent_data = None | ||||
|  | @ -562,7 +550,7 @@ class P2PConnection(IdleObject, PlugIn): | |||
| 			self.buff_is_message = False | ||||
| 
 | ||||
| 	def _on_send_failure(self): | ||||
| 		self.DEBUG("Socket error while sending data",'error') | ||||
| 		log.error('Socket error while sending data') | ||||
| 		self._owner.disconnected() | ||||
| 		self.sent_data = None | ||||
| 
 | ||||
|  | @ -698,19 +686,23 @@ class ClientZeroconf: | |||
| 		# look for hashed connections | ||||
| 		if to in self.recipient_to_hash: | ||||
| 			conn = self.connections[self.recipient_to_hash[to]] | ||||
| 			id = conn.Dispatcher.getAnID() | ||||
| 			stanza.setID(id) | ||||
| 			if conn.add_stanza(stanza, is_message): | ||||
| 				if on_ok: | ||||
| 					on_ok() | ||||
| 				return 0 | ||||
| 				return id | ||||
| 
 | ||||
| 		if item['address'] in self.ip_to_hash: | ||||
| 			hash = self.ip_to_hash[item['address']] | ||||
| 			if self.hash_to_port[hash] == item['port']: | ||||
| 				conn = self.connections[hash] | ||||
| 				id = conn.Dispatcher.getAnID() | ||||
| 				stanza.setID(id) | ||||
| 				if conn.add_stanza(stanza, is_message): | ||||
| 					if on_ok: | ||||
| 						on_ok() | ||||
| 					return 0 | ||||
| 					return id | ||||
| 
 | ||||
| 		# otherwise open new connection | ||||
| 		P2PClient(None, item['address'], item['port'], self, | ||||
|  |  | |||
|  | @ -460,7 +460,7 @@ class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionBytestream, connecti | |||
| 		else: | ||||
| 			# XXX this shouldn't be hardcoded | ||||
| 			if isinstance(session, ChatControlSession): | ||||
| 				session.received(frm, msgtxt, tim, encrypted, subject, msg) | ||||
| 				session.received(frm, msgtxt, tim, encrypted, msg) | ||||
| 			else: | ||||
| 				session.received(msg) | ||||
| 	# END messageCB | ||||
|  |  | |||
|  | @ -138,6 +138,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): | |||
| 		if gajim.handlers.has_key(event): | ||||
| 			gajim.handlers[event](self.name, data) | ||||
| 
 | ||||
| 
 | ||||
| 	def _reconnect(self): | ||||
| 		# Do not try to reco while we are already trying | ||||
| 		self.time_to_reconnect = None | ||||
|  | @ -455,12 +456,13 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): | |||
| 		def on_send_not_ok(reason): | ||||
| 			reason += ' ' + _('Your message could not be sent.') | ||||
| 			self.dispatch('MSGERROR', [jid, '-1', reason, None, None, session]) | ||||
| 
 | ||||
| 		ret = self.connection.send(msg_iq, msg != None, on_ok=on_send_ok, | ||||
| 			on_not_ok=on_send_not_ok) | ||||
| 		if ret == -1: | ||||
| 			# Contact Offline | ||||
| 			self.dispatch('MSGERROR', [jid, '-1', _('Contact is offline. Your message could not be sent.'), None, None, session]) | ||||
| 		return ret | ||||
| 
 | ||||
| 
 | ||||
| 	def send_stanza(self, stanza): | ||||
| 		# send a stanza untouched | ||||
|  | @ -547,9 +549,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): | |||
| 
 | ||||
| 	def _event_dispatcher(self, realm, event, data): | ||||
| 		if realm == '': | ||||
| 			if event == common.xmpp.transports.DATA_RECEIVED: | ||||
| 			if event == common.xmpp.transports_nb.DATA_RECEIVED: | ||||
| 				self.dispatch('STANZA_ARRIVED', unicode(data, errors = 'ignore')) | ||||
| 			elif event == common.xmpp.transports.DATA_SENT: | ||||
| 			elif event == common.xmpp.transports_nb.DATA_SENT: | ||||
| 				self.dispatch('STANZA_SENT', unicode(data)) | ||||
| 
 | ||||
| # END ConnectionZeroconf | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue