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