3 namespace CoreFramework\Container\Socket;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
9 * A Socket Container class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class SocketContainer extends BaseContainer implements Registerable {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this Container class and prepares it for usage
44 * @param $socketResource A valid socket resource
45 * @param $infoInstance An instance of a ShareableInfo class
46 * @param $packageData Raw package data
47 * @return $containerInstance An instance of this Container class
49 public static final function createSocketContainer ($socketResource, ShareableInfo $infoInstance = NULL, array $packageData = array()) {
51 $containerInstance = new SocketContainer();
53 // Remove unneeded entries
54 unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
55 unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]);
58 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']:socketResource=' . $socketResource . ',packageData='.print_r($packageData, TRUE));
60 // Is the info instance set?
61 if ($infoInstance instanceof ShareableInfo) {
62 // Get listener/helper from info class
63 $listenerInstance = $infoInstance->getListenerInstance();
64 $helperInstance = $infoInstance->getHelperInstance();
67 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance[]=' . gettype($listenerInstance));
69 // Is there a listener instance set?
70 if ($listenerInstance instanceof Listenable) {
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
74 // Set it here for later usage
75 $containerInstance->setListenerInstance($listenerInstance);
76 } elseif ($helperInstance instanceof ConnectionHelper) {
78 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
80 // Set it here for later usage
81 $containerInstance->setHelperInstance($helperInstance);
85 // Set the resource ...
86 $containerInstance->setSocketResource($socketResource);
88 // ... and package data
89 $containerInstance->setPackageData($packageData);
91 // Return the prepared instance
92 return $containerInstance;
96 * Checks whether the given Universal Node Locator matches with the one from package data
98 * @param $unl A Universal Node Locator
99 * @return $matches Whether $address matches with the one from package data
101 public final function ifAddressMatches ($unl) {
102 // Get current package data
103 $packageData = $this->getPackageData();
106 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',packageData=' . print_r($packageData, TRUE));
108 // So, does both match?
109 $matches = ((isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) && ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] === $unl));
116 * Checks whether the given socket matches with stored
118 * @param $socketResource A valid socket resource
119 * @return $matches Whether given socket matches
121 public final function ifSocketResourceMatches ($socketResource) {
123 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
125 // So, does both match?
126 $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));