]> git.mxchange.org Git - hub.git/blob - application/hub/main/statistics/connection/class_ConnectionStatisticsHelper.php
Continued on development of 'hub' project with many refactorings/addings:
[hub.git] / application / hub / main / statistics / connection / class_ConnectionStatisticsHelper.php
1 <?php
2 /**
3  * A helper class for maintaining connection statistics, no instance is
4  * required to use this class.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  * @todo                Find an interface for hub helper
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 class ConnectionStatisticsHelper extends BaseHubSystem {
27         /**
28          * Statistics array
29          */
30         private static $connectionStatistics = array(
31                 // Statistics for TCP connections
32                 'tcp' => array(
33                         // Tried TCP connection attempts
34                         'retry_count' => array(),
35                 ),
36                 // Statistics for UDP connections
37                 'udp' => array(
38                         // Tried UDP connection attempts
39                         'retry_count' => array(),
40                 )
41         );
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Checks wether the retry count has reached a configured limit for given
54          * connection.
55          *
56          * @param       $helperInstance         An instance of a ConnectionHelper class
57          * @return      $isExhausted            Wether the retry count has been reached
58          */
59         public static function isConnectRetryExhausted (ConnectionHelper $helperInstance) {
60                 //* NOISY-DEBUG: */ $helperInstance->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - ENTERED!');
61                 // Construct config entry
62                 $configEntry = $helperInstance->getProtocol() . '_connect_retry_max';
63
64                 // Check it out
65                 $isExhausted = (
66                         (
67                                 isset(self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'])
68                         ) && (
69                                 self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'] >= $helperInstance->getConfigInstance()->getConfigEntry($configEntry)
70                         )
71                 );
72
73                 // Return it
74                 //* NOISY-DEBUG: */ $helperInstance->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ',isExhausted=' . intval($isExhausted) . ' - EXIT!');
75                 return $isExhausted;
76         }
77
78         /**
79          * Increaes connect-retry count for given connection
80          *
81          * @param       $helperInstance         An instance of a ConnectionHelper class
82          * @return      void
83          */
84         public static function increaseConnectRetry (ConnectionHelper $helperInstance) {
85                 //* NOISY-DEBUG: */ $helperInstance->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - ENTERED!');
86                 // Is the counter there
87                 if (!isset(self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'])) {
88                         // First attempt
89                         //* NOISY-DEBUG: */ $helperInstance->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - FIRST!');
90                         self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'] = 1;
91                 } else {
92                         // Next attempt
93                         //* NOISY-DEBUG: */ $helperInstance->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - INCREMENT!');
94                         self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count']++;
95                 }
96
97                 // Create/update 'last_update' for later purging
98                 self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['last_update'] = time();
99         }
100 }
101
102 // [EOF]
103 ?>