Commit 0b058194 authored by Patrick Chen's avatar Patrick Chen

handle command/event and give out response

parent 9331faaa
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "test/Service.h" #include "test/Service.h"
...@@ -14,6 +15,9 @@ namespace xs { namespace test ...@@ -14,6 +15,9 @@ namespace xs { namespace test
{ {
class Config final class Config final
{ {
private:
using service_range_type = std::deque<Service>;
public: public:
Config() = delete; Config() = delete;
explicit Config(const boost::filesystem::path& file); explicit Config(const boost::filesystem::path& file);
...@@ -24,10 +28,14 @@ namespace xs { namespace test ...@@ -24,10 +28,14 @@ namespace xs { namespace test
Config& operator=(const Config&) = delete; Config& operator=(const Config&) = delete;
Config& operator=(Config&&) = delete; Config& operator=(Config&&) = delete;
boost::iterator_range<
service_range_type::const_iterator
> getServices() const noexcept;
private: private:
static bool verify(const nlohmann::json& object) noexcept; static bool verify(const nlohmann::json& object) noexcept;
public: private:
std::deque<Service> services_; std::deque<Service> services_;
}; };
} } } }
......
...@@ -28,6 +28,9 @@ namespace xs { namespace test ...@@ -28,6 +28,9 @@ namespace xs { namespace test
explicit Service(const nlohmann::json& object); explicit Service(const nlohmann::json& object);
static bool verify(const nlohmann::json& object) noexcept; static bool verify(const nlohmann::json& object) noexcept;
bool isCommand() const noexcept;
bool isEvent() const noexcept;
private: private:
Service(Type type, std::string name, nlohmann::json response) noexcept; Service(Type type, std::string name, nlohmann::json response) noexcept;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define XS_TEST_TESTER_H_INCLUDED #define XS_TEST_TESTER_H_INCLUDED
#include <memory> #include <memory>
#include <unordered_map>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
...@@ -25,11 +26,19 @@ namespace xs { namespace test ...@@ -25,11 +26,19 @@ namespace xs { namespace test
explicit Tester(const boost::filesystem::path& file); explicit Tester(const boost::filesystem::path& file);
void wait() noexcept;
private: private:
std::string onCommand(const std::string& name, const std::string& params) noexcept;
void onEvent(const std::string& name, const std::string& params) noexcept;
void registerServices() noexcept;
public: public:
Config config_; Config config_;
ExchangeServer server_; ExchangeServer server_;
std::unordered_map<std::string, const Service*> acceptCommands_;
std::unordered_map<std::string, const Service*> acceptEvents_;
}; };
} } } }
......
{
"mockData": [
{
"command": "aaa",
"response": "123"
},
{
"event" : "bbb",
"response" : { "value": 456 }
}
]
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fstream> #include <fstream>
#include <iterator>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
...@@ -42,6 +43,15 @@ Config::Config(const boost::filesystem::path& file) ...@@ -42,6 +43,15 @@ Config::Config(const boost::filesystem::path& file)
} }
} }
boost::iterator_range<
Config::service_range_type::const_iterator
> Config::getServices() const noexcept
{
return boost::make_iterator_range(
std::begin(services_), std::end(services_)
);
}
bool Config::verify(const nlohmann::json& object) noexcept bool Config::verify(const nlohmann::json& object) noexcept
{ {
if (!(object.count("mockData") && object["mockData"].is_array())) { if (!(object.count("mockData") && object["mockData"].is_array())) {
......
...@@ -43,4 +43,14 @@ namespace xs { namespace test ...@@ -43,4 +43,14 @@ namespace xs { namespace test
, name_(std::move(name)) , name_(std::move(name))
, response_(std::move(response)) , response_(std::move(response))
{ } { }
bool Service::isCommand() const noexcept
{
return type_ == Type::COMMAND;
}
bool Service::isEvent() const noexcept
{
return type_ == Type::EVENT;
}
} } } }
#include "test/Service.h"
#include "test/Tester.h" #include "test/Tester.h"
#include <boost/range/adaptors.hpp>
#include <chrono>
#include <functional>
#include <thread>
namespace xs { namespace test { namespace xs { namespace test {
...@@ -9,6 +15,60 @@ Tester::Tester(const boost::filesystem::path& file) ...@@ -9,6 +15,60 @@ Tester::Tester(const boost::filesystem::path& file)
, server_( , server_(
"xs-test", { }, { }, 0, nullptr "xs-test", { }, { }, 0, nullptr
) )
{ } {
registerServices();
}
void Tester::registerServices() noexcept
{
acceptCommands_.clear();
acceptEvents_.clear();
using namespace std::placeholders;
for (const auto& service : config_.getServices()) {
if (service.isCommand()) {
server_.onCmd(
service.name_,
std::bind(&Tester::onCommand, this, _1, _2)
);
acceptCommands_[service.name_] = &service;
} else {
server_.onEvent(
service.name_,
std::bind(&Tester::onEvent, this, _1, _2)
);
acceptEvents_[service.name_] = &service;
}
}
}
std::string Tester::onCommand(const std::string& name, const std::string& params) noexcept
{
const auto found = acceptCommands_.find(name);
const auto* service = std::get<1>(*found);
const auto& response = service->response_;
/// TODO: keep track the status
return response.is_string() ? response.get<std::string>()
: response.dump();
}
void Tester::onEvent(const std::string& name, const std::string& params) noexcept
{
const auto found = acceptEvents_.find(name);
const auto* service = std::get<1>(*found);
const auto& response = service->response_;
/// TODO: keep track the status
}
void Tester::wait() noexcept
{
using namespace std::chrono_literals;
while (true) {
std::this_thread::sleep_for(1s);
}
}
} } } }
...@@ -21,14 +21,6 @@ int main(int argc, char* argv[]) { ...@@ -21,14 +21,6 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
using namespace xs::test; xs::test::Tester tester{configFile};
const Tester tester{configFile}; tester.wait();
for (const auto& service : tester.config_.services_) {
if (service.type_ == Service::Type::COMMAND) {
std::cout << "command: " << service.name_;
} else {
std::cout << "event: " << service.name_;
}
std::cout << ", response: " << service.response_ << std::endl;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment