Commit | Line | Data |
---|---|---|
b5c0ad9a TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
b5c0ad9a | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
b5c0ad9a TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
b5c0ad9a TO |
27 | |
28 | /** | |
b5c0ad9a | 29 | * @package CRM |
f299f7db | 30 | * @copyright CiviCRM LLC (c) 2004-2020 |
b5c0ad9a TO |
31 | */ |
32 | ||
33 | /** | |
34 | * Simple static helpers for network operations | |
35 | */ | |
36 | class CRM_Utils_Network { | |
6714d8d2 | 37 | |
b5c0ad9a TO |
38 | /** |
39 | * Try connecting to a TCP service; if it fails, retry. Repeat until serverStartupTimeOut elapses. | |
40 | * | |
f4aaa82a EM |
41 | * @param $host |
42 | * @param $port | |
77855840 TO |
43 | * @param int $serverStartupTimeOut |
44 | * Seconds. | |
45 | * @param float $interval | |
46 | * Seconds to wait in between pollings. | |
f4aaa82a | 47 | * |
a6c01b45 CW |
48 | * @return bool |
49 | * TRUE if service is online | |
b5c0ad9a TO |
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 | /** | |
3bdf1f3a | 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 | |
b5c0ad9a TO |
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 | } | |
0db6c3e1 TO |
86 | } |
87 | catch (Exception $e) { | |
b5c0ad9a TO |
88 | } |
89 | error_reporting($old_error_reporting); | |
90 | return FALSE; | |
91 | } | |
96025800 | 92 | |
232624b1 | 93 | } |