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