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
|
#!/usr/bin/env python
# Copyright (C) 2007, Red Hat, Inc.
#
# 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 2 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 os, time, sys
import dbus, dbus.glib
import gobject
from sugar.presence import presenceservice
from sugar.p2p import network
class ReadHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
def translate_path(self, path):
return self.server._filepath
class ReadHTTPServer(network.GlibTCPServer):
def __init__(self, server_address, request_handler, filepath):
self._filepath = filepath
network.GlibTCPServer.__init__(self, server_address, request_handler);
class XMLRPCResponder(object):
def __init__(self, have_file=False):
self._have_file = have_file
def _set_have_file(self):
self._have_file = True
def have_file(self):
return self._have_file
class MockReadActivity(gobject.GObject):
__gproperties__ = {
'title' : (str, None, None, None, gobject.PARAM_READABLE)
}
def __init__(self, filepath):
self._actid = "ef60b3af42f7b5aa558ef9269e2ed7998798afc0"
self._name = "Test Read Activity"
self._type = "org.laptop.sugar.Xbook"
gobject.GObject.__init__(self)
self._ps_act = None
self._filepath = os.path.abspath(filepath)
self._file_server = ReadHTTPServer(("", 8867), ReadHTTPRequestHandler, self._filepath)
self._xmlrpc_server = network.GlibXMLRPCServer(("", 8868))
responder = XMLRPCResponder(have_file=True)
self._xmlrpc_server.register_instance(responder)
def _activity_appeared_cb(self, ps, activity):
if activity.props.id != self._actid:
return
self._ps_act = activity
def share(self):
ps = presenceservice.get_instance()
ps.connect("activity-appeared", self._activity_appeared_cb)
ps.share_activity(self)
return False
def do_get_property(self, pspec):
if pspec.name == "title":
return self._name
def get_id(self):
return self._actid
def get_service_name(self):
return self._type
def start_ps():
import commands
(s, o) = commands.getstatusoutput("which sugar-presence-service")
if s != 0:
raise RuntimeError("Failed to find sugar presence service: %s" % o)
argv = [o, "1"]
(pid, stdin, stdout, stderr) = gobject.spawn_async(argv, flags=gobject.SPAWN_LEAVE_DESCRIPTORS_OPEN)
# Wait until it shows up on the bus
tries = 0
bus = dbus.SessionBus()
while tries < 10:
time.sleep(0.5)
bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
try:
if bus_object.GetNameOwner(presenceservice.DBUS_SERVICE, dbus_interface='org.freedesktop.DBus'):
break
except dbus.exceptions.DBusException, err:
pass
tries += 1
if tries >= 5:
stop_ps(pid)
raise RuntimeError("Couldn't start the mock presence service")
print "Started presence service PID %d" % pid
return pid
def stop_ps(pid):
if pid >= 0:
os.kill(pid, 15)
print "Stopped presence service PID %d" % pid
def main():
if len(sys.argv) != 2:
raise RuntimeError("Must specify a PDF to share.")
path = os.path.abspath(sys.argv[1])
if not os.path.exists(path):
raise RuntimeError("File %s doesn't exist." % path)
mact = MockReadActivity(path)
pid = start_ps()
loop = gobject.MainLoop()
gobject.timeout_add(2000, mact.share)
try:
loop.run()
except KeyboardInterrupt:
pass
stop_ps(pid)
if __name__ == "__main__":
main()
|