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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# 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,
# 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 logging
from gettext import gettext as _
from sugar.activity import activity
from sugar.activity.activity import ActivityToolbox
import gtk
import subprocess
import os
import socket
import commands
class ClassRoomBroadcastActivity(activity.Activity):
"""Class Room Broadcast Activity
"""
# Color constants
_greenBG = '#00E500'
_redBG = '#FF0000'
# GUI
_box = None
_button = None
_label = None
_toolbar = None
_boxAlign = None
def __init__(self, handle):
"""Class constructor
"""
# Initialize parent class
activity.Activity.__init__(self, handle)
# Remove colaboration features
self.max_participants = 1
# Debug msg
logging.debug("Starting Class Room Broadcast Activity")
# Load GUI
self.loadGUI()
def loadGUI(self):
"""Create and show GUI
"""
# Toolbar
toolbox = ActivityToolbox(self)
self._toolbar = toolbox.get_activity_toolbar()
self.set_toolbox(toolbox)
# Box
self._box = gtk.VBox()
# Label
self._label = gtk.Label()
# Button
self._button = gtk.Button()
status = self.checkStatus()
if status[0]:
self.showServerStatus("on")
else:
self.showServerStatus("off")
self._button.set_size_request(200, 200)
self._button.connect("clicked", self.buttonClicked)
# Add button to box
self._box.pack_start(self._button)
# Add label to box
self._box.pack_start(self._label, padding=20)
# Box Align (xalign=0.0, yalign=0.0, xscale=0.0, yscale=0.0)
self._boxAlign = gtk.Alignment(0.5, 0.5, 0, 0)
self._boxAlign.add(self._box)
# Set canvas with box alignment
self.set_canvas(self._boxAlign)
# Show all
self.show_all()
def setButtonBG(self, color):
"""Change button bg color
"""
self._button.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
def setButtonLabel(self, txt):
"""Change button label
"""
self._button.set_label(txt)
def setLabelTXT(self, txt):
"""Change button label
"""
self._label.set_label(txt)
def showServerStatus(self, state="off"):
if state == "off":
self.setButtonBG(self._greenBG)
self.setButtonLabel("Start")
self.setLabelTXT("")
else:
self.setButtonBG(self._redBG)
self.setButtonLabel("Stop")
status = self.checkStatus()
txt = "Process ID = " + ",".join(status[1])
txt += "\n"
txt += "Hostname = " + self.getHostname()
txt += "\n"
txt += "IPs = " + self.getNetworkInfo()
self.setLabelTXT(txt)
def buttonClicked(self, widget, data=None):
"""Button clicked event handler
"""
status = self.checkStatus()
if status[0]:
self.stopServer()
self.showServerStatus("off")
else:
self.startServer()
self.showServerStatus("on")
def startServer(self):
"""Start vnc server
"""
cmd = ["x11vnc", "-viewonly", "-shared", "-bg", "-forever", "-solid", "-wireframe"]
subprocess.call(cmd, shell=False)
def stopServer(self):
"""Stop vnc server
"""
status = self.checkStatus()
pids = status[1]
for pid in pids:
os.system("kill -9 " + pid)
self.showServerStatus("off")
def getHostname(self):
"""Get server name
"""
return socket.gethostname()
def getNetworkInterfaces(self):
"""Get server network interfaces names
"""
f = open('/proc/net/dev', 'r')
lines = f.readlines()
f.close()
lines.pop(0)
lines.pop(0)
interfaces = []
for line in lines:
interface = line.strip().split(" ")[0].split(":")[0].strip()
interfaces.append(interface)
return interfaces
def getNetworkIPs(self, interfaces):
"""Get server IPs per interface
"""
pattern = "inet addr:"
cmdName = "/sbin/ifconfig"
ips = {}
for interface in interfaces:
cmd = cmdName + " " + interface
output = commands.getoutput(cmd)
inet = output.find(pattern)
if inet >= 0:
start = inet + len(pattern)
end = output.find(" ", start)
ip = output[start:end]
ips[interface] = ip
else:
ips[interface] = ""
return ips
def getNetworkInfo(self):
"""Get server network map {IFACE:IP}
"""
info = ""
interfaces = self.getNetworkInterfaces()
ips = self.getNetworkIPs(interfaces)
for interface, ip in ips.iteritems():
if info != "":
info += "\n "
info += interface + ": " + ip
return info
def checkStatus(self):
"""Check if X11VNC is running
"""
result = []
ps = subprocess.Popen(["pidof","x11vnc"],stdout=subprocess.PIPE)
pid = ps.communicate()[0].strip().split(" ")
ps.stdout.close()
pids = []
# Cleaning step
for p in pid:
p = p.strip()
if p != "":
pids.append(p)
if len(pids) > 0:
return [True, pids]
return [False, []]
"""
THIS METHOD IS LESS EFFICIENT THAN USING SHELL COMMAND 'pidof'
psCMD = ['ps', 'ax']
ps = subprocess.Popen(psCMD, stdout=subprocess.PIPE)
grepCMD = ['grep', 'x11vnc']
grep = subprocess.Popen(grepCMD, stdin=ps.stdout, stdout=subprocess.PIPE)
output = grep.communicate()[0]
ps.stdout.close()
grep.stdout.close()
procs = output.split("\n")
for proc in procs:
if proc == "":
continue
fields = proc.split()
procName = fields[4]
pid = fields[0]
if procName != "x11vnc":
continue
result.append(pid)
if len(result) > 0:
return [True, result]
return [False, []]
"""
|