Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fc8543bad7
Fetching contributors…

Cannot retrieve contributors at this time

file 91 lines (75 sloc) 2.633 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<?php
/**
* Fnordmetric PHP API
* Simple class to send events to Fnordmetric
* TODO: Bit more documentation
* TODO: Support ipv6 maybe?
**/
require_once __DIR__.'/SocketErrorException.php';
class FnordmetricApi {

  protected $socket;
  public $host,
            $port;

  /* store the host and port for future reference */
  public function __construct($host = '0.0.0.0', $port = 1337) {
    $this->host = $host;
    $this->port = $port;
  }

  /* close the socket connection when the class is no longer needed */
  public function __destruct() {
    $this->closeSocket();
  }

  /* send an event to the fnordmetric server */
  public function event($name, Array $data = array()) {
    $data = $this->formatData($name, $data);
    $this->write($data);
  }

  /* close the connection manually if necessary */
  public function close() {
    $this->closeSocket();
  }

  /* add the event name to the data array, and json encode the lot of it */
  private function formatData($name, $data = array()) {
    $data['_type'] = $name;
    return json_encode($data)."\n";
  }

  /* open up a socket connection */
  private function openSocket() {
    // create the TCP socket first
    $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (!$this->socket) throw new SocketErrorException;

    // add client connection to socket (ie. us)
    $client = socket_bind($this->socket, '127.0.0.1');
    if (!$client) throw new SocketErrorException;

    // connect to the Fnordmetric server
    $server = socket_connect($this->socket, $this->host, $this->port);
    if (!$server) throw new SocketErrorException;
  }

  /*
* write the data to the socket
* adapted from http://www.php.net/manual/en/function.socket-write.php#101492
*/
  private function write($data) {
    if (!$this->socketIsOpen()) $this->openSocket();

    $length = strlen($data);
    $offset = 0;

    while ($offset < $length) {
      $sent_data = socket_write($this->socket, substr($data, $offset), $length - $offset);
      if (!$sent_data) throw new SocketErrorException;

      $offset += $sent_data;
    }

    return true; // if something went wrong, there'd be an exception thrown
  }

  /* check that the socket is open, but not necessarily connected (if the var is true and is also a resource) */
  private function socketIsOpen() {
    return (bool) $this->socket && is_resource($this->socket);
  }

  /* close the socket connection and nullify it */
  private function closeSocket() {
    if ($this->socketIsOpen()) {
      @socket_shutdown($this->socket, 2);
      socket_close($this->socket);
    }
    $this->socket = null;
  }
}
Something went wrong with that request. Please try again.