Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/kandidtube.py
blob: 67c0dba7ea4879916d4db8e0f8897b5cd0f1bc74 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import hashlib 
import base64

from dbus.service import method, signal
from dbus.gobject_service import ExportedGObject
import ka_debug

SERVICE = 'net.sourceforge.kandid'
IFACE = SERVICE
PATH = '/net/sourceforge/kandid/minimal'

SEND_POPULATION = 'P'
SEND_PROTOZOON = 'I'

class KandidTube(ExportedGObject):
    """The bit that talks over the tubes"""

    def __init__(self, controller, tube, is_initiator, get_buddy):
        super(KandidTube, self).__init__(tube, PATH)
        self._controller = controller
        self._tube = tube
        self._is_initiator = is_initiator
        self._get_buddy = get_buddy  # Converts handle to Buddy object
        self._entered = False  # Have we set up the tube?
        self._tube.watch_participants(self.on_participant_change)

    def on_participant_change(self, added, removed):
        if added:
            ka_debug.info('Tube: Added participants: %r' % added)
        if removed:
            ka_debug.info('Tube: Removed participants: %r' % removed)
        for handle, bus_name in added:
            buddy = self._get_buddy(handle)
            if buddy is not None:
                ka_debug.info('Tube: Handle %u (Buddy [%s]) was added' % \
                                   (handle, buddy.props.nick))
        for handle in removed:
            buddy = self._get_buddy(handle)
            if buddy is not None:
                ka_debug.info('Buddy [%s] was removed' % buddy.props.nick)
        if not self._entered:
            if self._is_initiator:
                ka_debug.info("I'm initiating the tube, will watch for hellos.")
                self.add_hello_handler()
                self.add_publish_handler()
            else:
                ka_debug.info('Hello, everyone! What did I miss?')
                self.Hello()
                self.add_publish_handler()
        self._entered = True

    def add_hello_handler(self):
        ka_debug.info('I am initiating. Adding hello handler.')
        self._tube.add_signal_receiver(self.on_hello, 'Hello',
                                IFACE, path=PATH, sender_keyword='sender')

    def add_publish_handler(self):
        ka_debug.info('I am entered. Adding input handler.')
        self._tube.add_signal_receiver(self.on_publish_protozoon, 'PublishProtozoon',
                                IFACE, path=PATH, sender_keyword='sender')

    @signal(dbus_interface=IFACE, signature='')
    def Hello(self):
        """Say Hello to whoever else is in the tube."""
        ka_debug.info('I said Hello.')

    def on_hello(self, sender=None):
        """Somebody helloed me. Send them my population."""
        if sender == self._tube.get_unique_name():
            # sender is my bus name, so ignore my own signal
            return
        ka_debug.info('on_hello: Newcomer [%s] has joined. World them.' % sender)
        self.send_population(self._controller.serialize_model(), sender)

    def publish_protozoon(self, code_element):
        ka_debug.info('publish_protozoon: %s %s' % (type(code_element), code_element))
        code_element_base64 = base64.b64encode(code_element)
        code_md5 = hashlib.md5(code_element).hexdigest() 
        ka_debug.info('publish_protozoon: Sent %u bytes, type: [%s] md5: [%s]' % \
                   (len(code_element), SEND_PROTOZOON, code_md5))
        self.PublishProtozoon(SEND_PROTOZOON, code_element_base64, code_md5)

    @signal(dbus_interface=IFACE, signature='sss')
    def PublishProtozoon(self, code_type, code_element, code_md5):
        """Send protozoon."""
        ka_debug.info('PublishProtozoon: I published %u bytes, type: [%s] md5: [%s]' \
                   % (len(code_element), code_type, code_md5))

    def on_publish_protozoon(self, code_type, code_element, code_md5, sender=None):
        """Somebody published. Process received parameters."""
        if sender == self._tube.get_unique_name():
            # sender is my bus name, so ignore my own signal
            return
        ka_debug.info('on_publish: I got %u bytes, type: [%s] md5: [%s]' \
                   % (len(code_element), code_type, code_md5))
        dec = base64.b64decode(code_element)
        self._controller.on_received(code_type, dec, code_md5)

    def send_population(self, code_element, sender=None):
        code_element_base64 = base64.b64encode(code_element)
        code_md5 = hashlib.md5(code_element).hexdigest() 
        ka_debug.info('send_population: Sent %u bytes, type: [%s] md5: [%s]' % \
                   (len(code_element), SEND_POPULATION, code_md5))
        self._tube.get_object(sender, PATH).SendPopulation(SEND_POPULATION,
                                                           code_element_base64,
                                                           code_md5)

    #@signal(dbus_interface=IFACE, signature='sss')
    @method(dbus_interface=IFACE, in_signature='sss', out_signature='')
    def SendPopulation(self, code_type, code_element, code_md5):
        """Send to all participants."""
        ka_debug.info('SendPopulation: Sent %u bytes, type: [%s] md5: [%s]' \
                   % (len(code_element), code_type, code_md5))
        dec = base64.b64decode(code_element)
        self._controller.on_received(code_type, dec, code_md5)