2f7200a9cf70074a4adda81ddb437110d5884a21
[core.git] / framework / main / classes / listener / class_BaseListenerDecorator.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Listener;
4
5 // Import framework stuff
6 use CoreFramework\Generic\BaseDecorator;
7 use CoreFramework\Visitor\Visitor;
8
9 /**
10  * A general decorator for listeners to communicate to hubs
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 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 BaseListenerDecorator extends BaseDecorator implements Visitable {
32         /**
33          * Listener type
34          */
35         private $listenerType = 'invalid';
36
37         /**
38          * Protected constructor
39          *
40          * @param       $className      Name of the class
41          * @return      void
42          */
43         protected function __construct ($className) {
44                 // Call parent constructor
45                 parent::__construct($className);
46         }
47
48         /**
49          * Getter for listen address
50          *
51          * @return      $listenAddress  The address this listener should listen on
52          */
53         public final function getListenAddress () {
54                 return $this->getListenerInstance()->getListenAddress();
55         }
56
57         /**
58          * Getter for listen port
59          *
60          * @return      $listenPort             The port this listener should listen on
61          */
62         public final function getListenPort () {
63                 return $this->getListenerInstance()->getListenPort();
64         }
65
66         /**
67          * Getter for connection type
68          *
69          * @return      $connectionType         Connection type for this listener
70          */
71         public final function getConnectionType () {
72                 return $this->getListenerInstance()->getConnectionType();
73         }
74
75         /**
76          * Accepts the visitor to process the visit "request"
77          *
78          * @param       $visitorInstance        An instance of a Visitor class
79          * @return      void
80          */
81         public function accept (Visitor $visitorInstance) {
82                 // Visit this decorator
83                 $visitorInstance->visitDecorator($this);
84
85                 // Visit the covered class
86                 $visitorInstance->visitListener($this->getListenerInstance());
87         }
88
89         /**
90          * Getter for listener type.
91          *
92          * @return      $listenerType   The listener's type (hub/peer)
93          */
94         public final function getListenerType () {
95                 return $this->listenerType;
96         }
97
98         /**
99          * Setter for listener type.
100          *
101          * @param       $listenerType   The listener's type (hub/peer)
102          * @return      void
103          */
104         protected final function setListenerType ($listenerType) {
105                 $this->listenerType = $listenerType;
106         }
107
108         /**
109          * Getter for peer pool instance
110          *
111          * @return      $poolInstance   A peer pool instance
112          */
113         public final function getPoolInstance () {
114                 return $this->getListenerInstance()->getPoolInstance();
115         }
116
117         /**
118          * Monitors incoming raw data from the handler and transfers it to the
119          * given receiver instance.
120          *
121          * @return      void
122          */
123         public function monitorIncomingRawData () {
124                 // Get the handler instance
125                 $handlerInstance = $this->getListenerInstance()->getHandlerInstance();
126
127                 /*
128                  * Does the deocorated listener (or even a decorator again) have a
129                  * handler assigned? Remember that a handler will hold all incoming raw
130                  * data and not a listener.
131                  */
132                 if (!$handlerInstance instanceof Networkable) {
133                         // Skip this silently for now. Later on, this will become mandatory!
134                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('No handler assigned to this listener decorator. this=' . $this->__toString() . ', listenerInstance=' . $this->getListenerInstance()->__toString());
135                         return;
136                 } // END - if
137
138                 // Does the handler have some decoded data pending?
139                 if (!$handlerInstance->isRawDataPending()) {
140                         // No data is pending so skip further code silently
141                         return;
142                 } // END - if
143
144                 // Get receiver (network package) instance
145                 $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance();
146
147                 /*
148                  * There is some decoded data waiting. The receiver instance is an
149                  * abstract network package (which can be received and sent out) so
150                  * handle the decoded data over. At this moment it is not needed to
151                  * know if the decoded data origins from a TCP or UDP connection so it
152                  * can just be passed over to the network package receiver.
153                  */
154                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LISTENER-DECORATOR[' . __METHOD__ . ':' . __LINE__ . '] Going to handle over some raw data to receiver instance (' . $receiverInstance->__toString() . ') ...');
155                 $receiverInstance->addRawDataToIncomingStack($handlerInstance);
156         }
157
158 }