* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class HubTools extends BaseFrameworkSystem {
+ // Constants for exceptions
+ const EXCEPTION_SESSION_ID_IS_INVALID = 0x200;
+
+ /**
+ * Cache for session ids
+ */
+ private $sessionIdCache = array();
+
+ /**
+ * Length for session id (should be 32+salt_length
+ */
+ private $sessionIdLength = 0;
+
+ /**
+ * Self instance
+ */
+ private static $selfInstance = null;
+
/**
* Protected constructor
*
protected function __construct () {
// Call parent constructor
parent::__construct(__CLASS__);
+
+ // Init salt length
+ $this->sessionIdLength = 32 + $this->getConfigInstance()->getConfigEntry('salt_length');
}
/**
- * Resolves given session id into a ip:port string, if ip:port is set, it won't be translated
+ * Singleton getter for self instance
*
- * @param $sessionId Session id or ip:port string
- * @return $recipient Recipient as ip:port string
+ * @retuen $selfInstance An instance of this class
+ */
+ public final static function getInstance () {
+ // Is the instance set
+ if (is_null(self::$selfInstance)) {
+ // Then set it
+ self::$selfInstance = new HubTools();
+ } // END - if
+
+ // Return own instance
+ return self::$selfInstance;
+ }
+
+ /**
+ * Getter for session id length
+ *
+ * @return $sessionIdLength Length of session ids
+ */
+ protected final function getSessionIdLength () {
+ return $this->sessionIdLength;
+ }
+
+ /**
+ * Resolves a session id into an ip:port combination
+ *
+ * @param $sessionId A valid session id
+ * @return $recipient Recipient as ip:port combination
+ */
+ protected function resolveIpPortBySessionId ($sessionId) {
+ // Get a wrapper instance
+ $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
+
+ // And ask it for the session id
+ $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId);
+
+ // Return result
+ return $recipient;
+ }
+
+ /**
+ * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated
+ *
+ * @param $sessionId Session id or ip:port combination
+ * @return $recipient Recipient as ip:port combination
+ * @throws InvalidSessionIdException If the provided session id is invalid (and no ip:port combination)
*/
public static function resolveSessionId ($sessionId) {
+ // Get an own instance
+ $selfInstance = self::getInstance();
+
// Default is direct ip:port
$recipient = $sessionId;
// Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression)
if (!preg_match('/((?:2[0-5]{2}|1\d{2}|[1-9]\d|[1-9])\.(?:(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)\.){2}(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)):(\d|[1-9]\d|[1-9]\d{2,3}|[1-5]\d{4}|6[0-4]\d{3}|654\d{2}|655[0-2]\d|6553[0-5])/', $sessionId)) {
- die(__METHOD__.': sessionId=' . $sessionId . "\n");
+ // Is it in cache?
+ if (isset($selfInstance->sessionIdCache[$sessionId])) {
+ // Then use it
+ $recipient = $selfInstance->sessionIdCache[$sessionId];
+ } elseif (!preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) {
+ // Invalid session id
+ throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);
+ } else {
+ // Resolve it here
+ $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
+ }
} // END - if
+ // Output message
+ $selfInstance->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient);
+
// Return it
return $recipient;
}