3 * A Protocol discovery class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
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.
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.
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/>.
24 class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtocol, Registerable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Create an instance of this class
38 * @return $discoveryInstance An instance of this discovery class
40 public static final function createProtocolDiscovery () {
41 // Get an instance of this class
42 $discoveryInstance = new ProtocolDiscovery();
44 // Return the prepared instance
45 return $discoveryInstance;
49 * Determines the protoctol name
51 * @param $packageData Valid package data
52 * @return $protocolInstance An instance of a HandleableProtocol class
54 public static final function determineProtocolByPackageData (array $packageData) {
55 // First we need a tags instance
56 $tagsInstance = PackageTagsFactory::createPackageTagsInstance();
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.
65 $protocolInstance = $tagsInstance->chooseProtocolFromPackageData($packageData);
68 return $protocolInstance;
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.
75 * @param $packageData Raw package data
76 * @return $protocolType Type of protocol, e.g. 'tcp' for TCP/IPv4 connections
78 public static final function discoverProtocolByPackageData (array $packageData) {
79 //* DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
82 * "Explode" the 'recipient' array element into a new one, giving at
83 * least two entries: protocol://address
85 $recipient = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
87 // At least 2 entries must be found
88 assert(count($recipient) >= 2);
90 // Now get the first part (protocol type) and make all lower-case
91 $protocolType = strtolower($recipient[0]);
94 // @TODO Add some validation here???