mirror of
https://github.com/jwinterm/xmr-stak.git
synced 2026-10-17 05:41:41 +02:00
baish mircohttpd integration
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include "jconf.h"
|
||||
#include "console.h"
|
||||
#include "donate-level.h"
|
||||
#include "httpd.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@@ -69,6 +70,12 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!httpd::inst()->start_daemon())
|
||||
{
|
||||
win_exit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
printer::inst()->print_str("-------------------------------------------------------------------\n");
|
||||
printer::inst()->print_str("XMR-Stak-CPU mining software, CPU Version.\n");
|
||||
printer::inst()->print_str("Based on CPU mining code by wolf9466 (heavily optimized by myself).\n");
|
||||
|
||||
+94
-37
@@ -414,19 +414,19 @@ void executor::ex_main()
|
||||
break;
|
||||
|
||||
case EV_USR_HASHRATE:
|
||||
hashrate_report();
|
||||
break;
|
||||
|
||||
case EV_USR_RESULTS:
|
||||
result_report();
|
||||
case EV_USR_CONNSTAT:
|
||||
print_report(ev.iName);
|
||||
break;
|
||||
|
||||
case EV_USR_CONNSTAT:
|
||||
connection_report();
|
||||
case EV_HTML_HASHRATE:
|
||||
case EV_HTML_RESULTS:
|
||||
case EV_HTML_CONNSTAT:
|
||||
http_report(ev.iName);
|
||||
break;
|
||||
|
||||
case EV_HASHRATE_LOOP:
|
||||
hashrate_report();
|
||||
print_report(EV_USR_HASHRATE);
|
||||
push_timed_event(ex_event(EV_HASHRATE_LOOP), jconf::inst()->GetAutohashTime());
|
||||
break;
|
||||
|
||||
@@ -451,23 +451,22 @@ inline const char* hps_format(double h, char* buf, size_t l)
|
||||
return " (na)";
|
||||
}
|
||||
|
||||
void executor::hashrate_report()
|
||||
void executor::hashrate_report(std::string& out)
|
||||
{
|
||||
std::string output;
|
||||
char num[32];
|
||||
size_t nthd = pvThreads->size();
|
||||
|
||||
output.reserve(256 + nthd * 64);
|
||||
out.reserve(256 + nthd * 64);
|
||||
|
||||
double fTotal[3] = { 0.0, 0.0, 0.0};
|
||||
size_t i;
|
||||
|
||||
output.append("HASHRATE REPORT\n");
|
||||
output.append("| ID | 2.5s | 60s | 15m |");
|
||||
out.append("HASHRATE REPORT\n");
|
||||
out.append("| ID | 2.5s | 60s | 15m |");
|
||||
if(nthd != 1)
|
||||
output.append(" ID | 2.5s | 60s | 15m |\n");
|
||||
out.append(" ID | 2.5s | 60s | 15m |\n");
|
||||
else
|
||||
output.append(1, '\n');
|
||||
out.append(1, '\n');
|
||||
|
||||
for (i = 0; i < nthd; i++)
|
||||
{
|
||||
@@ -478,36 +477,34 @@ void executor::hashrate_report()
|
||||
fHps[2] = telem->calc_telemetry_data(900000, i);
|
||||
|
||||
snprintf(num, sizeof(num), "| %2u |", (unsigned int)i);
|
||||
output.append(num);
|
||||
output.append(hps_format(fHps[0], num, sizeof(num))).append(" |");
|
||||
output.append(hps_format(fHps[1], num, sizeof(num))).append(" |");
|
||||
output.append(hps_format(fHps[2], num, sizeof(num))).append(1, ' ');
|
||||
out.append(num);
|
||||
out.append(hps_format(fHps[0], num, sizeof(num))).append(" |");
|
||||
out.append(hps_format(fHps[1], num, sizeof(num))).append(" |");
|
||||
out.append(hps_format(fHps[2], num, sizeof(num))).append(1, ' ');
|
||||
|
||||
fTotal[0] += fHps[0];
|
||||
fTotal[1] += fHps[1];
|
||||
fTotal[2] += fHps[2];
|
||||
|
||||
if((i & 0x1) == 1) //Odd i's
|
||||
output.append("|\n");
|
||||
out.append("|\n");
|
||||
}
|
||||
|
||||
if((i & 0x1) == 1) //We had odd number of threads
|
||||
output.append("|\n");
|
||||
out.append("|\n");
|
||||
|
||||
if(nthd != 1)
|
||||
output.append("-----------------------------------------------------\n");
|
||||
out.append("-----------------------------------------------------\n");
|
||||
else
|
||||
output.append("---------------------------\n");
|
||||
out.append("---------------------------\n");
|
||||
|
||||
output.append("Totals: ");
|
||||
output.append(hps_format(fTotal[0], num, sizeof(num)));
|
||||
output.append(hps_format(fTotal[1], num, sizeof(num)));
|
||||
output.append(hps_format(fTotal[2], num, sizeof(num)));
|
||||
output.append(" H/s\nHighest: ");
|
||||
output.append(hps_format(fHighestHps, num, sizeof(num)));
|
||||
output.append(" H/s\n");
|
||||
|
||||
printer::inst()->print_str(output.c_str());
|
||||
out.append("Totals: ");
|
||||
out.append(hps_format(fTotal[0], num, sizeof(num)));
|
||||
out.append(hps_format(fTotal[1], num, sizeof(num)));
|
||||
out.append(hps_format(fTotal[2], num, sizeof(num)));
|
||||
out.append(" H/s\nHighest: ");
|
||||
out.append(hps_format(fHighestHps, num, sizeof(num)));
|
||||
out.append(" H/s\n");
|
||||
}
|
||||
|
||||
char* time_format(char* buf, size_t len, std::chrono::system_clock::time_point time)
|
||||
@@ -531,12 +528,11 @@ char* time_format(char* buf, size_t len, std::chrono::system_clock::time_point t
|
||||
return buf;
|
||||
}
|
||||
|
||||
void executor::result_report()
|
||||
void executor::result_report(std::string& out)
|
||||
{
|
||||
char num[128];
|
||||
char date[32];
|
||||
|
||||
std::string out;
|
||||
out.reserve(1024);
|
||||
|
||||
size_t iGoodRes = vMineResults[0].count, iTotalRes = iGoodRes;
|
||||
@@ -594,16 +590,13 @@ void executor::result_report()
|
||||
}
|
||||
else
|
||||
out.append("Yay! No errors.\n");
|
||||
|
||||
printer::inst()->print_str(out.c_str());
|
||||
}
|
||||
|
||||
void executor::connection_report()
|
||||
void executor::connection_report(std::string& out)
|
||||
{
|
||||
char num[128];
|
||||
char date[32];
|
||||
|
||||
std::string out;
|
||||
out.reserve(512);
|
||||
|
||||
jpsock* pool = pick_pool_by_id(dev_pool_id + 1);
|
||||
@@ -638,6 +631,70 @@ void executor::connection_report()
|
||||
}
|
||||
else
|
||||
out.append("Yay! No errors.\n");
|
||||
}
|
||||
|
||||
void executor::print_report(ex_event_name ev)
|
||||
{
|
||||
std::string out;
|
||||
switch(ev)
|
||||
{
|
||||
case EV_USR_HASHRATE:
|
||||
hashrate_report(out);
|
||||
break;
|
||||
|
||||
case EV_USR_RESULTS:
|
||||
result_report(out);
|
||||
break;
|
||||
|
||||
case EV_USR_CONNSTAT:
|
||||
connection_report(out);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
printer::inst()->print_str(out.c_str());
|
||||
}
|
||||
|
||||
void executor::http_report(ex_event_name ev)
|
||||
{
|
||||
assert(pHttpString != nullptr);
|
||||
|
||||
switch(ev)
|
||||
{
|
||||
case EV_HTML_HASHRATE:
|
||||
hashrate_report(*pHttpString);
|
||||
break;
|
||||
|
||||
case EV_HTML_RESULTS:
|
||||
result_report(*pHttpString);
|
||||
break;
|
||||
|
||||
case EV_HTML_CONNSTAT:
|
||||
connection_report(*pHttpString);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
httpReady.set_value();
|
||||
}
|
||||
|
||||
void executor::get_http_report(ex_event_name ev_id, std::string& data)
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(httpMutex);
|
||||
|
||||
assert(pHttpString == nullptr);
|
||||
assert(ev_id == EV_HTML_HASHRATE || ev_id == EV_HTML_RESULTS || ev_id == EV_HTML_CONNSTAT);
|
||||
|
||||
pHttpString = &data;
|
||||
httpReady = std::promise<void>();
|
||||
std::future<void> ready = httpReady.get_future();
|
||||
|
||||
push_event(ex_event(ev_id));
|
||||
|
||||
ready.wait();
|
||||
pHttpString = nullptr;
|
||||
}
|
||||
|
||||
+13
-3
@@ -4,6 +4,7 @@
|
||||
#include <atomic>
|
||||
#include <array>
|
||||
#include <list>
|
||||
#include <future>
|
||||
|
||||
class jpsock;
|
||||
class minethd;
|
||||
@@ -21,6 +22,8 @@ public:
|
||||
void ex_start() { my_thd = new std::thread(&executor::ex_main, this); }
|
||||
void ex_main();
|
||||
|
||||
void get_http_report(ex_event_name ev_id, std::string& data);
|
||||
|
||||
inline void push_event(ex_event&& ev) { oEventQ.push(std::move(ev)); }
|
||||
void push_timed_event(ex_event&& ev, size_t sec);
|
||||
|
||||
@@ -69,9 +72,16 @@ private:
|
||||
void ex_clock_thd();
|
||||
void pool_connect(jpsock* pool);
|
||||
|
||||
void hashrate_report();
|
||||
void result_report();
|
||||
void connection_report();
|
||||
void hashrate_report(std::string& out);
|
||||
void result_report(std::string& out);
|
||||
void connection_report(std::string& out);
|
||||
|
||||
void http_report(ex_event_name ev);
|
||||
void print_report(ex_event_name ev);
|
||||
|
||||
std::string* pHttpString = nullptr;
|
||||
std::promise<void> httpReady;
|
||||
std::mutex httpMutex;
|
||||
|
||||
struct sck_error_log
|
||||
{
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
#include "msgstruct.h"
|
||||
#include "httpd.h"
|
||||
#include "console.h"
|
||||
#include "executor.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "libmicrohttpd/microhttpd.h"
|
||||
#define strcasecmp _stricmp
|
||||
#else
|
||||
#include <microhttpd.h>
|
||||
#endif // _WIN32
|
||||
|
||||
httpd* httpd::oInst = nullptr;
|
||||
|
||||
httpd::httpd()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int httpd::req_handler(void * cls,
|
||||
MHD_Connection* connection,
|
||||
const char* url,
|
||||
const char* method,
|
||||
const char* version,
|
||||
const char* upload_data,
|
||||
size_t* upload_data_size,
|
||||
void ** ptr)
|
||||
{
|
||||
struct MHD_Response * rsp;
|
||||
|
||||
if (strcmp(method, "GET") != 0)
|
||||
return MHD_NO;
|
||||
|
||||
*ptr = nullptr;
|
||||
|
||||
std::string str;
|
||||
if(strcasecmp(url, "/h") == 0 || strcasecmp(url, "/hashrate") == 0)
|
||||
{
|
||||
str.append("<html><head><title>Hashrate Report</title></head><body><pre>");
|
||||
executor::inst()->get_http_report(EV_HTML_HASHRATE, str);
|
||||
str.append("</pre></body></html>");
|
||||
|
||||
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
|
||||
}
|
||||
else if(strcasecmp(url, "/c") == 0 || strcasecmp(url, "/connection") == 0)
|
||||
{
|
||||
str.append("<html><head><title>Connection Report</title></head><body><pre>");
|
||||
executor::inst()->get_http_report(EV_HTML_CONNSTAT, str);
|
||||
str.append("</pre></body></html>");
|
||||
|
||||
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
|
||||
}
|
||||
else if(strcasecmp(url, "/r") == 0 || strcasecmp(url, "/results") == 0)
|
||||
{
|
||||
str.append("<html><head><title>Results Report</title></head><body><pre>");
|
||||
executor::inst()->get_http_report(EV_HTML_RESULTS, str);
|
||||
str.append("</pre></body></html>");
|
||||
|
||||
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
|
||||
}
|
||||
else
|
||||
{
|
||||
char buffer[1024];
|
||||
snprintf(buffer, sizeof(buffer), "<html><head><title>Error</title></head><body>"
|
||||
"<pre>Unkown url %s - please use /h, /r or /c as url</pre></body></html>", url);
|
||||
|
||||
rsp = MHD_create_response_from_buffer(strlen(buffer),
|
||||
(void*)buffer, MHD_RESPMEM_MUST_COPY);
|
||||
}
|
||||
|
||||
int ret = MHD_queue_response(connection, MHD_HTTP_OK, rsp);
|
||||
MHD_destroy_response(rsp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool httpd::start_daemon()
|
||||
{
|
||||
d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
|
||||
8100, NULL, NULL,
|
||||
&httpd::req_handler,
|
||||
NULL, MHD_OPTION_END);
|
||||
|
||||
if(d == nullptr)
|
||||
{
|
||||
printer::inst()->print_str("HTTP Daemon failed to start.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
struct MHD_Daemon;
|
||||
struct MHD_Connection;
|
||||
|
||||
class httpd
|
||||
{
|
||||
public:
|
||||
static httpd* inst()
|
||||
{
|
||||
if (oInst == nullptr) oInst = new httpd;
|
||||
return oInst;
|
||||
};
|
||||
|
||||
bool start_daemon();
|
||||
|
||||
private:
|
||||
httpd();
|
||||
static httpd* oInst;
|
||||
|
||||
static int req_handler(void * cls,
|
||||
MHD_Connection* connection,
|
||||
const char* url,
|
||||
const char* method,
|
||||
const char* version,
|
||||
const char* upload_data,
|
||||
size_t* upload_data_size,
|
||||
void ** ptr);
|
||||
|
||||
MHD_Daemon *d;
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -40,7 +40,7 @@ struct job_result
|
||||
enum ex_event_name { EV_INVALID_VAL, EV_SOCK_READY, EV_SOCK_ERROR,
|
||||
EV_POOL_HAVE_JOB, EV_MINER_HAVE_RESULT, EV_PERF_TICK, EV_RECONNECT,
|
||||
EV_SWITCH_POOL, EV_DEV_POOL_EXIT, EV_USR_HASHRATE, EV_USR_RESULTS, EV_USR_CONNSTAT,
|
||||
EV_HASHRATE_LOOP };
|
||||
EV_HASHRATE_LOOP, EV_HTML_HASHRATE, EV_HTML_RESULTS, EV_HTML_CONNSTAT };
|
||||
|
||||
/*
|
||||
This is how I learned to stop worrying and love c++11 =).
|
||||
|
||||
Reference in New Issue
Block a user