protected function dataExchange() { $TcpSocketClientConnector = new TcpSocketClientConnector($this->_logger, 'tcp', $this->_synchronization->remote_server_ip, $this->_synchronization->tcp_client_command_port); // send to server $TcpSocketClientConnector->onSentMessage = function ($coming_message) { // $this->_logger->log(' $comming_message '.print_r($coming_message,1)); $answer = 'FALSE'; $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { if ($exchangeItem->init($coming_message)) { $answer = $exchangeItem->clientMessage(); } else { $exchangeItem->getErrors(); $this->_logger->log(' ERRORS ' . print_r($exchangeItem->getErrors(), 1)); } } return $answer; }; $TcpSocketClientConnector->onCloseConnection = function ($serverMessage) { $this->_logger->log(' $serverMessage ' . print_r(@unserialize($serverMessage), 1)); $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { if ($exchangeItem->init($serverMessage)) { if ($exchangeItem->closeConnection()) { return true; } } } return false; }; //comes messages $TcpSocketClientConnector->onReceiveDataMessage = function ($message) { //$this->_logger->log(__METHOD__.' $message : '. print_r($message,1)); $validMessage = "FALSE"; $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { if ($exchangeItem->init($message)) { $validMessage = $exchangeItem->returnReceivedMessage(); } else { $exchangeItem->getErrors(); $this->_logger->log(' ERRORS ' . print_r($exchangeItem->getErrors(), 1)); } } return $validMessage; }; $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { $TcpSocketClientConnector->connect($timeout = 1); $messageToServer = $exchangeItem->clientMessage(); $this->_logger->log(' $messageToServer ' . print_r($messageToServer, 1)); $TcpSocketClientConnector->sendMessage($messageToServer, $timeout = 1); } }
/** * Function returns array of periods. Script is able to generate schedule for these periods only. * * @param int $generationTime * @return array */ public static function getProperPeriods($generationTime) { self::$_logger->log(__METHOD__, array('generationTime' => $generationTime)); $compare_min = null; $cur_hour = date('H', $generationTime); $cur_min = date('i', $generationTime); if ($cur_min >= 0 && $cur_min <= 14) { $compare_min = '00'; } elseif ($cur_min >= 15 && $cur_min <= 29) { $compare_min = '15'; } elseif ($cur_min >= 30 && $cur_min <= 44) { $compare_min = '30'; } if ($cur_min >= 45 && $cur_min <= 59) { $compare_min = '45'; } $cur_time = $cur_hour . ':' . $compare_min; $proper_periods = array(); $scheduler = ['1' => self::generatePeriodArray(1), '5' => self::generatePeriodArray(5), '15' => ['00:00', '00:15', '00:30', '00:45', '01:00', '01:15', '01:30', '01:45', '02:00', '02:15', '02:30', '02:45', '03:00', '03:15', '03:30', '03:45', '04:00', '04:15', '04:30', '04:45', '05:00', '05:15', '05:30', '05:45', '06:00', '06:15', '06:30', '06:45', '07:00', '07:15', '07:30', '07:45', '08:00', '08:15', '08:30', '08:45', '09:00', '09:15', '09:30', '09:45', '10:00', '10:15', '10:30', '10:45', '11:00', '11:15', '11:30', '11:45', '12:00', '12:15', '12:30', '12:45', '13:00', '13:15', '13:30', '13:45', '14:00', '14:15', '14:30', '14:45', '15:00', '15:15', '15:30', '15:45', '16:00', '16:15', '16:30', '16:45', '17:00', '17:15', '17:30', '17:45', '18:00', '18:15', '18:30', '18:45', '19:00', '19:15', '19:30', '19:45', '20:00', '20:15', '20:30', '20:45', '21:00', '21:15', '21:30', '21:45', '22:00', '22:15', '22:30', '22:45', '23:00', '23:15', '23:30', '23:45'], '30' => ['00:00', '00:30', '01:00', '01:30', '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', '06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00', '22:30', '23:00', '23:30'], '60' => ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'], '120' => ['00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '16:00', '18:00', '20:00', '22:00'], '180' => ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'], '360' => ['00:00', '06:00', '12:00', '18:00'], '540' => ['00:00', '09:00', '18:00', '03:00', '12:00', '21:00', '06:00', '15:00'], '720' => ['00:00', '12:00'], '900' => ['00:00', '15:00', '06:00', '21:00', '12:00', '03:00', '18:00', '09:00'], '1080' => ['00:00', '18:00', '12:00', '06:00'], '1440' => ['00:00']]; foreach ($scheduler as $key => $value) { if (in_array($cur_time, $value)) { $proper_periods[] = $key; } } self::$_logger->log(__METHOD__, array('proper_periods' => implode(',', $proper_periods))); return $proper_periods; }
protected function sendReport($sendLogObject) { if (is_object($sendLogObject->report_processed->ex_schedule_report)) { $file_name = $sendLogObject->report_processed->getFileName($sendLogObject->report_processed->ex_schedule_report); $file_path = $sendLogObject->report_processed->full_file_name; if ($sendLogObject->destination->method === 'mail') { $this->logger->log(__METHOD__, array('method' => $sendLogObject->destination->method, 'email' => $sendLogObject->destination->destination_email)); $mail_params = array('actuality_time' => $sendLogObject->created, 'schedule_period' => '', 'report_file_name' => $file_name, 'link' => '', 'report_type' => $sendLogObject->report_processed->ex_schedule_report->report_type); $subject = Yii::t('letter', 'scheduled_report_mail_subject', $mail_params, null, 'en'); $settings = Settings::model()->findByPk(1); $mailSender = new mailSender('odss_reports', array()); $sendResult = $mailSender->setAttachments(array(array('file_path' => $file_path, 'file_name' => $file_name)))->setRecipient($sendLogObject->destination->destination_email)->setFrom($settings->mail__sender_address, $settings->mail__sender_name)->setSubject($subject)->setHtmlBody()->send(); if ($sendResult !== false) { $sendLogObject->sent = 1; $sendLogObject->save(); } $this->logger->log(__METHOD__ . ' $sendResult: ' . $sendResult); $this->logger->log(__METHOD__ . ' Message send with attached file'); $this->logger->log(__METHOD__ . ' Deliver via mail DONE.'); } else { if ($sendLogObject->destination->method === 'ftp') { // use it if you have some superstition about "../" // $fileCopier = new FileCopier; // $file_path = $fileCopier->rmPathSteps($file_path); $ftpClient = new FtpClient(); $errors = $ftpClient->connect($sendLogObject->destination->destination_ip, $sendLogObject->destination->destination_ip_port)->login($sendLogObject->destination->destination_ip_user, $sendLogObject->destination->destination_ip_password)->setFolder($sendLogObject->destination->destination_ip_folder)->openLocalFile($file_path)->upload($file_name)->closeLocalFile()->getErrors(); if (!count($errors)) { $sendLogObject->sent = 1; $sendLogObject->save(); } else { $sendLogObject->send_logs = serialize($errors); $sendLogObject->save(); } $this->logger->log(__METHOD__ . " ftp errors:" . print_r($errors, 1)); $this->logger->log(__METHOD__ . ' Deliver via ftp DONE.'); } else { if ($sendLogObject->destination->method === 'local_folder') { $this->logger->log(__METHOD__, array('report_type' => $sendLogObject->report_processed->ex_schedule_report->report_type, 'method' => $sendLogObject->destination->method, 'destination_folder' => $sendLogObject->destination->destination_local_folder)); $destinationPath = $sendLogObject->report_processed->getFileDir() . DIRECTORY_SEPARATOR . $sendLogObject->destination->destination_local_folder; $this->logger->log(__METHOD__ . ' $file_path: ' . $file_path); $this->logger->log(__METHOD__ . ' $destinationPath: ' . $destinationPath); $this->logger->log(__METHOD__ . ' $file_name: ' . $file_name); $fileCopier = new FileCopier(); $errors = $fileCopier->copy($file_path, $destinationPath . DIRECTORY_SEPARATOR . $file_name)->getErrors(); if (!count($errors)) { $sendLogObject->sent = 1; $sendLogObject->save(); } else { $sendLogObject->send_logs = serialize($errors); $sendLogObject->save(); } $this->logger->log(__METHOD__ . ' errors:' . print_r($errors, 1)); $this->logger->log(__METHOD__ . ' Deliver to local folder DONE.'); } } } } }
protected function pollData($address, $port, $command) { $this->_logger->log(__METHOD__, array('address' => $address, 'port' => $port, 'command' => $command)); $timeout = 6; // !!! PROTOCOL (TCP by default) SHOULD BE IN LOWER CASE !!! $uri = 'tcp' . '://' . $address . ':' . $port; $this->_logger->log(__METHOD__ . ': Trying to connect', array('uri' => $uri, 'timeout' => $timeout)); // creates socket connection with IP:port $client = @stream_socket_client($uri, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT); if ($client === false) { $this->_logger->log(__METHOD__ . ': Client connect error', array('error' => $errstr, 'number' => $errno)); return false; } @stream_set_timeout($client, $timeout); $this->_logger->log(__METHOD__ . ': Sending command', array('command' => $command)); $status = @stream_socket_sendto($client, $command . "\n"); if ($status === false) { $erroInfo = $this->getSocketErrorInfo($client); $this->_logger->log(__METHOD__ . ': Error on sending command', array('error' => $erroInfo['error'], 'number' => $erroInfo['number'])); @stream_socket_shutdown($client, STREAM_SHUT_RDWR); return false; } $this->_logger->log(__METHOD__ . ': Sent result', array('Bytes sent' => $status)); //$time = 0; $line = null; $message = ''; $attempt = 1; $this->_logger->log(__METHOD__ . ': Starting to read response.'); // Wait for message end while (strpos($line, '$') === false) { $line = @fread($client, 1024); $this->_logger->log(__METHOD__, array('read line' => $line)); if (strlen($line) === 0) { $this->_logger->log(__METHOD__ . ': Reading timeout exceeded.', array('attempt' => $attempt)); if ($attempt > 2) { $this->_logger->log(__METHOD__ . ': Exceeded maximun attempt number.'); @stream_socket_shutdown($client, STREAM_SHUT_RDWR); return false; } } $message .= $line; $attempt++; } @stream_socket_shutdown($client, STREAM_SHUT_RDWR); $this->_logger->log(__METHOD__, array('return' => $message)); return $message; }
public function dataExchange() { $TcpSocketServerConnector = new TcpSocketServerConnector($this->_logger, 'tcp', $this->_synchronization->server_ip, $this->_synchronization->tcp_server_command_port); $TcpSocketServerConnector->loadData($null); // send answer to client $TcpSocketServerConnector->onSentMessage = function ($coming_message) { $this->_logger->log(' $comming_message ' . print_r($coming_message, 1)); $answer = 'FALSE'; $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { // if ($exchangeItem->init($coming_message)) { $answer = $exchangeItem->serverMessage(); } else { $exchangeItem->getErrors(); $this->_logger->log(' ERRORS ' . print_r($exchangeItem->getErrors(), 1)); } } $this->_logger->log(' $answer ' . print_r($answer, 1)); return $answer; }; $TcpSocketServerConnector->onCloseConnection = function ($clientMessage) { $this->_logger->log(' $clientMessage ' . print_r(@unserialize($clientMessage), 1)); $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { // if ($exchangeItem->init($clientMessage)) { if ($exchangeItem->closeConnection()) { return true; } } } return false; }; //comes messages $TcpSocketServerConnector->onReceiveDataMessage = function ($message) { $this->_logger->log(' $message : ' . print_r($message, 1)); $validMessage = "FALSE"; $exchangeArray = array(ExchangeODSS::getInstance()); foreach ($exchangeArray as &$exchangeItem) { if ($exchangeItem->init($message)) { $validMessage = $exchangeItem->returnReceivedMessage(); } else { $exchangeItem->getErrors(); $this->_logger->log(' onReceiveDataMessage ERRORS ' . print_r($exchangeItem->getErrors(), 1)); } } return $validMessage; }; $TcpSocketServerConnector->readData($message); }
/** * * @param string $method * @param type $result * @return type */ protected function returnResult($method, $result) { $this->_logger->log($method, array('Return value' => $result)); return $result; }