X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fclasses%2Fdiscovery%2Fprotocol%2Fclass_ProtocolDiscovery.php;fp=application%2Fhub%2Fclasses%2Fdiscovery%2Fprotocol%2Fclass_ProtocolDiscovery.php;h=82ac8c693256feefb5f09ec665bc420dfe8155f1;hb=f3400989268a1994de44909e5d968cabf72de5b1;hp=0000000000000000000000000000000000000000;hpb=73aff29b9bc78031853b0b8c0fe0a8e04f66ac29;p=hub.git diff --git a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php new file mode 100644 index 000000000..82ac8c693 --- /dev/null +++ b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php @@ -0,0 +1,100 @@ + + * @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 + * + * 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 . + */ +class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtocol, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Create an instance of this class + * + * @return $discoveryInstance An instance of this discovery class + */ + public static final function createProtocolDiscovery () { + // Get an instance of this class + $discoveryInstance = new ProtocolDiscovery(); + + // Return the prepared instance + return $discoveryInstance; + } + + /** + * Determines the protoctol name + * + * @param $packageData Valid package data + * @return $protocolInstance An instance of a HandleableProtocol class + */ + public static final function determineProtocolByPackageData (array $packageData) { + // First we need a tags instance + $tagsInstance = PackageTagsFactory::createPackageTagsInstance(); + + /* + * We need to decide here which socket (TCP or UDP) should be used for + * the actual data transmission. In this process we will find out if + * the recipient of this package has already a known (registered) socket + * and if so we can re-use it. If there is no socket registered, we try + * to make a new connection to the given Universal Node Locator. + */ + $protocolInstance = $tagsInstance->chooseProtocolFromPackageData($packageData); + + // Return it + return $protocolInstance; + } + + /** + * "Discovers" the protocol type from given raw package data. This is done + * by looking at the 'recipient' field and extract the first part from it. + * + * @param $packageData Raw package data + * @return $protocolType Type of protocol, e.g. 'tcp' for TCP/IPv4 connections + */ + public static final function discoverProtocolByPackageData (array $packageData) { + //* DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); + + /* + * "Explode" the 'recipient' array element into a new one, giving at + * least two entries: protocol://address + */ + $recipient = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + + // At least 2 entries must be found + assert(count($recipient) >= 2); + + // Now get the first part (protocol type) and make all lower-case + $protocolType = strtolower($recipient[0]); + + // Return it + // @TODO Add some validation here??? + return $protocolType; + } +} + +// [EOF] +?>