delta push service

This commit is contained in:
scawful
2022-09-17 16:35:58 -05:00
parent 03e9ec90e7
commit 83e048ef5f
8 changed files with 142 additions and 2 deletions

View File

@@ -20,17 +20,19 @@ set(BUILD_SHARED_LIBS ON)
# Abseil Standard Specifications ----------------------------------------------
include(cmake/absl.cmake)
include(cmake/openssl.cmake)
set(PROTOBUF_INCLUDE_PATH ${CMAKE_CURRENT_BINARY_DIR}
CACHE INTERNAL "Path to generated protobuf files.")
include_directories(${PROTOBUF_INCLUDE_PATH})
# Video Libraries -------------------------------------------------------------
set(Protobuf_PROTOC_EXECUTABLE "/Users/scawful/code/protobuf/bin/protoc")
set(Protobuf_INCLUDE_DIRS "/Users/scawful/code/protobuf/include/")
find_package(PNG REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(gRPC REQUIRED)
include(cmake/openssl.cmake)
# Asar Assembly ---------------------------------------------------------------
add_subdirectory(src/lib/asar/src)

View File

@@ -106,6 +106,7 @@ add_executable(
yaze_delta
app/delta/delta.cc
app/delta/viewer.cc
app/delta/service.cc
app/delta/client.cc
app/rom.cc
${YAZE_APP_ASM_SRC}
@@ -115,18 +116,19 @@ add_executable(
${YAZE_APP_ZELDA3_SRC}
${YAZE_GUI_SRC}
${IMGUI_SRC}
${ASAR_STATIC_SRC}
)
target_include_directories(
yaze_delta PUBLIC
lib/
app/
lib/asar/src/
${ASAR_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/src/
${PNG_INCLUDE_DIRS}
${SDL2_INCLUDE_DIR}
${GLEW_INCLUDE_DIRS}
${ASAR_STATIC_SRC}
)
target_link_libraries(
@@ -141,6 +143,8 @@ target_link_libraries(
asar-static
ImGui
)
target_compile_definitions(yaze_delta PRIVATE "linux")
target_compile_definitions(yaze_delta PRIVATE "stricmp=strcasecmp")
set (source "${CMAKE_SOURCE_DIR}/assets")
set (destination "${CMAKE_CURRENT_BINARY_DIR}/assets")

View File

@@ -57,6 +57,9 @@ message CloneResponse {}
message PushRequest {
string author_name = 1;
string repository_name= 2;
string branch_name = 3;
repeated Commit commits = 4;
}
message PushResponse {}

View File

@@ -7,6 +7,7 @@
#include <grpcpp/health_check_service_interface.h>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "src/app/delta/delta.grpc.pb.h"
#include "src/app/delta/delta.pb.h"
@@ -22,13 +23,42 @@ using grpc::Server;
using grpc::ServerBuilder;
using grpc::Status;
namespace {
auto FindRepository(std::vector<Repository>& repos, const std::string& name) {
for (auto& repo : repos) {
if (repo.project_name() == name) {
return repo.mutable_tree();
}
}
}
auto FindBranch(google::protobuf::RepeatedPtrField<Branch>* repo,
const std::string& branch_name) {
for (auto it = repo->begin(); it != repo->end(); ++it) {
if (it->branch_name() == branch_name) {
return it->mutable_commits();
}
}
}
} // namespace
Status DeltaService::Init(grpc::ServerContext* context,
const InitRequest* request, InitResponse* reply) {
std::filesystem::create_directories("./.yaze");
repos_.push_back(request->repo());
return Status::OK;
}
Status DeltaService::Push(grpc::ServerContext* context,
const PushRequest* request, PushResponse* reply) {
const auto& repository_name = request->repository_name();
const auto& branch_name = request->branch_name();
auto repo = FindRepository(repos_, repository_name);
auto mutable_commits = FindBranch(repo, branch_name);
auto size = request->commits().size();
for (int i = 1; i < size; ++i) {
*mutable_commits->Add() = request->commits().at(i);
}
return Status::OK;
}
@@ -37,6 +67,11 @@ Status DeltaService::Pull(grpc::ServerContext* context,
return Status::OK;
}
Status DeltaService::Clone(grpc::ServerContext* context,
const CloneRequest* request, CloneResponse* reply) {
return Status::OK;
}
} // namespace delta
} // namespace app
} // namespace yaze

View File

@@ -1,6 +1,8 @@
#ifndef YAZE_APP_DELTA_SERVICE_H
#define YAZE_APP_DELTA_SERVICE_H
#include <filesystem>
#include <google/protobuf/message.h>
#include <grpc/support/log.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
@@ -17,6 +19,8 @@ namespace yaze {
namespace app {
namespace delta {
using grpc::Status;
class DeltaService final : public ::YazeDelta::Service {
public:
Status Init(grpc::ServerContext* context, const InitRequest* request,
@@ -28,6 +32,11 @@ class DeltaService final : public ::YazeDelta::Service {
Status Pull(grpc::ServerContext* context, const PullRequest* request,
PullResponse* reply) override;
Status Clone(grpc::ServerContext* context, const CloneRequest* request,
CloneResponse* reply) override;
auto Repos() const { return repos_; }
private:
std::vector<Repository> repos_;
};
@@ -35,3 +44,5 @@ class DeltaService final : public ::YazeDelta::Service {
} // namespace delta
} // namespace app
} // namespace yaze
#endif

View File

@@ -104,6 +104,9 @@ class ROM {
auto begin() { return rom_data_.begin(); }
auto end() { return rom_data_.end(); }
auto data() { return rom_data_.data(); }
auto char_data() {
return reinterpret_cast<char*>(rom_data_.data());
}
auto size() const { return size_; }
uchar& operator[](int i) {

View File

@@ -15,12 +15,15 @@ add_executable(
yaze_test.cc
rom_test.cc
asm_test.cc
delta_test.cc
../src/app/rom.cc
../src/app/asm/script.cc
../src/app/gfx/bitmap.cc
../src/app/gfx/snes_tile.cc
../src/app/gfx/snes_palette.cc
../src/app/core/common.cc
../src/app/delta/service.cc
../src/app/delta/client.cc
${ASAR_STATIC_SRC}
)
@@ -39,6 +42,7 @@ target_link_libraries(
${ABSL_TARGETS}
${OPENGL_LIBRARIES}
${CMAKE_DL_LIBS}
delta-service
asar-static
gmock_main
gmock

78
test/delta_test.cc Normal file
View File

@@ -0,0 +1,78 @@
#include <asar/interface-lib.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <google/protobuf/repeated_field.h>
#include <array>
#include <cstdint>
#include <fstream>
#include <sstream>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "app/delta/client.h"
#include "app/delta/service.h"
#include "app/core/constants.h"
#include "src/app/delta/delta.grpc.pb.h"
#include "src/app/delta/delta.pb.h"
#include "app/rom.h"
namespace yaze_test {
namespace delta_test {
TEST(DeltaTest, InitRepoAndPushOk) {
yaze::app::delta::DeltaService service;
yaze::app::ROM rom;
Bytes test_bytes;
test_bytes.push_back(0x40);
rom.LoadFromBytes(test_bytes);
grpc::ServerContext* context;
InitRequest init_request;
auto repo = init_request.mutable_repo();
repo->set_project_name("test_repo");
Branch branch;
branch.set_branch_name("test_branch");
auto new_mutable_commits = branch.mutable_commits();
new_mutable_commits->Reserve(5);
for (int i = 0; i < 5; ++i) {
auto new_commit = new Commit();
new_commit->set_commit_id(i);
new_commit->set_data(rom.char_data());
new_mutable_commits->Add();
new_mutable_commits->at(i) = *new_commit;
}
auto mutable_tree = repo->mutable_tree();
mutable_tree->Add();
mutable_tree->at(0) = branch;
InitResponse init_response;
auto init_status = service.Init(context, &init_request, &init_response);
EXPECT_TRUE(init_status.ok());
PushRequest request;
request.set_branch_name("test_branch");
request.set_repository_name("test_repo");
auto mutable_commits = request.mutable_commits();
mutable_commits->Reserve(5);
for (int i = 0; i < 5; ++i) {
auto new_commit = new Commit();
new_commit->set_commit_id(i * 2);
mutable_commits->Add();
mutable_commits->at(i) = *new_commit;
}
PushResponse reply;
auto status = service.Push(context, &request, &reply);
EXPECT_TRUE(status.ok());
auto repos = service.Repos();
auto result_branch = repos.at(0).tree();
std::cerr << result_branch.at(0).DebugString() << std::endl;
}
} // namespace asm_test
} // namespace yaze_test