start writing PHP P10 pack v2 (hopefully without bugs :D)
[PHP-P10.git] / Uplink / Uplink.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/Uplink.class.php
24  *
25  * This file contains the basic P10 Protocol handler.
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * void loop() 
31  *     loop function that should be calles as many times as possible. 
32  *     It reads from the socket and BLOCKS the script execution for a 
33  *     specific time if nothing is received.
34  *
35  * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
36  *     sets the Uplink connection information.
37  *
38  * void setLoopTimeout(int $timeout)
39  *     sets the maximum time loop() is blocking the script execution.
40  *
41  * void setUplinkServer(int $numeric, String $name, String $password)
42  *     sets the own P10 Server information.
43  *
44  *
45  */
46 require_once("Client.class.php");
47 require_once("Numerics.class.php");
48
49 class Uplink {
50         private $client = new Client();
51         private $settings = array();
52         
53         public function __construct() {
54                 $this->setSettings("recv_timeout", 1000);
55         }
56         
57         public function loop() {
58                 if(!$client->connected()) {
59                         $host = $this->getSetting("host");
60                         $port = $this->getSetting("port");
61                         if($host == null || $port == null) {
62                                 Throw new Exception("Uplink Settings missing.");
63                                 return;
64                         }
65                         $state = $client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
66                         if(!$state) {
67                                 usleep($this->getSetting("recv_timeout") / 1000);
68                                 return;
69                         }
70                 }
71                 //try to receive new data from the Uplink
72                 //null
73         }
74         
75         public function setUplink($host, $port, $ssl = false, $bind = null) {
76                 $this->setSetting("host", $host);
77                 $this->setSetting("port", $port);
78                 $this->setSetting("ssl", $ssl);
79                 $this->setSetting("bind", $bind);
80         }
81         
82         public function setLoopTimeout($timeout) {
83                 $this->setSetting("recv_timeout", $timeout);
84         }
85         
86         public function setUplinkServer($numeric, $name, $password) {
87                 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
88                 $this->setSetting("name", $name);
89                 $this->setSetting("password", $password);
90         }
91         
92         private function setSetting($setting, $value) {
93                 $this->settings[$setting] = $value;
94         }
95         
96         private function getSetting($setting) {
97                 if(array_key_exists($setting, $this->settings)) {
98                         return $this->settings[$setting];
99                 } else {
100                         return null;
101                 }
102         }
103         
104 }
105
106 ?>