-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpc_core.php
executable file
·144 lines (125 loc) · 2.98 KB
/
rpc_core.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
138
139
140
141
142
143
144
<?php
global $workers;
$string = file_get_contents("server-config.json");
$config=json_decode($string,true);
$workers = $config['worker_ips'];
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 api_convert_escape($str)
{
$res = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
$ch = substr($str, $i, 1);
if ($ch != '\\' || $i == ($len-1))
$res .= $ch;
else
{
$i++;
$ch = substr($str, $i, 1);
switch ($ch)
{
case '|':
$res .= "\1";
break;
case '\\':
$res .= "\2";
break;
case '=':
$res .= "\3";
break;
case ',':
$res .= "\4";
break;
default:
$res .= $ch;
}
}
}
return $res;
}
function revert($str) {
return str_replace(array("\1", "\2", "\3", "\4"), array("|", "\\", "=", ","), $str);
}
function rpc($addr, $port, $command, $parameter, $json=true) {
$socksndtimeoutsec = 1;
$sockrcvtimeoutsec = 1;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) {
return "{'error': 'could not create socket'}";
}
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $socksndtimeoutsec, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockrcvtimeoutsec, 'usec' => 0));
$res = socket_connect($socket, $addr, $port);
if ($res == false) {
return "{'error': 'socket could not connect'}";
}
$cmd = '{"command": "'.$command.'", "parameter":"'.$parameter.'"}';
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);
if ($json) return json_decode($line, $assoc=true);
return $line;
}
function workerInfo($ip) {
$rvs = array();
$j = rpc($ip, 4028, 'gpucount', '');
if (!array_key_exists("GPUS", $j)) return "error";
$cnt = $j["GPUS"][0]["Count"];
for ($i=0; $i<$cnt; $i++) {
$lj = rpc($ip, 4028, 'gpu', $i);
if (!array_key_exists("GPU", $lj)) return "error";
array_push($rvs, $lj['GPU'][0]);
}
return $rvs;
}
if (isset($_REQUEST['update'])) {
global $workers;
$rvs = [];
foreach ($workers as $worker => $ip) {
$rvs[$worker] = workerInfo($ip);
}
echo json_encode($rvs);
}
if (isset($_REQUEST['save'])) {
$save = $_REQUEST['save'];
file_put_contents("server-config.json", $save);
}
if (isset($_REQUEST['ip'])) {
$ip = $_REQUEST['ip'];
$rv = rpc($ip, 4028, "summary", "");
echo json_encode($rv);
}
if (isset($_REQUEST['name'])) {
global $workers;
//$f = fopen('rlog.txt', 'a');
//$txt = json_encode($_REQUEST);
//fwrite($f, $txt."\n");
//fclose($f);
$nm = $_REQUEST['name'];
$val = $_REQUEST['value'];
$pk = $_REQUEST['pk'];
if (strlen($pk) > 0 && strstr($pk, '|')) {
$els = explode('|', $pk);
$gpu = $els[1];
$worker = $els[0];
$param = $gpu.",".$val;
} else {
$worker = $_REQUEST['worker'];
$param = $_REQUEST['param'];
}
$rv = rpc($workers[$worker], 4028, $nm, $param);
echo json_encode($rv);
}
?>