datex-core 0.0.14

The DATEX Core Rust implementation
Documentation
use crate::{
    core_compiler::{core_compilation_context::CompileInput, panic::Panic},
    global::{
        dxb_block::{DXBBlock, IncomingSection, OutgoingContextId},
        protocol_structures::{
            block_header::{BlockHeader, BlockType, FlagsAndTimestamp},
            encrypted_header::EncryptedHeader,
            routing_header::RoutingHeader,
        },
    },
    runtime::{RuntimeInternal, execution::ExecutionError},
    values::{
        core_values::endpoint::Endpoint, value_container::ValueContainer,
    },
};

use crate::{
    core_compiler::value_compiler::compile_value_container, prelude::*,
};
use core::result::Result;
use log::info;

impl RuntimeInternal {
    pub(crate) async fn handle_incoming_sections_task(
        self: Rc<RuntimeInternal>,
    ) {
        let mut sections_receiver = self.incoming_sections_receiver_mut();

        while let Some(section) = sections_receiver.next().await {
            let self_clone = self.clone();
            // for embassy, run all sections in the same task to avoid spawning too many tasks
            #[cfg(feature = "embassy_runtime")]
            {
                self_clone.handle_incoming_section_task(section).await;
            }
            // otherwise, run each section in its own task
            #[cfg(not(feature = "embassy_runtime"))]
            {
                // TODO #741: task
                self.task_manager().register_task(
                    self_clone.handle_incoming_section_task(section),
                );
            }
        }
    }

    pub(crate) async fn handle_reconnect_requests_task(
        self: Rc<RuntimeInternal>,
    ) {
        let mut receiver = self.com_interface_reconnect_request_receiver_mut();

        while let Some(reconnect_request) = receiver.next().await {
            // for embassy, run all sections in the same task to avoid spawning too many tasks
            #[cfg(feature = "embassy_runtime")]
            {
                self.com_hub()
                    .clone()
                    .try_reconnect_interface(reconnect_request)
                    .await;
            }
            // otherwise, run each section in its own task
            #[cfg(not(feature = "embassy_runtime"))]
            {
                // TODO #741: task
                self.task_manager().register_task(
                    self.com_hub()
                        .clone()
                        .try_reconnect_interface(reconnect_request),
                );
            }
        }
    }

    async fn handle_incoming_section_task(
        self: Rc<RuntimeInternal>,
        section: IncomingSection,
    ) {
        let (result, endpoint, context_id, mark_debug) =
            RuntimeInternal::execute_incoming_section(
                self.clone(),
                section,
                None,
            )
            .await;
        match &result {
            Ok(Some(result)) => self.log_info(format!(
                "Successful Execution result (from {}): {}",
                endpoint, result
            )),
            Ok(None) => self.log_info(format!(
                "Successful Execution result (from {}): None",
                endpoint
            )),
            Err(e) => self
                .log_info(format!("Execution error (from {}): {e}", endpoint)),
        }

        // send response back to the sender
        let _res = RuntimeInternal::send_response_block(
            self.clone(),
            result,
            endpoint,
            context_id,
            mark_debug,
        )
        .await;
        // TODO #231: handle errors in sending response
    }

    async fn send_response_block(
        self: Rc<RuntimeInternal>,
        result: Result<Option<ValueContainer>, ExecutionError>,
        receiver_endpoint: Endpoint,
        context_id: OutgoingContextId,
        mark_debug: bool,
    ) -> Result<(), Vec<Endpoint>> {
        let routing_header: RoutingHeader = RoutingHeader::default()
            .with_sender(self.endpoint().clone())
            .to_owned();
        let block_header = BlockHeader {
            context_id,
            flags_and_timestamp: FlagsAndTimestamp::new()
                .with_block_type(BlockType::Response)
                .with_is_end_of_section(true)
                .with_is_end_of_context(true)
                .with_is_debug_block(mark_debug),
            ..BlockHeader::default()
        };
        let encrypted_header = EncryptedHeader::default();

        info!(
            "send response, context_id: {context_id:?}, receiver: {receiver_endpoint}"
        );

        let block = {
            let lookup = self.pointer_availability_lookup();
            let receivers = vec![receiver_endpoint.clone()];
            let compile_input = CompileInput::new(&lookup, &receivers);

            let dxb = match result {
                Ok(value) => {
                    // info!("Sending result value {:?}", value);
                    if let Some(value) = &value {
                        let mut cache = self.shared_references_cache_mut();
                        let res = compile_value_container(
                            value,
                            compile_input,
                            &mut cache,
                        );
                        drop(lookup);
                        drop(cache);

                        self.clone()
                            .register_shared_containers_for_single_endpoint(
                                &receiver_endpoint,
                                res.shared_values,
                            );

                        res.dxb
                    } else {
                        vec![]
                    }
                }
                Err(e) => {
                    info!("Execution error (on {}): {}", self.endpoint(), e);
                    let mut cache = self.shared_references_cache_mut();
                    Panic::new(e.to_string())
                        .to_bytes(compile_input, &mut cache)
                }
            };

            let mut block = DXBBlock::new(
                routing_header,
                block_header,
                encrypted_header,
                dxb,
            );
            block.set_receivers(core::slice::from_ref(&receiver_endpoint));
            block
        };

        self.com_hub().send_own_block_async(block).await
    }
}