added http server
This commit is contained in:
19
FlippR-Driver/src/utility/OutputHTTPServer.cpp
Normal file
19
FlippR-Driver/src/utility/OutputHTTPServer.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by rhetenor on 4/15/19.
|
||||
//
|
||||
|
||||
#include "OutputHTTPServer.h"
|
||||
#include "OutputRequestHandlerFactory.h"
|
||||
#include <Poco/Net/HTTPServerParams.h>
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace utility
|
||||
{
|
||||
using namespace Poco::Net;
|
||||
|
||||
OutputHTTPServer::OutputHTTPServer(std::shared_ptr<output::OutputDriver> output_driver, Socket &socket) :
|
||||
HTTPServer(new OutputRequestHandlerFactory(output_driver), socket, new HTTPServerParams())
|
||||
{}
|
||||
|
||||
}
|
||||
}
|
||||
27
FlippR-Driver/src/utility/OutputHTTPServer.h
Normal file
27
FlippR-Driver/src/utility/OutputHTTPServer.h
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by rhetenor on 4/15/19.
|
||||
//
|
||||
|
||||
#ifndef FLIPPR_CODE_OUTPUTHTTPSERVER_H
|
||||
#define FLIPPR_CODE_OUTPUTHTTPSERVER_H
|
||||
|
||||
#include <Poco/Net/HTTPServer.h>
|
||||
#include <Poco/Net/Socket.h>
|
||||
#include <output/OutputDriver.h>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace utility
|
||||
{
|
||||
|
||||
class OutputHTTPServer : public Poco::Net::HTTPServer
|
||||
{
|
||||
public:
|
||||
explicit OutputHTTPServer(std::shared_ptr<output::OutputDriver> output_driver, Poco::Net::Socket &socket);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //FLIPPR_CODE_OUTPUTHTTPSERVER_H
|
||||
@@ -4,10 +4,12 @@
|
||||
|
||||
#include "OutputRequestHandler.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <Poco/Net/HTTPServerResponse.h>
|
||||
#include <Poco/Net/HTTPServerRequest.h>
|
||||
#include <Poco/URI.h>
|
||||
#include <Poco/Exception.h>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace utility
|
||||
@@ -22,14 +24,13 @@ OutputRequestHandler::OutputRequestHandler(std::shared_ptr<output::OutputDriver>
|
||||
void OutputRequestHandler::handleRequest(HTTPServerRequest &request,
|
||||
HTTPServerResponse &response)
|
||||
{
|
||||
auto& path_segments = getPathSegments(URI(request.getURI()));
|
||||
auto path_segments = getPathSegments(URI(request.getURI()));
|
||||
|
||||
std::string item_type = path_segments.at(0);
|
||||
std::string item_name = path_segments.at(1);
|
||||
std::string action = path_segments.at(2);
|
||||
std::string score = "";
|
||||
|
||||
request.
|
||||
if(item_type == "displays")
|
||||
{
|
||||
score = path_segments.at(3);
|
||||
@@ -42,10 +43,14 @@ void OutputRequestHandler::handleRequest(HTTPServerRequest &request,
|
||||
{
|
||||
parseRequest(item_type, item_name, action, score);
|
||||
}
|
||||
catch(NotFoundException &e)
|
||||
catch(const NotFoundException &e)
|
||||
{
|
||||
response.setStatusAndReason(HTTPServerResponse::HTTP_NOT_FOUND, e.displayText());
|
||||
}
|
||||
catch(const Poco::InvalidArgumentException &e)
|
||||
{
|
||||
response.setStatusAndReason(HTTPServerResponse::HTTP_REASON_BAD_REQUEST, e.displayText());
|
||||
}
|
||||
}
|
||||
|
||||
void OutputRequestHandler::parseRequest(const std::string& item_type, const std::string& item_name, const std::string& action, const std::string& score)
|
||||
@@ -66,6 +71,10 @@ void OutputRequestHandler::parseRequest(const std::string& item_type, const std:
|
||||
{
|
||||
parseDisplay(item_name, action, score);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Poco::NotFoundException("No item type called " + item_type);
|
||||
}
|
||||
}
|
||||
|
||||
void OutputRequestHandler::parseSolenoid(const std::string& item_name, const std::string& action)
|
||||
@@ -150,8 +159,15 @@ void OutputRequestHandler::parseDisplay(const std::string& item_name, const std:
|
||||
|
||||
if(action == "write_score")
|
||||
{
|
||||
unsigned int score = std::stoi(score);
|
||||
display->write_score(score);
|
||||
try
|
||||
{
|
||||
unsigned int score = std::stoi(score);
|
||||
display->write_score(score);
|
||||
}
|
||||
catch(std::invalid_argument &e)
|
||||
{
|
||||
throw new Poco::InvalidArgumentException("Could not convert " + score + " to a number!\n" + e.what());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -159,7 +175,7 @@ void OutputRequestHandler::parseDisplay(const std::string& item_name, const std:
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> OutputRequestHandler::getPathSegements(Poco::URI uri)
|
||||
std::vector<std::string> OutputRequestHandler::getPathSegments(Poco::URI uri)
|
||||
{
|
||||
std::vector<std::string> path_segments;
|
||||
uri.getPathSegments(path_segments);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
|
||||
|
||||
#include <Poco/Net/HTTPRequestHandler.h>
|
||||
#include <Poco/URI.h>
|
||||
#include <memory>
|
||||
#include <output/OutputDriver.h>
|
||||
namespace flippR_driver
|
||||
@@ -27,6 +28,9 @@ private:
|
||||
void parseLamp(const std::string& item_name, const std::string& action);
|
||||
void parseSound(const std::string& item_name, const std::string& action);
|
||||
void parseDisplay(const std::string& item_name, const std::string& action, const std::string& score);
|
||||
|
||||
std::vector<std::string> getPathSegments(Poco::URI uri);
|
||||
|
||||
private:
|
||||
std::shared_ptr<output::OutputDriver> output_driver;
|
||||
|
||||
|
||||
24
FlippR-Driver/src/utility/OutputRequestHandlerFactory.cpp
Normal file
24
FlippR-Driver/src/utility/OutputRequestHandlerFactory.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by rhetenor on 4/15/19.
|
||||
//
|
||||
|
||||
#include "OutputRequestHandlerFactory.h"
|
||||
#include "OutputRequestHandler.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace utility
|
||||
{
|
||||
using namespace Poco::Net;
|
||||
|
||||
OutputRequestHandlerFactory::OutputRequestHandlerFactory(std::shared_ptr<output::OutputDriver> output_driver) :
|
||||
output_driver(output_driver)
|
||||
{}
|
||||
|
||||
HTTPRequestHandler *OutputRequestHandlerFactory::createRequestHandler(const HTTPServerRequest &request)
|
||||
{
|
||||
return new OutputRequestHandler(this->output_driver);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
28
FlippR-Driver/src/utility/OutputRequestHandlerFactory.h
Normal file
28
FlippR-Driver/src/utility/OutputRequestHandlerFactory.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by rhetenor on 4/15/19.
|
||||
//
|
||||
|
||||
#ifndef FLIPPR_CODE_OUTPUTREQUESTHANDLERFACTORY_H
|
||||
#define FLIPPR_CODE_OUTPUTREQUESTHANDLERFACTORY_H
|
||||
#include <memory>
|
||||
#include <output/OutputDriver.h>
|
||||
#include <Poco/Net/HTTPRequestHandlerFactory.h>
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace utility
|
||||
{
|
||||
|
||||
class OutputRequestHandlerFactory :: public Poco::Net::HTTPRequestHandlerFactory
|
||||
{
|
||||
public:
|
||||
explicit OutputRequestHandlerFactory(std::shared_ptr<output::OutputDriver> output_driver);
|
||||
Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<output::OutputDriver> output_driver;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //FLIPPR_CODE_OUTPUTREQUESTHANDLERFACTORY_H
|
||||
Reference in New Issue
Block a user