3 namespace CoreFramework\Container\Socket;
6 * A Socket Container class
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class SocketContainer extends BaseContainer implements Registerable {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
39 * Creates an instance of this Container class and prepares it for usage
41 * @param $socketResource A valid socket resource
42 * @param $infoInstance An instance of a ShareableInfo class
43 * @param $packageData Raw package data
44 * @return $containerInstance An instance of this Container class
46 public static final function createSocketContainer ($socketResource, ShareableInfo $infoInstance = NULL, array $packageData = array()) {
48 $containerInstance = new SocketContainer();
50 // Remove unneeded entries
51 unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
52 unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]);
55 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']:socketResource=' . $socketResource . ',packageData='.print_r($packageData, TRUE));
57 // Is the info instance set?
58 if ($infoInstance instanceof ShareableInfo) {
59 // Get listener/helper from info class
60 $listenerInstance = $infoInstance->getListenerInstance();
61 $helperInstance = $infoInstance->getHelperInstance();
64 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance[]=' . gettype($listenerInstance));
66 // Is there a listener instance set?
67 if ($listenerInstance instanceof Listenable) {
69 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
71 // Set it here for later usage
72 $containerInstance->setListenerInstance($listenerInstance);
73 } elseif ($helperInstance instanceof ConnectionHelper) {
75 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
77 // Set it here for later usage
78 $containerInstance->setHelperInstance($helperInstance);
82 // Set the resource ...
83 $containerInstance->setSocketResource($socketResource);
85 // ... and package data
86 $containerInstance->setPackageData($packageData);
88 // Return the prepared instance
89 return $containerInstance;
93 * Checks whether the given Universal Node Locator matches with the one from package data
95 * @param $unl A Universal Node Locator
96 * @return $matches Whether $address matches with the one from package data
98 public final function ifAddressMatches ($unl) {
99 // Get current package data
100 $packageData = $this->getPackageData();
103 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',packageData=' . print_r($packageData, TRUE));
105 // So, does both match?
106 $matches = ((isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) && ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] === $unl));
113 * Checks whether the given socket matches with stored
115 * @param $socketResource A valid socket resource
116 * @return $matches Whether given socket matches
118 public final function ifSocketResourceMatches ($socketResource) {
120 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
122 // So, does both match?
123 $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));