1
0
mirror of https://github.com/php/doc-es.git synced 2026-03-27 00:42:10 +01:00
Files
archived-doc-es/reference/gupnp/examples.xml
Jesús Ruiz-Ayúcar Vázquez 1ab4c0b30e actualizaciones minimas
git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@317731 c90b9560-bf6c-de11-be94-00142212c4b1
2011-10-04 21:12:32 +00:00

281 lines
8.2 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 8b6d169424ff189bb563ef4c3f35f8adff3f42c5 Maintainer: chuso Status: ready -->
<chapter xml:id="gupnp.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<section xml:id="gupnp.browsing">
<title>Navegando por dispositivos y servicios</title>
<para>
Este ejemplo muestra como obtener información acerca de todos los dispositivos y servicios.
Esto inicia un bucle infinito (usando CLI), y si alguno de los dispositivos o servicios disponibles
es hayado, la función callback correcta será invocada.
</para>
<para>
<example>
<title>Buscar toos los dispositivos y servicios UPnP.</title>
<programlisting role="php">
<![CDATA[
<?php
/* Callback para dispositivo disponible */
function device_proxy_available_cb($proxy, $arg)
{
$info = gupnp_device_info_get($proxy);
$type = $info['device_type'];
$location = $info['location'];
printf("Dispositivo Disponible:\n");
printf("\ttipo: %s\n", $type);
printf("\tubicación: %s\n", $location);
}
/* Callback para servicio disponible */
function service_proxy_available_cb($proxy, $arg)
{
$info = gupnp_service_info_get($proxy);
$type = $info['service_type'];
$location = $info['location'];
printf("Servicio Disponible:\n");
printf("\ttipo: %s\n", $type);
printf("\tubicación: %s\n", $location);
}
/* Crear el contexto UPnP */
$context = gupnp_context_new();
if (!$context) {
printf("Error creando el contexto GUPnP\n");
exit(-1);
}
/* Estamos interesados en todo */
$cp = gupnp_control_point_new($context, "ssdp:all");
/* Carga callbacks */
gupnp_control_point_callback_set($cp,
GUPNP_SIGNAL_DEVICE_PROXY_AVAILABLE, 'device_proxy_available_cb');
gupnp_control_point_callback_set($cp,
GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE, 'service_proxy_available_cb');
/* Inicio para navegar (lazo infinito, Ctrl-C para interrumpir) */
gupnp_control_point_browse_start($cp);
?>
]]>
</programlisting>
</example>
</para>
</section> <!-- xml:id=gupnp.browsing -->
<section xml:id="gupnp.binary-light">
<title>Implementando el dispositivo BinaryLight</title>
<para>
Este es un ejemplo de dispositivo/servicio, implementando el dispositivo BinaryLight
y el servicio SwitchPower para emular un interruptor de luz.
</para>
<para>
La interfaz de usuario se ha simplificado con el fin de demostrar los conceptos y métodos básicos.
</para>
<para>
<example>
<title>Implementing light server</title>
<programlisting role="php">
<![CDATA[
<?php
/* Establece Objetivo */
function set_target_cb($service, $action, $arg)
{
/* Obtiene nuevo valor del objetivo */
$target = gupnp_service_action_get($action, 'NewTargetValue', GUPNP_TYPE_BOOLEAN);
/* Si el nuevo objetivo no coincide con el status actual, cambiar status y
emitir una notificación de que el status ha cambiado. */
if ($target != $GLOBALS['status']) {
$GLOBALS['status'] = $target;
gupnp_service_notify($service, 'Status', GUPNP_TYPE_BOOLEAN, $GLOBALS['status']);
printf("La luz está ahora %s.\n", $GLOBALS['status'] ? "on" : "off");
}
/* Devuelve éxito al cliente */
gupnp_service_action_return($action);
}
/* Obtiene Objetivo*/
function get_target_cb($service, $action, $arg)
{
gupnp_service_action_set($action, 'RetTargetValue', GUPNP_TYPE_BOOLEAN, $GLOBALS['status']);
gupnp_service_action_return($action);
}
/* Obtiene Status*/
function get_status_cb($service, $action, $arg)
{
gupnp_service_action_set($action, 'ResultStatus', GUPNP_TYPE_BOOLEAN, $GLOBALS['status']);
gupnp_service_action_return($action);
}
/* La luz está apagada de forma predeterminada */
$GLOBALS['status'] = false;
printf("La luz está ahora %s.\n", $GLOBALS['status'] ? "on" : "off");
/* Crea el contexto UPnP */
$context = gupnp_context_new();
if (!$context) {
printf("Error creando el contexto GUPnP\n");
exit(-1);
}
/* Hospeda el directorio que contiene los archivos de descripción de los dispositivos y servicios */
gupnp_context_host_path($context, "./web", "");
/* crea el dispositivo raíz */
$location = "/BinaryLight.xml";
$dev = gupnp_root_device_new($context, $location);
gupnp_root_device_set_available($dev, true);
/* Obtiene el servicio switch del dispositivo raíz */
$service_type = "urn:schemas-upnp-org:service:SwitchPower:1";
$service = gupnp_device_info_get_service($dev, $service_type);
if (!$service) {
die("Cannot get SwitchPower1 service\n");
}
/* Set callback for action GetStatus */
gupnp_device_action_callback_set($service, GUPNP_SIGNAL_ACTION_INVOKED, "GetStatus",
"get_status_cb", "action data, GetStatus");
/* Establece el callback para la acción GetTarget */
gupnp_device_action_callback_set($service, GUPNP_SIGNAL_ACTION_INVOKED, "GetTarget",
"get_target_cb", "action data, GetTarget");
/* Establece el callback para la acción SetTarget */
gupnp_device_action_callback_set($service, GUPNP_SIGNAL_ACTION_INVOKED, "SetTarget",
"set_target_cb", "action data, SetTarget");
/* Ejecuta el bucle principal */
gupnp_root_device_start($dev);
?>
]]>
</programlisting>
</example>
<example>
<title>Implementando el cliente "luz"</title>
<programlisting role="php">
<![CDATA[
<?php
function service_proxy_available_cb($proxy, $arg)
{
$mode = $arg['mode'];
printf("Set subscribed\n");
gupnp_service_proxy_set_subscribed($proxy, true);
/* Agrega una notificación si el status cambia */
if (!gupnp_service_proxy_add_notify($proxy, "Status",
GUPNP_TYPE_BOOLEAN, "status_changed_cb", NULL)) {
printf("Falló agregando notificación\n");
}
if ($mode == 'TOGGLE') {
/* estamos alternando, así que primero obtenemos el status actual */
$target = gupnp_service_proxy_action_get($proxy, 'GetStatus', 'ResultStatus', GUPNP_TYPE_BOOLEAN);
/* y entonces alternamos */
$target = ! $target;
} else {
/* Mode es booleano, así que el objetivo es mode gracias a nuestra buena selección de valores numéricos. */
$target = ($mode == 'ON') ? true : false;
}
/* Establece el objetivo */
if (!gupnp_service_proxy_action_set($proxy, 'SetTarget', 'NewTargetValue', $target, GUPNP_TYPE_BOOLEAN)) {
printf("No se puede establecer interruptor\n");
} else {
printf("Establece interruptor a %s.\n", $target ? "on" : "off");
}
/* Detiene navegador */
gupnp_control_point_browse_stop($arg['cp']);
}
function status_changed_cb($variable, $value, $arg)
{
printf("Status ha cambiado\n");
printf("\tvariable: %s\n", $variable);
printf("\tvalor: %s\n", (int)$value);
printf("\n");
}
/* Verifica y descompone los argumentos de la línea de comandos */
if (count($argv) != 2) {
printf("Use: light-client.php [on|off|toggle]\n");
exit(-1);
}
if ($argv[1] == "on") {
$mode = 'ON';
} elseif ($argv[1] == "off") {
$mode = 'OFF';
} elseif ($argv[1] == "toggle") {
$mode = 'TOGGLE';
} else {
usage ();
exit(-1);
}
/* Crea el contexto UPnP */
$context = gupnp_context_new();
if (!$context) {
printf("Error creando el contexto GUPnP\n");
exit(-1);
}
/* Crea el punto de control, buscando en los servicios SwitchPower */
$cp = gupnp_control_point_new ($context,
"urn:schemas-upnp-org:service:SwitchPower:1");
/* Conecta al callback del servicio encontrado*/
$cb = "service_proxy_available_cb";
$arg = array('mode' => $mode, 'cp' => $cp);
gupnp_control_point_callback_set($cp, GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE, $cb, $arg);
/* Start for browsing */
gupnp_control_point_browse_start($cp);
?>
]]>
</programlisting>
</example>
</para>
</section> <!-- xml:id=gupnp.browsing -->
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->