forked from tpruvot/cpuminer-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-sample.php
137 lines (113 loc) · 2.53 KB
/
local-sample.php
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Sample Request API to ccminer
*/
defined('API_HOST') || define('API_HOST', '127.0.0.1');
defined('API_PORT') || define('API_PORT', 4048);
// 2 seconds max.
set_time_limit(2);
function getsock($port)
{
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) {
$error = socket_strerror(socket_last_error());
$msg = "socket create($port) failed";
echo "ERR: $msg '$error'\n";
return NULL;
}
socket_set_nonblock($socket);
$res = socket_connect($socket, API_HOST, $port);
$timeout = 50;
while ($res === false && $timeout > 0) {
$err = socket_last_error($socket);
echo ".";
if ($timeout > 1 && ($err == 115 || $err == 114)) {
$timeout--;
usleep(50);
$res = socket_connect($socket, API_HOST, $port);
continue;
}
$error = socket_strerror($err);
$msg = "socket connect($port) failed";
echo "ERR: $msg '$error'\n";
socket_close($socket);
return NULL;
}
socket_set_block($socket);
return $socket;
}
function readsockline($socket)
{
$line = '';
while (true) {
$byte = socket_read($socket, 1);
if ($byte === false || $byte === '')
break;
if ($byte === "\0")
break;
$line .= $byte;
}
return $line;
}
function request($cmd)
{
$socket = getsock(API_PORT);
if ($socket == null)
return NULL;
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);
if (strlen($line) == 0) {
echo "WARN: '$cmd' returned nothing\n";
return $line;
}
echo "$cmd returned '$line'\n";
$data = array();
$objs = explode('|', $line);
foreach ($objs as $obj)
{
if (strlen($obj) > 0)
{
$items = explode(';', $obj);
$item = $items[0];
$id = explode('=', $items[0], 2);
if (count($id) == 1)
$name = $id[0];
else
$name = $id[0].$id[1];
if (strlen($name) == 0)
$name = 'null';
if (isset($data[$name])) {
$num = 1;
while (isset($data[$name.$num]))
$num++;
$name .= $num;
}
$counter = 0;
foreach ($items as $item)
{
$id = explode('=', $item, 2);
if (count($id) == 2)
$data[$name][$id[0]] = $id[1];
else
$data[$name][$counter] = $id[0];
$counter++;
}
}
}
if ($cmd == 'summary')
return array_pop($data);
else
return $data;
}
ob_start();
error_reporting(0);
$summary = request('summary');
$threads = request('threads');
$histo = request('histo');
ob_end_clean(); /* swap to debug */
//echo ob_get_clean()."\n";
header("Content-Type: application/json");
echo json_encode(compact('summary', 'threads', 'histo'))."\n";
?>