Skip to content

Commit

Permalink
+ socket server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mister-good-deal committed Aug 15, 2015
1 parent b8f692c commit 4ddab4f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
87 changes: 87 additions & 0 deletions classes/socket/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace classes\socket;

use \classes\ExceptionManager as Exception;
use \classes\IniManager as Ini;

class Server
{
use \traits\EchoTrait;

/**
* @var resource $resource The server socket resource
*/
private $resource;
/**
* @var string $protocol The server socket protocol
*/
private $protocol;
/**
* @var string $address The server socket address
*/
private $address;
/**
* @var integer $port The server socket port
*/
private $port;
/**
* @var boolean $verbose True to print info in console else false
*/
private $verbose;
/**
* @var integer $errorNum The error code if an error occured
*/
private $errorNum;
/**
* @var string $errorString The error string if an error occured
*/
private $errorString;

/**
* Constructor that load parameters in the ini conf file and run the socket server
*/
public function __construct()
{
cli_set_process_title('PHP socket server');

$params = Ini::getSectionParams('Socket');
$this->protocol = $params['protocol'];
$this->address = $params['address'];
$this->port = $params['port'];
$this->verbose = $params['verbose'];
$this->resource = stream_socket_server(
$this->protocol . '://' . $this->address . ':' . $this->port,
$this->errorNum,
$this->errorString
);

if ($this->resource === false) {
throw new Exception('Error ' . $this->errorNum . '::' . $this->errorString, Exception::$ERROR);
}

$this->run();
}

/**
* Run the server
*/
private function run()
{
while (1) {
if ($this->verbose) {
$this->runningInfo();
}

sleep(60);
}
}

private function runningInfo()
{
static::out(
'[' . date('Y-m-d H:i:s') . ']'
. ' Server running on ' . stream_socket_get_name($this->resource, false) . PHP_EOL
);
}
}
6 changes: 6 additions & 0 deletions conf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ ATTR_AUTOCOMMIT = 1
maxLength = 115 ; The console maxLength
encoding = "CP850" ; The output console encoding
printSql = true ; If the SQL requests should be printed in the console

[Socket]
protocol = 'tcp'
address = '127.0.0.1'
port = 0 ; 0 for random unused port
verbose = true
18 changes: 18 additions & 0 deletions devTests/testSocket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Test script for the socket Server class
*
* @category Test
* @author Romain Laneuville <[email protected]>
*/

use \classes\socket\Server as Server;

include_once '../autoloader.php';

try {
$server = new Server();
} catch (Exception $e) {
} finally {
exit(0);
}
1 change: 1 addition & 0 deletions log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,4 @@
2015-07-29 22:48:09 There is no image at this path : "./test.png"
2015-07-29 22:48:44 There is no image at this path : "C:\prog\utilities\devTests/test.png"
2015-07-29 23:11:09 There is no image at this path : "C:\prog\utilities\devTests\test.png"
2015-08-15 14:02:55 Error 0::php_network_getaddresses: getaddrinfo failed: H�te inconnu.

0 comments on commit 4ddab4f

Please sign in to comment.