← Back to team overview

brigas-team team mailing list archive

[Branch ~brigas-team/brigas/trunk] Rev 5: Coloquei um control point bem simples para poder testar o device e game manager.

 

------------------------------------------------------------
revno: 5
committer: Diogo Dutra <dda@optimus>
branch nick: trunk
timestamp: Wed 2009-06-03 21:23:02 -0300
message:
  Coloquei um control point bem simples para poder testar o device e game manager.
  Criei um jogo truco fake, não tem nada nele, é só um serviço q eu pego como jogo para testar.
  Criei o XML da game manager, nele contém as assinaturas dos métodos SOAP e as variáveis de 
  estado de acordo com o padrão UPnP.
  
  game-server/src/game_server_impl.py:
  Ajeitei algumas coisas para funcionar direito a inclusão de serviços dinamicamente
  
  game-server/src/services/game_manager.py:
  Comecei a fazer os métodos, falta fazer tratamento de erros, mas os contro points já podem 
  pegar os jogos, pegas as salas dos jogos, criar sala e entrar numa sala.
  Fiz um método bem simples para gerar o uuid para o jogador, na verdade é só um contador, estou 
  esperando o felipe me mandar o código do gerador de uuid para colocar lá.
added:
  game-server/bin/simple_control_point
  game-server/src/games/truco/
  game-server/src/games/truco/__init__.py
  game-server/src/services/xmls/game-manager-scpd.xml
modified:
  game-server/src/game_server_impl.py
  game-server/src/services/game_manager.py

=== added file 'game-server/bin/simple_control_point'
--- game-server/bin/simple_control_point	1970-01-01 00:00:00 +0000
+++ game-server/bin/simple_control_point	2009-06-04 00:23:02 +0000
@@ -0,0 +1,126 @@
+#!/usr/bin/python
+
+# Licensed under the MIT license
+# http://opensource.org/licenses/mit-license.php or see LICENSE file.
+# Copyright 2007-2008 Brisa Team <brisa-develop@xxxxxxxxxxxxxxxx>
+
+from brisa.core.reactors import install_default_reactor
+reactor = install_default_reactor()
+
+from brisa.upnp.control_point import ControlPoint
+from brisa.core.threaded_call import run_async_function
+
+
+devices = []
+
+
+def on_new_device(dev):
+  t = dev.device_type
+  if 'GameServer' in t:
+    print 'Got new game server'
+
+    if dev.udn not in [d.udn for d in devices]:
+        devices.append(dev)
+
+
+def on_removed_device(udn):
+    print 'A game server gone'
+
+    for dev in devices:
+        if dev.udn == udn:
+            devices.remove(dev)
+
+
+def create():
+    c = ControlPoint()
+    c.subscribe('new_device_event', on_new_device)
+    c.subscribe('removed_device_event', on_removed_device)
+    return c
+
+
+def list_devices(devices):
+    count = 0
+    for d in devices:
+        print 'Device number: ', count
+        print 'UDN (id): ', d.udn
+        print 'Name: ', d.friendly_name
+        print 'Device type:', d.device_type
+        print 'Services:', d.services.keys()
+        print 'Embedded devices: ',\
+            [dev.friendly_name for dev in d.devices.values()]
+        print
+        count += 1
+
+
+def list_services(dev):
+    count = 0
+    for k, serv in dev.services.items():
+        print 'Service number: ', count
+        print 'Service id: ' + serv.id
+        print
+        count += 1
+
+
+def main():
+    c = create()
+    c.start()
+    reactor.add_after_stop_func(c.stop)
+    run_async_function(run, (c, ))
+    reactor.main()
+
+
+def run(c):
+    c.start_search(600, 'urn:schemas-upnp-org:device:GameServer:1')
+    while True:
+        try:
+            input = raw_input('>>> ')
+        except KeyboardInterrupt, EOFError:
+            break
+
+        if input == '':
+            print
+            continue
+
+        elif input == 'list':
+            list_devices(devices)
+
+        elif input == 'exit':
+            break
+
+        elif input == 'stop':
+            c.stop_search()
+
+        elif input == 'uuid':
+            device = devices[0]
+            k, service = device.services.items()[1]
+            print service.GetUUID()
+
+        elif input == 'games':
+            device = devices[0]
+            k, service = device.services.items()[1]
+            print service.GetAvailableGames()
+
+        elif input == 'rooms':
+            device = devices[0]
+            k, service = device.services.items()[1]
+            print service.GetAvailableRooms(Game='Truco')
+
+        elif input == 'createroom':
+            device = devices[0]
+            k, service = device.services.items()[1]
+            print service.CreateRoom(Game='Truco')
+
+        elif input == 'enterroom':
+            device = devices[0]
+            k, service = device.services.items()[1]
+            print service.EnterRoom(Game='Truco', RoomID=0, UUID=0)
+
+        elif input == 'help':
+            print 'Commands available: list, exit, ' \
+            'search, stop, help, HelloGamer'
+
+    reactor.main_quit()
+
+
+if __name__ == '__main__':
+    main()

