Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gtk_JAMtank_Ventana.py
blob: f8947ca1be9b41ea3775dba29addbd6e0e378b29 (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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   gtk_JAMtank_Ventana.py por:
#   Flavio Danesse <fdanesse@gmail.com>
#   CeibalJAM! - Uruguay - Plan Ceibal
#
# 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 pygame
from pygame.locals import *

import gc
gc.enable()

import socket
import SocketServer
import gobject
import threading

import gtk
import pygtk
pygtk.require("2.0")

from sugar.activity import activity

import time
import string
import sys
import dbus
import os
import commands
import threading

from JAMtank import JAMtank
from TCP_Server_thread_Stream import ThreadedTCPServer, ThreadedTCPRequestHandler

DIRECTORIODEIMAGENES = os.getcwd()+"/Imagenes/"
DIRECTORIODESONIDOS = os.getcwd()+"/Sonidos/"
PUERTO = 5000

# ------ Inicio de Funciones de Presencia ----------
OLPC_SESSION_BUS = "/tmp/olpc-session-bus"
PRESENCE_SERVICE = "org.laptop.Sugar.Presence"
PRESENCE_PATH = "/org/laptop/Sugar/Presence"
PRESENCE_IFACE = "org.laptop.Sugar.Presence"
BUDDY_IFACE = "org.laptop.Sugar.Presence.Buddy"

def get_bus():
# sugar presence
	if os.path.exists(OLPC_SESSION_BUS):
		address = "unix:path=%s" % OLPC_SESSION_BUS
		return dbus.bus.BusConnection(address_or_type=address)
	else:
		if len(sys.argv) != 2:
		    sys.exit(1)

		if 'DBUS_SESSION_BUS_ADDRESS' in os.environ:
		    del os.environ['DBUS_SESSION_BUS_ADDRESS']

		display = sys.argv[1]
		os.environ['DISPLAY'] = display
		return dbus.bus.BusConnection()
def get_Xos():
# devuelve la ip y el nombre de todas las xo en la red
# En realidad devuelve el nick de todas las xo, la ip local y las ip en la mesh de las demás xo
    	bus = get_bus()
    	ps = bus.get_object(PRESENCE_SERVICE, PRESENCE_PATH)
    	ps_iface = dbus.Interface(ps, PRESENCE_IFACE)
	buddies = map(lambda b: bus.get_object(PRESENCE_SERVICE, b), ps_iface.GetBuddies())

	xos = []
	for buddy in buddies:
		buddy_iface = dbus.Interface(buddy, BUDDY_IFACE)
		try:
			props = buddy_iface.GetProperties()
			ip = "%s" % (props['ip4-address'].encode('utf-8'))
			nick = "%s" % (props['nick'].encode('utf-8'))
			xo = (ip, nick)
			xos.append(xo)

		except dbus.DBusException:
		    	pass
	return xos
# ------ Fin de Funciones de Presencia ----------


class GTK_JAMtank(activity.Activity):
# Interface gtk
	def __init__(self, handle):

		activity.Activity.__init__(self, handle, False)

		self.set_title("-.JAMtank (versión 1).- -. CeibalJAM! 2010.-")

		pygame.mixer.init(44100, -16, 2, 2048)
		pygame.mixer.music.set_volume(1.0)
		self.sonido = pygame.mixer.Sound(DIRECTORIODESONIDOS+"Menu.ogg")

		self.jamtank = None
		self.servidor = None
		self.modo = None
		self.estado = "Presentacion"
		self.nick = None
		self.lista_servidores = None
		self.ip = None 			# ip local
		self.ip_server = None		# ip servidor

		# Canvas
		self.contenedor_principal = gtk.VBox()

		# imagen Creditos
		imagen = gtk.Image()
		imagen.set_from_file(DIRECTORIODEIMAGENES + "JAMtank.png")
		self.contenedor_principal.pack_start(imagen, False, False, 0)

		# Lista de Servidores Activos
		# self.contenedor_principal.pack_start(gtk.Label("Servidores JAMtank Actualmente Activos en la Red"), False, False, 0)

		# Lista de Servidores Activos
		self.caja_servers = gtk.VBox()
		viewport = gtk.ScrolledWindow()
		viewport.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		viewport.add_with_viewport(self.caja_servers)

		frame = gtk.Frame()
		frame.add(viewport)

		# busca servidores JAMtank en la red
		self.actualiza_servers(None)

		# Botones servidor local, actualizar lista de servidores y salir
		caja_botones = gtk.VBox()

		boton_servidor = gtk.Button("Crear Nuevo Juego en Red")
		boton_servidor.connect("clicked", self.set_Estado, "servidor", self.ip)
		boton_servidor.connect_object("clicked", gtk.Widget.destroy, self)

		boton_scan = gtk.Button("Actualizar Lista de Juegos en la Red")
		boton_scan.connect("clicked", self.actualiza_servers)

		boton_salir = gtk.Button("Salir de JAMtank")
		boton_salir.connect_object("clicked", self.salir, None)

		caja_botones.pack_start(boton_servidor, True, True, 5)
		caja_botones.pack_start(boton_scan, True, True, 5)
		caja_botones.pack_end(boton_salir, True, True, 5)

		# imagen Teclado
		imagen1 = gtk.Image()
		imagen1.set_from_file(DIRECTORIODEIMAGENES + "Teclado.png")

		caja_secundaria = gtk.HBox()
		caja_secundaria.pack_start(imagen1, False, False, 0)
		caja_secundaria.pack_start(frame, True, True, 5)
		caja_secundaria.pack_end(caja_botones, False, False, 5)

		self.contenedor_principal.pack_start(caja_secundaria, True, True, 0)
	
		# para introducir una IP conocida
		#caja = gtk.HBox()
		#etiqueta = gtk.Label("IP: ")
		self.entrada = None
		#conectar = gtk.Button("Conectarse a Esta IP")
		#conectar.connect("clicked", self.get_connect_ip_conocida)
		#caja.pack_start (etiqueta, False, False, 5)
		#caja.pack_start (self.entrada, False, False, 5)
		#caja.pack_start (conectar, False, False, 5)
		#self.contenedor_principal.pack_start(caja, False, False, 5)

		self.set_canvas(self.contenedor_principal)

		self.show_all()

		self.sonido.play(loops=-1)

		# switch gtk pygame
		reloj = gobject.timeout_add(500, self.get_Juego)

	def set_entrada_ip_conocida(self):
	# objetos para agregar ip de servidor conocido
		caja = gtk.HBox()
		etiqueta = gtk.Label("Conectarse a (IP): ")
		self.entrada = gtk.Entry()
		conectar = gtk.Button("Jugar")
		conectar.connect("clicked", self.get_connect_ip_conocida)
		caja.pack_start (etiqueta, True, True, 5)
		caja.pack_start (self.entrada, True, True, 5)
		caja.pack_end (conectar, True, True, 5)
		caja.show_all()
		self.caja_servers.pack_start(caja, False, False, 5)

	def get_connect_ip_conocida(self, widget):
	# Conecta a la ip ingresada
		ip = self.entrada.get_text()
		if ip:
			self.set_Estado(None, "cliente", ip)
			gtk.Widget.destroy(self)

	def actualiza_servers(self, widget):
	# Verifica tener conexión a una red

		self.lista_servidores = []

		# obtiene el nombre de la xo
		XOs = get_Xos()
		self.ip, self.nick = XOs[0]

		while not self.ip or self.ip == "localhost":
			# obtiene el nombre de la xo
			XOs = get_Xos()
			self.ip, self.nick = XOs[0]
			self.get_avisar_no_hay_ip()

		red = self.ip.split(".")
		for x in range(2, 254):
			ip = "%s.%s.%s.%s" % (red[0],red[1],red[2],x)
			thread = threading.Thread( target=self.scan_red, args=( ip, PUERTO) )
			thread.start()
			time.sleep(0.02)

		self.get_lista_xos()

	def scan_red (self, ip, puerto):
	# escanea la red en busca del puerto abierto para JAMtant
		conexion = socket.socket()
		try:
			conexion.connect( (ip, puerto) )
			self.lista_servidores.append(ip)
		except:
			pass
		conexion.close()
	
	def get_lista_xos(self):
	# arma la lista de xo en la red con server JAMtank corriendo

		for child in self.caja_servers:
            		self.caja_servers.remove(child)

		self.set_entrada_ip_conocida()

		for ip in self.lista_servidores:
			caja = gtk.HBox()
			lab_ip = gtk.Label(ip)
			conectar = gtk.Button("Jugar")

			conectar.connect("clicked", self.set_Estado, "cliente", ip)
			conectar.connect_object("clicked", gtk.Widget.destroy, self)

			caja.pack_start(lab_ip, True, True, 5)
			caja.pack_end(conectar, True, True, 5)
			caja.show_all()
			self.caja_servers.pack_start(caja, False, False, 5)

	def get_avisar_no_hay_ip(self):
	# Crea un cuadro de dialogo para avisar que no está conectado a ninguna red.

		dialog = gtk.Dialog("Atención !!!", None, gtk.DIALOG_MODAL, None)

		etiqueta0 = gtk.Label("No estás conectado a ninguna Red.")
		dialog.vbox.pack_start(etiqueta0, True, True, 5)

		etiqueta1 = gtk.Label("JAMtank es un juego en red, debes conectarte a una Red")
		dialog.vbox.pack_start(etiqueta1, True, True, 5)

		dialog.add_button("OK", 1)
		dialog.add_button("Cancelar", 2)

		dialog.show_all()

		if dialog.run() == 1:
			pass
		elif dialog.run() == 2:
		# sale automaticamente
			sys.exit()

		dialog.destroy()

	def delete_event(self, widget, event, data=None):
	# Cierra la ventana gtk
		return gtk.FALSE

	def salir(self, widget):
		sys.exit()
	
	def set_Estado(self, widget, modo, ip):
	# para poder llamar al juego luego de que se cierre la ventana gtk
		self.sonido.stop()
		pygame.quit()
		self.ip_server = ip
		self.modo = modo # Servidor o Cliente
		self.estado = "Juego"

	#----------------------Juego pygame-------------------------------------------------------------
	def get_Juego(self):
	# Se llama al juego pygame
		if self.estado == "Juego":
			if self.modo == "servidor":
				server = ThreadedTCPServer((self.ip, PUERTO), ThreadedTCPRequestHandler)
				server.allow_reuse_address = True
				server.socket.setblocking(0)

				server_thread = threading.Thread(target=server.serve_forever)
				server_thread.setDaemon(True)
				server_thread.start()
				
				self.jamtank = JAMtank(ip=self.ip, PUERTO=PUERTO)

			elif self.modo == "cliente":
				self.jamtank = JAMtank(ip=self.ip_server, PUERTO=PUERTO)
			
			return False # no volver por acá
		else:
			pass

		return True # seguir viniendo por acá
	#----------------------Juego pygame-------------------------------------------------------------


if __name__ == "__main__":
	GTK_JAMtank()