5e3c959b06d55f26c23fe893a255f5955b7bef29
[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\Visitor\Visitor;
7
8 /**
9  * A general decorator for listeners to communicate to hubs
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class BaseListenerDecorator extends BaseDecorator implements Visitable {
31         /**
32          * Listener type
33          */
34         private $listenerType = 'invalid';
35
36         /**
37          * Protected constructor
38          *
39          * @param       $className      Name of the class
40          * @return      void
41          */
42         protected function __construct ($className) {
43                 // Call parent constructor
44                 parent::__construct($className);
45         }
46
47         /**
48          * Getter for listen address
49          *
50          * @return      $listenAddress  The address this listener should listen on
51          */
52         public final function getListenAddress () {
53                 return $this->getListenerInstance()->getListenAddress();
54         }
55
56         /**
57          * Getter for listen port
58          *
59          * @return      $listenPort             The port this listener should listen on
60          */
61         public final function getListenPort () {
62                 return $this->getListenerInstance()->getListenPort();
63         }
64
65         /**
66          * Getter for connection type
67          *
68          * @return      $connectionType         Connection type for this listener
69          */
70         public final function getConnectionType () {
71                 return $this->getListenerInstance()->getConnectionType();
72         }
73
74         /**
75          * Accepts the visitor to process the visit "request"
76          *
77          * @param       $visitorInstance        An instance of a Visitor class
78          * @return      void
79          */
80         public function accept (Visitor $visitorInstance) {
81                 // Visit this decorator
82                 $visitorInstance->visitDecorator($this);
83
84                 // Visit the covered class
85                 $visitorInstance->visitListener($this->getListenerInstance());
86         }
87
88         /**
89          * Getter for listener type.
90          *
91          * @return      $listenerType   The listener's type (hub/peer)
92          */
93         public final function getListenerType () {
94                 return $this->listenerType;
95         }
96
97         /**
98          * Setter for listener type.
99          *
100          * @param       $listenerType   The listener's type (hub/peer)
101          * @return      void
102          */
103         protected final function setListenerType ($listenerType) {
104                 $this->listenerType = $listenerType;
105         }
106
107         /**
108          * Getter for peer pool instance
109          *
110          * @return      $poolInstance   A peer pool instance
111          */
112         public final function getPoolInstance () {
113                 return $this->getListenerInstance()->getPoolInstance();
114         }
115
116         /**
117          * Monitors incoming raw data from the handler and transfers it to the
118          * given receiver instance.
119          *
120          * @return      void
121          */
122         public function monitorIncomingRawData () {
123                 // Get the handler instance
124                 $handlerInstance = $this->getListenerInstance()->getHandlerInstance();
125
126                 /*
127                  * Does the deocorated listener (or even a decorator again) have a
128                  * handler assigned? Remember that a handler will hold all incoming raw
129                  * data and not a listener.
130                  */
131                 if (!$handlerInstance instanceof Networkable) {
132                         // Skip this silently for now. Later on, this will become mandatory!
133                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('No handler assigned to this listener decorator. this=' . $this->__toString() . ', listenerInstance=' . $this->getListenerInstance()->__toString());
134                         return;
135                 } // END - if
136
137                 // Does the handler have some decoded data pending?
138                 if (!$handlerInstance->isRawDataPending()) {
139                         // No data is pending so skip further code silently
140                         return;
141                 } // END - if
142
143                 // Get receiver (network package) instance
144                 $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance();
145
146                 /*
147                  * There is some decoded data waiting. The receiver instance is an
148                  * abstract network package (which can be received and sent out) so
149                  * handle the decoded data over. At this moment it is not needed to
150                  * know if the decoded data origins from a TCP or UDP connection so it
151                  * can just be passed over to the network package receiver.
152                  */
153                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LISTENER-DECORATOR[' . __METHOD__ . ':' . __LINE__ . '] Going to handle over some raw data to receiver instance (' . $receiverInstance->__toString() . ') ...');
154                 $receiverInstance->addRawDataToIncomingStack($handlerInstance);
155         }
156
157 }