| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2025 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use std::sync::Arc;
- use async_trait::async_trait;
- use tinyjson::JsonValue;
- use tracing::{debug, error, info};
- use darkfi::{
- impl_p2p_message,
- net::{
- metering::MeteringConfiguration,
- protocol::protocol_generic::{
- ProtocolGenericAction, ProtocolGenericHandler, ProtocolGenericHandlerPtr,
- },
- session::SESSION_DEFAULT,
- Message, P2pPtr,
- },
- rpc::jsonrpc::JsonSubscriber,
- system::ExecutorPtr,
- util::time::NanoTimestamp,
- Error, Result,
- };
- use darkfi_serial::{SerialDecodable, SerialEncodable};
- /// Structure represening a foo request.
- #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
- pub struct FooRequest {
- /// Request message
- pub message: String,
- }
- impl_p2p_message!(
- FooRequest,
- "foorequest",
- 0,
- 0,
- MeteringConfiguration { threshold: 0, sleep_step: 0, expiry_time: NanoTimestamp::from_secs(0) }
- );
- /// Structure representing the response to `FooRequest`.
- #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
- pub struct FooResponse {
- /// Response code
- pub code: u8,
- }
- impl_p2p_message!(
- FooResponse,
- "fooresponse",
- 0,
- 0,
- MeteringConfiguration { threshold: 0, sleep_step: 0, expiry_time: NanoTimestamp::from_secs(0) }
- );
- /// Atomic pointer to the `ProtocolFoo` handler.
- pub type ProtocolFooHandlerPtr = Arc<ProtocolFooHandler>;
- /// Handler managing all `ProtocolFoo` messages, over generic P2P protocols.
- pub struct ProtocolFooHandler {
- /// The generic handler for `FooRequest` messages.
- handler: ProtocolGenericHandlerPtr<FooRequest, FooResponse>,
- }
- impl ProtocolFooHandler {
- /// Initialize the generic prototocol handlers for all `ProtocolFoo` messages
- /// and register them to the provided P2P network, using the default session flag.
- pub async fn init(p2p: &P2pPtr) -> ProtocolFooHandlerPtr {
- debug!(
- target: "damd::proto::protocol_foo::init",
- "Adding all foo protocols to the protocol registry"
- );
- let handler = ProtocolGenericHandler::new(p2p, "ProtocolFoo", SESSION_DEFAULT).await;
- Arc::new(Self { handler })
- }
- /// Start all `ProtocolFoo` background tasks.
- pub async fn start(&self, executor: &ExecutorPtr, subscriber: JsonSubscriber) -> Result<()> {
- debug!(
- target: "damd::proto::protocol_foo::start",
- "Starting foo protocols handlers tasks..."
- );
- self.handler.task.clone().start(
- handle_receive_foo_request(self.handler.clone(), subscriber),
- |res| async move {
- match res {
- Ok(()) | Err(Error::DetachedTaskStopped) => { /* Do nothing */ }
- Err(e) => error!(target: "damd::proto::protocol_foo::start", "Failed starting ProtocolFoo handler task: {e}"),
- }
- },
- Error::DetachedTaskStopped,
- executor.clone(),
- );
- debug!(
- target: "damd::proto::protocol_foo::start",
- "Foo protocols handlers tasks started!"
- );
- Ok(())
- }
- /// Stop all `ProtocolSync` background tasks.
- pub async fn stop(&self) {
- debug!(target: "damd::proto::protocol_foo::stop", "Terminating foo protocols handlers tasks...");
- self.handler.task.stop().await;
- debug!(target: "damd::proto::protocol_foo::stop", "Foo protocols handlers tasks terminated!");
- }
- }
- /// Background handler function for ProtocolFoo.
- async fn handle_receive_foo_request(
- handler: ProtocolGenericHandlerPtr<FooRequest, FooResponse>,
- subscriber: JsonSubscriber,
- ) -> Result<()> {
- debug!(target: "damd::proto::protocol_foo::handle_receive_foo_request", "START");
- loop {
- // Wait for a new foo request message
- let (channel, request) = match handler.receiver.recv().await {
- Ok(r) => r,
- Err(e) => {
- debug!(
- target: "damd::proto::protocol_foo::handle_receive_foo_request",
- "recv fail: {e}"
- );
- continue
- }
- };
- let notification = format!("Received foo request from {channel}: {}", request.message);
- info!(target: "damd::proto::protocol_foo::handle_receive_foo_request", "{notification}");
- // Notify subscriber
- subscriber.notify(vec![JsonValue::String(notification)].into()).await;
- // Send response
- handler
- .send_action(channel, ProtocolGenericAction::Response(FooResponse { code: 42 }))
- .await;
- }
- }
|