Matware

Creando un servidor usando Joomla! Platform

Este articulo pretende explicar como desarrollar un servidor usando Joomla! Platform. No es intencion del autor explicar que es Joomla! Platform asi que el que necesite alguna informacion extra de lo que es Joomla! Platform, Isidro Baquero ha escrito un excelente articulo explicativo.

Introduccion a JSockets

Para escribir un servidor desarrollado en PHP y usando Joomla! Platform como estructura se necesitan tener conocimiento de programacion usando sockets. Estos objetos no estan incluidos en la version oficial de Joomla! Platform, por lo tanto he creado unas nuevas librerias para Joomla! Platform que incluyen el manejo y configuracion de sockets.

Estas librerias se pueden descargar desde el fork que he creado en mi cuenta de GitHub: https://github.com/fastslack/joomla-platform

Instalacion de Joomla! Platform con JSockets

git clone git://github.com/fastslack/joomla-platform.git
mkdir my-server
cd my-server
cp -r ../joomla-platform/libraries/ .

Creando el objecto servidor

El archivo my-server debe estar en el directorio 'my-server':

#!/usr/bin/php
<?php
/**
* @version            $Id:
* @package            Joomla! Platform
* @subpackage      myServer
* @copyright            Copyright (C) 1996 - 2011 Matware - All rights reserved.
* @author                Matias Aguirre
* @email               This e-mail address is being protected from spambots. You need JavaScript enabled to view it
* @link                    http://www.matware.com.ar/
* @license                GNU/GPL http://www.gnu.org/licenses/gpl-2.0-standalone.html
*/
define( '_JEXEC', 1 );

include_once ( './libraries/import.php' );
include_once ( JPATH_PLATFORM . '/joomla/sockets/server.php' );

class myServerServer extends JSocketsServer {
/**
* A JCli object for the application to use.
*
* @var JCli
* @since 11.3
*/
protected $cli = null;
/**
* A database object for the application to use.
*
* @var JDatabase
* @since 11.3
*/
protected $dbo = null;
/**
* A database object for the application to use.
*
* @var JDatabase
* @since 11.3
*/
protected $address = '127.0.0.1';
/**
* A database object for the application to use.
*
* @var JDatabase
* @since 11.3
*/
protected $port = 8080;

/**
* Class constructor.
*
* @return  void
*
* @since   11.1
*/
public function __construct()
{
// Init cli
jimport( 'joomla.application.cli' );
$this->cli = JCli::getInstance();

// Init database
/*
jimport( 'joomla.database.database' );
$config =             array(
'driver' => 'mysqli',
'host' => 'localhost',
'user' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DB',
'prefix' => 'jos_',
);
$this->dbo = JDatabase::getInstance($config);
*/

// Parent construct
parent::__construct();

// Start server
$msg = "\nWelcome to My Server. \n" .
"To more info, type 'help'.\n";
$this->listen($this->address, $this->port, $msg);
}

/**
* Process the command
* @return  bool
*/
protected function __processCommand($childId = 0, $command = false)
{

switch($command) {
case "test":
$this->test($childId);
break;
case "help":
$this->help($childId);
break;
case "quit":
$this->killAll();
die("Kill message received\n");
break;
}
}

/**
* Print the help
*
* @return  bool
*/
protected function help($childId)
{
$output = "\nPlease type 'test' for testing...\n";
$this->threads[$childId]->write($output);
}

/**
* Print the help
*
* @return  bool
*/
protected function test($childId)
{
$output = "\nThis is a test server...\n";
$this->threads[$childId]->write($output);
}

}

$myServer = new myServerServer();

?>

Ejecutando el servidor

Ahora lo unico que nos resta hacer es ejecutar el servidor para iniciar todo el proceso, para realizar esto necesitamos darle los permisos necesarior al archivo my-server y ejecutarlo:

chmod 755 ./my-server
./my-server

Probando la conexion

Para probar nuestro servidor necesitamos llamar al famoso programa 'telnet':

telnet localhost 8080

Deberia aparecer 'Welcome to My Server".

Have fun.

You are here