Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. function atomjs_connect($host, $port)
  6. {
  7.   $socket = pfsockopen($host, $port, $errno, $errstr);
  8.  
  9.   if (!$socket)
  10.   {
  11.     throw new Exception("$errstr ($errno)");
  12.   }
  13.  
  14.   return $socket;
  15. }
  16.  
  17. function atomjs_read($socket)
  18. {
  19.   if (feof($socket))
  20.   {
  21.     throw new Exception("socket not open");
  22.   }
  23.  
  24.   $result = fread($socket, 8096);
  25.  
  26.   return $result;
  27. }
  28.  
  29. function atomjs_write($socket, $command)
  30. {
  31.   if (feof($socket))
  32.   {
  33.     throw new Exception("socket not open");
  34.   }
  35.  
  36.   if (fwrite($socket, $command) === FALSE)
  37.   {
  38.     throw new Exception("failed to write");
  39.   }
  40. }
  41.  
  42. function atomjs_data_available($socket)
  43. {
  44.   $read   = array($socket);
  45.   $write  = NULL;
  46.   $except = NULL;
  47.  
  48.   if (false === ($num_changed_streams = stream_select($read, $write, $except, 0)))
  49.   {
  50.     throw new Exception("could not do select on socket");
  51.   }
  52.  
  53.   return $num_changed_streams > 0;
  54. }
  55.  
  56. ?>