]> git.mxchange.org Git - hub.git/blobdiff - application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php
Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / statistics / connection / class_ConnectionStatisticsHelper.php
diff --git a/application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php b/application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php
new file mode 100644 (file)
index 0000000..a624e8d
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+/**
+ * A helper class for maintaining connection statistics, no instance is
+ * required to use this class.
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ * @todo               Find an interface for hub helper
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class ConnectionStatisticsHelper extends BaseHubSystem {
+       /**
+        * Statistics array
+        * @TODO Add more protocols to use
+        */
+       private static $connectionStatistics = array(
+               // Statistics for TCP connections
+               'tcp' => array(
+                       // Tried TCP connection attempts
+                       'retry_count' => array(),
+               ),
+               // Statistics for UDP connections
+               'udp' => array(
+                       // Tried UDP connection attempts
+                       'retry_count' => array(),
+               )
+       );
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Checks whether the retry count has reached a configured limit for given
+        * connection.
+        *
+        * @param       $helperInstance         An instance of a ConnectionHelper class
+        * @return      $isExhausted            Whether the retry count has been reached
+        */
+       public static function isConnectRetryExhausted (ConnectionHelper $helperInstance) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - CALLED!');
+               // Construct config entry
+               $configEntry = $helperInstance->getProtocolName() . '_connect_retry_max';
+
+               // Check it out
+               $isExhausted = (
+                       (
+                               isset(self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'])
+                       ) && (
+                               self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'] >= $helperInstance->getConfigInstance()->getConfigEntry($configEntry)
+                       )
+               );
+
+               // Return it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ',isExhausted=' . intval($isExhausted) . ' - EXIT!');
+               return $isExhausted;
+       }
+
+       /**
+        * Increaes connect-retry count for given connection
+        *
+        * @param       $helperInstance         An instance of a ConnectionHelper class
+        * @return      void
+        */
+       public static function increaseConnectRetry (ConnectionHelper $helperInstance) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - CALLED!');
+               // Is the counter there
+               if (!isset(self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'])) {
+                       // First attempt
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - FIRST!');
+                       self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count'] = 1;
+               } else {
+                       // Next attempt
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-STATISTICS[' . __METHOD__ . ':' . __LINE__ . ']: helperInstance=' . $helperInstance->__toString() . ' - INCREMENT!');
+                       self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['retry_count']++;
+               }
+
+               // Create/update 'last_update' for purging
+               // @TODO last_update is not being used at the moment
+               self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['last_update'] = time();
+       }
+}
+
+// [EOF]
+?>