Subversion Repositories HomeAutomation

Rev

Rev 2111 | 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 * 8);
  25.  
  26.   while (strpos($result, "\n") === FALSE)
  27.   {
  28.     $result .= fread($socket, 8096 * 8);
  29.   }
  30.  
  31.   return $result;
  32. }
  33.  
  34. function atomjs_write($socket, $command)
  35. {
  36.   if (feof($socket))
  37.   {
  38.     throw new Exception("socket not open");
  39.   }
  40.  
  41.   if (fwrite($socket, $command) === FALSE)
  42.   {
  43.     throw new Exception("failed to write");
  44.   }
  45. }
  46.  
  47. function atomjs_data_available($socket)
  48. {
  49.   $read   = array($socket);
  50.   $write  = NULL;
  51.   $except = NULL;
  52.  
  53.   if (false === ($num_changed_streams = stream_select($read, $write, $except, 0)))
  54.   {
  55.     throw new Exception("could not do select on socket");
  56.   }
  57.  
  58.   return $num_changed_streams > 0;
  59. }
  60.  
  61. ?>