Skip to content

Commit 2033ed3

Browse files
committed
baish mircohttpd integration
1 parent 3025fe1 commit 2033ed3

File tree

7 files changed

+3118
-42
lines changed

7 files changed

+3118
-42
lines changed

cli-miner.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "jconf.h"
1919
#include "console.h"
2020
#include "donate-level.h"
21+
#include "httpd.h"
2122

2223
#include <stdlib.h>
2324
#include <stdio.h>
@@ -69,6 +70,12 @@ int main(int argc, char *argv[])
6970
return 0;
7071
}
7172

73+
if (!httpd::inst()->start_daemon())
74+
{
75+
win_exit();
76+
return 0;
77+
}
78+
7279
printer::inst()->print_str("-------------------------------------------------------------------\n");
7380
printer::inst()->print_str("XMR-Stak-CPU mining software, CPU Version.\n");
7481
printer::inst()->print_str("Based on CPU mining code by wolf9466 (heavily optimized by myself).\n");

executor.cpp

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -414,19 +414,19 @@ void executor::ex_main()
414414
break;
415415

416416
case EV_USR_HASHRATE:
417-
hashrate_report();
418-
break;
419-
420417
case EV_USR_RESULTS:
421-
result_report();
418+
case EV_USR_CONNSTAT:
419+
print_report(ev.iName);
422420
break;
423421

424-
case EV_USR_CONNSTAT:
425-
connection_report();
422+
case EV_HTML_HASHRATE:
423+
case EV_HTML_RESULTS:
424+
case EV_HTML_CONNSTAT:
425+
http_report(ev.iName);
426426
break;
427427

428428
case EV_HASHRATE_LOOP:
429-
hashrate_report();
429+
print_report(EV_USR_HASHRATE);
430430
push_timed_event(ex_event(EV_HASHRATE_LOOP), jconf::inst()->GetAutohashTime());
431431
break;
432432

@@ -451,23 +451,22 @@ inline const char* hps_format(double h, char* buf, size_t l)
451451
return " (na)";
452452
}
453453

454-
void executor::hashrate_report()
454+
void executor::hashrate_report(std::string& out)
455455
{
456-
std::string output;
457456
char num[32];
458457
size_t nthd = pvThreads->size();
459458

460-
output.reserve(256 + nthd * 64);
459+
out.reserve(256 + nthd * 64);
461460

462461
double fTotal[3] = { 0.0, 0.0, 0.0};
463462
size_t i;
464463

465-
output.append("HASHRATE REPORT\n");
466-
output.append("| ID | 2.5s | 60s | 15m |");
464+
out.append("HASHRATE REPORT\n");
465+
out.append("| ID | 2.5s | 60s | 15m |");
467466
if(nthd != 1)
468-
output.append(" ID | 2.5s | 60s | 15m |\n");
467+
out.append(" ID | 2.5s | 60s | 15m |\n");
469468
else
470-
output.append(1, '\n');
469+
out.append(1, '\n');
471470

472471
for (i = 0; i < nthd; i++)
473472
{
@@ -478,36 +477,34 @@ void executor::hashrate_report()
478477
fHps[2] = telem->calc_telemetry_data(900000, i);
479478

480479
snprintf(num, sizeof(num), "| %2u |", (unsigned int)i);
481-
output.append(num);
482-
output.append(hps_format(fHps[0], num, sizeof(num))).append(" |");
483-
output.append(hps_format(fHps[1], num, sizeof(num))).append(" |");
484-
output.append(hps_format(fHps[2], num, sizeof(num))).append(1, ' ');
480+
out.append(num);
481+
out.append(hps_format(fHps[0], num, sizeof(num))).append(" |");
482+
out.append(hps_format(fHps[1], num, sizeof(num))).append(" |");
483+
out.append(hps_format(fHps[2], num, sizeof(num))).append(1, ' ');
485484

486485
fTotal[0] += fHps[0];
487486
fTotal[1] += fHps[1];
488487
fTotal[2] += fHps[2];
489488

490489
if((i & 0x1) == 1) //Odd i's
491-
output.append("|\n");
490+
out.append("|\n");
492491
}
493492

494493
if((i & 0x1) == 1) //We had odd number of threads
495-
output.append("|\n");
494+
out.append("|\n");
496495

497496
if(nthd != 1)
498-
output.append("-----------------------------------------------------\n");
497+
out.append("-----------------------------------------------------\n");
499498
else
500-
output.append("---------------------------\n");
501-
502-
output.append("Totals: ");
503-
output.append(hps_format(fTotal[0], num, sizeof(num)));
504-
output.append(hps_format(fTotal[1], num, sizeof(num)));
505-
output.append(hps_format(fTotal[2], num, sizeof(num)));
506-
output.append(" H/s\nHighest: ");
507-
output.append(hps_format(fHighestHps, num, sizeof(num)));
508-
output.append(" H/s\n");
509-
510-
printer::inst()->print_str(output.c_str());
499+
out.append("---------------------------\n");
500+
501+
out.append("Totals: ");
502+
out.append(hps_format(fTotal[0], num, sizeof(num)));
503+
out.append(hps_format(fTotal[1], num, sizeof(num)));
504+
out.append(hps_format(fTotal[2], num, sizeof(num)));
505+
out.append(" H/s\nHighest: ");
506+
out.append(hps_format(fHighestHps, num, sizeof(num)));
507+
out.append(" H/s\n");
511508
}
512509

