]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/protocol/class_BaseProtocolHandler.php
Continued rewrite:
[hub.git] / application / hub / main / handler / protocol / class_BaseProtocolHandler.php
1 <?php
2 /**
3  * A general handler for protocols such as TCP, UDP and others.
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseProtocolHandler extends BaseHandler {
25         /**
26          * Whole UNL data array
27          */
28         private $universalNodeLocatorData = array();
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Name of the class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39         }
40
41         /**
42          * Setter for UNL data array to satify HandleableProtocol
43          *
44          * @para        $unlData        The UNL data array
45          * @return      void
46          */
47         protected final function setUniversalNodeLocatorData (array $unlData) {
48                 // Set new UNL data array
49                 $this->universalNodeLocatorData = $unlData;
50         }
51
52         /**
53          * Getter for UNL data array to satify HandleableProtocol
54          *
55          * @return      $unlData        The UNL data array
56          */
57         public final function getUniversalNodeLocatorData () {
58                 // Return UNL data array
59                 return $this->universalNodeLocatorData;
60         }
61
62         /**
63          * Validates given UNL very basicly by given regular expression. You
64          * normally don't need/want to overwrite this method as this is a very basic
65          * validation only based on a regex.
66          *
67          * @param       $unl            Universal Node Locator to validate
68          * @return      $isValid        Whether the UNL is valid
69          */
70         protected final function isValidUniversalNodeLocator ($unl) {
71                 // Debug message
72                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',regex=' . $regex . ' - CALLED!');
73
74                 // Very basic regex check
75                 $isValid = (preg_match($this->getRegularExpression(), $unl) === 1);
76
77                 // Return result
78                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
79                 return $isValid;
80         }
81
82         /**
83          * Parses the given UNL by splitting it up in its components. The UNL ...
84          *
85          * protocol://address[:port]
86          *
87          * ... becomes:
88          *
89          * array(
90          *     'protocol' => 'value',
91          *     'address'  => 'value',
92          *     'extra'    => 'port'
93          * )
94          *
95          * The value for 'extra' then must be handled by parseUniversalNodeLocator()
96          * of the individual protocol handler as this is protocol-specific.
97          *
98          * @param       $unl            Universal Node Locator (UNL) to "parse"
99          * @return      $unlData        Array with all components of the UNL
100          */
101         protected function parseGenericUniversalNodeLocator ($unl) {
102                 // Debug message
103                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
104
105                 // Make sure the UNL is valid
106                 assert($this->isValidUniversalNodeLocator($unl));
107
108                 /*
109                  * "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-(
110                  * @TODO If you know why, please fix and explain it to me.
111                  */
112                 $unlParts = explode('://', $unl);
113
114                 // Split again the last part as: address:port
115                 $unlParts[1] = explode(':', $unlParts[1]);
116
117                 // Now there is an almost useable array which then can be copied to the "real" array.
118                 $unlData = array(
119                         UniversalNodeLocator::UNL_PART_PROTOCOL => $unlParts[0],
120                         UniversalNodeLocator::UNL_PART_ADDRESS  => $unlParts[1][0],
121                         UniversalNodeLocator::UNL_PART_EXTRA    => $unlParts[1][1]
122                 );
123
124                 // Debug message
125                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
126
127                 // Return the generic array
128                 return $unlData;
129         }
130 }
131
132 // [EOF]
133 ?>