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):
"""Send given stanza list"""
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:
self.stream.send(stanza)
@@ -892,7 +892,7 @@ class JCLComponent(Component, object):
query = info_query.new_query("jabber:iq:gateway")
query.newTextChild(query.ns(), "desc", lang_class.get_gateway_desc)
query.newTextChild(query.ns(), "prompt", lang_class.get_gateway_prompt)
self.stream.send(info_query)
self.send_stanzas([info_query])
return 1
def handle_set_gateway(self, info_query):
@@ -910,7 +910,7 @@ class JCLComponent(Component, object):
query.newTextChild(query.ns(), "jid", jid)
# XEP-0100 - section 6: should be <jid> but PSI only work with <prompt>
query.newTextChild(query.ns(), "prompt", jid)
self.stream.send(info_query)
self.send_stanzas([info_query])
return 1
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.newTextChild(query.ns(), "name", self.name)
query.newTextChild(query.ns(), "version", self.version)
self.stream.send(info_query)
self.send_stanzas([info_query])
return 1
def handle_get_register(self, info_query):
@@ -1053,7 +1053,7 @@ class JCLComponent(Component, object):
presence = Presence(from_jid=stanza.get_to(),
to_jid=stanza.get_from(),
stanza_type="unavailable")
self.stream.send(presence)
self.send_stanzas([presence])
return 1
def handle_message(self, message):

View File

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

View File

@@ -2689,6 +2689,10 @@ class JCLComponent_TestCase(JCLTestCase):
self.comp.send_stanzas(None)
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):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file

View File

@@ -187,6 +187,14 @@ class MessageSender_TestCase(JCLTestCase):
self.assertEquals(message.get_type(), self.message_type)
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):
def setUp(self):
MessageSender_TestCase.setUp(self)