=== modified file 'game-server/src/game_server_impl.py'
--- game-server/src/game_server_impl.py	2009-06-03 19:57:05 +0000
+++ game-server/src/game_server_impl.py	2009-06-04 00:23:02 +0000
@@ -21,7 +21,7 @@
   def __init__(self, server_name, listen_url):
     self._server_name = server_name
     self._listen_url = listen_url
-    self._games = []
+    self.games = {}
     self.device = None
 
   def _create_device(self):
@@ -46,7 +46,7 @@
       try:
         module_path = "%s.%s" % (self.games_module_path, dir)
         __import__(module_path)
-        self._games.append(eval("%s.service.id" % module_path))
+        self.games[eval("%s.service.id" % module_path)] = []
         self.device.add_service(eval("%s.service" % module_path))
       except Exception, e:
         msg = 'Error while importing %s game. The module '\
@@ -54,11 +54,9 @@
               (dir, self.games_folder, e)
         log.error(msg)
 
-      print self._games
-
   def _add_services(self):
     self._add_games()
-    self.device.add_service(GameManager())
+    self.device.add_service(GameManager(self))
 
   def start(self):
     self._create_device()

=== added directory 'game-server/src/games/truco'
=== added file 'game-server/src/games/truco/__init__.py'
--- game-server/src/games/truco/__init__.py	1970-01-01 00:00:00 +0000
+++ game-server/src/games/truco/__init__.py	2009-06-04 00:23:02 +0000
@@ -0,0 +1,13 @@
+
+import os
+
+from brisa.upnp.device.service import Service
+
+from brisa_game_server.services.xmls import xml_path
+
+service_name = 'Truco'
+service_type = 'urn:schemas-upnp-org:service:Truco:1'
+scpd_xml_path = os.path.join(xml_path, 'game-manager-scpd.xml')
+
+
+service = Service(service_name, service_type, '', scpd_xml_path)

=== modified file 'game-server/src/services/game_manager.py'
--- game-server/src/services/game_manager.py	2009-06-03 19:57:05 +0000
+++ game-server/src/services/game_manager.py	2009-06-04 00:23:02 +0000
@@ -1,19 +1,58 @@
 
-from brisa.core import log
+
+import os
+
 from brisa.upnp.device.service import Service
-from brisa.upnp.device.action import Action, Argument
-from brisa.upnp.device.service import StateVariable
 
-log = log.getLogger('services.gm')
+from brisa_game_server.services.xmls import xml_path
 
 service_name = 'GameManager'
 service_type = 'urn:schemas-upnp-org:service:GameManager:1'
+scpd_xml_path = os.path.join(xml_path, 'game-manager-scpd.xml')
 
 class GameManager(Service):
-  def __init__(self):
-    Service.__init__(self, service_name, service_type, '')
-    self._create_methods()
-
-  def _create_methods(self):
-		pass
+  def __init__(self, device):
+    Service.__init__(self, service_name, service_type, '', scpd_xml_path)
+    self._device = device
+    self._players = []
+
+  def soap_GetUUID(self, *args, **kwargs):
+    self._players.append(str(len(self._players)))
+    return {'UUID' : (len(self._players) - 1)}
+
+  def soap_GetAvailableGames(self, *args, **kwargs):
+    games = ':'.join(self._device.games.keys())
+    return {'Games' : games}
+
+  def soap_GetAvailableRooms(self, *args, **kwargs):
+    try:
+      game = self._device.games[kwargs['Game']]
+      rooms = ':'.join([str(len(room)) for room in game])
+      return {'Rooms' : rooms}
+    except:
+      return {'Rooms' : 'ERROR: Game does not exist'}
+
+  def soap_CreateRoom(self, *args, **kwargs):
+    try:
+      game = self._device.games[kwargs['Game']]
+      game.append([])
+      return {'RoomID' : (len(game) - 1)}
+    except:
+      return {'RoomID' : -2}
+
+  def soap_EnterRoom(self, *args, **kwargs):
+    try:
+      game = self._device.games[kwargs['Game']]
+      rid = int(kwargs['RoomID'])
+      uuid = kwargs['UUID']
+      if self._players.count(uuid):
+        try:
+          game[rid].append(uuid)
+          return {'Return' : 1}
+        except:
+           return {'Return' : 0}
+      else:
+        return {'Return' : -1}
+    except:
+      return {'Return' : -2}
 

