Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
Last active December 29, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtemGr/7607371 to your computer and use it in GitHub Desktop.
Save ArtemGr/7607371 to your computer and use it in GitHub Desktop.
Duetto server stub for Tntnet
#include <cxxtools/log/cxxtools.h>
#include <tnt/tntnet.h>
#include <tnt/httperror.h>
#include <tnt/httpreply.h>
#include <tnt/ecpp.h>
#include <tnt/worker.h>
#include <iostream>
#include <fstream>
using std::cout; using std::endl; using std::string;
typedef void (*entryPointSig)(char* inData, const char* outData);
struct DuettoMap {
char* funcName;
entryPointSig entryPoint;
};
extern DuettoMap duettoFuncMap[];
struct TntService: public tnt::EcppComponent {
TntService (const tnt::Compident& ci, const tnt::Urlmapper& um, tnt::Comploader& cl): EcppComponent(ci, um, cl) {}
unsigned operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparam) {
const string& callName = qparam["f"];
const string& callArgs = qparam["a"];
entryPointSig callFunc = nullptr;
for (DuettoMap* cur = duettoFuncMap; cur->funcName != nullptr; ++cur) {
if (callName == cur->funcName) {
cout << "Found " << cur->funcName << endl;
callFunc = cur->entryPoint;
break;
}
}
if (callFunc == nullptr) {
cout << "Invalid call " << callName << endl;
return HTTP_NOT_FOUND;
}
// By convention the output buffer size is statically set at 1024
// This should be changed to a serialization interface
char outBuf[1024];
callFunc (outBuf, callArgs.c_str());
reply.out() << outBuf;
return HTTP_OK;
}
};
static tnt::EcppComponentFactoryImpl<TntService> Factory ("requestHandler");
int main() {
{ cxxtools::LogConfiguration lc;
lc.setRootLevel (cxxtools::Logger::INFO);
cxxtools::LogManager::logInit (lc); }
tnt::Tntnet tntnet;
tntnet.listen ("0.0.0.0", 1987);
tntnet.setAppName ("Duetto");
tntnet.mapUrl ("^/duetto_call$", "requestHandler");
tntnet.mapUrl ("^/([\\w\\-\\.]+)", tnt::Maptarget ("static@tntnet") .setPathInfo ("./$1")); // Serve files from the current directory.
tntnet.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment