Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
jgniecki committed Apr 13, 2021
1 parent 52b9b54 commit f5cdac8
Show file tree
Hide file tree
Showing 15 changed files with 1,171 additions and 171 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
"DevLancer\\ServerController\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"DevLancer\\ServerController\\Tests\\": "tests/"
}
},
"require": {
"php": ">=7.4",
"phpseclib/phpseclib": "~3.0"
Expand Down
147 changes: 147 additions & 0 deletions example/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php declare(strict_types=1);

use DevLancer\ServerController\LocatorInterface;
use DevLancer\ServerController\MachineMonitor;
use DevLancer\ServerController\ServerControl;
use DevLancer\ServerController\ServerMonitor;
use DevLancer\ServerController\Terminal;

/**
* @author Jakub Gniecki
* @copyright Jakub Gniecki <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Response
{
/**
* @var string[]
*/
private array $body;

/**
* @var int
*/
private int $status = 200;

/**
* @var string[]
*/
private array $header = [];

private Terminal $terminal;

private ServerControl $serverControl;

public function __construct(Terminal $terminal)
{
$this->serverControl = new ServerControl($terminal);
$this->terminal = $terminal;

}

public function start(LocatorInterface $locator): void
{
$port = 25565;
$name = sprintf(ServerControl::SERVER_NAME, $port);
$command = "screen -dmS $name top";

if ($this->serverControl->isRunning($port)) {
$this->body["is_running"] = true;
$this->body["try_start"] = false;
return;
}

try {
$result = $this->serverControl->start($locator, $port, $command);
} catch (Exception $exception) {
$result = false;
}

$this->body["is_running"] = $result;
$this->body["try_start"] = true;
}

public function stop(): void
{
$port = 25565;
$name = sprintf(ServerControl::SERVER_NAME, $port);
$command = "screen -X -S $name quit";

if (!$this->serverControl->isRunning($port)) {
$this->body["is_running"] = false;
$this->body["try_stop"] = false;
return;
}

try {
$result = $this->serverControl->stop($port, $command);
} catch (Exception $exception) {
$result = false;
}

$this->body["is_running"] = $result;
$this->body["try_stop"] = true;
}

public function info(): void
{
$port = 25565;
if (!$this->serverControl->isRunning($port)) {
$this->body = [
"is_running" => false,
"type" => "info"
];
return;
}

$this->body = [
"is_running" => true,
"type" => "info"
];
}

public function infoServer(): void
{
$port = 25565;
$pid = $this->serverControl->getPid($port);
if (!$pid) {
$this->body = ["is_running" => false, "type" => "server"];
return;
}

$this->body = ["is_running" => true];
$serverMonitor = new ServerMonitor($this->terminal);

$this->body = [
"uptime" => $serverMonitor->getUptime($pid),
"cpu_usage" => $serverMonitor->getCpuUsage($pid),
"memory_usage" => $serverMonitor->getMemoryUsage($pid),
"type" => "server"
];
}

public function infoMachine(): void
{
$machineMonitor = new MachineMonitor($this->terminal);

$this->body = [
"cpu_usage" => $machineMonitor->getCpuUsage(),
"memory_usage" => $machineMonitor->getMemoryUsage(MachineMonitor::FORMAT_PERCENTAGE),
"memory" => $machineMonitor->getMemory(),
"memory_free" => $machineMonitor->getMemoryFree(MachineMonitor::FORMAT_PERCENTAGE),
"type" => "machine"
];
}

public function get(float $microtime)
{
http_response_code($this->status);
$this->body['ping'] = microtime(true) - $microtime;
header('Content-Type: application/json');
foreach ($this->header as $header)
header($header);

echo json_encode($this->body);
}
}
52 changes: 52 additions & 0 deletions example/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/**
* @author Jakub Gniecki
* @copyright Jakub Gniecki <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use DevLancer\ServerController\Locator;
use DevLancer\ServerController\Terminal;
use phpseclib3\Net\SFTP;
use phpseclib3\Net\SSH2;

require_once ("../vendor/autoload.php");
require ("Response.php");

$host = "";
$login = "";
$password = "";

$microtime = microtime(true);
$ssh = new SSH2($host);
$ssh->login($login, $password);
$terminal = new Terminal($ssh, $password);
$response = new Response($terminal);

if (!isset($_GET['action'])) {
$response->info();
} else {
switch (strtolower($_GET['action'])) {
case "start":
$sftp = new SFTP($host);
$sftp->login($login, $password);
$locator = new Locator($sftp, "");
$response->start($locator);
break;
case "stop":
$response->stop();
break;
case "info-server":
$response->infoServer();
break;
case "info-machine":
$response->infoMachine();
break;
default:
$response->info();
break;
}
}

$response->get($microtime);
Loading

0 comments on commit f5cdac8

Please sign in to comment.