[PHP>=5.3.0]Récupérer les informations d'un serveur Simple Radio Standalone

Salle dédiée aux mods/addons et applications pour DCS World
Avatar de l’utilisateur

Topic author
ergo
Webmaster
Webmaster
Messages : 14484
Inscription : 27 avril 2008

[PHP>=5.3.0]Récupérer les informations d'un serveur Simple Radio Standalone

#1

Message par ergo »

Bonjour à tous,

Avec l'EVAC nous utilisons SRS, et je cherchais un moyen de surveiller automatiquement les serveurs SRS pour pouvoir (entre autre) récupérer leur état, et savoir si ils étaient en ligne ou non.

J'ai donc créé une classe PHP qui permet de se connecter à un serveur SRS pour en extraire les informations utiles. (version du serveur, clients, paramètre du serveur, etc ...)

Je vous livre la class ici pour votre usage ( CC-BY-NC-SA )

Vous pouvez en voir une utilisation sur mon site de monitoring ici : http://niprod.net/etat (vous pouvez cliquer sur le numéro de version pour avoir plus d'info)

La classe PHP est assez simple à utiliser, exemple :

Code : Tout sélectionner

 <?php include('SRSClient.php'); $srs = new SRSClient('xx.xx.xx.xx',5004) ;  if ($srs->connect()->isConnect()) {     $srs->waitResponse() ;      echo '<h1>Serveur SRS</h1><pre>' ;      echo 'Version : '. $srs->getVersion() . "\n";      echo 'Clients count : '. (count($srs->getClients()) - 1) . "\n"; // do not count me      echo 'Serveur settings : ' . "\n" ;      foreach ($srs->getServerSettings() as $key => $value) {         echo "\t" . $key . ' : ' . ($value ? 'ON' : 'OFF') . "\n";      };     echo '</pre>';     $srs->disconnect(); } else {     echo 'Connection failed' ;  }
ce qui permet d'afficher ceci : (voir capture d'écran plus bas)

ATTENTION : Si le serveur SRS n'est pas dispo, la classe php va attendre le socket_timeout (paramètre php qui par défaut vaut 30sec). Je vous conseil donc de mettre en place une tache cron pour automatiser la récupération des infos que vous stockerez dans un fichier/base de donnée avant de l'afficher sur votre site.

Le code de la classe :

Code : Tout sélectionner

