]> git.mxchange.org Git - hub.git/blob - ship-simu/application/hub/main/class_HubConnector.php
Initial import
[hub.git] / ship-simu / application / hub / main / class_HubConnector.php
1 <?php
2 /**
3  * A class for making socket connections to other hubs including the master hub
4  */
5 class HubConnector extends BaseFrameworkSystem {
6         /**
7          * The socket we shall use for communication
8          */
9         private $socketResource = null;
10
11         /**
12          * The peer instance (HubPeer class)
13          */
14         private $peerInstance = null;
15
16         /**
17          * The private constructor
18          *
19          * @return      void
20          */
21         private function __construct () {
22                 // Call parent constructor
23                 parent::constructor(__CLASS__);
24
25                 // Set description
26                 $this->setPartDescr("Hub-Connector");
27
28                 // Set unique ID
29                 $this->createUniqueID();
30
31                 // Tidy up a little
32                 $this->removeSystemArray();
33                 $this->removeNumberFormaters();
34         }
35
36         /**
37          * Creates an instance based on the (IP) address and port number of this
38          * class. Next it tries to connect to the specified peer and communicates
39          * to it over a socket
40          *
41          * @param               $address                                The peer's IP address
42          * @param               $port                           The peer's port number
43          * @param               $hubInstance                    An instance of a HubCoreLoop class
44          * @return      $connector                      An instance of this class
45          * @throws      HubHelloException               If HELLO retries reached maximum
46          */
47         public final static function createHubConnectorByAddressPort ($address, $port, HubCoreLoop $hubInstance) {
48                 // Get a new instance of this class
49                 $connector = new HubConnector();
50
51                 // Connect to the given address and IP number
52                 $connector->aquireConnectionToAddress($address, $port);
53
54                 // Message to console
55                 $hubInstance->getOutputInstance()->output(sprintf("[%s] Sending %s request to hub %s:%d...",
56                         __METHOD__,
57                         $connector->getConfigInstance()->readConfig("hub_peer_hello"),
58                         $address,
59                         $port
60                 ));
61
62                 // Get a HubPeer instance
63                 $peerInstance = HubPeer::createHubPeerBySocket($connector->getSocketResource(), $hubInstance);
64
65                 // Send a HELLO to the master hub
66                 $helloed = 0; $retries = 0;
67                 while (!$peerInstance->ifHelloReplied()) {
68                         // Within timeout?
69                         if ((time() - $helloed) >= $connector->getConfigInstance()->readConfig("hub_hello_timeout")) {
70                                 // Send HELLOs out in periodic times
71                                 $peerInstance->sendMessage($connector->getConfigInstance()->readConfig("hub_peer_hello"));
72                                 $helloed = time(); $retries++;
73                                 if ($retries == $connector->getConfigInstance()->readConfig("hub_hello_retires")) {
74                                         // Maximum retries reached
75                                         throw new HubHelloException(
76                                                 array(
77                                                         'this'  => $connector,
78                                                         'peer'  => $peerInstance
79                                                 ), HubCoreLoop::EXCEPTION_HELLO_TIMED_OUT
80                                         );
81                                 }
82                         } // END - if
83                 } // END - while
84
85                 // Set the peer instance
86                 $connector->setPeerInstance($peerInstance);
87
88                 // Return the instance
89                 return $connector;
90         }
91
92         /**
93          * Aquires a socket link to a specified IP address and port number. It
94          * throws subclasses of SocketException-s if the peer is down or not
95          * responding. If the connection is up it writes it's resource into
96          * the attribute $socketResource and waits for incoming data packages.
97          *
98          * @param               $address                                The peer's IP address
99          * @param               $port                           The peer's port number
100          * @return      void
101          * @throws      SocketCreationException If creation of the socket wents wrong
102          * @throws      SocketConnectException  If the connection was not established
103          */
104         public function aquireConnectionToAddress ($address, $port) {
105                 // Create a socket for binding to the address
106                 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
107
108                 // Was it a success?
109                 if (!is_resource($socket)) {
110                         // This fails! :(
111                         throw new SocketCreationException(
112                                 array(
113                                         'this' => $this,
114                                         'code' => socket_last_error()
115                                 ), HubCoreLoop::EXCEPTION_SOCKET_PROBLEM
116                         );
117                 } // END - fi
118
119                 // Now connect to the peer
120                 if (@socket_connect($socket, $address, $port) === false) {
121                         // Something wents wrong!
122                         throw new SocketConnectException (
123                                 array(
124                                         'this'  => $this,
125                                         'code'  => socket_last_error()
126                                 ), HubCoreLoop::EXCEPTION_SOCKET_PROBLEM
127                         );
128                 } // END - if
129
130                 // Set non-blocking mode
131                 @socket_set_nonblock($socket);
132
133                 // Set the socket
134                 $this->socketResource = $socket;
135         }
136
137         /**
138          * Getter for current socket
139          *
140          * @return      $socketResource The current socket resource or null if not set
141          */
142         public final function getSocketResource() {
143                 return $this->socketResource;
144         }
145
146         /**
147          * Setter for a HubPeer instance
148          *
149          * @param               $peerInstance           An instance of a HubPeer class
150          * @return      void
151          */
152         public final function setPeerInstance (HubPeer $peerInstance) {
153                 $this->peerInstance = $peerInstance;
154         }
155
156         /**
157          * Handles any incoming master requests
158          *
159          * @return      void
160          */
161         public function handleMasterRequests () {
162                 // Just ask the peer instance
163                 $command = $this->peerInstance->handleMasterRequests();
164         }
165
166 } // END - class
167 ?>