513510
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
531528
return buf;
532529
}
533530

534-
void executor::result_report()
531+
void executor::result_report(std::string& out)
535532
{
536533
char num[128];
537534
char date[32];
538535

539-
std::string out;
540536
out.reserve(1024);
541537

542538
size_t iGoodRes = vMineResults[0].count, iTotalRes = iGoodRes;
@@ -594,16 +590,13 @@ void executor::result_report()
594590
}
595591
else
596592
out.append("Yay! No errors.\n");
597-
598-
printer::inst()->print_str(out.c_str());
599593
}
600594

601-
void executor::connection_report()
595+
void executor::connection_report(std::string& out)
602596
{
603597
char num[128];
604598
char date[32];
605599

606-
std::string out;
607600
out.reserve(512);
608601

609602
jpsock* pool = pick_pool_by_id(dev_pool_id + 1);
@@ -638,6 +631,70 @@ void executor::connection_report()
638631
}
639632
else
640633
out.append("Yay! No errors.\n");
634+
}
635+
636+
void executor::print_report(ex_event_name ev)
637+
{
638+
std::string out;
639+
switch(ev)
640+
{
641+
case EV_USR_HASHRATE:
642+
hashrate_report(out);
643+
break;
644+
645+
case EV_USR_RESULTS:
646+
result_report(out);
647+
break;
648+
649+
case EV_USR_CONNSTAT:
650+
connection_report(out);
651+
break;
652+
default:
653+
assert(false);
654+
break;
655+
}
641656

642657
printer::inst()->print_str(out.c_str());
643658
}
659+
660+
void executor::http_report(ex_event_name ev)
661+
{
662+
assert(pHttpString != nullptr);
663+
664+
switch(ev)
665+
{
666+
case EV_HTML_HASHRATE:
667+
hashrate_report(*pHttpString);
668+
break;
669+
670+
case EV_HTML_RESULTS:
671+
result_report(*pHttpString);
672+
break;
673+
674+
case EV_HTML_CONNSTAT:
675+
connection_report(*pHttpString);
676+
break;
677+
default:
678+
assert(false);
679+
break;
680+
}
681+
682+
httpReady.set_value();
683+
}
684+
685+
void executor::get_http_report(ex_event_name ev_id, std::string& data)
686+
{
687+
std::lock_guard<std::mutex> lck(httpMutex);
688+
689+
assert(pHttpString == nullptr);
690+
assert(ev_id == EV_HTML_HASHRATE || ev_id == EV_HTML_RESULTS || ev_id == EV_HTML_CONNSTAT);
691+
692+
pHttpString = &data;
693+
httpReady = std::promise<void>();
694+
std::future<void> ready = httpReady.get_future();
695+
696+
push_event(ex_event(ev_id));
697+
698+
ready.wait();
699+
pHttpString = nullptr;
700+
}

executor.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <atomic>
55
#include <array>
66
#include <list>
7+
#include <future>
78

89
class jpsock;
910
class minethd;
@@ -21,6 +22,8 @@ class executor
2122
void ex_start() { my_thd = new std::thread(&executor::ex_main, this); }
2223
void ex_main();
2324

25+
void get_http_report(ex_event_name ev_id, std::string& data);
26+
2427
inline void push_event(ex_event&& ev) { oEventQ.push(std::move(ev)); }
2528
void push_timed_event(ex_event&& ev, size_t sec);
2629

