Skip to content

Commit

Permalink
add gateway router (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull authored Aug 27, 2024
1 parent ca71127 commit 6dcefa0
Show file tree
Hide file tree
Showing 32 changed files with 2,102 additions and 36 deletions.
95 changes: 95 additions & 0 deletions cpp/ppc-framework/front/FrontConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright (C) 2023 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 FrontConfig.h
* @author: yujiechen
* @date 2024-08-22
*/

#pragma once
#include <memory>
#include <string>
#include <vector>

namespace ppc::front
{
/**
* @brief the gateway endpoint
*
*/
class GatewayEndPoint
{
public:
GatewayEndPoint() = default;
GatewayEndPoint(std::string const& host, uint16_t port) : m_host(std::move(host)), m_port(port)
{}
virtual ~GatewayEndPoint() = default;

virtual std::string const& host() const { return m_host; }
uint16_t port() const { return m_port; }

void setHost(std::string host) { m_host = std::move(host); }
void setPort(uint16_t port) { m_port = port; }

private:
// the host
std::string m_host;
// the port
uint16_t m_port;
};

// Note: swig explosed interface
class FrontConfig
{
public:
using Ptr = std::shared_ptr<FrontConfig>;
FrontConfig(int threadPoolSize, std::string agencyID)
: m_threadPoolSize(threadPoolSize), m_agencyID(std::move(agencyID))
{}
virtual ~FrontConfig() = default;

virtual int threadPoolSize() const { return m_threadPoolSize; }
virtual std::string const agencyID() const { return m_agencyID; }
virtual std::vector<GatewayEndPoint> const& gatewayInfo() const { return m_gatewayInfo; }
virtual void setGatewayInfo(std::vector<GatewayEndPoint> gatewayInfo)
{
m_gatewayInfo = std::move(gatewayInfo);
}

virtual void appendGatewayInfo(GatewayEndPoint&& endpoint)
{
// TODO:check the endpoint
m_gatewayInfo.push_back(endpoint);
}

private:
int m_threadPoolSize;
std::string m_agencyID;
std::vector<GatewayEndPoint> m_gatewayInfo;
};

class FrontConfigBuilder
{
public:
using Ptr = std::shared_ptr<FrontConfigBuilder>;
FrontConfigBuilder() = default;
virtual ~FrontConfigBuilder() = default;

FrontConfig::Ptr build(int threadPoolSize, std::string agencyID)
{
return std::make_shared<FrontConfig>(threadPoolSize, agencyID);
}
};
} // namespace ppc::front
106 changes: 106 additions & 0 deletions cpp/ppc-framework/front/IFront.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright (C) 2023 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 IFront.h
* @author: yujiechen
* @date 2024-08-22
*/
#pragma once
#include "FrontConfig.h"
#include "ppc-framework/protocol/Message.h"
#include "ppc-framework/protocol/RouteType.h"
#include <bcos-utilities/Error.h>

namespace ppc::front
{
class IFront
{
public:
using Ptr = std::shared_ptr<IFront>;

IFront() = default;
virtual ~IFront() = default;

/**
* @brief start the IFront
*
* @param front the IFront to start
*/
virtual void start() const = 0;
/**
* @brief stop the IFront
*
* @param front the IFront to stop
*/
virtual void stop() const = 0;

/**
*
* @param front the front object
* @param topic the topic
* @param callback the callback called when receive specified topic
*/
virtual void registerTopicHandler(
std::string const& topic, ppc::protocol::MessageCallback callback) = 0;

/**
* @brief async send message
*
* @param routeType the route type
* @param topic the topic
* @param dstInst the dst agency(must set when 'route by agency' and 'route by
* component')
* @param dstNodeID the dst nodeID(must set when 'route by nodeID')
* @param componentType the componentType(must set when 'route by component')
* @param payload the payload to send
* @param seq the message seq
* @param timeout timeout
* @param callback callback
*/
virtual void asyncSendMessage(ppc::protocol::RouteType routeType, std::string const& topic,
std::string const& dstInst, bcos::bytes const& dstNodeID, std::string const& componentType,
bcos::bytes&& payload, int seq, long timeout, ppc::protocol::MessageCallback callback) = 0;

// the sync interface for async_send_message
virtual ppc::protocol::Message::Ptr push(ppc::protocol::RouteType routeType, std::string topic,
std::string dstInst, std::string dstNodeID, std::string const& componentType,
bcos::bytes&& payload, int seq, long timeout) = 0;

/**
* @brief: receive message from gateway, call by gateway
* @param _message: received ppc message
* @return void
*/
virtual void onReceiveMessage(
ppc::protocol::Message::Ptr const& _msg, ppc::protocol::ReceiveMsgFunc _callback) = 0;
};

class IFrontBuilder
{
public:
using Ptr = std::shared_ptr<IFrontBuilder>;
IFrontBuilder() = default;
virtual ~IFrontBuilder() = default;

/**
* @brief create the Front using specified config
*
* @param config the config used to build the Front
* @return IFront::Ptr he created Front
*/
virtual IFront::Ptr build(ppc::front::FrontConfig::Ptr config) const = 0;
virtual IFront::Ptr buildClient(std::string endPoint) const = 0;
};
} // namespace ppc::front
12 changes: 12 additions & 0 deletions cpp/ppc-framework/gateway/GatewayProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace ppc::gateway
enum class GatewayPacketType : uint16_t
{
P2PMessage = 0x00,
BroadcastMessage = 0x01,
RouterTableSyncSeq = 0x10,
RouterTableResponse = 0x11,
RouterTableRequest = 0x12
Expand All @@ -33,5 +34,16 @@ enum class GatewayPacketType : uint16_t
enum class GatewayMsgExtFlag : uint16_t
{
Response = 0x1,
RouteByNodeID = 0x2,
RouteByAgency = 0x4,
RouteByComponent = 0x8,
RouteByTopic = 0x10
};

enum CommonError : int32_t
{
SUCCESS = 0,
TIMEOUT = 1000, // for gateway
NotFoundFrontServiceDispatchMsg = 1001
};
} // namespace ppc::gateway
70 changes: 70 additions & 0 deletions cpp/ppc-framework/gateway/IGateway.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2023 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 IGateway.h
* @author: yujiechen
* @date 2024-08-26
*/
#pragma once
#include "../protocol/INodeInfo.h"
#include "../protocol/Message.h"
#include "../protocol/RouteType.h"
#include <bcos-utilities/Error.h>


namespace ppc::gateway
{
using ErrorCallbackFunc = std::function<void(bcos::Error::Ptr)>;
/**
* @brief: A list of interfaces provided by the gateway which are called by the front service.
*/
class IGateway
{
public:
using Ptr = std::shared_ptr<IGateway>;
IGateway() = default;
virtual ~IGateway() {}

/**
* @brief: start/stop service
*/
virtual void start() = 0;
virtual void stop() = 0;

/**
* @brief send message to gateway
*
* @param routeType the route type
* @param topic the topic
* @param dstInst the dst agency(must set when 'route by agency' and 'route by
* component')
* @param dstNodeID the dst nodeID(must set when 'route by nodeID')
* @param componentType the componentType(must set when 'route by component')
* @param payload the payload to send
* @param seq the message seq
* @param timeout timeout
* @param callback callback
*/
virtual void asyncSendMessage(ppc::protocol::RouteType routeType, std::string const& topic,
std::string const& dstInst, bcos::bytes const& dstNodeID, std::string const& componentType,
bcos::bytes&& payload, long timeout, ppc::protocol::ReceiveMsgFunc callback) = 0;

virtual void registerNodeInfo(ppc::protocol::INodeInfo::Ptr const& nodeInfo);
virtual void unRegisterNodeInfo(bcos::bytesConstRef nodeID);
virtual void registerTopic(bcos::bytesConstRef nodeID, std::string const& topic);
virtual void unRegisterTopic(bcos::bytesConstRef nodeID, std::string const& topic);
};

} // namespace ppc::gateway
65 changes: 65 additions & 0 deletions cpp/ppc-framework/protocol/INodeInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (C) 2023 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 INodeInfo.h
* @author: yujiechen
* @date 2024-08-26
*/
#pragma once
#include "ppc-framework/front/IFront.h"
#include <memory>

namespace ppc::protocol
{
// the node information
class INodeInfo
{
public:
using Ptr = std::shared_ptr<INodeInfo>;
INodeInfo() = default;
virtual ~INodeInfo() = default;

virtual std::string const& endPoint() const = 0;
virtual bcos::bytesConstRef nodeID() const = 0;

// components
virtual void setComponents(std::vector<std::string> const& components) = 0;
virtual std::set<std::string> const& components() const = 0;

virtual void encode(bcos::bytes& data) const = 0;
virtual void decode(bcos::bytesConstRef data) = 0;

virtual void setFront(ppc::front::IFront::Ptr&& front) = 0;
virtual ppc::front::IFront::Ptr const& getFront() const = 0;

virtual bool equal(INodeInfo::Ptr const& info)
{
return (nodeID() == info->nodeID()) && (components() == info->components());
}
};
class INodeInfoFactory
{
public:
using Ptr = std::shared_ptr<INodeInfoFactory>;
INodeInfoFactory(bcos::bytes nodeID) : m_nodeID(std::move(nodeID)) {}
virtual ~INodeInfoFactory() = default;

virtual INodeInfo::Ptr build() = 0;
virtual INodeInfo::Ptr build(std::string const& endPoint) = 0;

protected:
bcos::bytes m_nodeID;
};
} // namespace ppc::protocol
Loading

0 comments on commit 6dcefa0

Please sign in to comment.