Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/recordtube.py
blob: b3c82d260709d93890643a7b24f442aee4cd44ff (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
import os
import logging
import gi
from gi.repository import GObject
from dbus.service import signal
from dbus.gobject_service import ExportedGObject

import constants
from instance import Instance

logger = logging.getLogger('recordtube')

class RecordTube(ExportedGObject):

    __gsignals__ = {
        'recd-bits-arrived':
            (GObject.SIGNAL_RUN_FIRST, None, [object,object,object,object,object]),
        'recd-request':
            (GObject.SIGNAL_RUN_FIRST, None, [object,object]),
        'new-recd':
            (GObject.SIGNAL_RUN_FIRST, None, [object,object]),
        'recd-unavailable':
            (GObject.SIGNAL_RUN_FIRST, None, [object,object])
    }


    def __init__(self, tube):
        super(RecordTube, self).__init__(tube, constants.DBUS_PATH)
        self.tube = tube

        self.idNotify = self.tube.add_signal_receiver(self._newRecdTubeCb, 'notifyBudsOfNewRecd', constants.DBUS_IFACE, path=constants.DBUS_PATH, sender_keyword='sender')
        self.idRequest = self.tube.add_signal_receiver(self._reqRecdTubeCb, 'requestRecdBits', constants.DBUS_IFACE, path=constants.DBUS_PATH, sender_keyword='sender')
        self.idBroadcast = self.tube.add_signal_receiver(self._getRecdTubeCb, 'broadcastRecdBits', constants.DBUS_IFACE, path=constants.DBUS_PATH, sender_keyword='sender', byte_arrays=True)
        self.idUnavailable = self.tube.add_signal_receiver(self._unavailableRecdTubeCb, 'unavailableRecd', constants.DBUS_IFACE, path=constants.DBUS_PATH, sender_keyword='sender')


    @signal(dbus_interface=constants.DBUS_IFACE, signature='ss') #dual s for 2x strings
    def notifyBudsOfNewRecd(self, recorder, recdXml):
        logger.debug('Ive taken a new pho-ideo-audio!  I hereby send you an xml thumb of said media via this interface.')


    def _newRecdTubeCb(self, recorder, recdXml, sender=None):
        logger.debug("_newRecdTubeCb from " + recorder )
        if sender == self.tube.get_unique_name():
            logger.debug("_newRecdTubeCb: sender is my bus name, so ignore my own signal")
            return
        elif (recorder == Instance.keyHashPrintable):
            logger.debug('_newRecdTubeCb: excuse me?  you are asking me to share a photo with myself?')
            return

        self.emit( "new-recd", str(recorder), str(recdXml) )


    @signal(dbus_interface=constants.DBUS_IFACE, signature='sss') #triple s for 3x strings
    def requestRecdBits(self, whoWantsIt, whoTheyWantItFrom, recdMd5sumOfIt ):
        logger.debug('I am requesting a high-res version of someones media.')


    def _reqRecdTubeCb(self, whoWantsIt, whoTheyWantItFrom, recdMd5sumOfIt, sender=None):
        if sender == self.tube.get_unique_name():
            logger.debug("_reqRecdTubeCb: sender is my bus name, so ignore my own signal")
            return
        elif (whoWantsIt == Instance.keyHashPrintable):
            logger.debug('_reqRecdTubeCb: excuse me?  you are asking me to share a photo with myself?')
            return
        elif (whoTheyWantItFrom != Instance.keyHashPrintable):
            logger.debug('_reqRecdTubeCb: ive overhead someone wants a photo, but not from me')
            return

        self.emit( "recd-request", str(whoWantsIt), str(recdMd5sumOfIt) )


    def broadcastRecd(self, md5, filepath, sendThisTo ):
        size = os.path.getsize(filepath)
        f = open(filepath)
        chunk_size = 1000
        chunks = size / chunk_size
        if (size%chunk_size != 0):
            chunks += 1

        for chunk in range(chunks):
            bytes = f.read(chunk_size)
            if chunk == 0:
                logger.debug("sending " + str(chunk+1) + " of " + str(chunks) + " to " + sendThisTo )
            if chunk == chunks-1:
                logger.debug("sending " + str(chunk+1) + " of " + str(chunks) + " to " + sendThisTo )
            self.broadcastRecdBits(md5, chunk+1, chunks, bytes, sendThisTo, Instance.keyHashPrintable)

        f.close()
        return True


    @signal(dbus_interface=constants.DBUS_IFACE, signature='suuayss')
    def broadcastRecdBits(self, md5, part, numparts, bytes, sendTo, fromWho ):
        pass


    def _getRecdTubeCb(self, md5, part, numparts, bytes, sentTo, fromWho, sender=None):
        if sender == self.tube.get_unique_name():
            #record.Record.log.debug("_reqRecdTubeCb: sender is my bus name, so ignore my own signal")
            return
        if (fromWho == Instance.keyHashPrintable):
            #record.Record.log.debug('_getRecdTubeCb: i dont want bits from meself, thx anyway.  schizophrenic?')
            return
        if (sentTo != Instance.keyHashPrintable):
            #record.Record.log.debug('_getRecdTubeCb: ive overhead someone sending bits, but not to me!')
            return

        self.emit( "recd-bits-arrived", md5, part, numparts, bytes, fromWho )


    @signal(dbus_interface=constants.DBUS_IFACE, signature='sss') #triple s for 3x strings
    def unavailableRecd(self, md5sumOfIt, whoDoesntHaveIt, whoAskedForIt):
        logger.debug('unavailableRecd: id love to share this photo, but i am without a copy meself chum')


    def _unavailableRecdTubeCb( self, md5sumOfIt, whoDoesntHaveIt, whoAskedForIt, sender=None):
        if sender == self.tube.get_unique_name():
            logger.debug("_unavailableRecdTubeCb: sender is my bus name, so ignore my own signal")
            return
        if whoDoesntHaveIt == Instance.keyHashPrintable:
            logger.debug('_unavailableRecdTubeCb: yes, i know i dont have it, i just told you/me/us.')
            return
        if whoAskedForIt != Instance.keyHashPrintable:
            logger.debug('_unavailableRecdTubeCb: ive overheard someone doesnt have a photo, but i didnt ask for that one anyways')
            return

        self.emit("recd-unavailable", md5sumOfIt, whoDoesntHaveIt)