Skip to content

Commit

Permalink
Make parameter api synchrounous
Browse files Browse the repository at this point in the history
  • Loading branch information
madelen-at-work committed Aug 27, 2024
1 parent 3bc0ae4 commit 7bcc7f3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
2 changes: 1 addition & 1 deletion apis/keyvaluestore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package keyvaluestore;
// A simple key-value storage service
service KeyValueStore {
// Provides a value for each key request
rpc GetValues (stream Request) returns (stream Response) {}
rpc GetValues (Request) returns (Response) {}
}

// The request message containing the key
Expand Down
45 changes: 20 additions & 25 deletions src/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,28 @@ bool Parameter::Init(const bool verbose) {
return true;
}

Status Parameter::GetValues(ServerContext* context, ServerReaderWriter<Response, Request>* stream) {
Request request;
Response response;
while (stream->Read(&request)) {
if (ax_parameter == NULL) {
ERRORLOG << "axparameter not initalized" << endl;
return Status::CANCELLED;
}

const gchar* parameter_key = request.key().c_str();
const regex pattern("[a-zA-Z0-9.]+");
if (!regex_match(parameter_key, pattern)) {
TRACELOG << "No valid input request" << endl;
return Status(StatusCode::INVALID_ARGUMENT, "No valid input request");
}
char* parameter_value = NULL;
if (!ax_parameter_get(ax_parameter, parameter_key, &parameter_value, &_error)) {
ERRORLOG << "Error when getting axparameter: " << _error->message << endl;
parameter_value = g_strdup("");
g_clear_error(&_error);
}
TRACELOG << parameter_key << ": " << parameter_value << endl;
Status Parameter::GetValues(ServerContext* context, const Request* request, Response* response) {
if (ax_parameter == NULL) {
ERRORLOG << "axparameter not initalized" << endl;
return Status::CANCELLED;
}

response.set_value(parameter_value);
stream->Write(response);
free(parameter_value);
const gchar* parameter_key = request->key().c_str();
const regex pattern("[a-zA-Z0-9.]+");
if (!regex_match(parameter_key, pattern)) {
TRACELOG << "No valid input request" << endl;
return Status(StatusCode::INVALID_ARGUMENT, "No valid input request");
}
char* parameter_value = NULL;
if (!ax_parameter_get(ax_parameter, parameter_key, &parameter_value, &_error)) {
ERRORLOG << "Error when getting axparameter: " << _error->message << endl;
parameter_value = g_strdup("");
g_clear_error(&_error);
}
TRACELOG << parameter_key << ": " << parameter_value << endl;

response->set_value(parameter_value);
free(parameter_value);

return Status::OK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Parameter final : public KeyValueStore::Service {
bool Init(const bool verbose);

private:
Status GetValues(ServerContext* context, ServerReaderWriter<Response, Request>* stream);
Status GetValues(ServerContext* context, const Request* request, Response* response);

AXParameter* ax_parameter;
GError* _error;
Expand Down
20 changes: 8 additions & 12 deletions test/parameter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,25 @@ vector<pair<string, string>> GetValues(unique_ptr<KeyValueStore::Stub>& stub,
const vector<string>& keys) {
vector<pair<string, string>> values;
ClientContext context;
auto stream = stub->GetValues(&context);
Request request;
Response response;

for (const auto& key : keys) {
// Key we are sending to the server.
Request request;
request.set_key(key);
stream->Write(request);
auto status = stub->GetValues(&context, request, &response);
if (!status.ok()) {
throw std::runtime_error("Error from gRPC: " + status.error_message());
}
EXPECT_TRUE(status.ok());

// Get the value for the sent key
Response response;
std::string value;
stream->Read(&response);
value = response.value();
value.erase(std::remove(value.begin(), value.end(), '\n'), value.end());
values.push_back(make_pair(key, value));
}

stream->WritesDone();
Status status = stream->Finish();
if (!status.ok()) {
throw std::runtime_error("Error from gRPC: " + status.error_message());
}
EXPECT_TRUE(status.ok());

return values;
}

Expand Down

0 comments on commit 7bcc7f3

Please sign in to comment.