Subversion Repositories HomeAutomation

Rev

Rev 2111 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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