Skip to content

Commit

Permalink
cleanup(falco): consolidate falco::grpc::server in one class
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed Mar 28, 2024
1 parent a8018a2 commit 13c8e37
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 153 deletions.
2 changes: 0 additions & 2 deletions userspace/falco/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT MINIMAL_BUILD)
outputs_http.cpp
webserver.cpp
grpc_context.cpp
grpc_server_impl.cpp
grpc_request_context.cpp
grpc_server.cpp
grpc_context.cpp
grpc_server_impl.cpp
${CMAKE_CURRENT_BINARY_DIR}/version.grpc.pb.cc
${CMAKE_CURRENT_BINARY_DIR}/version.pb.cc
${CMAKE_CURRENT_BINARY_DIR}/outputs.grpc.pb.cc
Expand Down
77 changes: 77 additions & 0 deletions userspace/falco/grpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ limitations under the License.
#include <grpc++/grpc++.h>
#endif

#include "config_falco.h"
#include "falco_engine.h"
#include "falco_engine_version.h"
#include "logger.h"
#include "grpc_server.h"
#include "grpc_queue.h"
#include "grpc_request_context.h"
#include "falco_utils.h"

Expand Down Expand Up @@ -252,3 +256,76 @@ void falco::grpc::server::stop()

falco_logger::log(falco_logger::level::INFO, "Shutting down gRPC server complete\n");
}

bool falco::grpc::server::is_running()
{
if(m_stop)
{
return false;
}
return true;
}

void falco::grpc::server::get(const stream_context& ctx, const outputs::request& req, outputs::response& res)
{
if(ctx.m_status == stream_context::SUCCESS || ctx.m_status == stream_context::ERROR)
{
// todo(leodido) > log "status=ctx->m_status, stream=ctx->m_stream"
ctx.m_stream = nullptr;
return;
}

ctx.m_is_running = is_running();

// Start or continue streaming
// m_status == stream_context::STREAMING?
// todo(leodido) > set m_stream

ctx.m_has_more = queue::get().try_pop(res);
}

void falco::grpc::server::sub(const bidi_context& ctx, const outputs::request& req, outputs::response& res)
{
if(ctx.m_status == stream_context::SUCCESS || ctx.m_status == stream_context::ERROR)
{
ctx.m_stream = nullptr;
return;
}

ctx.m_is_running = is_running();

// Start or continue streaming
// m_status == stream_context::STREAMING?
// todo(leodido) > set m_stream

ctx.m_has_more = queue::get().try_pop(res);
}

void falco::grpc::server::version(const context& ctx, const version::request&, version::response& res)
{
auto& build = *res.mutable_build();
build = FALCO_VERSION_BUILD;

auto& prerelease = *res.mutable_prerelease();
prerelease = FALCO_VERSION_PRERELEASE;

auto& version = *res.mutable_version();
version = FALCO_VERSION;

res.set_engine_version(FALCO_ENGINE_VERSION);
res.set_engine_fields_checksum(FALCO_ENGINE_CHECKSUM);
auto engine_version = falco_engine::engine_version();
res.set_engine_major(engine_version.major());
res.set_engine_minor(engine_version.minor());
res.set_engine_patch(engine_version.patch());

res.set_major(FALCO_VERSION_MAJOR);
res.set_minor(FALCO_VERSION_MINOR);
res.set_patch(FALCO_VERSION_PATCH);
}

void falco::grpc::server::shutdown()
{
m_stop = true;
m_server->Shutdown();
}
21 changes: 19 additions & 2 deletions userspace/falco/grpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ limitations under the License.

#include <thread>
#include <string>
#include <atomic>

#include "grpc_server_impl.h"
#include "outputs.grpc.pb.h"
#include "version.grpc.pb.h"
#include "grpc_context.h"

namespace falco
{
namespace grpc
{

class server : public server_impl
class server
{
public:
server() = default;
Expand All @@ -44,6 +47,7 @@ class server : public server_impl
void thread_process(int thread_index);
void run();
void stop();
void shutdown();

outputs::service::AsyncService m_output_svc;
version::service::AsyncService m_version_svc;
Expand All @@ -61,6 +65,19 @@ class server : public server_impl
::grpc::ServerBuilder m_server_builder;
void init_mtls_server_builder();
void init_unix_server_builder();

bool is_running();

// Outputs
void get(const stream_context& ctx, const outputs::request& req, outputs::response& res);
void sub(const bidi_context& ctx, const outputs::request& req, outputs::response& res);

// Version
void version(const context& ctx, const version::request& req, version::response& res);

std::unique_ptr<::grpc::Server> m_server;

std::atomic<bool> m_stop{false};
};

} // namespace grpc
Expand Down
96 changes: 0 additions & 96 deletions userspace/falco/grpc_server_impl.cpp

This file was deleted.

53 changes: 0 additions & 53 deletions userspace/falco/grpc_server_impl.h

This file was deleted.

0 comments on commit 13c8e37

Please sign in to comment.