-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dcefa0
commit 0c03b81
Showing
13 changed files
with
334 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
cpp/ppc-gateway/ppc-gateway/gateway/router/GatewayRouterManager.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/** | ||
* 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 GatewayRouterManager.h | ||
* @author: yujiechen | ||
* @date 2024-08-26 | ||
*/ | ||
#include "GatewayRouterManager.h" | ||
#include "ppc-framework/gateway/GatewayProtocol.h" | ||
#include <ppc-gateway/Common.h> | ||
|
||
using namespace ppc::protocol; | ||
using namespace ppc; | ||
using namespace bcos; | ||
using namespace ppc::gateway; | ||
using namespace bcos::boostssl; | ||
using namespace bcos::boostssl::ws; | ||
|
||
GatewayRouterManager::GatewayRouterManager(Service::Ptr service, | ||
GatewayNodeInfoFactory::Ptr nodeStatusFactory, LocalRouter::Ptr localRouter, | ||
PeerRouterTable::Ptr peerRouter) | ||
: m_service(std::move(service)), | ||
m_nodeStatusFactory(std::move(nodeStatusFactory)), | ||
m_localRouter(std::move(localRouter)), | ||
m_peerRouter(std::move(peerRouter)) | ||
{ | ||
m_service->registerMsgHandler((uint16_t)GatewayPacketType::SyncNodeSeq, | ||
boost::bind(&GatewayRouterManager::onReceiveNodeSeqMessage, this, boost::placeholders::_1, | ||
boost::placeholders::_2)); | ||
|
||
m_service->registerMsgHandler((uint16_t)GatewayPacketType::RequestNodeStatus, | ||
boost::bind(&GatewayRouterManager::onReceiveRequestNodeStatusMsg, this, | ||
boost::placeholders::_1, boost::placeholders::_2)); | ||
|
||
m_service->registerMsgHandler((uint16_t)GatewayPacketType::ResponseNodeStatus, | ||
boost::bind(&GatewayRouterManager::onRecvResponseNodeStatusMsg, this, | ||
boost::placeholders::_1, boost::placeholders::_2)); | ||
|
||
m_timer = std::make_shared<Timer>(SEQ_SYNC_PERIOD, "seqSync"); | ||
// broadcast seq periodically | ||
m_timer->registerTimeoutHandler([this]() { broadcastStatusSeq(); }); | ||
} | ||
|
||
|
||
void GatewayRouterManager::start() | ||
{ | ||
if (m_running) | ||
{ | ||
GATEWAY_LOG(INFO) << LOG_DESC("GatewayRouterManager has already been started"); | ||
return; | ||
} | ||
m_running = true; | ||
m_timer->start(); | ||
GATEWAY_LOG(INFO) << LOG_DESC("start GatewayRouterManager success"); | ||
} | ||
|
||
void GatewayRouterManager::stop() | ||
{ | ||
if (!m_running) | ||
{ | ||
GATEWAY_LOG(INFO) << LOG_DESC("GatewayRouterManager has already been stopped"); | ||
return; | ||
} | ||
m_running = false; | ||
m_timer->stop(); | ||
GATEWAY_LOG(INFO) << LOG_DESC("stop GatewayRouterManager success"); | ||
} | ||
|
||
void GatewayRouterManager::onReceiveNodeSeqMessage(MessageFace::Ptr msg, WsSession::Ptr session) | ||
{ | ||
auto statusSeq = | ||
boost::asio::detail::socket_ops::network_to_host_long(*((uint32_t*)msg->payload()->data())); | ||
|
||
auto p2pMessage = std::dynamic_pointer_cast<Message>(msg); | ||
auto const& from = (p2pMessage->header()->srcGwNode().size() > 0) ? | ||
p2pMessage->header()->srcGwNode() : | ||
session->nodeId(); | ||
auto statusSeqChanged = statusChanged(from, statusSeq); | ||
if (!statusSeqChanged) | ||
{ | ||
return; | ||
} | ||
// status changed, request for the nodeStatus | ||
GATEWAY_LOG(TRACE) << LOG_DESC("onReceiveNodeSeqMessage") << LOG_KV("from", from) | ||
<< LOG_KV("statusSeq", statusSeq); | ||
m_service->asyncSendMessageByP2PNodeID( | ||
(uint16_t)GatewayPacketType::RequestNodeStatus, from, std::make_shared<bcos::bytes>()); | ||
} | ||
|
||
|
||
bool GatewayRouterManager::statusChanged(std::string const& p2pNodeID, uint32_t seq) | ||
{ | ||
bool ret = true; | ||
ReadGuard l(x_p2pID2Seq); | ||
auto it = m_p2pID2Seq.find(p2pNodeID); | ||
if (it != m_p2pID2Seq.end()) | ||
{ | ||
ret = (seq > it->second); | ||
} | ||
return ret; | ||
} | ||
|
||
void GatewayRouterManager::broadcastStatusSeq() | ||
{ | ||
m_timer->restart(); | ||
auto message = std::dynamic_pointer_cast<Message>(m_service->messageFactory()->buildMessage()); | ||
message->setPacketType((uint16_t)GatewayPacketType::SyncNodeSeq); | ||
auto seq = m_localRouter->statusSeq(); | ||
auto statusSeq = boost::asio::detail::socket_ops::host_to_network_long(seq); | ||
auto payload = std::make_shared<bytes>((byte*)&statusSeq, (byte*)&statusSeq + 4); | ||
message->setPayload(payload); | ||
GATEWAY_LOG(TRACE) << LOG_DESC("broadcastStatusSeq") << LOG_KV("seq", seq); | ||
m_service->asyncBroadcastMessage(message); | ||
} | ||
|
||
|
||
void GatewayRouterManager::onReceiveRequestNodeStatusMsg( | ||
MessageFace::Ptr msg, WsSession::Ptr session) | ||
{ | ||
auto p2pMessage = std::dynamic_pointer_cast<Message>(msg); | ||
auto const& from = (!p2pMessage->header()->srcGwNode().empty()) ? | ||
p2pMessage->header()->srcGwNode() : | ||
session->nodeId(); | ||
|
||
auto nodeStatusData = m_localRouter->generateNodeStatus(); | ||
if (!nodeStatusData) | ||
{ | ||
GATEWAY_LOG(WARNING) << LOG_DESC("onReceiveRequestNodeStatusMsg: generate nodeInfo error") | ||
<< LOG_KV("from", from); | ||
return; | ||
} | ||
GATEWAY_LOG(TRACE) << LOG_DESC("onReceiveRequestNodeStatusMsg: response the latest nodeStatus") | ||
<< LOG_KV("from", from); | ||
m_service->asyncSendMessageByP2PNodeID( | ||
(uint16_t)GatewayPacketType::ResponseNodeStatus, from, nodeStatusData); | ||
} | ||
|
||
void GatewayRouterManager::onRecvResponseNodeStatusMsg(MessageFace::Ptr msg, WsSession::Ptr session) | ||
{ | ||
auto nodeStatus = m_nodeStatusFactory->build(); | ||
nodeStatus->decode(bytesConstRef(msg->payload()->data(), msg->payload()->size())); | ||
|
||
auto p2pMessage = std::dynamic_pointer_cast<Message>(msg); | ||
auto const& from = (!p2pMessage->header()->srcGwNode().empty()) ? | ||
p2pMessage->header()->srcGwNode() : | ||
session->nodeId(); | ||
|
||
GATEWAY_LOG(INFO) << LOG_DESC("onRecvResponseNodeStatusMsg") << LOG_KV("from", from) | ||
<< LOG_KV("statusSeq", nodeStatus->statusSeq()) | ||
<< LOG_KV("agency", nodeStatus->agency()); | ||
updatePeerNodeStatus(from, nodeStatus); | ||
} | ||
|
||
void GatewayRouterManager::updatePeerNodeStatus( | ||
std::string const& p2pID, GatewayNodeInfo::Ptr status) | ||
{ | ||
auto statusSeq = status->statusSeq(); | ||
{ | ||
UpgradableGuard l(x_p2pID2Seq); | ||
if (m_p2pID2Seq.contains(p2pID) && (m_p2pID2Seq.at(p2pID) >= statusSeq)) | ||
{ | ||
return; | ||
} | ||
UpgradeGuard ul(l); | ||
m_p2pID2Seq[p2pID] = statusSeq; | ||
} | ||
GATEWAY_LOG(INFO) << LOG_DESC("updatePeerNodeStatus") << LOG_KV("from", p2pID) | ||
<< LOG_KV("statusSeq", status->statusSeq()) | ||
<< LOG_KV("agency", status->agency()); | ||
m_peerRouter->updateGatewayInfo(status); | ||
} |
70 changes: 70 additions & 0 deletions
70
cpp/ppc-gateway/ppc-gateway/gateway/router/GatewayRouterManager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GatewayRouterManager.h | ||
* @author: yujiechen | ||
* @date 2024-08-26 | ||
*/ | ||
#pragma once | ||
#include "ppc-gateway/gateway/router/GatewayNodeInfo.h" | ||
#include "ppc-gateway/gateway/router/LocalRouter.h" | ||
#include "ppc-gateway/gateway/router/PeerRouterTable.h" | ||
#include "ppc-gateway/p2p/Service.h" | ||
#include <bcos-utilities/Timer.h> | ||
#include <memory> | ||
|
||
namespace ppc::gateway | ||
{ | ||
class GatewayRouterManager | ||
{ | ||
public: | ||
using Ptr = std::shared_ptr<GatewayRouterManager>; | ||
GatewayRouterManager(Service::Ptr service, GatewayNodeInfoFactory::Ptr nodeStatusFactory, | ||
LocalRouter::Ptr localRouter, PeerRouterTable::Ptr peerRouter); | ||
virtual void start(); | ||
virtual void stop(); | ||
|
||
protected: | ||
virtual void onReceiveNodeSeqMessage( | ||
bcos::boostssl::MessageFace::Ptr msg, bcos::boostssl::ws::WsSession::Ptr session); | ||
|
||
virtual void onReceiveRequestNodeStatusMsg( | ||
bcos::boostssl::MessageFace::Ptr msg, bcos::boostssl::ws::WsSession::Ptr session); | ||
|
||
virtual void onRecvResponseNodeStatusMsg( | ||
bcos::boostssl::MessageFace::Ptr msg, bcos::boostssl::ws::WsSession::Ptr session); | ||
bool statusChanged(std::string const& p2pNodeID, uint32_t seq); | ||
void broadcastStatusSeq(); | ||
|
||
void updatePeerNodeStatus(std::string const& p2pID, GatewayNodeInfo::Ptr status); | ||
|
||
private: | ||
Service::Ptr m_service; | ||
GatewayNodeInfoFactory::Ptr m_nodeStatusFactory; | ||
std::shared_ptr<bcos::Timer> m_timer; | ||
|
||
LocalRouter::Ptr m_localRouter; | ||
PeerRouterTable::Ptr m_peerRouter; | ||
|
||
bool m_running = false; | ||
|
||
// P2pID => statusSeq | ||
std::map<std::string, uint32_t> m_p2pID2Seq; | ||
mutable bcos::SharedMutex x_p2pID2Seq; | ||
|
||
// TODO: make this configurable | ||
unsigned const SEQ_SYNC_PERIOD = 3000; | ||
}; | ||
} // namespace ppc::gateway |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.