add microhttpd server

This commit is contained in:
2024-11-14 18:15:27 +03:00
parent ee34e8a72e
commit cdde340efe
12 changed files with 599 additions and 221 deletions

250
main.cpp
View File

@@ -1,220 +1,64 @@
#include "pibytearray.h"
#include "piclientserver_client.h"
#include "piclientserver_server.h"
#include "picodeparser.h"
#include "piintrospection_server.h"
#include "piiostream.h"
#include "pijson.h"
#include "pilog.h"
#include "pimathbase.h"
#include "pihttpserver.h"
#include "pip.h"
#include "piprotectedvariable.h"
#include "pitranslator_p.h"
#include "pivaluetree_conversions.h"
#include <windows.h>
using namespace PICoutManipulators;
/*
class MyStr: public PIString {
public:
MyStr(): PIString() {}
MyStr(const char * o): PIString(o) { piCout << "MyStr *"; }
MyStr(const MyStr & o): PIString(o) { piCout << "MyStr &"; }
// MyStr(const MyStr & o): PIString(o) { piCout << "MyStr &"; }
MyStr(MyStr && o): PIString(o) { piCout << "MyStr &&"; }
MyStr & operator=(const MyStr & o) {
*this += o;
piCout << "MyStr =&";
return *this;
}
MyStr & operator=(MyStr && o) {
*this += o;
piCout << "MyStr =&&";
return *this;
}
};
*/
PIKbdListener kbd;
const char * pageTitle = "<!DOCTYPE html>"
"<html>"
"<body>"
"<h1>Title</h1>"
"</body>"
"</html>";
class RWL {
public:
void lockWrite() {
PIMutexLocker _ml(mutex);
while (reading > 0 || writing) {
var.wait(mutex);
}
writing = true;
}
void unlockWrite() {
PIMutexLocker _ml(mutex);
writing = false;
var.notifyAll();
}
void lockRead() {
PIMutexLocker _ml(mutex);
while (writing) {
var.wait(mutex);
}
++reading;
}
void unlockRead() {
PIMutexLocker _ml(mutex);
--reading;
var.notifyAll();
}
private:
PIConditionVariable var;
int reading = 0;
bool writing = false;
PIMutex mutex;
};
PIMutex mutex;
PISemaphore sem(10);
PIReadWriteLock rwl;
int main(int argc, char * argv[]) {
/*sem.acquire(2);
piCout << sem.tryAcquire(2);
piCout << sem.available();
return 0;*/
PIThread t_w0(
[] {
// PIMutexLocker _ml(mutex);
PIWriteLocker rl(rwl);
piCout << "write0 start ...";
piMSleep(500);
piCout << "write0 end"
<< "\n";
},
true,
1_Hz);
PIThread t_w1(
[] {
// PIMutexLocker _ml(mutex);
PIWriteLocker rl(rwl);
piCout << "write1 start ...";
piMSleep(500);
piCout << "write1 end"
<< "\n";
},
true,
1_Hz);
kbd.enableExitCapture();
int cnt0 = 0, cnt1 = 0;
PIThread t_r0(
[&cnt0] {
// PIMutexLocker _ml(mutex);
PIReadLocker rl(rwl);
piCout << "read0 start ...";
piMSleep(50);
// bool ok = rwl.tryLockRead(100_ms);
// if (ok) ++cnt0;
piCout << "read0 end";
// if (ok) rwl.unlockRead();
},
true,
10_Hz);
PIThread t_r1(
[&cnt1] {
// PIMutexLocker _ml(mutex);
// PIReadLocker rl(rwl);
piCout << "read1 start ...";
piMSleep(50);
bool ok = rwl.tryLockRead(100_ms);
if (ok) ++cnt1;
piCout << "read1 end" << ok;
if (ok) rwl.unlockRead();
},
true,
11_Hz);
PIHTTPServer server;
piSleep(8.);
server.setFavicon(PIFile::readAll("logo.png", false));
server.listen({"127.0.0.1", 7777});
t_r0.stopAndWait();
t_r1.stopAndWait();
t_w0.stopAndWait();
t_w1.stopAndWait();
server.registerPath("/", [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody(PIByteArray::fromAscii(pageTitle));
return ret;
});
piCout << cnt0 << cnt1;
server.registerPath("/html", [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody("<!DOCTYPE html><html><body><p>arg=%1</p></body></html>"_a.arg(r.args.value("a0")).toUTF8());
return ret;
});
/*PICodeParser parser;
parser.parseFile("client_server.h");
for (auto m: parser.enums) {
server.registerPath("/api", [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody(PIByteArray::fromAscii("<!DOCTYPE html><html><body>API</body></html>"));
return ret;
});
server.registerPath("/api/*", [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply ret;
ret.setBody("<!DOCTYPE html><html><body>API etry %1</body></html>"_a.arg(r.path).toUTF8());
ret.setCode(405);
return ret;
});
/*server.setRequestCallback([](MicrohttpdServer::Request r) -> MicrohttpdServer::Reply {
MicrohttpdServer::Reply rep;
piCout << "request" << r.path;
piCout << " header" << r.headers;
piCout << " args" << r.args;
piCout << " body" << r.body;
piCout << "";
piCout << m.name; // << m.args << m.value;
// piCout << m.expand({"hello"});
}*/
rep.setBody(PIByteArray::fromAscii("[{\"value1\": true, \"value2\": \"ыекштп\"}]"));
return rep;
});*/
piCout << "start" << server.isListen();
return 0;
PITranslator::loadLang("ru");
PISerial f("COM123");
f.open();
/*auto test = [](PIString s, PIString v) {
piCout << " in:" << s;
piCout << "arg:" << minArgPlaceholder(s);
piCout << "out:" << arg(s, v);
piCout << "";
};
test(" %", "asd");
test("%", "asd");
test("1", "asd");
test(" %11", "asd");
test("%1%2 %0f%0g", "asd");
test("%01 ", "asd");*/
/*piCout << PIString::readableSize(50_KiB);
piCout << PIString::readableSize(1_GB);
PITranslator::loadLang("ru");
piCout << PIString::readableSize(50_KiB);
piCout << PIString::readableSize(1_GB);
piCout << "test\nstring"_tr;
PITranslator::clear();
piCout << PIString::readableSize(50_KiB);
piCout << PIString::readableSize(1_GB);
piCout << "test\nstring"_tr;
piCout << "hello!"_tr;
PITranslator::loadConfig("[]\nhello!=привет!\n[Co]\nhello!=привет CO!\n"_u8);
piCout << "hello!"_tr("Co") << "hello!"_tr;*/
// piCout << "hello!"_trNoOp;
/*PISet<int> set;
piCout << set << piSerialize(set) << piDeserialize<PISet<int>>(piSerialize(set));
set << 1 << 2 << 3;
piCout << set << piSerialize(set) << piDeserialize<PISet<int>>(piSerialize(set));
set << 1 << -2 << 50 << -100;
piCout << set << piSerialize(set) << piDeserialize<PISet<int>>(piSerialize(set));*/
return 0;
std::numeric_limits<complexf>::epsilon();
using cmlp = complexf;
PIMathMatrixT<3, 3, double> v0;
PIMathMatrixT<3, 3, cmlp> v1;
v0[0][1] = 1;
v0[1][1] = 2;
v0[2][1] = 3;
v1[0][1] = cmlp(1., 0);
v1[1][1] = cmlp(2., -1);
v1[2][1] = cmlp(3., 2);
piCout << v0 << v1;
piCout << (v0 * 2.) << (v1 * cmlp(2., 2.));
piCout << (v0 + 2.) << (v1 + cmlp(2.));
piCout << (v0 - 2.) << (v1 - cmlp(2.));
piCout << (v0 / 2.) << (v1 / cmlp(2., 1.));
// piCout << (v0.length()) << (v1.length());
// piCout << (v0.lengthSqr()) << (v1.lengthSqr());
// piCout << (v0.manhattanLength()) << (v1.manhattanLength());
/*foo<int>();
foo<double>();
foo<complexf>();
foo<complexd>();
return 0;*/
WAIT_FOR_EXIT
server.stop();
return 0;
}