Download: php-orbited.php

File php-orbited.php, 3.8 kB (added by frank, 6 months ago)
Line 
1<?php
2/**
3 *  Orbited PHP5 Client
4 * 
5 *  Copyright (c) 2007 Michael Zaic <mzaic@cafemom.com>, CafeMom.com
6 *  http://www.cafemom.com
7 *  http://www.orbited.org
8 *
9 *  Licensed under The MIT License: <http://www.opensource.org/licenses/mit-license.php>
10 */
11
12class OrbitedClient {
13   
14    // CONSTANTS
15    const   BUFFER_SIZE = 2048;
16    const   LINE_END    = "\r\n";
17    const   VERSION     = 'Orbit 1.0';
18   
19    // PRIVATE VARIABLES
20    private $address;
21    private $port;
22    private $socket;
23    private $error_code;
24    private $error_string;
25    private $id;
26    private $connected;
27   
28    // CONSTRUCTOR
29    function __construct($address, $port)
30    {
31        $this->address      = $address;
32        $this->port         = $port;
33        $this->socket       = null;
34        $this->id           = 0;
35        $this->connected    = false;
36    }
37   
38    // PRIVATE FUNCTIONS
39    private function sendline( $line = '' )
40    {
41        $line = $line.self::LINE_END;
42        fwrite($this->socket, $line);
43        return $line;
44    }
45   
46    private function read_response()
47    {
48        $contents = '';
49        while(1) {
50            $packet = fread($this->socket, self::BUFFER_SIZE);
51            $contents .= $packet;
52            if(substr($packet, -2) == self::LINE_END) { break; }
53        }
54        return $contents;
55    }
56   
57    // PUBLIC FUNCTIONS
58    public function connect()
59    {
60        if($this->socket) {
61            return;
62        }
63        else {
64            $this->socket = fsockopen($this->address, $this->port, $this->error_code, $this->error_string);
65            if($this->socket) {
66                $this->connected = true;
67                return;
68            }
69            else{
70                $this->error();
71            }
72        }
73    }
74   
75    public function disconnect()
76    {
77        if($connected) {
78            fclose($this->socket);
79            $this->socket    = null;
80            $this->connected = false;
81        }
82       
83        return;
84    }
85   
86    public function reconnect()
87    {
88        $this->disconnect();
89        $this->connect();
90        return;
91    }
92   
93    public function event($recipients, $body, $json = true, $retry = true)
94    {
95        if(!$this->connected){ $this->connect(); }
96        if($json) { $body = json_encode($body); }
97       
98        try {
99            if(!is_array($recipients)) {
100                $recipients = array($recipients);
101            }
102            if(!$this->socket) { 
103                throw new Exception('Connection Lost');
104            }
105            try {
106                $this->id = $this->id + 1;
107                $this->sendline(self::VERSION);
108                $this->sendline('Event');
109                $this->sendline('id: '.$this->id);
110                foreach($recipients as $recipient){
111                    $recipient = (string)$recipient;
112                    $this->sendline('recipient: '.$recipient);
113                }
114                $this->sendline('length: '.strlen($body));
115                $this->sendline();
116                fwrite($this->socket, $body);
117                return $this->read_response();
118            } catch (Exception $e) {
119                $this->disconnect();
120                throw new Exception('Connection Lost');
121            }
122        } catch (Exception $e) {
123            if($retry) {
124                $this->reconnect();
125                $this->event($recipients, $body, $json, false);
126            }
127            else {
128                throw new Exception('Send Failed');
129            }
130        }
131    }
132   
133    public function get_error_code()
134    {
135        return $this->error_code;
136    }
137   
138    public function get_error_string()
139    {
140        return $this->error_string;
141    }
142}
143?>