]> git.mxchange.org Git - hub.git/blob - application/hub/main/class_HubCoreLoop.php
e2cc62994ee3c00c92a7f5ef67a68e6b99807f99
[hub.git] / application / hub / main / class_HubCoreLoop.php
1 <?php
2 /**
3  * The hub's main loop. The hub will wait and listen for incoming requests in
4  * this loop.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class HubCoreLoop extends BaseFrameworkSystem {
25         /**
26          * Wether the hub is active and running
27          */
28         private $hubActivated = false;
29
30         /**
31          * Wether the hub is shutting down
32          */
33         private $hubShutsDown = false;
34
35         /**
36          * A list of all connected peers (sockets)
37          */
38         private $connectedPeers = null;
39
40         /**
41          * A console output handler
42          */
43         private $outputInstance = null;
44
45         /**
46          * Reading peers
47          */
48         private $readPeers = array();
49
50         /**
51          * Writing peers
52          */
53         private $writePeers = array();
54
55         /**
56          * The main socket (listening) for this hub
57          */
58         private $main_socket = null;
59
60         /**
61          * A list of authenticated peers
62          */
63         private $authPeers = null;
64
65         /**
66          * Last peer instance
67          */
68         private $lastPeerInstance = null;
69
70         /**
71          * Wether this hub is a master hub
72          */
73         private $hubIsMaster = false;
74
75         /**
76          * Maximum auth retries (cached)
77          */
78         private $authRetries = 0;
79
80         /**
81          * Auth request message
82          */
83         private $authRequest = "";
84
85         /**
86          * Cached timeout for auth requests
87          */
88         private $authRequestTimeout = 0;
89
90         /**
91          * An instance to the HubConnector class for master hub connection
92          */
93         private $masterConnector = null;
94
95         // Exception codes
96         const EXCEPTION_SOCKET_PROBLEM                  = 0xb00;
97         const EXCEPTION_HUB_PEER_TIMEOUT                = 0xb01;
98         const EXCEPTION_HUB_PEER_FAILED_AUTH    = 0xb02;
99         const EXCEPTION_HELLO_TIMED_OUT                 = 0xb03;
100
101         /**
102          * The private constructor
103          *
104          * @return      void
105          */
106         protected function __construct () {
107                 // Call parent constructor
108                 parent::__construct(__CLASS__);
109
110                 // Tidy up a little
111                 $this->removeSystemArray();
112                 $this->removeNumberFormaters();
113
114                 // Init the peer list
115                 $this->initPeerList();
116         }
117
118         /**
119          * Factory for main loop
120          *
121          * @return      $hubInstance            An instance of this class
122          */
123         public final static function createHubCoreLoop () {
124                 // Get an instance
125                 $hubInstance = new HubCoreLoop();
126
127                 // Try to setup the socket
128                 $hubInstance->setupHub();
129
130                 // Get the configuration variable
131                 $outEngine = $hubInstance->getConfigInstance()->readConfig("tpl_engine");
132
133                 // Setup the console output handler
134                 $eval = sprintf("\$hubInstance->setOutputInstance(%s::create%s(\"%s\"));",
135                         $outEngine,
136                         $outEngine,
137                         $hubInstance->getConfigInstance()->readConfig("web_content_type")
138                 );
139
140                 // Debug message
141                 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $hubInstance->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: %s",
142                         $hubInstance->__toString(),
143                         htmlentities($eval)
144                 ));
145                 @eval($eval);
146
147                 // Return the prepared instance
148                 return $hubInstance;
149         }
150
151         /**
152          * Initializes the peer list
153          *
154          * @return      void
155          */
156         private final function initPeerList () {
157                 $this->connectedPeers = new FrameworkArrayObject("FakedConnectedPeers");
158                 $this->authPeers      = new FrameworkArrayObject("FakeAuthPeers");
159         }
160
161         /**
162          * Get total number of connected peers
163          *
164          * @return      $num            The total number of connected peers
165          */
166         private final function getTotalConnectedPeers () {
167                 $read = array($this->connectedPeers->getIterator()->current());
168                 $write = array();
169                 $except = array();
170
171                 // Get socket number
172                 $num = socket_select(
173                         $read,
174                         $write,
175                         $except,
176                         0
177                 );
178
179                 // Transfer readers and writers
180                 $this->readPeers        = $read;
181                 $this->writePeers       = $write;
182
183                 // Return the number
184                 return $num;
185         }
186
187         /**
188          * Handle newly connected peers
189          *
190          * @return      void
191          */
192         private final function handleNewPeers () {
193                 // Output message
194                 $this->getOutputInstance()->output(sprintf("[%s] Validating peer...", __METHOD__));
195
196                 // Is the main socket in the array?
197                 if (in_array($this->main_socket, $this->readPeers)) {
198                         // Accept the connection and add him to the list
199                         $peer_socket = socket_accept($this->main_socket);
200
201                         // Get a new peer instance
202                         $this->setCurrPeerInstance(HubPeer::createHubPeerBySocket($peer_socket, $this));
203
204                         // Register the new peer
205                         $this->getOutputInstance()->output(sprintf("[%s] Registering new peer with IP %s.",
206                                 __METHOD__,
207                                 $this->getCurrPeerInstance()->getValidatedIP()
208                         ));
209                         $this->connectedPeers->append($this->lastPeerInstance);
210
211                         // A new peer has connected
212                         $this->getOutputInstance()->output(sprintf("[%s] New peer with IP %s has been registered.",
213                                 __METHOD__,
214                                 $this->getCurrPeerInstance()->getValidatedIP()
215                         ));
216
217                         // Remove him from the list
218                         $key = array_search($this->main_socket, $this->readPeers);
219                         unset($this->readPeers[$key]);
220                 } // END - if
221         }
222
223         /**
224          * Handles unauthenticated peers
225          *
226          * @return      void
227          * @throws      HubPeerTimeoutException                 If the peer times out to answer a request
228          * @throws      HubPeerAuthorizationException           If the peer fails to authorize himself (to many retries)
229          */
230         private final function handleUnauthPeers () {
231                 // Are there some peers?
232                 if ($this->connectedPeers->count() > 1) {
233                         // Iterate through all connected peers
234                         for ($idx = $this->connectedPeers->getIterator(); $idx->valid(); $idx->next()) {
235                                 // Get current peer
236                                 $this->lastPeerInstance = $idx->current();
237
238                                 // Ignore own socket and invalid entries
239                                 if (($this->getCurrPeerInstance() !== $this->main_socket) && (is_object($this->getCurrPeerInstance())) && ($this->getCurrPeerInstance() instanceof HubPeer)) {
240                                         // Is this peer already authorized or is this the master hub?
241                                         // If this is the master hub then there is no auth required
242                                         if ((!$this->getCurrPeerInstance()->ifPeerIsAuthorized()) && (!$this->getCurrPeerInstance()->ifPeerIsLocalAdmin()) && (!$this->hubIsMaster)) {
243                                                 // This peer waits for authorization, so does he have some tries left?
244                                                 if ($this->getCurrPeerInstance()->getAuthRetries() <= $this->authRetries) {
245                                                         // This peer is still allowed to try his authorization
246                                                         if ($this->getCurrPeerInstance()->getLastSentMessage() == $this->authRequest) {
247                                                                 // Already asked so maybe timed out?
248                                                                 if ((time() - $this->getCurrPeerInstance()->getLastSentMessageStamp()) >= $this->authRequestTimeout) {
249                                                                         // Timed out so disconnect the peer
250                                                                         throw new HubPeerTimeoutException (
251                                                                                 array(
252                                                                                         'this'  => $this,
253                                                                                         'peer'  => $this->getCurrPeerInstance()
254                                                                                 ), self::EXCEPTION_HUB_PEER_TIMEOUT
255                                                                         );
256                                                                 } // END - if
257                                                         } elseif ($this->getCurrPeerInstance()->ifHelloReceived()) {
258                                                                 // HELLO received so we need to sent the AUTH request
259                                                                 $this->getCurrPeerInstance()->askAuthorizationKey();
260                                                         }
261                                                 } else {
262                                                         // This peer needs disconnecting!
263                                                         throw new HubPeerAuthorizationException (
264                                                                 array(
265                                                                         'this'  => $this,
266                                                                         'peer'  => $this->getCurrPeerInstance(),
267                                                                         'max'   => $this->authRetries
268                                                                 ), self::EXCEPTION_HUB_PEER_FAILED_AUTH
269                                                         );
270                                                 } // END - else
271                                         } elseif ((!$this->getCurrPeerInstance()->ifPeerIsAuthorized()) && ($this->getCurrPeerInstance()->ifPeerIsLocalAdmin())) {
272                                                 // This peer is a local admin so he is always authorized!
273                                                 $this->getCurrPeerInstance()->enableLocalAdmin();
274
275                                                 // Output debug message
276                                                 $this->getOutputInstance()->output(sprintf("[%s] A local admin has connected.",
277                                                         $this->__toString()
278                                                 ));
279
280                                                 // Say "hi" to the admin
281                                                 $this->getCurrPeerInstance()->sayHi2Admin();
282                                         } elseif (($this->hubIsMaster) && ($this->getCurrPeerInstance()->ifHelloReceived()) && (!$this->getCurrPeerInstance()->ifELHOsent())) {
283                                                 // Is the master hub so authorize the peer here...
284                                                 $this->getCurrPeerInstance()->enableIsAuthorized();
285                                                 $this->authPeers->append($this->getCurrPeerInstance());
286
287                                                 // Reply the HELLO request
288                                                 $this->getCurrPeerInstance()->replyHelloMessage();
289                                         }
290                                 } // END - if
291                         } // END - for
292                 } // END - if
293         }
294
295         /**
296          * Handles only authorized peers
297          *
298          * @return      void
299          */
300         public function handleAuthPeers () {
301                 // Are there some peers?
302                 if (($this->connectedPeers->count() > 1) && ($this->authPeers->count() > 0)) {
303                         // Iterate through all connected peers
304                         for ($idx = $this->authPeers->getIterator(); $idx->valid(); $idx->next()) {
305                                 // Get current peer
306                                 $this->lastPeerInstance = $idx->current();
307
308                                 // Do the ping and update our authPeer list (LATER!)
309                                 $this->lastPeerInstance->handlePingPeer();
310
311                                 // Avoids a notice...
312                                 if (is_null($this->getCurrPeerInstance())) break;
313                         } // END - for
314                 } // END - for
315         }
316
317         /**
318          * Setup the hub: Create a socket for listening on incoming requests,
319          * try to bind to a port, etc.
320          *
321          * @return      void
322          * @throws      SocketCreationException         If a socket cannot be created
323          * @throws      SocketSetupException            If a socket cannot be setuped
324          * @throws      SocketBindException                     If a socket cannot be bind to
325          *                                                                      an address and port
326          * @throws      SocketListeningException                If listening to a socket fails
327          */
328         private final function setupHub () {
329                 // Create a new TCP socket
330                 $main_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
331
332                 // Was it a success?
333                 if (!is_resource($main_socket)) {
334                         // This fails! :(
335                         throw new SocketCreationException(
336                                 array(
337                                         'this' => $this,
338                                         'code' => socket_last_error()
339                                 ), self::EXCEPTION_SOCKET_PROBLEM
340                         );
341                 }
342
343                 // Set socket options
344                 if (!socket_set_option($main_socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
345                         // Close the socket
346                         $this->closeSocket($main_socket);
347
348                         // Problems setting socket options
349                         throw new SocketSetupException (
350                                 array(
351                                         'this' => $this,
352                                         'code' => socket_last_error()
353                                 ), self::EXCEPTION_SOCKET_PROBLEM
354                         );
355                 }
356
357                 // Set to non-blocking
358                 socket_set_nonblock($main_socket);
359
360                 // Bind the socket to an address
361                 if (!socket_bind($main_socket, $this->getConfigInstance()->readConfig("hub_listen_addr"), $this->getConfigInstance()->readConfig("hub_listen_port"))) {
362                         // Bind failed
363                         throw new SocketBindException (
364                                 array(
365                                         'this' => $this,
366                                         'host' => $this->getConfigInstance()->readConfig("hub_listen_addr"),
367                                         'port' => $this->getConfigInstance()->readConfig("hub_listen_port"),
368                                         'code' => socket_last_error()
369                                 ), self::EXCEPTION_SOCKET_PROBLEM
370                         );
371                 }
372
373                 // Listen to ... the socket ;-)
374                 if (!socket_listen($main_socket)) {
375                         // Opps, that didn't work. Next time better listen to your heart... Roxette
376                         throw new SocketListeningException(
377                                 array(
378                                         'this' => $this,
379                                         'code' => socket_last_error()
380                                 ), self::EXCEPTION_SOCKET_PROBLEM
381                         );
382                 }
383
384                 // Ignore user abort and do not time out
385                 @set_time_limit(0);
386                 @ignore_user_abort(true);
387
388                 // Cache more configuration stuff
389                 $this->authRetries        = $this->getConfigInstance()->readConfig("hub_max_auth_tries");
390                 $this->authRequest        = $this->getConfigInstance()->readConfig("hub_auth_request");
391                 $this->authRequestTimeout = $this->getConfigInstance()->readConfig("hub_auth_request_timeout");
392
393                 // The hub is running now!
394                 $this->hubActivated = true;
395
396                 // Append the main hub
397                 $this->main_socket = $main_socket;
398                 $this->connectedPeers->append($main_socket);
399         }
400
401         /**
402          * Removes the current peer from the list
403          *
404          * @return      void
405          */
406         public final function removePeerFromConnectList () {
407                 // Iterate through all connected peers
408                 for ($idx = $this->connectedPeers->getIterator(); $idx->valid(); $idx->next()) {
409                         // Get current peer from list
410                         $peer = $idx->current();
411
412                         // Is it a peer instance?
413                         if (($peer !== $this->main_socket) && (is_object($peer)) && ($peer instanceof HubPeer)) {
414                                 // Okay, we have a peer instance so is it the same?
415                                 if ($this->getCurrPeerInstance()->equals($peer)) {
416                                         // Remove him!
417                                         $idx->offsetUnset($idx->key());
418
419                                         // Remove the last instance as well
420                                         $this->lastPeerInstance = null;
421
422                                         // Remove the socket as well
423                                         $this->removeSocket($peer);
424
425                                         // Stop searching here
426                                         break;
427                                 }
428                         }
429                 }
430         }
431
432         /**
433          * Getter for console output handler
434          *
435          * @return      $outputInstance An instance of the output handler
436          */
437         public final function getOutputInstance () {
438                 return $this->outputInstance;
439         }
440
441         /**
442          * Setter for console output handler
443          *
444          * @param               $outputInstance An instance of the output handler
445          */
446         public final function setOutputInstance ($outputInstance) {
447                 $this->outputInstance = $outputInstance;
448         }
449
450         /**
451          * Getter for current peer instance to HubPeer class
452          *
453          * @return      $lastPeerInstance               Last peer instance
454          */
455         public final function getCurrPeerInstance () {
456                 return $this->lastPeerInstance;
457         }
458
459         /**
460          * Setter for current peer instance to HubPeer class
461          *
462          * @param               $lastPeerInstance               Last peer instance
463          * @return      void
464          */
465         public final function setCurrPeerInstance (HubPeer $lastPeerInstance) {
466                 $this->lastPeerInstance = $lastPeerInstance;
467         }
468
469         /**
470          * Checks wether the hub is running and not in shutdown phase
471          *
472          * @return      $isRunning      If the hub is running and not in shutdown phase
473          */
474         public function ifHubIsRunning () {
475                 return ((($this->hubActivated) || (!$this->hubShutsDown)) && ($this->connectedPeers->count() > 0));
476         }
477
478         /**
479          * Output the intro text
480          *
481          * @return      void
482          */
483         public final function outputIntro () {
484                 if ($this->getConfigInstance()->readConfig("hub_intro_enabled") == "Y") {
485                         // Output intro text
486                         $this->getOutputInstance()->output(sprintf("%s v%s Copyright (c) 2007, 2008 by Roland H&auml;der",
487                                 ApplicationHelper::getInstance()->getAppName(),
488                                 ApplicationHelper::getInstance()->getAppVersion()
489                         ));
490                         $this->getOutputInstance()->output("");
491                         $this->getOutputInstance()->output("This software is free software licensed under the GNU GPL. In telnet session enter &quot;/license&quot; to read the license.");
492                         $this->getOutputInstance()->output("This software uses the MXChange Framework, Copyright (c) 2007, 2008 by Roland H&auml;der which is licensed under the GNU GPL.");
493                         $this->getOutputInstance()->output("Enter &quot;/framework&quot; in telnet session to read that license.");
494                         $this->getOutputInstance()->output("");
495                         $this->getOutputInstance()->output("All core systems are initialized. Input on *this* console will currently be ignored!");
496                         $this->getOutputInstance()->output("");
497                         $this->getOutputInstance()->output(sprintf("[%s] Listening on: %s:%d",
498                                 $this->__toString(),
499                                 $this->getConfigInstance()->readConfig("hub_listen_addr"),
500                                 $this->getConfigInstance()->readConfig("hub_listen_port")
501                         ));
502                         $this->getOutputInstance()->output("----------------------------------------------------------------------------------------------------------------------------");
503                 }
504         }
505
506         /**
507                 * Do the main loop
508                 *
509                 * @return       void
510                 * @throws       SocketCreationException         If the main socket is not a resource
511                 */
512          public function coreLoop () {
513                 // Is the main socket vailid?
514                 if (!is_resource($this->main_socket)) {
515                         // Is not valid!
516                         throw new SocketCreationException(
517                                 array(
518                                         'this'  => $this,
519                                         'code'  => socket_last_error()
520                                 ), self::EXCEPTION_SOCKET_PROBLEM
521                         );
522                 } // END - if
523
524                 // We are ready to serve requests
525                 $this->getOutputInstance()->output(sprintf("[%s] Ready to serve requests.",
526                         $this->__toString()
527                 ));
528
529                 // Wait until the hub is shutting down
530                 while ($this->ifHubIsRunning()) {
531                         // Get number of total connected peers
532                         $num = $this->getTotalConnectedPeers();
533
534                         try {
535                                 // Handle the master hub connection
536                                 if ($this->masterConnector instanceof HubConnector) {
537                                         $this->masterConnector->handleMasterRequests();
538                                 }
539
540                                 // Check for unauthorized peers
541                                 $this->handleUnauthPeers();
542
543                                 // Handle authorized peers
544                                 $this->handleAuthPeers();
545                         } catch (HubPeerAuthorizationException $e) {
546                                 // Authorization has failed
547                                 $this->disconnectPeerWithReason("hub_msg_auth_tries");
548
549                                 // Get new total connected peers
550                                 $num = $this->getTotalConnectedPeers();
551                         } catch (HubPeerTimeoutException $e) {
552                                 // Disconnect and remove the peer
553                                 $this->disconnectPeerWithReason("hub_msg_auth_reply_timeout");
554
555                                 // Get new total connected peers
556                                 $num = $this->getTotalConnectedPeers();
557                         } catch (HubMasterDisconnectedException $e) {
558                                 // The master hub has disconnected us... :(
559                                 $this->masterConnector = null;
560                                 $this->getOutputInstance()->output(sprintf("[%s] The master has disconnected us. Reason given: %s",
561                                         $this->__toString(),
562                                         $e->getMessage()
563                                 ));
564
565                                 // Get new total connected peers
566                                 $num = $this->getTotalConnectedPeers();
567                         } catch (BrokenPipeException $e) {
568                                 // Broken pipes are bad for us... :(
569                                 $this->removePeerFromConnectList();
570                                 $this->getOutputInstance()->output(sprintf("[%s] A peer has closed the connection unexpected: %s",
571                                         $this->__toString(),
572                                         $e->getMessage()
573                                 ));
574
575                                 // Get new total connected peers
576                                 $num = $this->getTotalConnectedPeers();
577                         } catch (FrameworkException $e) {
578                                 // Catch all other exceptions and output them
579                                 hub_exception_handler($e);
580
581                                 // Get new total connected peers
582                                 $num = $this->getTotalConnectedPeers();
583                         }
584
585                         // Are there some peers?
586                         if ($num < 1) {
587                                 // Wait for more peers
588                                 continue;
589                         } // END - if
590
591                         // A new peer has connected
592                         $this->getOutputInstance()->output(sprintf("[%s] A new peer is connecting.",
593                                 $this->__toString()
594                         ));
595
596                         try {
597                                 // Check for new peers
598                                 $this->handleNewPeers();
599                         } catch (IPSpoofingException $e) {
600                                 // Output message
601                                 $this->getOutputInstance()->output(sprintf("[%s] The peer's IP number has changed!",
602                                         $this->__toString()
603                                 ));
604
605                                 // Output debug message
606                                 $this->getDebugInstance()->output(sprintf("[%s] Peer spoofes IP number: %s",
607                                         $this->__toString(),
608                                         $e->getMessage()
609                                 ));
610
611                                 // Disconnect the peer first
612                                 $this->disconnectPeerWithReason("hub_msg_spoofing");
613
614                                 // Get new total connected peers
615                                 $num = $this->getTotalConnectedPeers();
616                         } catch (FrameworkException $e) {
617                                 // Catch all exceptions and output them to avoid aborting the program unexpectly
618                                 hub_exception_handler($e);
619
620                                 // Get new total connected peers
621                                 $num = $this->getTotalConnectedPeers();
622                         }
623
624                 } // END - while
625         }
626
627         /**
628          * Tries to contact the master server or simply reports that we are the master server
629          *
630          * @return      void
631          */
632         public function contactMasterHub () {
633                 // Checks wether we are the master hub
634                 if ($_SERVER['SERVER_ADDR'] == $this->getConfigInstance()->readConfig("hub_master_ip")) {
635                         // We are master!
636                         $this->hubIsMaster = true;
637                         $this->getOutputInstance()->output(sprintf("[%s] Our IP %s matches the master IP. Becoming master hub...",
638                                 $this->__toString(),
639                                 $_SERVER['SERVER_ADDR']
640                         ));
641                 } else {
642                         // A regular hub or ultra hub so let's contact the master
643                         $this->getOutputInstance()->output(sprintf("[%s] Contacting the master hub at %s:%d...",
644                                 $this->__toString(),
645                                 $this->getConfigInstance()->readConfig("hub_master_ip"),
646                                 $this->getConfigInstance()->readConfig("hub_master_port")
647                         ));
648
649                         // Try to aquire a connection to the master...
650                         try {
651                                 // Announce us to the master hub
652                                 $this->announceToMasterHub();
653                         } catch (FrameworkException $e) {
654                                 // Catch all exceptions and output them
655                                 hub_exception_handler($e);
656                         }
657                 } // END - else
658         }
659
660         /**
661          * Disconnects the current with a configurable reason
662          *
663          * @param               $reasonEntry    The entry with the disconnection reason aka. message to the peer
664          * @return      void
665          */
666          public function disconnectPeerWithReason ($reasonEntry) {
667                 // Default is that we cannot read the peer's IP number
668                 $ip = "0.0.0.0";
669
670                 // Try to disconnect here
671                 try {
672                         // First get the raw IP number
673                         $ip = $this->getCurrPeerInstance()->getValidatedIP();
674
675                         // Disconnect the peer...
676                         $this->getCurrPeerInstance()->disconnectWithReason($this->getConfigInstance()->readConfig($reasonEntry));
677                 } catch (FrameworkException $e) {
678                         // Catch all exceptions and output them
679                         hub_exception_handler($e);
680                 }
681
682                 // Remove him from the list anyway
683                 $this->removePeerFromConnectList();
684
685                 // Output the message
686                 $this->getOutputInstance()->output(sprintf("[%s] Peer %s has been disconnected.",
687                         $this->__toString(),
688                         $ip
689                 ));
690         }
691
692         /**
693          * Announces this hub to the master hub. This is being done by connecting to it and asking for auth request
694          *
695          * @return      void
696          */
697         public function announceToMasterHub () {
698                 try {
699                         // Create a new instance for communicating to the master hub
700                         $this->masterConnector = HubConnector::createHubConnectorByAddressPort(
701                                 $this->getConfigInstance()->readConfig("hub_master_ip"),
702                                 $this->getConfigInstance()->readConfig("hub_master_port"),
703                                 $this
704                         );
705
706                         // Send all our accepted objects to the master hub
707                         $this->masterConnector->sendAllAcceptedObjects();
708                 } catch (SocketConnectException $e) {
709                         // The master hub is down!
710                         ApplicationEntryPoint::app_die($e->getMessage());
711                 }
712         }
713
714         /**
715          * Removes a given peer instance (socket) from all lists
716          *
717          * @param       $peerInstance           An instance of a HubPeer class
718          * @return      void
719          */
720         private function removeSocket (HubPeer $peerInstance) {
721                 // Get socket from peer
722                 $socket = $peerInstance->getPeerSocket();
723
724                 // Search for readers
725                 $key = array_search($socket, $this->readPeers, true);
726                 if ($key !== false) {
727                         // Remove from reader list
728                         unset($this->readPeers[$key]);
729                 }
730
731                 // Search for writers
732                 $key = array_search($socket, $this->writePeers, true);
733                 if ($key !== false) {
734                         // Remove from writer list
735                         unset($this->writePeers[$key]);
736                 }
737
738                 // Remove from auth peers as well
739                 for ($idx = $this->authPeers->getIterator(); $idx->valid(); $idx->next()) {
740                         // Get current entry
741                         $current = $idx->current();
742
743                         // Is it the same?
744                         if ($current->equals($peerInstance)) {
745                                 // Remove from auth (-awaiting) list
746                                 $idx->offsetUnset($idx->key());
747                                 break;
748                         }
749                 }
750         }
751
752 } // END - class
753
754 // [EOF]
755 ?>