src/Service/JavaService/Server.php line 130

Open in your IDE?
  1. <?php
  2. namespace App\Service\JavaService;
  3. class Server
  4. {
  5.     protected $extension_query    = 'query';
  6.     protected $extension_result   = 'result';
  7.     protected $socket_port  = 0;
  8.     protected $host = null;
  9.     protected $path  = null;
  10.     protected $local_path_files = null;
  11.     protected $remote_path_files = null;
  12.     protected $path_local_query = null;
  13.     protected $path_local_result = null;
  14.     protected $path_remote_query = null;
  15.     protected $path_remote_result = null;
  16.     protected $log = '';
  17.     public function __construct($port, $host, $path, $local_path, $remote_path)
  18.     {
  19.         $this->socket_port        = $port;
  20.         $this->host               = $host;
  21.         $this->path               = $path;
  22.         $this->local_path_files   = $local_path;
  23.         $this->path_local_query   = $local_path.'/files/'.$this->extension_query  .'/';
  24.         $this->path_local_result  = $local_path.'/files/'.$this->extension_result .'/';
  25.         $this->remote_path_files  = $remote_path;
  26.         $this->path_remote_query  = $remote_path.'/files/'.$this->extension_query  .'/';
  27.         $this->path_remote_result = $remote_path.'/files/'.$this->extension_result .'/';
  28.     }
  29.     public function isOnline()
  30.     {
  31.         try {
  32.             $timeout = 5;
  33.             $socket = fsockopen($this->host,$this->socket_port,$errnum,$errstr,$timeout) ;
  34.             
  35.             if(!is_resource($socket)) {
  36.                 return false;
  37.             }
  38.             else {
  39.                 fclose($socket);
  40.                 return true;
  41.             }
  42.         }
  43.         catch(\Exception $e) {
  44.             return false;
  45.         }
  46.     }
  47.     public function getStatus() {
  48.         if ($this->status())
  49.             return 'Online';
  50.         else
  51.             return 'Offline';
  52.     }
  53.     public function status() {
  54.         $command = 'ps -p '.$this->getPID();
  55.         exec($command,$op);
  56.         if (!isset($op[1]))
  57.             return false;
  58.         else
  59.             return true;
  60.     }
  61.     public function getPID() {
  62.         $command = 'cat '.$this->path.'/run/server.pid';
  63.         exec($command,$op);
  64.         if (!isset($op[0]))
  65.             return 0;
  66.         else
  67.             return (int)$op[0];
  68.     }
  69.     public function start() {
  70.         if (!$this->isOnline()) {
  71.             $command = $this->path.'/bin/startup.sh';
  72.             exec($command);
  73.             $this->addLog('Servicio ha iniciado');
  74.             return true;
  75.         } else {
  76.             $this->addLog('Servicio ya iniciado');
  77.             return true;
  78.         }
  79.     }
  80.     public function stop(){
  81.         if($this->getPID() == 0) {
  82.             $this->addLog('Servicio ya esta detenido');
  83.             return false;
  84.         } else {
  85.             $command = $this->path.'/bin/shutdown.sh';
  86.             exec($command);
  87.             if ($this->status() == false) {
  88.                 $this->addLog('Se ha detenido el servicio');
  89.                 return true;
  90.             } else {
  91.                 $this->addLog('ERROR: {Stop} fail! ');
  92.                 return false;
  93.             }
  94.         }
  95.     }
  96.     public function getTailLogFile($lineas = 100)
  97.     {
  98.         try {
  99.             $archivo = $this->path. '/log/server.log';
  100.             $command = 'tail -n '.$lineas.' '.$archivo;
  101.             exec($command,$op);
  102.             return $op;
  103.         }
  104.         catch(\Exception $e) {
  105.             return array('log files esta con errores.', $e->getMessage());
  106.         }
  107.     }
  108.     /**
  109.      * Run and Return file
  110.      *
  111.      * @param string $program
  112.      * @param string $flag
  113.      * @param unknown_type $data
  114.      * @return file
  115.      */
  116.     public function runAndReturnFile($program, $flag = 'flag', $srcData)
  117.     {
  118.         return $this->run($program, $flag, $srcData);
  119.     }
  120.     /**
  121.      * Run and Return Data
  122.      *
  123.      * @param string $program
  124.      * @param string $flag
  125.      * @param unknown_type $data
  126.      * @return string data
  127.      */
  128.     public function runAndReturnData($program, $flag = 'flag', $srcData)
  129.     {
  130.         $file = $this->run($program, $flag, $srcData);
  131.         $data_file = file_get_contents($file);
  132.         return $data_file;
  133.     }
  134.     private function generateIdForFile()
  135.     {
  136.         return  date('dmy_His') . "_" . rand(100,999);
  137.     }
  138.     private function generateLocalFile($data, $flag, $send_file)
  139.     {
  140.         $id      = $this->generateIdForFile();
  141.         $id_file = $id.'_'.$flag;
  142.         $name_file = $id_file.'.'.$this->extension_query;
  143.         if($send_file == true) {
  144.             $file = fopen($this->path_local_query . $name_file,'w');
  145.             $data = mb_convert_encoding($data, "ISO-8859-15", "UTF-8");
  146.             fwrite($file,$data);
  147.             fclose($file);
  148.         }
  149.         return $id_file;
  150.     }
  151.     private function getCommandForSendFile($id_file)
  152.     {
  153.         $cmd  = "copy::send::";
  154.         $cmd .= $this->path_local_query  . $id_file . '.' . $this->extension_query;
  155.         $cmd .= "::";
  156.         $cmd .= $this->path_remote_query . $id_file . '.' . $this->extension_query;
  157.         $cmd .= "\n";
  158.         return $cmd;
  159.     }
  160.     private function getCommandForGetFile($id_file)
  161.     {
  162.         $cmd  = "copy::get::";
  163.         $cmd .= $this->path_remote_result . $id_file . '.' . $this->extension_result;
  164.         $cmd .= "::";
  165.         $cmd .= $this->path_local_result  . $id_file . '.' . $this->extension_result;
  166.         $cmd .= "\n";
  167.         return $cmd;
  168.     }
  169.     private function getCommandForExec($exec)
  170.     {
  171.         $cmd  = "exec::";
  172.         $cmd .= $exec;
  173.         $cmd .= "\n";
  174.         return $cmd;
  175.     }
  176.     private function getCommandForRemoteDelete($id_file)
  177.     {
  178.         $cmd  = "dlte::";
  179.         $cmd .= $id_file;
  180.         $cmd .= "\n";
  181.         return $cmd;
  182.     }
  183.     private function run($program = null, $flag, $srcData = null, $send_file = true, $more_data = null)
  184.     {
  185.         if($program == null) {
  186.             throw new Exception("Server::run() -> Program is null");
  187.         }
  188.         $log = "";
  189.         $id_file = $this->generateLocalFile($srcData, $flag, $send_file);
  190.         $comando1 = $this->getCommandForSendFile($id_file);
  191.         $comando2 = null;
  192.         if($more_data != null) {
  193.             $comando2 = $this->getCommandForExec($this->remote_path_files."/apps/".$program." ".$id_file.",".$more_data . "\n");
  194.         }
  195.         else {
  196.             $comando2 = $this->getCommandForExec($this->remote_path_files."/apps/".$program." ".$id_file."\n");
  197.         }
  198.         $comando3 = $this->getCommandForGetFile($id_file);
  199.         $comando4 = $this->getCommandForRemoteDelete($id_file);
  200.         $timeout = 5;
  201.         $socket = fsockopen($this->host,$this->socket_port,$errnum,$errstr,$timeout) ;
  202.         if(!is_resource($socket)) {
  203.             exit("Connection socket fail:: ".$errnum." ".$errstr) ;
  204.         }
  205.         else {
  206.             if($send_file) {
  207.                 fputs($socket, $comando1) ;
  208.                 while(!feof($socket)) {
  209.                     $linea = fgets($socket, 1024);
  210.                     $log .= $linea;
  211.                     if(trim($linea) == "copy::send::ok") {
  212.                         break;
  213.                     }
  214.                     if(trim($linea) == "msg::fin_socket") {
  215.                         break;
  216.                     }
  217.                 }
  218.             }
  219.             fputs($socket, $comando2);
  220.             while(!feof($socket)){
  221.                 $linea = fgets($socket, 1024);
  222.                 $log .= $linea;
  223.                 if(trim($linea) == "exec::ok") {
  224.                     break;
  225.                 }
  226.                 if(trim($linea) == "msg::fin_socket") {
  227.                     break;
  228.                 }
  229.             }
  230.             fputs($socket, $comando3);
  231.             while(!feof($socket)) {
  232.                 $linea = fgets($socket, 1024);
  233.                 $log .= $linea;
  234.                 if(trim($linea) == "copy::get::ok")
  235.                 {
  236.                     break;
  237.                 }
  238.                 if(trim($linea) == "msg::fin_socket")
  239.                 {
  240.                     break;
  241.                 }
  242.             }
  243.             // fputs($socket, $comando4);
  244.             // while(!feof($socket))
  245.             // {
  246.             //     $linea = fgets($socket, 1024);
  247.             //     $log .= $linea;
  248.             //     if(trim($linea) == "delete::ok")
  249.             //     {
  250.             //         break;
  251.             //     }
  252.             //     if(trim($linea) == "msg::fin_socket")
  253.             //     {
  254.             //         break;
  255.             //     }
  256.             // }
  257.         }
  258.         fclose($socket);
  259.         $this->setLog($log);
  260.         return $this->path_local_result . $id_file. '.' . $this->extension_result;
  261.     }
  262.     public function getLog()
  263.     {
  264.         return $this->log;
  265.     }
  266.     private function setLog($msg)
  267.     {
  268.         global $log;
  269.         $log .= $msg . "\n";
  270.     }
  271.     private function addLog($msg)
  272.     {
  273.         $this->log .= $msg . "\n";
  274.     }
  275. }