From: Ed Page Date: Thu, 17 Dec 2009 13:40:12 +0000 (-0600) Subject: Lots of work to try and get duplex conversations going plus disabled cookies X-Git-Url: http://git.maemo.org/git/?p=theonering;a=commitdiff_plain;h=bc433598796afa36b5d75b7c40af19abdb573f67 Lots of work to try and get duplex conversations going plus disabled cookies --- diff --git a/hand_tests/generic.py b/hand_tests/generic.py index 41fa6fb..809401c 100755 --- a/hand_tests/generic.py +++ b/hand_tests/generic.py @@ -399,6 +399,19 @@ class SendText(Action): super(SendText, self)._on_done() +class Block(Action): + + def __init__(self): + super(Block, self).__init__() + + def queue_action(self): + print "Blocking" + + def _on_done(self): + #super(SendText, self)._on_done() + pass + + class Disconnect(Action): def __init__(self, connAction): @@ -529,6 +542,10 @@ if __name__ == '__main__': lastAction.append_action(sendtext) lastAction = sendtext + if True: + bl = Block() + lastAction.append_action(bl) + lastAction = bl dis = Disconnect(con) lastAction.append_action(dis) diff --git a/src/channel/contact_list.py b/src/channel/contact_list.py index 970b809..a66e4cd 100644 --- a/src/channel/contact_list.py +++ b/src/channel/contact_list.py @@ -51,6 +51,7 @@ class AllContactsListChannel(AbstractListChannel): @coroutines.func_sink @coroutines.expand_positional @gobject_utils.async + @gtk_toolbox.log_exception(_moduleLogger) def _on_contacts_refreshed(self, addressbook, added, removed, changed): self._process_refresh(addressbook, added, removed) diff --git a/src/channel/text.py b/src/channel/text.py index f9c00ae..b9f6fe5 100644 --- a/src/channel/text.py +++ b/src/channel/text.py @@ -67,14 +67,16 @@ class TextChannel(telepathy.server.ChannelTypeText): @coroutines.func_sink @coroutines.expand_positional @gobject_utils.async + @gtk_toolbox.log_exception(_moduleLogger) def _on_conversations_updated(self, conversationIds): if self._contactKey not in conversationIds: return + _moduleLogger.info("Incoming messages from %r for existing conversation" % (self._contactKey, )) conversation = self._conn.session.conversations.get_conversation(self._contactKey) self._report_conversation(conversation) def _report_conversation(self, conversation): - # @bug Check if messages sent need to be filtered out + # @bug? Check if messages sent need to be filtered out completeMessageHistory = conversation["messageParts"] messages = self._filter_seen_messages(completeMessageHistory) self._lastMessageTimestamp = messages[-1][0] diff --git a/src/connection.py b/src/connection.py index 139e63d..fa87b31 100644 --- a/src/connection.py +++ b/src/connection.py @@ -59,7 +59,7 @@ class TheOneRingConnection( self._callbackNumber = parameters['forward'].encode('utf-8') self._channelManager = channel_manager.ChannelManager(self) - cookieFilePath = "%s/cookies.txt" % constants._data_path_ + cookieFilePath = None self._session = gvoice.session.Session(cookieFilePath) self.set_self_handle(handle.create_handle(self, 'connection')) @@ -96,6 +96,9 @@ class TheOneRingConnection( telepathy.CONNECTION_STATUS_REASON_REQUESTED ) try: + self.session.conversations.updateSignalHandler.register_sink( + self._on_conversations_updated + ) self.session.login(*self._credentials) self.session.backend.set_callback_number(self._callbackNumber) except gvoice.backend.NetworkError, e: @@ -125,6 +128,9 @@ class TheOneRingConnection( """ _moduleLogger.info("Disconnecting") try: + self.session.conversations.updateSignalHandler.unregister_sink( + self._on_conversations_updated + ) self._channelManager.close() self.session.logout() _moduleLogger.info("Disconnected") @@ -202,9 +208,11 @@ class TheOneRingConnection( @coroutines.func_sink @coroutines.expand_positional @gobject_utils.async + @gtk_toolbox.log_exception(_moduleLogger) def _on_conversations_updated(self, conversationIds): # @todo get conversations update running # @todo test conversatiuons + _moduleLogger.info("Incoming messages from: %r" % (conversationIds, )) channelManager = self._channelManager for contactId, phoneNumber in conversationIds: h = self._create_contact_handle(contactId, phoneNumber) diff --git a/src/gvoice/conversations.py b/src/gvoice/conversations.py index 357a844..43514c0 100644 --- a/src/gvoice/conversations.py +++ b/src/gvoice/conversations.py @@ -18,7 +18,6 @@ class Conversations(object): self._conversations = {} self.updateSignalHandler = coroutines.CoTee() - self.update() def update(self, force=False): if not force and self._conversations: diff --git a/src/gvoice/session.py b/src/gvoice/session.py index bf5e328..19ffbe0 100644 --- a/src/gvoice/session.py +++ b/src/gvoice/session.py @@ -5,6 +5,7 @@ import logging import backend import addressbook import conversations +import state_machine _moduleLogger = logging.getLogger("gvoice.session") @@ -12,31 +13,38 @@ _moduleLogger = logging.getLogger("gvoice.session") class Session(object): - def __init__(self, cookiePath): - self._cookiePath = cookiePath + def __init__(self, cookiePath = None): self._username = None self._password = None - self._backend = None - self._addressbook = None - self._conversations = None + + self._backend = backend.GVoiceBackend(cookiePath) + self._addressbook = addressbook.Addressbook(self._backend) + self._conversations = conversations.Conversations(self._backend) + self._stateMachine = state_machine.StateMachine([self.addressbook], [self.conversations]) + + self._conversations.updateSignalHandler.register_sink( + self._stateMachine.request_reset_timers + ) def login(self, username, password): self._username = username self._password = password - self._backend = backend.GVoiceBackend(self._cookiePath) if not self._backend.is_authed(): self._backend.login(self._username, self._password) + self._stateMachine.start() + def logout(self): + self._loggedIn = False + self._stateMachine.stop() + self._backend.logout() + self._username = None self._password = None - self._backend = None - self._addressbook = None - self._conversations = None def is_logged_in(self): - if self._backend is None: - _moduleLogger.info("No Backend") + if self._username is None and self._password is None: + _moduleLogger.info("Hasn't even attempted to login yet") return False elif self._backend.is_authed(): return True @@ -66,9 +74,6 @@ class Session(object): """ Delay initialized addressbook """ - if self._addressbook is None: - _moduleLogger.info("Initializing addressbook") - self._addressbook = addressbook.Addressbook(self.backend) return self._addressbook @property @@ -76,7 +81,4 @@ class Session(object): """ Delay initialized addressbook """ - if self._conversations is None: - _moduleLogger.info("Initializing conversations") - self._conversations = conversations.Conversations(self.backend) return self._conversations diff --git a/src/gvoice/state_machine.py b/src/gvoice/state_machine.py index 09ed28b..8e81cb7 100644 --- a/src/gvoice/state_machine.py +++ b/src/gvoice/state_machine.py @@ -1,5 +1,10 @@ #!/usr/bin/env python +""" +@todo Look into switching from POLL_TIME = min(F * 2^n, MAX) to POLL_TIME = min(CONST + F * 2^n, MAX) +@todo Look into supporting more states that have a different F and MAX +""" + import Queue import threading import logging @@ -9,6 +14,7 @@ import gobject import util.algorithms as algorithms import util.coroutines as coroutines + _moduleLogger = logging.getLogger("gvoice.state_machine") @@ -78,6 +84,8 @@ class StateMachine(object): self.reset_timers() def _run(self): + logging.basicConfig(level=logging.DEBUG) + _moduleLogger.info("Starting State Machine") for item in self._initItems: try: item.update() @@ -95,11 +103,14 @@ class StateMachine(object): actions = list(algorithms.itr_available(self._actions, initiallyBlock = True)) if self._ACTION_STOP in actions: + _moduleLogger.info("Requested to stop") self._stop_update() break elif self._ACTION_RESET in actions: + _moduleLogger.info("Reseting timers") self._reset_timers() elif self._ACTION_UPDATE in actions: + _moduleLogger.info("Update") for item in self._updateItems: try: item.update(force=True) diff --git a/tests/test_conversations.py b/tests/test_conversations.py index 3444431..3b6d64b 100644 --- a/tests/test_conversations.py +++ b/tests/test_conversations.py @@ -75,6 +75,10 @@ def test_a_conversation(): conversings.updateSignalHandler.register_sink(callback) assert len(callbackData) == 0, "%r" % callbackData + conversings.update() + assert len(callbackData) == 1, "%r" % callbackData + del callbackData[:] + cons = list(conversings.get_conversations()) assert len(cons) == 1 assert cons[0] == ("con1", "5555551224"), cons @@ -109,6 +113,10 @@ def test_adding_a_conversation(): conversings.updateSignalHandler.register_sink(callback) assert len(callbackData) == 0, "%r" % callbackData + conversings.update() + assert len(callbackData) == 1, "%r" % callbackData + del callbackData[:] + cons = list(conversings.get_conversations()) assert len(cons) == 1 assert cons[0] == ("con1", "5555551224"), cons @@ -172,6 +180,10 @@ def test_merging_a_conversation(): conversings.updateSignalHandler.register_sink(callback) assert len(callbackData) == 0, "%r" % callbackData + conversings.update() + assert len(callbackData) == 1, "%r" % callbackData + del callbackData[:] + cons = list(conversings.get_conversations()) assert len(cons) == 1 assert cons[0] == ("con1", "5555551224"), cons