Continued:
[core.git] / inc / main / classes / container / socket / class_SocketContainer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Container\Socket;
4
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7
8 /**
9  * A Socket Container class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
30 class SocketContainer extends BaseContainer implements Registerable {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this Container class and prepares it for usage
43          *
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
48          */
49         public static final function createSocketContainer ($socketResource, ShareableInfo $infoInstance = NULL, array $packageData = array()) {
50                 // Get a new instance
51                 $containerInstance = new SocketContainer();
52
53                 // Remove unneeded entries
54                 unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
55                 unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]);
56
57                 // Debug message
58                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']:socketResource=' . $socketResource . ',packageData='.print_r($packageData, TRUE));
59
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();
65
66                         // Debug message
67                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance[]=' . gettype($listenerInstance));
68
69                         // Is there a listener instance set?
70                         if ($listenerInstance instanceof Listenable) {
71                                 // Debug message
72                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
73
74                                 // Set it here for later usage
75                                 $containerInstance->setListenerInstance($listenerInstance);
76                         } elseif ($helperInstance instanceof ConnectionHelper) {
77                                 // Debug message
78                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
79
80                                 // Set it here for later usage
81                                 $containerInstance->setHelperInstance($helperInstance);
82                         }
83                 } // END - if
84
85                 // Set the resource ...
86                 $containerInstance->setSocketResource($socketResource);
87
88                 // ... and package data
89                 $containerInstance->setPackageData($packageData);
90
91                 // Return the prepared instance
92                 return $containerInstance;
93         }
94
95         /**
96          * Checks whether the given Universal Node Locator matches with the one from package data
97          *
98          * @param       $unl            A Universal Node Locator
99          * @return      $matches        Whether $address matches with the one from package data
100          */
101         public final function ifAddressMatches ($unl) {
102                 // Get current package data
103                 $packageData = $this->getPackageData();
104
105                 // Debug message
106                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',packageData=' . print_r($packageData, TRUE));
107
108                 // So, does both match?
109                 $matches = ((isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) && ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] === $unl));
110
111                 // Return result
112                 return $matches;
113         }
114
115         /**
116          * Checks whether the given socket matches with stored
117          *
118          * @param       $socketResource         A valid socket resource
119          * @return      $matches                        Whether given socket matches
120          */
121         public final function ifSocketResourceMatches ($socketResource) {
122                 // Debug message
123                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
124
125                 // So, does both match?
126                 $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));
127
128                 // Return result
129                 return $matches;
130         }
131
132 }