3 namespace CoreFramework\Listener;
5 // Import framework stuff
6 use CoreFramework\Generic\BaseDecorator;
7 use CoreFramework\Visitor\Visitor;
10 * A general decorator for listeners to communicate to hubs
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseListenerDecorator extends BaseDecorator implements Visitable {
35 private $listenerType = 'invalid';
38 * Protected constructor
40 * @param $className Name of the class
43 protected function __construct ($className) {
44 // Call parent constructor
45 parent::__construct($className);
49 * Getter for listen address
51 * @return $listenAddress The address this listener should listen on
53 public final function getListenAddress () {
54 return $this->getListenerInstance()->getListenAddress();
58 * Getter for listen port
60 * @return $listenPort The port this listener should listen on
62 public final function getListenPort () {
63 return $this->getListenerInstance()->getListenPort();
67 * Getter for connection type
69 * @return $connectionType Connection type for this listener
71 public final function getConnectionType () {
72 return $this->getListenerInstance()->getConnectionType();
76 * Accepts the visitor to process the visit "request"
78 * @param $visitorInstance An instance of a Visitor class
81 public function accept (Visitor $visitorInstance) {
82 // Visit this decorator
83 $visitorInstance->visitDecorator($this);
85 // Visit the covered class
86 $visitorInstance->visitListener($this->getListenerInstance());
90 * Getter for listener type.
92 * @return $listenerType The listener's type (hub/peer)
94 public final function getListenerType () {
95 return $this->listenerType;
99 * Setter for listener type.
101 * @param $listenerType The listener's type (hub/peer)
104 protected final function setListenerType ($listenerType) {
105 $this->listenerType = $listenerType;
109 * Getter for peer pool instance
111 * @return $poolInstance A peer pool instance
113 public final function getPoolInstance () {
114 return $this->getListenerInstance()->getPoolInstance();
118 * Monitors incoming raw data from the handler and transfers it to the
119 * given receiver instance.
123 public function monitorIncomingRawData () {
124 // Get the handler instance
125 $handlerInstance = $this->getListenerInstance()->getHandlerInstance();
128 * Does the deocorated listener (or even a decorator again) have a
129 * handler assigned? Remember that a handler will hold all incoming raw
130 * data and not a listener.
132 if (!$handlerInstance instanceof Networkable) {
133 // Skip this silently for now. Later on, this will become mandatory!
134 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('No handler assigned to this listener decorator. this=' . $this->__toString() . ', listenerInstance=' . $this->getListenerInstance()->__toString());
138 // Does the handler have some decoded data pending?
139 if (!$handlerInstance->isRawDataPending()) {
140 // No data is pending so skip further code silently
144 // Get receiver (network package) instance
145 $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance();
148 * There is some decoded data waiting. The receiver instance is an
149 * abstract network package (which can be received and sent out) so
150 * handle the decoded data over. At this moment it is not needed to
151 * know if the decoded data origins from a TCP or UDP connection so it
152 * can just be passed over to the network package receiver.
154 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LISTENER-DECORATOR[' . __METHOD__ . ':' . __LINE__ . '] Going to handle over some raw data to receiver instance (' . $receiverInstance->__toString() . ') ...');
155 $receiverInstance->addRawDataToIncomingStack($handlerInstance);