Browse Source

api-sample: use non blocking sockets

fix php warnings and enable cpuminer compat
2upstream
Tanguy Pruvot 10 years ago
parent
commit
851a26c2a2
  1. 26
      api/index.php
  2. 31
      api/local-sample.php

26
api/index.php

@ -7,8 +7,8 @@ $configs = array( @@ -7,8 +7,8 @@ $configs = array(
//'EPSYTOUR'=>'epsytour.php', /* copy local.php file and edit target IP:PORT */
);
// 5 seconds max.
set_time_limit(5);
// 3 seconds max.
set_time_limit(3);
error_reporting(0);
function getdataFromPears()
@ -26,7 +26,7 @@ function getdataFromPears() @@ -26,7 +26,7 @@ function getdataFromPears()
function ignoreField($key)
{
$ignored = array('API','VER','GPU','CARD');
$ignored = array('API','VER','GPU','CARD','GPUS','CPU');
return in_array($key, $ignored);
}
@ -38,6 +38,7 @@ function translateField($key) @@ -38,6 +38,7 @@ function translateField($key)
$intl['ALGO'] = 'Algorithm';
$intl['GPUS'] = 'GPUs';
$intl['CPUS'] = 'Threads';
$intl['KHS'] = 'Hash rate (kH/s)';
$intl['ACC'] = 'Accepted shares';
$intl['ACCMN'] = 'Accepted / mn';
@ -63,11 +64,19 @@ function translateValue($key,$val,$data=array()) @@ -63,11 +64,19 @@ function translateValue($key,$val,$data=array())
case 'UPTIME':
$min = floor(intval($val) / 60);
$sec = intval($val) % 60;
$val = "${min}mn ${sec}s";
$val = "${min}mn${sec}s";
if ($min > 180) {
$hrs = floor($min / 60);
$min = $min % 60;
$val = "${hrs}h${min}mn";
}
break;
case 'NAME':
$val = $data['NAME'].' '.$data['VER'];
break;
case 'FREQ':
$val = sprintf("%d MHz", round(floatval($val)/1000.0));
break;
case 'TS':
$val = strftime("%H:%M:%S", (int) $val);
break;
@ -83,14 +92,14 @@ function displayData($data) @@ -83,14 +92,14 @@ function displayData($data)
$htm .= '<table id="tb_'.$name.'" class="stats">'."\n";
$htm .= '<tr><th class="machine" colspan="2">'.$name."</th></tr>\n";
if (!empty($stats)) {
$summary = $stats['summary'];
$summary = (array) $stats['summary'];
foreach ($summary as $key=>$val) {
if (!empty($val) && !ignoreField($key))
$htm .= '<tr><td class="key">'.translateField($key).'</td>'.
'<td class="val">'.translateValue($key, $val, $summary)."</td></tr>\n";
}
if (isset($summary['KHS']))
$totals[$summary['ALGO']] += floatval($summary['KHS']);
@ $totals[$summary['ALGO']] += floatval($summary['KHS']);
foreach ($stats['threads'] as $g=>$gpu) {
$card = isset($gpu['CARD']) ? $gpu['CARD'] : '';
$htm .= '<tr><th class="gpu" colspan="2">'.$g." $card</th></tr>\n";
@ -117,6 +126,7 @@ function displayData($data) @@ -117,6 +126,7 @@ function displayData($data)
$data = getdataFromPears();
?>
<html>
<head>
<title>ccminer rig api sample</title>
@ -164,10 +174,10 @@ th.gpu { color: white; padding: 3px 3px; font: bolder; text-align: left; backgro @@ -164,10 +174,10 @@ th.gpu { color: white; padding: 3px 3px; font: bolder; text-align: left; backgro
td.key { width: 99px; max-width: 180px; }
td.val { width: 40px; max-width: 100px; color: white; }
div.totals { margin: 16px; }
div.totals { margin: 16px; padding-bottom: 16px; }
div.totals h2 { color: darkcyan; font-size: 16px; margin-bottom: 4px; }
div.totals li { list-style-type: none; font-size: 16px; margin-left: 4px; margin-bottom: 8px; }
li span.algo { display: inline-block; width: 50px; max-width: 120px; }
li span.algo { display: inline-block; width: 100px; max-width: 180px; }
</style>
</head>

31
api/local-sample.php

@ -5,9 +5,8 @@ @@ -5,9 +5,8 @@
defined('API_HOST') || define('API_HOST', '127.0.0.1');
defined('API_PORT') || define('API_PORT', 4068);
// 3 seconds max.
set_time_limit(3);
error_reporting(0);
// 2 seconds max.
set_time_limit(2);
function getsock($port)
{
@ -20,14 +19,28 @@ function getsock($port) @@ -20,14 +19,28 @@ function getsock($port)
return NULL;
}
socket_set_nonblock($socket);
$res = socket_connect($socket, API_HOST, $port);
if ($res === false) {
$error = socket_strerror(socket_last_error());
$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;
}
@ -110,12 +123,14 @@ function request($cmd) @@ -110,12 +123,14 @@ function request($cmd)
ob_start();
error_reporting(0);
$summary = request('summary');
$threads = request('threads');
$histo = array();//request('histo'); /* only enable it if required... */
$histo = request('histo');
ob_end_clean();
//echo ob_get_clean()."\n"; /* allow to print protocol debug message */
ob_end_clean(); /* swap to debug */
//echo ob_get_clean()."\n";
header("Content-Type: application/json");
echo json_encode(compact('summary', 'threads', 'histo'))."\n";

Loading…
Cancel
Save