Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Server extends Daemon
- {
- public $socket;
- public $ip, $port;
- public $clients = array();
- public $salons = array();
- public $commands = array();
- public function __construct($ip, $port)
- {
- // Ici on souhaite gérer les signaux SIGUSR1 et SIGUSR2 en plus
- parent::__construct("server", array(
- SIGUSR1,
- SIGUSR2
- ));
- $this->ip = $ip;
- $this->port = $port;
- // salon
- $this->salons[] = new Salon("Acceuil", - 1, 2, false);
- $this->salons[] = new Salon("Salon 1", 5, 7, true);
- $this->salons[] = new Salon("Salon 2", 5, 7, true);
- $this->salons[] = new Salon("Salon 3", 5, 7, true);
- $this->salons[] = new Salon("Sauna/Hamam", 15, 1, true);
- $this->salons[] = new Salon("Salon de thé", 10, 3, true);
- $this->salons[] = new Salon("Zoo", - 1, - 1, true);
- $this->salons[] = new Salon("Pour les schizophrènes", 1, 1, true);
- $this->salons[] = new Salon("AFK", - 1, 0, false);
- // commands
- $this->commands[] = new HelpCommand("/help", "Affichage des commandes", 0);
- $this->commands[] = new PseudoCommand("/pseudo", "/pseudo <votre pseudo> - Définir un pseudo", 1);
- $this->commands[] = new ChannelSwitchCommand("/salon", "/salon <ID du salon>", 1);
- $this->commands[] = new ChannelDisplayCommand("/listsalon", "Affichage des salons disponible", 0);
- $this->commands[] = new RestartCommand("/restart", "Relance le serveur", 0);
- $this->commands[] = new StopCommand("/stop", "Stop le serveur", 0);
- $this->commands[] = new LeaveCommand("/leave", "Déconnexion du serveur", 0);
- // Démarrage du démon
- parent::start();
- }
- public function run()
- {
- // Le code qui s'exécute infiniment
- if ($c = socket_accept($this->socket)) {
- socket_set_nonblock($c);
- $client_ip;
- $client_port;
- @socket_getpeername($c, $client_ip, $client_port);
- $clientInstance = new Client($client_ip, $client_port, $c);
- array_push($this->clients, $clientInstance);
- $this->logs("Ajout du client " . $clientInstance->id . " sur " . $client_ip . ":" . $client_port);
- $this->salons[0]->moveClient($clientInstance, $this);
- $this->displayChannels($clientInstance);
- }
- foreach ($this->clients as $client) {
- $this->handleClient($client);
- }
- usleep(500);
- }
- public function handleClient($client)
- {
- $socket = $client->socket;
- if ($socket !== FALSE) {
- if ($buf = @socket_read($socket, 2048)) {
- if (strpos($buf, '/') === 0) {
- $this->handleCommand($client, $buf);
- } else {
- $result = $this->onTalk($client, $buf);
- if ($result) {
- $message = new ChatMessage($buf, $client->username, $client->currentSalon);
- $client->sendMessage($message->format(), false);
- foreach ($this->clients as $otherClient) {
- if ($client->id !== $otherClient->id && $client->currentSalon->isClientIn($otherClient)) {
- $otherClient->sendMessage($message->format(), false);
- }
- }
- $client->currentSalon->storeMessage($message);
- }
- }
- }
- } else {
- $clients = array_splice($this->clients, $client, 1);
- }
- }
- public function handleCommand($client, $command)
- {
- $any = false;
- foreach ($this->commands as $commandInstance) {
- $startWith = (substr($command, 0, strlen($commandInstance->commandName)) === $commandInstance->commandName);
- if ($startWith) {
- $any = true;
- $argsString = substr($command, strlen($commandInstance->commandName)); // supprésion du prefix de la commande
- $args = explode(" ", $argsString); // sépération des arguments
- $formattedArgs = array();
- // supprésion des éléments vides de la commande
- foreach ($args as $arg) {
- if ($arg != null && strlen($arg) > 0 && trim($arg) !== '') {
- $formattedArgs[] = $arg;
- }
- }
- $args = $formattedArgs;
- if ($commandInstance->argsLength != 0 && sizeof($args) < $commandInstance->argsLength) {
- $client->sendMessage("Command invalide, usage de la commande: " . $commandInstance->usage);
- } else {
- $commandInstance->execute($this, $client, $args);
- $this->logs("Commande par l'utilisateur " . $client->id . " " . $command);
- break;
- }
- }
- }
- if (! $any) {
- $client->sendMessage("Commande inconnue, /help pour afficher la liste des commandes.");
- }
- }
- public function onTalk($client, $message)
- {
- if ($client->username == null) {
- $client->sendMessage("Vous devez avoir un pseudo pous écrire dans le tchat.");
- $client->sendMessage("Utiliser la commande suivante pour en définir un: /pseudo <votre pseudo>", false);
- return false;
- }
- $salon = $client->currentSalon;
- if ($salon == null) {
- $this->salons[0]->moveClient($client, $this);
- return false;
- }
- if ($salon->canTalk === FALSE) {
- $client->sendMessage("Vous ne pouvez pas discuter dans se salon.", false);
- return false;
- }
- if ($salon->cooldownCheck($client)) {
- $client->sendMessage("Vous ne pouvez parler que toutes les " . $salon->talk_cooldown_time . " secondes.", false);
- return false;
- }
- return true;
- }
- public function onStart()
- {
- $this->logs("Démarrage du processus avec le pid " . getmypid());
- if (($this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) == FALSE) {
- $this->logs("socket_create_listen() a échoué : " . socket_strerror(socket_last_error($this->socket)));
- exit(1);
- }
- socket_set_nonblock($this->socket);
- // Modification de l'option SO_REUSEADDR à la valeur 1 !
- if (! socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
- $this->logs('Impossible de définir l\'option du socket : ' . socket_strerror(socket_last_error($this->socket)));
- exit(1);
- }
- if (@socket_bind($this->socket, $this->ip, $this->port) == FALSE) {
- $this->logs("socket_bind() a échoué : " . socket_strerror(socket_last_error($this->socket)));
- exit(1);
- }
- if (@socket_listen($this->socket) == FALSE) {
- $this->logs("socket_listen() a échoué : " . socket_strerror(socket_last_error($this->socket)));
- exit(1);
- }
- $this->logs("Serveur en écoute sur " . $this->ip . ":" . $this->port);
- }
- public function onStop()
- {
- $this->logs("Arrêt du processus avec le pid " . getmypid());
- socket_close($this->socket);
- }
- public function handleOtherSignals($signal)
- {
- $this->logs("Signal non géré par la classe Daemon : " . $signal);
- }
- public function dcClient($client)
- {
- socket_close($client->socket);
- $this->logs("Déconnexion du client " . $client->id . ".");
- }
- public function displayChannels($client)
- {
- $client->sendMessage("ID - Nom du salon - Nombre de client en ligne / Slots", false);
- $id = 0;
- foreach ($this->salons as $salon) {
- $client->sendMessage($id ++ . " - " . $salon->nom . " - " . $salon->countClients($this) . "/" . ($salon->max_clients == - 1 ? "Infini" : $salon->max_clients), false);
- }
- }
- public function logs($message)
- {
- file_put_contents("/var/log/" . $this->name . ".log", $message . "\n", FILE_APPEND);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement