Skip to content

Commit

Permalink
add gateway initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Sep 4, 2024
1 parent b3b53bf commit 836adea
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 37 deletions.
7 changes: 7 additions & 0 deletions cpp/ppc-framework/protocol/GrpcConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
* @date 2024-09-02
*/
#pragma once
#include "ppc-framework/protocol/EndPoint.h"
#include <memory>
#include <string>

namespace ppc::protocol
{
struct GrpcServerConfig
{
ppc::protocol::EndPoint endPoint;

std::string listenEndPoint() const { return endPoint.listenEndPoint(); }
};
class GrpcConfig
{
public:
Expand Down
2 changes: 2 additions & 0 deletions cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "NetworkConfig.h"
#include "ParamChecker.h"
#include "ppc-framework/front/FrontConfig.h"
#include "ppc-framework/protocol/GrpcConfig.h"
#include "ppc-framework/storage/CacheStorage.h"
#include <bcos-utilities/Common.h>
#include <memory.h>
Expand Down Expand Up @@ -99,6 +100,7 @@ struct GatewayConfig

bool disableCache;
NetworkConfig networkConfig;
ppc::protocol::GrpcServerConfig grpcConfig;
ppc::storage::CacheStorageConfig cacheStorageConfig;
std::string nodeFileName;
std::string nodePath;
Expand Down
2 changes: 1 addition & 1 deletion cpp/wedpr-main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(ppc-main VERSION ${VERSION})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_subdirectory(air-node)
add_subdirectory(pro-node)
#TODO: ppc-gateway
add_subdirectory(gateway)

if (BUILD_CEM)
add_subdirectory(cem-node)
Expand Down
5 changes: 2 additions & 3 deletions cpp/wedpr-main/gateway/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
cmake_minimum_required(VERSION 3.14)

project(ppctars-Gateway)
project(wedpr-gateway)

include_directories(${CMAKE_SOURCE_DIR})


aux_source_directory(./ SRC_LIST)
aux_source_directory(../../ppc-tars-service/GatewayService SRC_LIST)
add_executable(${GATEWAY_BINARY_NAME} ${SRC_LIST})

target_link_libraries(${GATEWAY_BINARY_NAME} ${GATEWAY_TARGET} ${STORAGE_TARGET} ${PROTOCOL_TARGET} ${HELPER_TARGET})
target_link_libraries(${GATEWAY_BINARY_NAME} ${GATEWAY_TARGET} ${STORAGE_TARGET} ${SERVICE_CLIENT_TARGET} ${SERVICE_SERVER_TARGET} ${HELPER_TARGET})
83 changes: 83 additions & 0 deletions cpp/wedpr-main/gateway/GatewayInitializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (C) 2022 WeDPR.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @file GatewayInitializer.cpp
* @author: yujiechen
* @date 2022-11-14
*/
#include "GatewayInitializer.h"
#include "grpc/server/GatewayServer.h"
#include "grpc/server/GrpcServer.h"
#include "ppc-gateway/GatewayFactory.h"
#include "ppc-tools/src/config/PPCConfig.h"
#include "protobuf/NodeInfoImpl.h"
#include "wedpr-protocol/grpc/client/RemoteFrontBuilder.h"
#include "wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h"

using namespace ppc::tools;
using namespace ppc::protocol;
using namespace bcos;
using namespace ppc::gateway;
using namespace ppc::front;

void GatewayInitializer::init(std::string const& _configPath)
{
// init the log
boost::property_tree::ptree pt;
boost::property_tree::read_ini(_configPath, pt);

m_logInitializer = std::make_shared<BoostLogInitializer>();
m_logInitializer->initLog(pt);
INIT_LOG(INFO) << LOG_DESC("initLog success");

INIT_LOG(INFO) << LOG_DESC("initGateway: ") << _configPath;
auto config = std::make_shared<PPCConfig>();

config->loadGatewayConfig(ppc::protocol::NodeArch::PRO, nullptr, _configPath);
auto threadPool = std::make_shared<bcos::ThreadPool>(
"gateway", config->gatewayConfig().networkConfig.threadPoolSize);

GatewayFactory gatewayFactory(config);
m_gateway = gatewayFactory.build(std::make_shared<RemoteFrontBuilder>(config->grpcConfig()));

m_server = std::make_shared<GrpcServer>(config->gatewayConfig().grpcConfig);
// register the gateway service
auto gatewayService = std::make_shared<GatewayServer>(m_gateway,
std::make_shared<MessageOptionalHeaderBuilderImpl>(), std::make_shared<NodeInfoFactory>());
m_server->registerService(gatewayService);
}

void GatewayInitializer::start()
{
if (m_gateway)
{
m_gateway->start();
}
if (m_server)
{
m_server->start();
}
}
void GatewayInitializer::stop()
{
if (m_server)
{
m_server->stop();
}
if (m_gateway)
{
m_gateway->start();
}
}
49 changes: 49 additions & 0 deletions cpp/wedpr-main/gateway/GatewayInitializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2022 WeDPR.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @file GatewayInitializer.h
* @author: yujiechen
* @date 2022-11-14
*/
#pragma once
#include "ppc-framework/gateway/IGateway.h"
#include <bcos-utilities/BoostLogInitializer.h>
#include <bcos-utilities/Log.h>
#include <memory>

#define INIT_LOG(LEVEL) BCOS_LOG(LEVEL) << LOG_BADGE("GATEWAYInit")
namespace ppc::protocol
{
class GrpcServer;
}
namespace ppc::gateway
{
class GatewayInitializer
{
public:
using Ptr = std::shared_ptr<GatewayInitializer>;
GatewayInitializer() = default;
virtual ~GatewayInitializer() { stop(); }

virtual void init(std::string const& _configPath);
virtual void start();
virtual void stop();

protected:
bcos::BoostLogInitializer::Ptr m_logInitializer;
ppc::gateway::IGateway::Ptr m_gateway;
std::shared_ptr<ppc::protocol::GrpcServer> m_server;
};
} // namespace ppc::gateway
34 changes: 8 additions & 26 deletions cpp/wedpr-main/gateway/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 WeDPR.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,32 +15,14 @@
*
* @file main.cpp
* @author: yujiechen
* @date 2022-11-25
* @date 2022-11-14
*/
#include "GatewayServiceApp.h"
#include "libhelper/CommandHelper.h"
#include "GatewayInitializer.h"
#include "wedpr-main/common/NodeStarter.h"

using namespace ppctars;

int main(int argc, char* argv[])
using namespace ppc::node;
int main(int argc, const char* argv[])
{
try
{
ppc::initAppCommandLine(argc, argv);
GatewayServiceApp app;
app.main(argc, argv);
app.waitForShutdown();

return 0;
}
catch (std::exception& e)
{
cerr << "ppc-gateway-service std::exception:" << boost::diagnostic_information(e)
<< std::endl;
}
catch (...)
{
cerr << "ppc-gateway-service unknown exception." << std::endl;
}
return -1;
auto initializer = std::make_shared<ppc::gateway::GatewayInitializer>();
startProgram(argc, argv, "ppc-gateway-service", initializer);
}
8 changes: 1 addition & 7 deletions cpp/wedpr-protocol/grpc/server/GrpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
* @date 2024-09-03
*/
#pragma once
#include "ppc-framework/protocol/EndPoint.h"
#include "ppc-framework/protocol/GrpcConfig.h"
#include <grpcpp/grpcpp.h>
#include <memory>
#include <string>

namespace ppc::protocol
{
// refer to: https://grpc.io/docs/languages/cpp/callback/
struct GrpcServerConfig
{
ppc::protocol::EndPoint endPoint;

std::string listenEndPoint() const { return endPoint.listenEndPoint(); }
};
class GrpcServer
{
public:
Expand Down

0 comments on commit 836adea

Please sign in to comment.