Browse Source

api-sample: use non blocking sockets

fix php warnings and enable cpuminer compat
master
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(
//'EPSYTOUR'=>'epsytour.php', /* copy local.php file and edit target IP:PORT */ //'EPSYTOUR'=>'epsytour.php', /* copy local.php file and edit target IP:PORT */
); );
// 5 seconds max. // 3 seconds max.
set_time_limit(5); set_time_limit(3);
error_reporting(0); error_reporting(0);
function getdataFromPears() function getdataFromPears()
@ -26,7 +26,7 @@ function getdataFromPears()
function ignoreField($key) function ignoreField($key)
{ {
$ignored = array('API','VER','GPU','CARD'); $ignored = array('API','VER','GPU','CARD','GPUS','CPU');
return in_array($key, $ignored); return in_array($key, $ignored);
} }
@ -38,6 +38,7 @@ function translateField($key)
$intl['ALGO'] = 'Algorithm'; $intl['ALGO'] = 'Algorithm';
$intl['GPUS'] = 'GPUs'; $intl['GPUS'] = 'GPUs';
$intl['CPUS'] = 'Threads';
$intl['KHS'] = 'Hash rate (kH/s)'; $intl['KHS'] = 'Hash rate (kH/s)';
$intl['ACC'] = 'Accepted shares'; $intl['ACC'] = 'Accepted shares';
$intl['ACCMN'] = 'Accepted / mn'; $intl['ACCMN'] = 'Accepted / mn';
@ -63,11 +64,19 @@ function translateValue($key,$val,$data=array())
case 'UPTIME': case 'UPTIME':
$min = floor(intval($val) / 60); $min = floor(intval($val) / 60);
$sec = 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; break;
case 'NAME': case 'NAME':
$val = $data['NAME'].' '.$data['VER']; $val = $data['NAME'].' '.$data['VER'];
break; break;
case 'FREQ':
$val = sprintf("%d MHz", round(floatval($val)/1000.0));
break;
case 'TS': case 'TS':
$val = strftime("%H:%M:%S", (int) $val); $val = strftime("%H:%M:%S", (int) $val);
break; break;
@ -83,14 +92,14 @@ function displayData($data)
$htm .= '<table id="tb_'.$name.'" class="stats">'."\n"; $htm .= '<table id="tb_'.$name.'" class="stats">'."\n";
$htm .= '<tr><th class="machine" colspan="2">'.$name."</th></tr>\n"; $htm .= '<tr><th class="machine" colspan="2">'.$name."</th></tr>\n";
if (!empty($stats)) { if (!empty($stats)) {
$summary = $stats['summary']; $summary = (array) $stats['summary'];
foreach ($summary as $key=>$val) { foreach ($summary as $key=>$val) {
if (!empty($val) && !ignoreField($key)) if (!empty($val) && !ignoreField($key))
$htm .= '<tr><td class="key">'.translateField($key).'</td>'. $htm .= '<tr><td class="key">'.translateField($key).'</td>'.
'<td class="val">'.translateValue($key, $val, $summary)."</td></tr>\n"; '<td class="val">'.translateValue($key, $val, $summary)."</td></tr>\n";
} }
if (isset($summary['KHS'])) if (isset($summary['KHS']))
$totals[$summary['ALGO']] += floatval($summary['KHS']); @ $totals[$summary['ALGO']] += floatval($summary['KHS']);
foreach ($stats['threads'] as $g=>$gpu) { foreach ($stats['threads'] as $g=>$gpu) {
$card = isset($gpu['CARD']) ? $gpu['CARD'] : ''; $card = isset($gpu['CARD']) ? $gpu['CARD'] : '';
$htm .= '<tr><th class="gpu" colspan="2">'.$g." $card</th></tr>\n"; $htm .= '<tr><th class="gpu" colspan="2">'.$g." $card</th></tr>\n";
@ -117,6 +126,7 @@ function displayData($data)
$data = getdataFromPears(); $data = getdataFromPears();
?> ?>
<html> <html>
<head> <head>
<title>ccminer rig api sample</title> <title>ccminer rig api sample</title>
@ -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.key { width: 99px; max-width: 180px; }
td.val { width: 40px; max-width: 100px; color: white; } 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 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; } 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> </style>
</head> </head>

31
api/local-sample.php

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

Loading…
Cancel
Save