check if stream is still open before using it

darcs-hash:20080821201840-86b55-4da2698f2916bf73c0f38166e542ada166d20a89.gz
This commit is contained in:
David Rousselie
2008-08-21 22:18:40 +02:00
parent aab567d4b8
commit 391ce083e0
4 changed files with 20 additions and 7 deletions

View File

@@ -787,7 +787,7 @@ class JCLComponent(Component, object):
def send_stanzas(self, stanzas): def send_stanzas(self, stanzas):
"""Send given stanza list""" """Send given stanza list"""
self.__logger.debug("Sending responses: " + str(stanzas)) self.__logger.debug("Sending responses: " + str(stanzas))
if stanzas is not None: if stanzas is not None and self.stream is not None:
for stanza in stanzas: for stanza in stanzas:
self.stream.send(stanza) self.stream.send(stanza)
@@ -892,7 +892,7 @@ class JCLComponent(Component, object):
query = info_query.new_query("jabber:iq:gateway") query = info_query.new_query("jabber:iq:gateway")
query.newTextChild(query.ns(), "desc", lang_class.get_gateway_desc) query.newTextChild(query.ns(), "desc", lang_class.get_gateway_desc)
query.newTextChild(query.ns(), "prompt", lang_class.get_gateway_prompt) query.newTextChild(query.ns(), "prompt", lang_class.get_gateway_prompt)
self.stream.send(info_query) self.send_stanzas([info_query])
return 1 return 1
def handle_set_gateway(self, info_query): def handle_set_gateway(self, info_query):
@@ -910,7 +910,7 @@ class JCLComponent(Component, object):
query.newTextChild(query.ns(), "jid", jid) query.newTextChild(query.ns(), "jid", jid)
# XEP-0100 - section 6: should be <jid> but PSI only work with <prompt> # XEP-0100 - section 6: should be <jid> but PSI only work with <prompt>
query.newTextChild(query.ns(), "prompt", jid) query.newTextChild(query.ns(), "prompt", jid)
self.stream.send(info_query) self.send_stanzas([info_query])
return 1 return 1
def disco_get_info(self, node, info_query): def disco_get_info(self, node, info_query):
@@ -955,7 +955,7 @@ class JCLComponent(Component, object):
query = info_query.new_query("jabber:iq:version") query = info_query.new_query("jabber:iq:version")
query.newTextChild(query.ns(), "name", self.name) query.newTextChild(query.ns(), "name", self.name)
query.newTextChild(query.ns(), "version", self.version) query.newTextChild(query.ns(), "version", self.version)
self.stream.send(info_query) self.send_stanzas([info_query])
return 1 return 1
def handle_get_register(self, info_query): def handle_get_register(self, info_query):
@@ -1053,7 +1053,7 @@ class JCLComponent(Component, object):
presence = Presence(from_jid=stanza.get_to(), presence = Presence(from_jid=stanza.get_to(),
to_jid=stanza.get_from(), to_jid=stanza.get_from(),
stanza_type="unavailable") stanza_type="unavailable")
self.stream.send(presence) self.send_stanzas([presence])
return 1 return 1
def handle_message(self, message): def handle_message(self, message):

View File

@@ -109,8 +109,9 @@ class MessageSender(Sender):
"""Implement abstract method from Sender class and send """Implement abstract method from Sender class and send
data as Jabber message. data as Jabber message.
""" """
self.component.stream.send(self.create_message(to_account, if self.component.stream is not None:
data)) self.component.stream.send(self.create_message(to_account,
data))
class HeadlineSender(MessageSender): class HeadlineSender(MessageSender):
"""Send data as Jabber Headline""" """Send data as Jabber Headline"""

View File

@@ -2689,6 +2689,10 @@ class JCLComponent_TestCase(JCLTestCase):
self.comp.send_stanzas(None) self.comp.send_stanzas(None)
self.assertEquals(len(self.comp.stream.sent), 0) self.assertEquals(len(self.comp.stream.sent), 0)
def test_send_stanzas_closed_connection(self):
self.comp.stream = None
self.comp.send_stanzas([Message()])
def test_get_motd(self): def test_get_motd(self):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR) config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file self.comp.config_file = config_file

View File

@@ -187,6 +187,14 @@ class MessageSender_TestCase(JCLTestCase):
self.assertEquals(message.get_type(), self.message_type) self.assertEquals(message.get_type(), self.message_type)
model.db_disconnect() model.db_disconnect()
def test_send_closed_connection(self):
self.comp.stream = None
model.db_connect()
account11 = Account(user=User(jid="user1@test.com"),
name="account11",
jid="account11@jcl.test.com")
self.sender.send(account11, ("subject", "Body message"))
class HeadlineSender_TestCase(MessageSender_TestCase): class HeadlineSender_TestCase(MessageSender_TestCase):
def setUp(self): def setUp(self):
MessageSender_TestCase.setUp(self) MessageSender_TestCase.setUp(self)