3 namespace CoreFramework\Listener;
5 // Import framework stuff
6 use CoreFramework\Generic\BaseDecorator;
7 use CoreFramework\Visitor\Visitable;
8 use CoreFramework\Visitor\Visitor;
11 * A general decorator for listeners to communicate to hubs
13 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
32 class BaseListenerDecorator extends BaseDecorator implements Visitable {
36 private $listenerType = 'invalid';
39 * Protected constructor
41 * @param $className Name of the class
44 protected function __construct ($className) {
45 // Call parent constructor
46 parent::__construct($className);
50 * Getter for listen address
52 * @return $listenAddress The address this listener should listen on
54 public final function getListenAddress () {
55 return $this->getListenerInstance()->getListenAddress();
59 * Getter for listen port
61 * @return $listenPort The port this listener should listen on
63 public final function getListenPort () {
64 return $this->getListenerInstance()->getListenPort();
68 * Getter for connection type
70 * @return $connectionType Connection type for this listener
72 public final function getConnectionType () {
73 return $this->getListenerInstance()->getConnectionType();
77 * Accepts the visitor to process the visit "request"
79 * @param $visitorInstance An instance of a Visitor class
82 public function accept (Visitor $visitorInstance) {
83 // Visit this decorator
84 $visitorInstance->visitDecorator($this);
86 // Visit the covered class
87 $visitorInstance->visitListener($this->getListenerInstance());
91 * Getter for listener type.
93 * @return $listenerType The listener's type (hub/peer)
95 public final function getListenerType () {
96 return $this->listenerType;
100 * Setter for listener type.
102 * @param $listenerType The listener's type (hub/peer)
105 protected final function setListenerType ($listenerType) {
106 $this->listenerType = $listenerType;
110 * Getter for peer pool instance
112 * @return $poolInstance A peer pool instance
114 public final function getPoolInstance () {
115 return $this->getListenerInstance()->getPoolInstance();
119 * Monitors incoming raw data from the handler and transfers it to the
120 * given receiver instance.
124 public function monitorIncomingRawData () {
125 // Get the handler instance
126 $handlerInstance = $this->getListenerInstance()->getHandlerInstance();
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.
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());
139 // Does the handler have some decoded data pending?
140 if (!$handlerInstance->isRawDataPending()) {
141 // No data is pending so skip further code silently
145 // Get receiver (network package) instance
146 $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance();
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.
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);