Created
May 5, 2020 06:27
-
-
Save rozaydin/414a76bf83349259bc8f9c871cbe72f9 to your computer and use it in GitHub Desktop.
Sample Pistache Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rest Server | |
void restServer(AfSIPProcessor *afSipProcessor, int sipSockHandle, Logger *pLogger) | |
{ | |
class ConfigServerMockRestHandler : public Pistache::Http::Handler | |
{ | |
private: | |
AfSIPProcessor *afSipProcessor; | |
const int sipSockHandle; | |
Logger *pLogger; | |
public: | |
HTTP_PROTOTYPE(ConfigServerMockRestHandler) | |
ConfigServerMockRestHandler(AfSIPProcessor *_afSipProcessor, int _sipSockHandle, Logger *pLogger) : afSipProcessor( | |
_afSipProcessor), | |
sipSockHandle( | |
_sipSockHandle), | |
pLogger(pLogger) {} | |
void | |
onRequest(const Pistache::Http::Request &request, Pistache::Http::ResponseWriter response) | |
{ | |
const std::string ACTION_START = "start"; | |
const std::string ACTION_STOP = "stop"; | |
const std::string requestPath = request.resource(); | |
if (requestPath == "/moh") | |
{ | |
std::string requestBody = request.body(); | |
pLogger->info("RestServer::MohHandler::onRequest", "Received Request: " + requestBody); | |
auto _jsonObj = json::parse(requestBody); | |
const std::string action = _jsonObj["action"]; | |
// === Action Processing === | |
if (ACTION_START == action) | |
{ | |
std::string templateSipRequest = _jsonObj["reinv_req"]; | |
const bool operationResult = afSipProcessor->MakeMohCall(templateSipRequest, sipsock); | |
// for all other cases | |
pLogger->info("RestServer::MohHandler::onRequest", "Performed MakeMohCall with operation result: " + std::to_string(operationResult)); | |
// | |
response.setMime(MIME(Application, Json)); | |
response.send(Pistache::Http::Code::Ok, operationResult ? "Media Server Initiated MOH" | |
: "Media Server failed to initiate MOH!"); | |
} | |
else if (ACTION_STOP == action) | |
{ | |
const std::string callId = _jsonObj["call_id"]; | |
const bool operationResult = afSipProcessor->RemoveCall(callId); | |
// | |
pLogger->info("RestServer::MohHandler::onRequest", "destroyed existing MohCall with callId: " + callId + " operation result: " + std::to_string(operationResult)); | |
// | |
response.setMime(MIME(Application, Json)); | |
response.send(Pistache::Http::Code::Ok, | |
operationResult ? "Call identified with callId: [ " + callId + " ] Terminated!" | |
: " Call Failed to be terminated!"); | |
} | |
else | |
{ | |
pLogger->info("RestServer::MohHandler::onRequest", " Unsupported action " + action); | |
response.send(Pistache::Http::Code::Bad_Request, "Unsupported Action: " + action); | |
} | |
} | |
else | |
{ // url not supported | |
pLogger->info("RestServer::MohHandler::onRequest", " Unsupported url " + requestPath); | |
response.send(Pistache::Http::Code::Bad_Request, "Requested Path is Unsupported! Path: " + requestPath); | |
} | |
} | |
}; | |
std::cout << "Generic Rest Server is starting ..." << std::endl; | |
const int __PORT__ = 8080; | |
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(__PORT__)); | |
auto opts = Pistache::Http::Endpoint::options().threads(1); | |
Pistache::Http::Endpoint server(addr); | |
server.init(opts); | |
// | |
server.setHandler(Pistache::Http::make_handler<ConfigServerMockRestHandler>(afSipProcessor, sipSockHandle, pLogger)); | |
server.serve(); | |
// | |
std::cout << "Generic Rest Server is exiting ..." << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment