087f2c58cc80f11aeb78d6ce927ddf3e50b4b9de
[core.git] / inc / main / classes / container / socket / class_SocketContainer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Container\Socket;
4
5 /**
6  * A Socket Container class
7  *
8  * @author              Roland Haeder <webmaster@shipsimu.org>
9  * @version             0.0.0
10  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.shipsimu.org
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class SocketContainer extends BaseContainer implements Registerable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36         }
37
38         /**
39          * Creates an instance of this Container class and prepares it for usage
40          *
41          * @param       $socketResource         A valid socket resource
42          * @param       $infoInstance           An instance of a  ShareableInfo class
43          * @param       $packageData            Raw package data
44          * @return      $containerInstance      An instance of this Container class
45          */
46         public static final function createSocketContainer ($socketResource, ShareableInfo $infoInstance = NULL, array $packageData = array()) {
47                 // Get a new instance
48                 $containerInstance = new SocketContainer();
49
50                 // Remove unneeded entries
51                 unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
52                 unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]);
53
54                 // Debug message
55                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']:socketResource=' . $socketResource . ',packageData='.print_r($packageData, TRUE));
56
57                 // Is the info instance set?
58                 if ($infoInstance instanceof ShareableInfo) {
59                         // Get listener/helper from info class
60                         $listenerInstance = $infoInstance->getListenerInstance();
61                         $helperInstance = $infoInstance->getHelperInstance();
62
63                         // Debug message
64                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance[]=' . gettype($listenerInstance));
65
66                         // Is there a listener instance set?
67                         if ($listenerInstance instanceof Listenable) {
68                                 // Debug message
69                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
70
71                                 // Set it here for later usage
72                                 $containerInstance->setListenerInstance($listenerInstance);
73                         } elseif ($helperInstance instanceof ConnectionHelper) {
74                                 // Debug message
75                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
76
77                                 // Set it here for later usage
78                                 $containerInstance->setHelperInstance($helperInstance);
79                         }
80                 } // END - if
81
82                 // Set the resource ...
83                 $containerInstance->setSocketResource($socketResource);
84
85                 // ... and package data
86                 $containerInstance->setPackageData($packageData);
87
88                 // Return the prepared instance
89                 return $containerInstance;
90         }
91
92         /**
93          * Checks whether the given Universal Node Locator matches with the one from package data
94          *
95          * @param       $unl            A Universal Node Locator
96          * @return      $matches        Whether $address matches with the one from package data
97          */
98         public final function ifAddressMatches ($unl) {
99                 // Get current package data
100                 $packageData = $this->getPackageData();
101
102                 // Debug message
103                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',packageData=' . print_r($packageData, TRUE));
104
105                 // So, does both match?
106                 $matches = ((isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) && ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] === $unl));
107
108                 // Return result
109                 return $matches;
110         }
111
112         /**
113          * Checks whether the given socket matches with stored
114          *
115          * @param       $socketResource         A valid socket resource
116          * @return      $matches                        Whether given socket matches
117          */
118         public final function ifSocketResourceMatches ($socketResource) {
119                 // Debug message
120                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-CONTAINER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
121
122                 // So, does both match?
123                 $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));
124
125                 // Return result
126                 return $matches;
127         }
128
129 }