summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof <kkszysiu@gmail.com>2011-03-28 00:58:55 +0200
committerKrzysztof <kkszysiu@gmail.com>2011-03-28 00:58:55 +0200
commit6705bc0c4faf6ab400567b092f23a83f029f6741 (patch)
tree3733369cab9cc7295624d7726ac712b8eefff703
parent47de597525aabb387359d2e74a95c49624002735 (diff)
Make it more bulletproff
-rw-r--r--sunshine/avatars.py9
-rw-r--r--sunshine/connection.py52
-rw-r--r--sunshine/connection_manager.py5
-rw-r--r--sunshine/presence.py10
-rw-r--r--sunshine/util/config.py5
-rwxr-xr-xtelepathy-sunshine4
6 files changed, 61 insertions, 24 deletions
diff --git a/sunshine/avatars.py b/sunshine/avatars.py
index 9181990..2038555 100644
--- a/sunshine/avatars.py
+++ b/sunshine/avatars.py
@@ -72,9 +72,10 @@ class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
result = {}
for handle_id in contacts:
handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handle_id)
- #if handle == self.GetSelfHandle():
- # #tutaj kiedys trzeba napisac kod odp za naszego avatara
- # result[handle] = ""
+ if handle == self.GetSelfHandle():
+ logger.info("Avatar for self handle...")
+ #tutaj kiedys trzeba napisac kod odp za naszego avatara
+ result[handle] = "1"
url = 'http://api.gadu-gadu.pl/avatars/%s/0.xml' % (str(handle.name))
d = getPage(url, timeout=10)
d.addCallback(self.on_fetch_avatars_file_ok, url, handle)
@@ -123,7 +124,7 @@ class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
def on_fetch_avatars_file_ok(self, result, url, handle):
try:
if result:
- #logger.info("Avatar file retrieved from %s" % (url))
+ logger.info("Avatar file retrieved from %s" % (url))
e = minidom.parseString(result)
if e.getElementsByTagName('avatar')[0].attributes["blank"].value != '1':
timestamp = e.getElementsByTagName('timestamp')[0].firstChild.data
diff --git a/sunshine/connection.py b/sunshine/connection.py
index b8a0d6f..6657572 100644
--- a/sunshine/connection.py
+++ b/sunshine/connection.py
@@ -35,6 +35,7 @@ from twisted.internet import reactor, protocol
from twisted.web.client import getPage
from twisted.internet import task
from twisted.python import log
+from twisted.internet import threads
import dbus
import telepathy
@@ -89,25 +90,31 @@ class GaduClientFactory(protocol.ClientFactory):
def clientConnectionLost(self, connector, reason):
logger.info('Lost connection. Reason: %s' % (reason))
- if self.config.contactsLoop != None:
- self.config.contactsLoop.stop()
- self.config.contactsLoop = None
- if self.config.exportLoop != None:
- self.config.exportLoop.stop()
- self.config.exportLoop = None
+ try:
+ if self.config.contactsLoop != None:
+ self.config.contactsLoop.stop()
+ self.config.contactsLoop = None
+ if self.config.exportLoop != None:
+ self.config.exportLoop.stop()
+ self.config.exportLoop = None
+ except:
+ logger.info("Loops was not running")
if reactor.running:
reactor.stop()
os._exit(1)
def clientConnectionFailed(self, connector, reason):
logger.info('Connection failed. Reason: %s' % (reason))
- if self.config.contactsLoop != None:
- self.config.contactsLoop.stop()
- self.config.contactsLoop = None
- if self.config.exportLoop != None:
- self.config.exportLoop.stop()
- self.config.exportLoop = None
- if reactor.running:
+ try:
+ if self.config.contactsLoop != None:
+ self.config.contactsLoop.stop()
+ self.config.contactsLoop = None
+ if self.config.exportLoop != None:
+ self.config.exportLoop.stop()
+ self.config.exportLoop = None
+ except:
+ logger.info("Loops was not running")
+ if reactor.running:
reactor.stop()
os._exit(1)
@@ -223,12 +230,22 @@ class SunshineConnection(telepathy.server.Connection,
self._initial_personal_message = None
self._personal_message = ''
+ self.conn_checker = task.LoopingCall(self.connection_checker)
+ self.conn_checker.start(5.0, False)
+
logger.info("Connection to the account %s created" % account)
except Exception, e:
import traceback
logger.exception("Failed to create Connection")
raise
+ def connection_checker(self):
+ logger.info("Conn checker: %s" % len(self.manager._connections))
+ if len(self.manager._connections) == 0:
+ logger.info("Connection checker killed CM")
+ #self.quit()
+ reactor.stop()
+
@property
def manager(self):
return self._manager
@@ -362,18 +379,23 @@ class SunshineConnection(telepathy.server.Connection,
self.signal_new_channels([channel])
#@async
+ #@deferred
def updateContactsFile(self):
"""Method that updates contact file when it changes and in loop every 5 seconds."""
- self.configfile.make_contacts_file(self.profile.groups, self.profile.contacts)
+ reactor.callInThread(self.configfile.make_contacts_file, self.profile.groups, self.profile.contacts)
+ #self.configfile.make_contacts_file(self.profile.groups, self.profile.contacts)
#@async
+ #@deferred
def exportContactsFile(self):
logger.info("Exporting contacts.")
+ # TODO: make fully non-blocking
file = open(self.configfile.path, "r")
contacts_xml = file.read()
file.close()
if len(contacts_xml) != 0:
- self.profile.exportContacts(contacts_xml)
+ reactor.callInThread(self.profile.exportContacts, contacts_xml)
+ #self.profile.exportContacts(contacts_xml)
@async
def makeTelepathyContactsChannel(self):
diff --git a/sunshine/connection_manager.py b/sunshine/connection_manager.py
index b2d99c5..1c66aa3 100644
--- a/sunshine/connection_manager.py
+++ b/sunshine/connection_manager.py
@@ -16,6 +16,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+import os
import telepathy
import gobject
import dbus
@@ -36,10 +37,10 @@ class SunshineConnectionManager(telepathy.server.ConnectionManager):
def __init__(self, shutdown_func=None):
"Initializer"
telepathy.server.ConnectionManager.__init__(self, 'sunshine')
-
+
self._protos['gadugadu'] = SunshineConnection
self._shutdown = shutdown_func
- logger.info("Connection manager created")
+ logger.info("Connection manager created")
def GetParameters(self, proto):
"Returns the mandatory and optional parameters for the given proto."
diff --git a/sunshine/presence.py b/sunshine/presence.py
index b9662d3..860f1ca 100644
--- a/sunshine/presence.py
+++ b/sunshine/presence.py
@@ -101,7 +101,10 @@ class SunshinePresenceMapping(object):
0x4104: ONLINE,
0x4105: AWAY,
0x4122: DND,
- 0x4116: INVISIBLE
+ 0x4116: INVISIBLE,
+ # Jakis dziwny status, nie wiem skad sie wzial
+ 0x4020: OFFLINE,
+ 0x4120: OFFLINE
}
to_presence_type = {
@@ -164,7 +167,10 @@ class SunshinePresence(telepathy.server.ConnectionInterfaceSimplePresence):
#I dont know what to do here. Do I really need this? :P
contact = handle.contact
if contact is not None:
- presence = SunshinePresenceMapping.from_gg_to_tp[contact.status]
+ if contact.status in SunshinePresenceMapping.from_gg_to_tp:
+ presence = SunshinePresenceMapping.from_gg_to_tp[contact.status]
+ else:
+ presence = SunshinePresenceMapping.from_gg_to_tp[0]
personal_message = str('')
else:
presence = SunshinePresenceMapping.OFFLINE
diff --git a/sunshine/util/config.py b/sunshine/util/config.py
index 9a3adf5..74a31d0 100644
--- a/sunshine/util/config.py
+++ b/sunshine/util/config.py
@@ -3,6 +3,8 @@ import logging
import xml.etree.ElementTree as ET
+from twisted.internet import defer
+
__all__ = ['SunshineConfig']
logger = logging.getLogger('Sunshine.Config')
@@ -35,6 +37,7 @@ class SunshineConfig(object):
self.path2 = os.path.join(path, 'alias')
return os.path.join(path, 'profile.xml')
+ #@defer.inlineCallbacks
def get_contacts(self):
self.roster = {'groups':[], 'contacts':[]}
try:
@@ -51,9 +54,11 @@ class SunshineConfig(object):
return self.roster
except:
+ logger.error("Problem with contacts file parsing.")
self.contacts_count = 0
return self.roster
+
def make_contacts_file(self, groups, contacts):
contactbook_xml = ET.Element("ContactBook")
diff --git a/telepathy-sunshine b/telepathy-sunshine
index d1dc536..286373a 100755
--- a/telepathy-sunshine
+++ b/telepathy-sunshine
@@ -12,6 +12,8 @@ from twisted.internet import gtk2reactor
gtk2reactor.install()
from twisted.internet import reactor, protocol
+from twisted.internet.task import LoopingCall
+
if sys.version_info < (2, 5):
print >> sys.stderr, 'Critical: python >= 2.5 required. Exiting.'
sys.exit(1)
@@ -45,7 +47,7 @@ if __name__ == '__main__':
except Exception, e:
logger.warning('Unable to set processName: %s" % e')
- @async
+ #@async
def quit():
manager.quit()
if reactor.running: