2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
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. *
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. *
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. *
21 ************************************************************************
23 * Uplink/Uplink.class.php
25 * This file contains the basic P10 Protocol handler.
27 ************************************************************************
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.
35 * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
36 * sets the Uplink connection information.
38 * void setLoopTimeout(int $timeout)
39 * sets the maximum time loop() is blocking the script execution.
41 * void setUplinkServer(int $numeric, String $name, String $password)
42 * sets the own P10 Server information.
46 require_once("Client.class.php");
47 require_once("Numerics.class.php");
50 private $client = new Client();
51 private $settings = array();
53 public function __construct() {
54 $this->setSettings("recv_timeout", 1000);
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.");
65 $state = $client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
67 usleep($this->getSetting("recv_timeout") / 1000);
71 //try to receive new data from the Uplink
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);
82 public function setLoopTimeout($timeout) {
83 $this->setSetting("recv_timeout", $timeout);
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);
92 private function setSetting($setting, $value) {
93 $this->settings[$setting] = $value;
96 private function getSetting($setting) {
97 if(array_key_exists($setting, $this->settings)) {
98 return $this->settings[$setting];