@@ -69,9 +72,16 @@ class executor
6972
void ex_clock_thd();
7073
void pool_connect(jpsock* pool);
7174

72-
void hashrate_report();
73-
void result_report();
74-
void connection_report();
75+
void hashrate_report(std::string& out);
76+
void result_report(std::string& out);
77+
void connection_report(std::string& out);
78+
79+
void http_report(ex_event_name ev);
80+
void print_report(ex_event_name ev);
81+
82+
std::string* pHttpString = nullptr;
83+
std::promise<void> httpReady;
84+
std::mutex httpMutex;
7585

7686
struct sck_error_log
7787
{

httpd.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <string>
5+
6+
#include "msgstruct.h"
7+
#include "httpd.h"
8+
#include "console.h"
9+
#include "executor.h"
10+
11+
#ifdef _WIN32
12+
#include "libmicrohttpd/microhttpd.h"
13+
#define strcasecmp _stricmp
14+
#else
15+
#include <microhttpd.h>
16+
#endif // _WIN32
17+
18+
httpd* httpd::oInst = nullptr;
19+
20+
httpd::httpd()
21+
{
22+
23+
}
24+
25+
int httpd::req_handler(void * cls,
26+
MHD_Connection* connection,
27+
const char* url,
28+
const char* method,
29+
const char* version,
30+
const char* upload_data,
31+
size_t* upload_data_size,
32+
void ** ptr)
33+
{
34+
struct MHD_Response * rsp;
35+
36+
if (strcmp(method, "GET") != 0)
37+
return MHD_NO;
38+
39+
*ptr = nullptr;
40+
41+
std::string str;
42+
if(strcasecmp(url, "/h") == 0 || strcasecmp(url, "/hashrate") == 0)
43+
{
44+
str.append("<html><head><title>Hashrate Report</title></head><body><pre>");
45+
executor::inst()->get_http_report(EV_HTML_HASHRATE, str);
46+
str.append("</pre></body></html>");
47+
48+
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
49+
}
50+
else if(strcasecmp(url, "/c") == 0 || strcasecmp(url, "/connection") == 0)
51+
{
52+
str.append("<html><head><title>Connection Report</title></head><body><pre>");
53+
executor::inst()->get_http_report(EV_HTML_CONNSTAT, str);
54+
str.append("</pre></body></html>");
55+
56+
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
57+
}
58+
else if(strcasecmp(url, "/r") == 0 || strcasecmp(url, "/results") == 0)
59+
{
60+
str.append("<html><head><title>Results Report</title></head><body><pre>");
61+
executor::inst()->get_http_report(EV_HTML_RESULTS, str);
62+
str.append("</pre></body></html>");
63+
64+
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
65+
}
66+
else
67+
{
68+
char buffer[1024];
69+
snprintf(buffer, sizeof(buffer), "<html><head><title>Error</title></head><body>"
70+
"<pre>Unkown url %s - please use /h, /r or /c as url</pre></body></html>", url);
71+
72+
rsp = MHD_create_response_from_buffer(strlen(buffer),
73+
(void*)buffer, MHD_RESPMEM_MUST_COPY);
74+
}
75+
76+
int ret = MHD_queue_response(connection, MHD_HTTP_OK, rsp);
77+
MHD_destroy_response(rsp);
78+
return ret;
79+
}
80+
81+
bool httpd::start_daemon()
82+
{
83+
d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
84+
8100, NULL, NULL,
85+
&httpd::req_handler,
86+
NULL, MHD_OPTION_END);
87+
88+
if(d == nullptr)
89+
{
90+
printer::inst()->print_str("HTTP Daemon failed to start.");
91+
return false;
92+
}
93+
94+
return true;
95+
}
96+

httpd.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
struct MHD_Daemon;
4+
struct MHD_Connection;
5+
6+
class httpd
7+
{
8+
public:
9+
static httpd* inst()
10+
{
11+
if (oInst == nullptr) oInst = new httpd;
12+
return oInst;
13+
};
14+
15+
bool start_daemon();
16+
17+
private:
18+
httpd();
19+
static httpd* oInst;
20+
21+
static int req_handler(void * cls,
22+
MHD_Connection* connection,
23+
const char* url,
24+
const char* method,
25+
const char* version,
26+
const char* upload_data,
27+
size_t* upload_data_size,
28+
void ** ptr);
29+
30+
MHD_Daemon *d;
31+
};

0 commit comments

Comments
 (0)