summaryrefslogtreecommitdiff
path: root/telepathy-sunshine
blob: d1dc5361baa3a3ebbf12d0cd5d7d6990d2ebeb5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python

import gobject
import dbus.glib
import signal
import os
import sys
import logging
from twisted.python import log

from twisted.internet import gtk2reactor
gtk2reactor.install()

from twisted.internet import reactor, protocol
if sys.version_info < (2, 5):
    print >> sys.stderr, 'Critical: python >= 2.5 required. Exiting.'
    sys.exit(1)

import telepathy
if telepathy.version < (0, 15, 17):
    print >> sys.stderr, 'Critical: telepathy-python >= 0.15.17 required. Exiting.'
    sys.exit(1)

from telepathy.utils import debug_divert_messages

debug_divert_messages(os.getenv('SUNSHINE_LOGFILE'))
logging.basicConfig(level=logging.DEBUG)

from sunshine import SunshineConnectionManager
from sunshine import SunshineDebug
from sunshine.util.decorator import async

logger = logging.getLogger('Sunshine')
observer = log.PythonLoggingObserver(loggerName='Sunshine')
observer.start()

IDLE_TIMEOUT = 5000
PROCESS_NAME = 'telepathy-sunshine'

if __name__ == '__main__':
    try: # change process name for killall
       import ctypes
       libc = ctypes.CDLL('libc.so.6')
       libc.prctl(15, PROCESS_NAME, 0, 0, 0)
    except Exception, e:
       logger.warning('Unable to set processName: %s" % e')

    @async
    def quit():
        manager.quit()
        if reactor.running:
            reactor.stop()

    if 'SUNSHINE_PERSIST' not in os.environ:
        def timeout_cb():
            if len(manager._connections) == 0:
                logger.info('No connection received - quitting')
                quit()
            return False
        gobject.timeout_add(IDLE_TIMEOUT, timeout_cb)
        shutdown_callback = quit
    else:
        shutdown_callback = None

    signal.signal(signal.SIGTERM, lambda : quit)

    try:
        manager = SunshineConnectionManager(shutdown_func=shutdown_callback)
        handler = SunshineDebug(manager)
    except dbus.exceptions.NameExistsException:
        logger.warning('Failed to acquire bus name, connection manager already running?')
        sys.exit(1)

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    try:
        reactor.run()
    except KeyboardInterrupt:
        manager.quit()
        if reactor.running:
            reactor.stop()