forked from sugarlabs/record-activity
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordtube.py
179 lines (146 loc) · 7.55 KB
/
recordtube.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright (C) 2011, One Laptop per Child (3bc80c7)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import logging
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.SignalFlags.RUN_FIRST, None, [object, object, object,
object, object]),
'recd-request':
(GObject.SignalFlags.RUN_FIRST, None, [object, object]),
'new-recd':
(GObject.SignalFlags.RUN_FIRST, None, [object, object]),
'recd-unavailable':
(GObject.SignalFlags.RUN_FIRST, None, [object, object])
}
def __init__(self, tube):
ExportedGObject.__init__(self, 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')
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')
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')
def unavailableRecd(self, md5sumOfIt, whoDoesntHaveIt, whoAskedForIt):
logger.debug('unavailableRecd: i\'d 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 don\'t have it, '
'i just told you/me/us.')
return
if whoAskedForIt != Instance.keyHashPrintable:
logger.debug('_unavailableRecdTubeCb: i\'ve overheard someone '
'doesn\'t have a photo, '
'but i didn\'t ask for that one anyways')
return
self.emit("recd-unavailable", md5sumOfIt, whoDoesntHaveIt)