Lots of work to try and get duplex conversations going plus disabled cookies
authorEd Page <eopage@byu.net>
Thu, 17 Dec 2009 13:40:12 +0000 (07:40 -0600)
committerEd Page <eopage@byu.net>
Thu, 17 Dec 2009 13:40:12 +0000 (07:40 -0600)
hand_tests/generic.py
src/channel/contact_list.py
src/channel/text.py
src/connection.py
src/gvoice/conversations.py
src/gvoice/session.py
src/gvoice/state_machine.py
tests/test_conversations.py

index 41fa6fb..809401c 100755 (executable)
@@ -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)
index 970b809..a66e4cd 100644 (file)
@@ -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)
 
index f9c00ae..b9f6fe5 100644 (file)
@@ -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]
index 139e63d..fa87b31 100644 (file)
@@ -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)
index 357a844..43514c0 100644 (file)
@@ -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:
index bf5e328..19ffbe0 100644 (file)
@@ -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
index 09ed28b..8e81cb7 100644 (file)
@@ -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)
index 3444431..3b6d64b 100644 (file)
@@ -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