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