3 namespace CoreFramework\Listener;
6 * A general decorator for listeners to communicate to hubs
8 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
27 class BaseListenerDecorator extends BaseDecorator implements Visitable {
31 private $listenerType = 'invalid';
34 * Protected constructor
36 * @param $className Name of the class
39 protected function __construct ($className) {
40 // Call parent constructor
41 parent::__construct($className);
45 * Getter for listen address
47 * @return $listenAddress The address this listener should listen on
49 public final function getListenAddress () {
50 return $this->getListenerInstance()->getListenAddress();
54 * Getter for listen port
56 * @return $listenPort The port this listener should listen on
58 public final function getListenPort () {
59 return $this->getListenerInstance()->getListenPort();
63 * Getter for connection type
65 * @return $connectionType Connection type for this listener
67 public final function getConnectionType () {
68 return $this->getListenerInstance()->getConnectionType();
72 * Accepts the visitor to process the visit "request"
74 * @param $visitorInstance An instance of a Visitor class
77 public function accept (Visitor $visitorInstance) {
78 // Visit this decorator
79 $visitorInstance->visitDecorator($this);
81 // Visit the covered class
82 $visitorInstance->visitListener($this->getListenerInstance());
86 * Getter for listener type.
88 * @return $listenerType The listener's type (hub/peer)
90 public final function getListenerType () {
91 return $this->listenerType;
95 * Setter for listener type.
97 * @param $listenerType The listener's type (hub/peer)
100 protected final function setListenerType ($listenerType) {
101 $this->listenerType = $listenerType;
105 * Getter for peer pool instance
107 * @return $poolInstance A peer pool instance
109 public final function getPoolInstance () {
110 return $this->getListenerInstance()->getPoolInstance();
114 * Monitors incoming raw data from the handler and transfers it to the
115 * given receiver instance.
119 public function monitorIncomingRawData () {
120 // Get the handler instance
121 $handlerInstance = $this->getListenerInstance()->getHandlerInstance();
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.
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());
134 // Does the handler have some decoded data pending?
135 if (!$handlerInstance->isRawDataPending()) {
136 // No data is pending so skip further code silently
140 // Get receiver (network package) instance
141 $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance();
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.
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);