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