3 namespace CoreFramework\Container\Socket;
5 // Import framework stuff
6 use CoreFramework\Listener\Listenable;
7 use CoreFramework\Registry\Registerable;
10 * A Socket Container class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class SocketContainer extends BaseContainer implements Registerable {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this Container class and prepares it for usage
45 * @param $socketResource A valid socket resource
46 * @param $infoInstance An instance of a ShareableInfo class
47 * @param $packageData Raw package data
48 * @return $containerInstance An instance of this Container class
50 public static final function createSocketContainer ($socketResource, ShareableInfo $infoInstance = NULL, array $packageData = array()) {
52 $containerInstance = new SocketContainer();
54 // Remove unneeded entries
55 unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
56 unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]);
59 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']:socketResource=' . $socketResource . ',packageData='.print_r($packageData, TRUE));
61 // Is the info instance set?
62 if ($infoInstance instanceof ShareableInfo) {
63 // Get listener/helper from info class
64 $listenerInstance = $infoInstance->getListenerInstance();
65 $helperInstance = $infoInstance->getHelperInstance();
68 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance[]=' . gettype($listenerInstance));
70 // Is there a listener instance set?
71 if ($listenerInstance instanceof Listenable) {
73 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
75 // Set it here for later usage
76 $containerInstance->setListenerInstance($listenerInstance);
77 } elseif ($helperInstance instanceof ConnectionHelper) {
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
81 // Set it here for later usage
82 $containerInstance->setHelperInstance($helperInstance);
86 // Set the resource ...
87 $containerInstance->setSocketResource($socketResource);
89 // ... and package data
90 $containerInstance->setPackageData($packageData);
92 // Return the prepared instance
93 return $containerInstance;
97 * Checks whether the given Universal Node Locator matches with the one from package data
99 * @param $unl A Universal Node Locator
100 * @return $matches Whether $address matches with the one from package data
102 public final function ifAddressMatches ($unl) {
103 // Get current package data
104 $packageData = $this->getPackageData();
107 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',packageData=' . print_r($packageData, TRUE));
109 // So, does both match?
110 $matches = ((isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) && ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] === $unl));
117 * Checks whether the given socket matches with stored
119 * @param $socketResource A valid socket resource
120 * @return $matches Whether given socket matches
122 public final function ifSocketResourceMatches ($socketResource) {
124 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
126 // So, does both match?
127 $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));