added Server PING -> PONG
[PHP-P10.git] / Uplink / Uplink.class.php
index c9de4102de829d949f599e781311dc8db3ae50bf..2dee4da2f213caf4d93bc98d9c50bc7f1ba2f0d6 100644 (file)
@@ -73,6 +73,10 @@ class Uplink {
                $self_numeric = $this->getSetting("numeric");
                $self_name = $this->getSetting("name");
                $self_description = $this->getSetting("description");
+               if(!$self_numeric || !$self_name) {
+                       trigger_error("Server Settings missing.", E_USER_ERROR);
+                       return;
+               }
                $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
        }
        
@@ -99,10 +103,11 @@ class Uplink {
                        $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
                        if(!$state) {
                                usleep($this->getSetting("recv_timeout") / 1000);
-                               $flags |= self::FLAG_NOT_CONNECTABLE;
+                               $this->flags |= self::FLAG_NOT_CONNECTABLE;
                                return;
                        }
                        $this->flags = 0;
+                       $this->loginServer();
                }
                //try to receive new data from the Uplink
                $lines = $this->client->recv();
@@ -147,13 +152,18 @@ class Uplink {
                }
        }
        
+       private function loginServer() {
+               $password = $this->getSetting("password");
+               $this->send("PASS", $password);
+               $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
+       }
+       
        private function parseLine($line) {
                $highExplode = explode(" :", $line, 2);
                $tokens = explode(" ", $highExplode[0]);
                if(count($highExplode) > 1)
                        $tokens[] = $highExplode[1];
                $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
-               if($tokens[0] == "ERROR") $cmdPos = 0; //override
                if($cmdPos == 1) $from = $tokens[0];
                else $from = null;
                $arguments = array_slice($tokens, $cmdPos + 1);
@@ -169,6 +179,13 @@ class Uplink {
                                $this->recv_error($from, $arguments);
                                break;
                //P10 Session
+                       case "S":
+                               $this->recv_server($from, $arguments);
+                               break;
+                       case "G":
+                               $this->recv_ping($from, $arguments);
+                               break;
+               //default
                        default:
                                //unknown cmd
                                break;
@@ -217,6 +234,7 @@ class Uplink {
                        }
                        $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
                        $this->server->add_server($new_server);
+                       $this->flags |= self::FLAG_P10SESSION;
                } else {
                        //another server got a new slave server ^^
                        $server = P10_Server::getServerByNum($from);
@@ -229,6 +247,10 @@ class Uplink {
                }
        }
        
+       private function recv_ping($from, $args) {
+               $this->send("Z", $args[0]); //simply PONG
+       }
+       
 }
 
 ?>
\ No newline at end of file