<?php /*************** SRSClient.php * Simple Library for SRS (Simple Radio Standalone) * v1.0 * Author : "Ergo" Nicolas Dumas 2018 (http://niprod.net) * Licence : Creative Commun - CC-BY-NC-SA (https://creativecommons.org/licenses/by-nc-sa/4.0/) * compatibility : SRS Server 1.5.3.x **/ class SRSClient { private $_socket = false ; private $_ip ; private $_port ; private $_timeout = 5 ; private $_uuid ; private $_response = array ( 'Client' => NULL, 'MsgType' => NULL, 'Clients' => NULL, 'ServerSettings' => NULL, 'ExternalAWACSModePassword' => NULL, 'Version' => NULL ); public static $msgType = array( 'UPDATE' => 0, //META Data update - No Radio Information 'PING' => 1, 'SYNC' => 2, 'RADIO_UPDATE' => 3, //Only received server side 'SERVER_SETTINGS' => 4, 'CLIENT_DISCONNECT' => 5, // Client disconnected 'VERSION_MISMATCH' => 6, 'EXTERNAL_AWACS_MODE_PASSWORD' => 7, // Received server side to "authenticate"/pick side for external AWACS mode 'EXTERNAL_AWACS_MODE_DISCONNECT' => 8 // Received server side on "voluntary" disconnect by the client (without closing the server connection) ) ; private $_clientName = 'PHP SRS Client' ; private $_version = '1.5.3.5' ; /** * Construct function * @string $ip : need ip or internet adresse of SRS Server * @int $port : need port number */ public function __construct($ip, $port=5002) { if ($port < 0 or $port > 65535) throw new InvalidArgumentException('Port must be betwwen 0 and 65535, given : '.$port); if (!is_string($ip)) throw new InvalidArgumentException('Ip must be a string, given : '.gettype($ip)); $this->_port = $port ; $this->_ip = $ip ; $this->_uuid = uniqid('php_srsClient_') ; //add specific prefix for srsServer return $this ; } public function isConnect() { return ($this->_socket !== false) ; } public function connect() { // already connected ? if ($this->isConnect()) $this->disconnect() ; $this->_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if (!$this->isConnect()) return $this ; $result = @socket_connect($this->_socket, $this->_ip, $this->_port); if ($result === false ) { @fclose($this->_socket) ; $this->_socket = false ; return $this ; } $this->send($this->createMsg(self::$msgType['SYNC'])) ; return $this ; } public function disconnect() { // not connected ? nothing to do. if (! $this->isConnect()) return $this ; // send disconnect information : socket_close($this->_socket); $this->_socket = false ; } public function waitResponse() { $out = @socket_read($this->_socket, 1,PHP_BINARY_READ); $response = ''; while ($out != "\n") { $response .= $out ; $out = @socket_read($this->_socket, 1,PHP_BINARY_READ); } $this->_response = $this->decode_msg($response); return $this ; } public function getMsgTypeResponse() { if (! $this->isConnect()) return false ; return $this->_response['MsgType'] ; } public function getVersion() { if (! $this->isConnect()) return false ; return $this->_response['Version'] ; } public function getClients() { if (! $this->isConnect()) return false ; return $this->_response['Clients'] ; } public function getServerSettings() { if (! $this->isConnect()) return false ; return $this->_response['ServerSettings'] ; } /** * Destruct function * need to close connection with SRS Client */ public function __destruct() { if ($this->isConnect()) $this->disconnect(); } private function recusive_str2Bool_array($array) { if (!is_array($array)) { if (is_string($array)) { if (strtolower($array) == 'false') $array = false ; if (strtolower($array) == 'true') $array = true ; } } else { $a = array() ; foreach ($array as $key => $value) { $a[$key] = $this->recusive_str2Bool_array($value) ; } $array = $a ; } return $array ; } private function recusive_bool2Str_array($array) { if (!is_array($array)) { if (is_bool($array)) { if ($array) { $array = 'True' ; } else { $array = 'False' ; } } } else { foreach ($array as $key => $value) { $array[$key] = $this->recusive_bool2Str_array($value) ; } } return $array ; } private function encode_msg($msg) { return json_encode($this->recusive_bool2Str_array($msg)); } private function decode_msg($msg) { return $this->recusive_str2Bool_array(json_decode($msg, true)); } private function send($msg) { // not connected ? nothing to do. if (! $this->isConnect()) return $this ; $jsonMsg = $this->encode_msg($msg) . "\n"; @socket_write($this->_socket, $jsonMsg, strlen($jsonMsg)); return $this ; } private function createMsg($msgType) { if ($msgType < 0 or $msgType >= count(self::$msgType)) throw new InvalidArgumentException('msgType must be betwwen 0 and '.count(self::$msgType).', given : '.$msgType); $msg = array ( 'Client' => array ( 'ClientGuid' => $this->_uuid, 'Name' => $this->_clientName, 'Coalition' => 0, 'RadioInfo' => NULL, 'Position' => array ( 'x' => 0, 'y' => 0, 'z' => 0 ), 'ClientChannelId' => NULL, ), 'MsgType' => $msgType, 'Clients' => $this->_response['Clients'], 'ServerSettings' => $this->_response['ServerSettings'], 'ExternalAWACSModePassword' => $this->_response['ExternalAWACSModePassword'], 'Version' => $this->_version ) ; return $msg ; } }
Pièces jointes
Capture d’écran 2018-08-16 à 10.57.12.jpg
Capture d’écran 2018-08-16 à 10.57.12.jpg (101.42 Kio) Consulté 680 fois
Tout travail mérite son dû, n'est-ce pas "Oui Oui" ?
(Vainqueur de la Boulet's Cup Démo Team C6 en 2011 et 2013)
[F.A.Q.] Bien paramètrer ses forums Checksix

Revenir à « Mods et Applications »