<?php
namespace App\Service\JavaService;
class Server
{
protected $extension_query = 'query';
protected $extension_result = 'result';
protected $socket_port = 0;
protected $host = null;
protected $path = null;
protected $local_path_files = null;
protected $remote_path_files = null;
protected $path_local_query = null;
protected $path_local_result = null;
protected $path_remote_query = null;
protected $path_remote_result = null;
protected $log = '';
public function __construct($port, $host, $path, $local_path, $remote_path)
{
$this->socket_port = $port;
$this->host = $host;
$this->path = $path;
$this->local_path_files = $local_path;
$this->path_local_query = $local_path.'/files/'.$this->extension_query .'/';
$this->path_local_result = $local_path.'/files/'.$this->extension_result .'/';
$this->remote_path_files = $remote_path;
$this->path_remote_query = $remote_path.'/files/'.$this->extension_query .'/';
$this->path_remote_result = $remote_path.'/files/'.$this->extension_result .'/';
}
public function isOnline()
{
try {
$timeout = 5;
$socket = fsockopen($this->host,$this->socket_port,$errnum,$errstr,$timeout) ;
if(!is_resource($socket)) {
return false;
}
else {
fclose($socket);
return true;
}
}
catch(\Exception $e) {
return false;
}
}
public function getStatus() {
if ($this->status())
return 'Online';
else
return 'Offline';
}
public function status() {
$command = 'ps -p '.$this->getPID();
exec($command,$op);
if (!isset($op[1]))
return false;
else
return true;
}
public function getPID() {
$command = 'cat '.$this->path.'/run/server.pid';
exec($command,$op);
if (!isset($op[0]))
return 0;
else
return (int)$op[0];
}
public function start() {
if (!$this->isOnline()) {
$command = $this->path.'/bin/startup.sh';
exec($command);
$this->addLog('Servicio ha iniciado');
return true;
} else {
$this->addLog('Servicio ya iniciado');
return true;
}
}
public function stop(){
if($this->getPID() == 0) {
$this->addLog('Servicio ya esta detenido');
return false;
} else {
$command = $this->path.'/bin/shutdown.sh';
exec($command);
if ($this->status() == false) {
$this->addLog('Se ha detenido el servicio');
return true;
} else {
$this->addLog('ERROR: {Stop} fail! ');
return false;
}
}
}
public function getTailLogFile($lineas = 100)
{
try {
$archivo = $this->path. '/log/server.log';
$command = 'tail -n '.$lineas.' '.$archivo;
exec($command,$op);
return $op;
}
catch(\Exception $e) {
return array('log files esta con errores.', $e->getMessage());
}
}
/**
* Run and Return file
*
* @param string $program
* @param string $flag
* @param unknown_type $data
* @return file
*/
public function runAndReturnFile($program, $flag = 'flag', $srcData)
{
return $this->run($program, $flag, $srcData);
}
/**
* Run and Return Data
*
* @param string $program
* @param string $flag
* @param unknown_type $data
* @return string data
*/
public function runAndReturnData($program, $flag = 'flag', $srcData)
{
$file = $this->run($program, $flag, $srcData);
$data_file = file_get_contents($file);
return $data_file;
}
private function generateIdForFile()
{
return date('dmy_His') . "_" . rand(100,999);
}
private function generateLocalFile($data, $flag, $send_file)
{
$id = $this->generateIdForFile();
$id_file = $id.'_'.$flag;
$name_file = $id_file.'.'.$this->extension_query;
if($send_file == true) {
$file = fopen($this->path_local_query . $name_file,'w');
$data = mb_convert_encoding($data, "ISO-8859-15", "UTF-8");
fwrite($file,$data);
fclose($file);
}
return $id_file;
}
private function getCommandForSendFile($id_file)
{
$cmd = "copy::send::";
$cmd .= $this->path_local_query . $id_file . '.' . $this->extension_query;
$cmd .= "::";
$cmd .= $this->path_remote_query . $id_file . '.' . $this->extension_query;
$cmd .= "\n";
return $cmd;
}
private function getCommandForGetFile($id_file)
{
$cmd = "copy::get::";
$cmd .= $this->path_remote_result . $id_file . '.' . $this->extension_result;
$cmd .= "::";
$cmd .= $this->path_local_result . $id_file . '.' . $this->extension_result;
$cmd .= "\n";
return $cmd;
}
private function getCommandForExec($exec)
{
$cmd = "exec::";
$cmd .= $exec;
$cmd .= "\n";
return $cmd;
}
private function getCommandForRemoteDelete($id_file)
{
$cmd = "dlte::";
$cmd .= $id_file;
$cmd .= "\n";
return $cmd;
}
private function run($program = null, $flag, $srcData = null, $send_file = true, $more_data = null)
{
if($program == null) {
throw new Exception("Server::run() -> Program is null");
}
$log = "";
$id_file = $this->generateLocalFile($srcData, $flag, $send_file);
$comando1 = $this->getCommandForSendFile($id_file);
$comando2 = null;
if($more_data != null) {
$comando2 = $this->getCommandForExec($this->remote_path_files."/apps/".$program." ".$id_file.",".$more_data . "\n");
}
else {
$comando2 = $this->getCommandForExec($this->remote_path_files."/apps/".$program." ".$id_file."\n");
}
$comando3 = $this->getCommandForGetFile($id_file);
$comando4 = $this->getCommandForRemoteDelete($id_file);
$timeout = 5;
$socket = fsockopen($this->host,$this->socket_port,$errnum,$errstr,$timeout) ;
if(!is_resource($socket)) {
exit("Connection socket fail:: ".$errnum." ".$errstr) ;
}
else {
if($send_file) {
fputs($socket, $comando1) ;
while(!feof($socket)) {
$linea = fgets($socket, 1024);
$log .= $linea;
if(trim($linea) == "copy::send::ok") {
break;
}
if(trim($linea) == "msg::fin_socket") {
break;
}
}
}
fputs($socket, $comando2);
while(!feof($socket)){
$linea = fgets($socket, 1024);
$log .= $linea;
if(trim($linea) == "exec::ok") {
break;
}
if(trim($linea) == "msg::fin_socket") {
break;
}
}
fputs($socket, $comando3);
while(!feof($socket)) {
$linea = fgets($socket, 1024);
$log .= $linea;
if(trim($linea) == "copy::get::ok")
{
break;
}
if(trim($linea) == "msg::fin_socket")
{
break;
}
}
// fputs($socket, $comando4);
// while(!feof($socket))
// {
// $linea = fgets($socket, 1024);
// $log .= $linea;
// if(trim($linea) == "delete::ok")
// {
// break;
// }
// if(trim($linea) == "msg::fin_socket")
// {
// break;
// }
// }
}
fclose($socket);
$this->setLog($log);
return $this->path_local_result . $id_file. '.' . $this->extension_result;
}
public function getLog()
{
return $this->log;
}
private function setLog($msg)
{
global $log;
$log .= $msg . "\n";
}
private function addLog($msg)
{
$this->log .= $msg . "\n";
}
}