Merge pull request #15816 from MiyaNoctem/dev-core-1383-fix-reinstallation-of-extensi...
[civicrm-core.git] / CRM / Utils / Network.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2019
31 */
32
33 /**
34 * Simple static helpers for network operations
35 */
36 class CRM_Utils_Network {
37
38 /**
39 * Try connecting to a TCP service; if it fails, retry. Repeat until serverStartupTimeOut elapses.
40 *
41 * @param $host
42 * @param $port
43 * @param int $serverStartupTimeOut
44 * Seconds.
45 * @param float $interval
46 * Seconds to wait in between pollings.
47 *
48 * @return bool
49 * TRUE if service is online
50 */
51 public static function waitForServiceStartup($host, $port, $serverStartupTimeOut, $interval = 0.333) {
52 $start = time();
53 $end = $start + $serverStartupTimeOut;
54 $found = FALSE;
55 $interval_usec = (int) 1000000 * $interval;
56
57 while (!$found && $end >= time()) {
58 $found = self::checkService($host, $port, $end - time());
59 if ($found) {
60 return TRUE;
61 }
62 usleep($interval_usec);
63 }
64 return FALSE;
65 }
66
67 /**
68 * Check whether a TCP service is available on $host and $port.
69 *
70 * @param string $host
71 * @param string $port
72 * @param string $serverConnectionTimeOut
73 *
74 * @return bool
75 */
76 public static function checkService($host, $port, $serverConnectionTimeOut) {
77 $old_error_reporting = error_reporting();
78 error_reporting($old_error_reporting & ~E_WARNING);
79 try {
80 $fh = fsockopen($host, $port, $errno, $errstr, $serverConnectionTimeOut);
81 if ($fh) {
82 fclose($fh);
83 error_reporting($old_error_reporting);
84 return TRUE;
85 }
86 }
87 catch (Exception $e) {
88 }
89 error_reporting($old_error_reporting);
90 return FALSE;
91 }
92
93 }