]> git.mxchange.org Git - hub.git/blob - application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php
Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / discovery / protocol / class_ProtocolDiscovery.php
1 <?php
2 /**
3  * A Protocol discovery class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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 ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtocol, Registerable {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Create an instance of this class
37          *
38          * @return      $discoveryInstance      An instance of this discovery class
39          */
40         public static final function createProtocolDiscovery () {
41                 // Get an instance of this class
42                 $discoveryInstance = new ProtocolDiscovery();
43
44                 // Return the prepared instance
45                 return $discoveryInstance;
46         }
47
48         /**
49          * Determines the protoctol name
50          *
51          * @param       $packageData            Valid package data
52          * @return      $protocolInstance       An instance of a HandleableProtocol class
53          */
54         public static final function determineProtocolByPackageData (array $packageData) {
55                 // First we need a tags instance
56                 $tagsInstance = PackageTagsFactory::createPackageTagsInstance();
57
58                 /*
59                  * We need to decide here which socket (TCP or UDP) should be used for
60                  * the actual data transmission. In this process we will find out if
61                  * the recipient of this package has already a known (registered) socket
62                  * and if so we can re-use it. If there is no socket registered, we try
63                  * to make a new connection to the given Universal Node Locator.
64                  */
65                 $protocolInstance = $tagsInstance->chooseProtocolFromPackageData($packageData);
66
67                 // Return it
68                 return $protocolInstance;
69         }
70
71         /**
72          * "Discovers" the protocol type from given raw package data. This is done
73          * by looking at the 'recipient' field and extract the first part from it.
74          *
75          * @param       $packageData    Raw package data
76          * @return      $protocolType   Type of protocol, e.g. 'tcp' for TCP/IPv4 connections
77          */
78         public static final function discoverProtocolByPackageData (array $packageData) {
79                 //* DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
80
81                 /*
82                  * "Explode" the 'recipient' array element into a new one, giving at
83                  * least two entries: protocol://address
84                  */
85                 $recipient = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
86
87                 // At least 2 entries must be found
88                 assert(count($recipient) >= 2);
89
90                 // Now get the first part (protocol type) and make all lower-case
91                 $protocolType = strtolower($recipient[0]);
92
93                 // Return it
94                 // @TODO Add some validation here???
95                 return $protocolType;
96         }
97 }
98
99 // [EOF]
100 ?>