3 namespace CoreFramework\Listener;
5 // Import framework stuff
6 use CoreFramework\Visitor\Visitor;
9 * A general decorator for listeners to communicate to hubs
11 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
30 class BaseListenerDecorator extends BaseDecorator implements Visitable {
34 private $listenerType = 'invalid';
37 * Protected constructor
39 * @param $className Name of the class
42 protected function __construct ($className) {
43 // Call parent constructor
44 parent::__construct($className);
48 * Getter for listen address
50 * @return $listenAddress The address this listener should listen on
52 public final function getListenAddress () {
53 return $this->getListenerInstance()->getListenAddress();
57 * Getter for listen port
59 * @return $listenPort The port this listener should listen on
61 public final function getListenPort () {
62 return $this->getListenerInstance()->getListenPort();
66 * Getter for connection type
68 * @return $connectionType Connection type for this listener
70 public final function getConnectionType () {
71 return $this->getListenerInstance()->getConnectionType();
75 * Accepts the visitor to process the visit "request"
77 * @param $visitorInstance An instance of a Visitor class
80 public function accept (Visitor $visitorInstance) {
81 // Visit this decorator
82 $visitorInstance->visitDecorator($this);
84 // Visit the covered class
85 $visitorInstance->visitListener($this->getListenerInstance());
89 * Getter for listener type.
91 * @return $listenerType The listener's type (hub/peer)
93 public final function getListenerType () {
94 return $this->listenerType;
98 * Setter for listener type.
100 * @param $listenerType The listener's type (hub/peer)
103 protected final function setListenerType ($listenerType) {
104 $this->listenerType = $listenerType;
108 * Getter for peer pool instance
110 * @return $poolInstance A peer pool instance
112 public final function getPoolInstance () {
113 return $this->getListenerInstance()->getPoolInstance();
117 * Monitors incoming raw data from the handler and transfers it to the
118 * given receiver instance.
122 public function monitorIncomingRawData () {
123 // Get the handler instance
124 $handlerInstance = $this->getListenerInstance()->getHandlerInstance();
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.
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());
137 // Does the handler have some decoded data pending?
138 if (!$handlerInstance->isRawDataPending()) {
139 // No data is pending so skip further code silently
143 // Get receiver (network package) instance
144 $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance();
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.
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);