=== added file 'game-server/src/services/xmls/game-manager-scpd.xml'
--- game-server/src/services/xmls/game-manager-scpd.xml	1970-01-01 00:00:00 +0000
+++ game-server/src/services/xmls/game-manager-scpd.xml	2009-06-04 00:23:02 +0000
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="utf-8"?>
+<scpd xmlns="urn:schemas-upnp-org:service-1-0">
+    <specVersion>
+        <major>1</major>
+        <minor>0</minor>
+    </specVersion>
+    <actionList>
+        <action>
+            <name>GetUUID</name>
+            <argumentList>
+                <argument>
+                    <name>UUID</name>
+                    <direction>out</direction>
+                    <relatedStateVariable>A_ARG_TYPE_UUID</relatedStateVariable>
+                </argument>
+            </argumentList>
+        </action>
+        <action>
+            <name>GetAvailableGames</name>
+            <argumentList>
+                <argument>
+                    <name>Games</name>
+                    <direction>out</direction>
+                    <relatedStateVariable>A_ARG_TYPE_Games</relatedStateVariable>
+                </argument>
+            </argumentList>
+        </action>
+        <action>
+            <name>GetAvailableRooms</name>
+            <argumentList>
+                <argument>
+                    <name>Game</name>
+                    <direction>in</direction>
+                    <relatedStateVariable>A_ARG_TYPE_Game</relatedStateVariable>
+                </argument>
+                <argument>
+                    <name>Rooms</name>
+                    <direction>out</direction>
+                    <relatedStateVariable>A_ARG_TYPE_Rooms</relatedStateVariable>
+                </argument>
+            </argumentList>
+        </action>
+        <action>
+            <name>CreateRoom</name>
+            <argumentList>
+                <argument>
+                    <name>Game</name>
+                    <direction>in</direction>
+                    <relatedStateVariable>A_ARG_TYPE_Game</relatedStateVariable>
+                </argument>
+                <argument>
+                    <name>RoomID</name>
+                    <direction>out</direction>
+                    <relatedStateVariable>A_ARG_TYPE_RoomID</relatedStateVariable>
+                </argument>
+            </argumentList>
+        </action>
+        <action>
+            <name>EnterRoom</name>
+            <argumentList>
+                <argument>
+                    <name>UUID</name>
+                    <direction>in</direction>
+                    <relatedStateVariable>A_ARG_TYPE_UUID</relatedStateVariable>
+                </argument>
+                <argument>
+                    <name>Game</name>
+                    <direction>in</direction>
+                    <relatedStateVariable>A_ARG_TYPE_Game</relatedStateVariable>
+                </argument>
+                <argument>
+                    <name>RoomID</name>
+                    <direction>in</direction>
+                    <relatedStateVariable>A_ARG_TYPE_RoomID</relatedStateVariable>
+                </argument>
+                <argument>
+                    <name>Return</name>
+                    <direction>out</direction>
+                    <relatedStateVariable>A_ARG_TYPE_Return</relatedStateVariable>
+                </argument>
+            </argumentList>
+        </action>
+    </actionList>
+    <serviceStateTable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_UUID</name>
+            <dataType>uuid</dataType>
+        </stateVariable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_Games</name>
+            <dataType>string</dataType>
+        </stateVariable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_Game</name>
+            <dataType>string</dataType>
+        </stateVariable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_Rooms</name>
+            <dataType>string</dataType>
+        </stateVariable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_RoomID</name>
+            <dataType>i1</dataType>
+        </stateVariable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_Return</name>
+            <dataType>i1</dataType>
+        </stateVariable>
+    </serviceStateTable>
+</scpd>



--
lp:brigas
https://code.launchpad.net/~brigas-team/brigas/trunk

Your team Brisa Game Server Team is subscribed to branch lp:brigas.
To unsubscribe from this branch go to https://code.launchpad.net/~brigas-team/brigas/trunk/+edit-subscription.