]> git.mxchange.org Git - hub.git/blob - application/hub/main/statistics/connection/class_ConnectionStatisticsHelper.php
Hub project continued:
[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 connection attempts
34                         'retry_count' => array(),
35                 ),
36                 // Statistics for UDP connections
37                 'udp' => array(
38                         'retry_count' => array(),
39                 )
40         );
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 parent::__construct(__CLASS__);
49         }
50
51         /**
52          * Checks wether the retry count has reached a configured limit for given
53          * connection.
54          *
55          * @param       $helperInstance         An instance of a ConnectionHelper class
56          * @return      $isExhausted            Wether the retry count has been reached
57          */
58         public static function isConnectRetryExhausted (ConnectionHelper $helperInstance) {
59                 //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - ENTERED!');
60                 // Construct config entry
61                 $configEntry = $helperInstance->getProtocol() . '_connect_retry_max';
62
63                 // Check it out
64                 $isExhausted = (
65                         (
66                                 isset(self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'])
67                         ) && (
68                                 self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'] >= $helperInstance->getConfigInstance()->getConfigEntry($configEntry)
69                         )
70                 );
71
72                 // Return it
73                 //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ',isExhausted=' . intval($isExhausted) . ' - EXIT!');
74                 return $isExhausted;
75         }
76
77         /**
78          * Increaes connect-retry count for given connection
79          *
80          * @param       $helperInstance         An instance of a ConnectionHelper class
81          * @return      void
82          */
83         public static function increaseConnectRetry (ConnectionHelper $helperInstance) {
84                 //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - ENTERED!');
85                 // Is the counter there
86                 if (!isset(self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'])) {
87                         // First attempt
88                         //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - FIRST!');
89                         self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'] = 1;
90                 } else {
91                         // Next attempt
92                         //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - INCREMENT!');
93                         self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count']++;
94                 }
95
96                 // Create/update 'last_update' for later purging
97                 self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['last_update'] = time();
98         }
99 }
100
101 // [EOF]
102 ?>