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