Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / 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@shipsimu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.shipsimu.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          * @TODO Add more protocols to use
30          */
31         private static $connectionStatistics = array(
32                 // Statistics for TCP connections
33                 'tcp' => array(
34                         // Tried TCP connection attempts
35                         'retry_count' => array(),
36                 ),
37                 // Statistics for UDP connections
38                 'udp' => array(
39                         // Tried UDP connection attempts
40                         'retry_count' => array(),
41                 )
42         );
43
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         protected function __construct () {
50                 parent::__construct(__CLASS__);
51         }
52
53         /**
54          * Checks whether the retry count has reached a configured limit for given
55          * connection.
56          *
57          * @param       $helperInstance         An instance of a ConnectionHelper class
58          * @return      $isExhausted            Whether the retry count has been reached
59          */
60         public static function isConnectRetryExhausted (ConnectionHelper $helperInstance) {
61                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - CALLED!');
62                 // Construct config entry
63                 $configEntry = $helperInstance->getProtocolName() . '_connect_retry_max';
64
65                 // Check it out
66                 $isExhausted = (
67                         (
68                                 isset(self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'])
69                         ) && (
70                                 self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'] >= $helperInstance->getConfigInstance()->getConfigEntry($configEntry)
71                         )
72                 );
73
74                 // Return it
75                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ',isExhausted=' . intval($isExhausted) . ' - EXIT!');
76                 return $isExhausted;
77         }
78
79         /**
80          * Increaes connect-retry count for given connection
81          *
82          * @param       $helperInstance         An instance of a ConnectionHelper class
83          * @return      void
84          */
85         public static function increaseConnectRetry (ConnectionHelper $helperInstance) {
86                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - CALLED!');
87                 // Is the counter there
88                 if (!isset(self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'])) {
89                         // First attempt
90                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - FIRST!');
91                         self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'] = 1;
92                 } else {
93                         // Next attempt
94                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - INCREMENT!');
95                         self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count']++;
96                 }
97
98                 // Create/update 'last_update' for purging
99                 // @TODO last_update is not being used at the moment
100                 self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['last_update'] = time();
101         }
102 }
103
104 // [EOF]
105 ?>