Skip to content

Commit

Permalink
aslp-cpp: support extra query params
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinafyi committed Jul 10, 2024
1 parent aaf9810 commit 29d5630
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
38 changes: 22 additions & 16 deletions aslp-cpp/include/aslp-cpp/aslp-cpp.hpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
#pragma once

#include <string>
#include <map>
#include <memory>
#include <string>
#include <vector>

namespace httplib {
namespace httplib
{
class Client;
} // namespace httplib;
} // namespace httplib

// tuple of encoding and semantics
using aslp_opcode_result_t = std::tuple<std::string, std::string>;

class aslp_connection
{
using params_t = std::multimap<std::string, std::string>;

std::unique_ptr<httplib::Client> client {nullptr};
const params_t& extra_params;

public:
aslp_connection(const std::string& server_addr, int server_port);
aslp_connection(const std::string& server_addr, int server_port, const params_t& extra_params = {});
aslp_connection(aslp_connection&&) noexcept;
auto get_opcode(uint32_t opcode) -> aslp_opcode_result_t;
void wait_active();
~aslp_connection();
};



class aslp_client {
class aslp_client
{
private:
const std::string server_addr;
pid_t server_pid;
int server_port;
void shutdown();

public:
aslp_client(const aslp_client&) = delete;
aslp_client(aslp_client&&) = delete;
Expand All @@ -43,26 +49,26 @@ class aslp_client {
: server_pid(pid)
, server_port(port)
, server_addr(std::move(addr))
{ }
{
}

/**
* Creates a new aslp_client with a managed server on
* the default address, localhost:8000.
*/
std::unique_ptr<aslp_client> static start() {
std::unique_ptr<aslp_client> static start()
{
return start("127.0.0.1", 8000);
}

/** Creates a new managed aslp_client with the given address and port. */
auto static start(const std::string& addr, int server_port) -> std::unique_ptr<aslp_client>;
auto static start(const std::string& addr,
int server_port) -> std::unique_ptr<aslp_client>;

/** Returns the semantics for the given opcode, as a newline-separated string. */
/** Returns the semantics for the given opcode, as a newline-separated string.
*/
auto get_opcode(uint32_t opcode) -> aslp_opcode_result_t;

/** Destroys the aslp_client and terminates the managed server as well. */
virtual ~aslp_client() {
shutdown();
};

virtual ~aslp_client() { shutdown(); };
};

14 changes: 9 additions & 5 deletions aslp-cpp/source/aslp-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ aslp_opcode_result_t aslp_connection::get_opcode(uint32_t opcode)
{
auto codestr = std::format("{:#x}", opcode);
std::cout << codestr << "\n";
const auto params = httplib::Params({{"opcode", codestr}});
auto params = httplib::Params({{"opcode", codestr}});
for (const auto& pair : extra_params) {
params.insert(pair);
}
auto req = client->Get("/", params, httplib::Headers());

if (req.error() != httplib::Error::Success) {
Expand All @@ -174,10 +177,11 @@ aslp_opcode_result_t aslp_connection::get_opcode(uint32_t opcode)
}

aslp_connection::aslp_connection(const std::string& server_addr,
int server_port)
{
client = std::make_unique<httplib::Client>(server_addr, server_port);
}
int server_port,
const params_t& extra_params) :
extra_params{extra_params},
client{std::make_unique<httplib::Client>(server_addr, server_port)}
{}

aslp_connection::aslp_connection(aslp_connection&&) noexcept = default;
aslp_connection::~aslp_connection() = default;
Expand Down

0 comments on commit 29d5630

Please sign in to comment.