Skip to main content

datex_native/com_interfaces/
mod.rs

1use datex_core::network::com_hub::ComHub;
2
3pub mod tcp;
4pub mod websocket;
5pub mod http;
6pub mod serial;
7pub mod webrtc;
8
9
10/// Registers all enabled native interface factories to the provided ComHub.
11pub fn register_native_interface_factories(com_hub: &ComHub) {
12    #[cfg(feature = "websocket")]
13    {
14        com_hub.register_async_interface_factory::<websocket::websocket_client::WebSocketClientInterfaceSetupDataNative>();
15        com_hub.register_async_interface_factory::<websocket::websocket_server::WebSocketServerInterfaceSetupDataNative>();
16    }
17    #[cfg(feature = "serial")]
18    {
19        com_hub.register_async_interface_factory::<serial::serial_client::SerialClientInterfaceSetupDataNative>();
20    }
21    #[cfg(feature = "tcp")]
22    {
23        com_hub.register_async_interface_factory::<tcp::tcp_client::TCPClientInterfaceSetupDataNative>();
24        com_hub.register_async_interface_factory::<tcp::tcp_server::TCPServerInterfaceSetupDataNative>();
25    }
26    #[cfg(feature = "http")]
27    {
28        com_hub.register_async_interface_factory::<http::http_server::HTTPServerInterfaceSetupDataNative>();
29        com_hub.register_async_interface_factory::<http::http_client::HTTPClientInterfaceSetupDataNative>();
30    }
31    // TODO:
32    // #[cfg(feature = "webrtc")]
33}
34
35
36#[cfg(test)]
37pub mod tests {
38    use datex_core::global::dxb_block::DXBBlock;
39    use datex_core::network::com_interfaces::com_interface::factory::{ComInterfaceConfiguration, SendCallback, SocketConfiguration, SocketDataIterator};
40    use datex_core::utils::async_iterators::async_next_pin_box;
41
42    /// Test utility function to test client-server communication for two sockets
43    /// Sends and receives data in both directions
44    pub async fn test_client_server_sockets(
45        server_socket_configuration: SocketConfiguration,
46        client_socket_configuration: SocketConfiguration,
47    ) {
48        // send data from client to server
49        let message = DXBBlock::new_with_body(b"Hello, World!");
50        test_send_block_async_callback(&client_socket_configuration.send_callback, message.clone()).await;
51
52        // receive data on server
53        test_receive_block(&mut server_socket_configuration.iterator.unwrap(), message).await;
54
55        // send data from server to client
56        let response_message = DXBBlock::new_with_body(b"Hello back!");
57        test_send_block_async_callback(&server_socket_configuration.send_callback, response_message.clone()).await;
58
59        // receive data on client
60        test_receive_block(&mut client_socket_configuration.iterator.unwrap(), response_message).await;
61    }
62    
63    /// Test utility function to test client-server communication for two interfaces
64    pub async fn test_client_server_interfaces(
65        server_interface_configuration: ComInterfaceConfiguration,
66        client_interface_configuration: ComInterfaceConfiguration,
67    ) {
68        // check if sockets were created
69        let mut server_socket_iterator = server_interface_configuration.new_sockets_iterator;
70        let server_socket = async_next_pin_box(&mut server_socket_iterator).await;
71        assert!(server_socket.is_some());
72        let server_socket = server_socket.unwrap();
73        assert!(server_socket.is_ok());
74        let server_socket = server_socket.unwrap();
75
76        let mut client_socket_iterator = client_interface_configuration.new_sockets_iterator;
77        let client_socket = async_next_pin_box(&mut client_socket_iterator).await;
78        assert!(client_socket.is_some());
79        let client_socket = client_socket.unwrap();
80        assert!(client_socket.is_ok());
81        let client_socket = client_socket.unwrap();
82
83        test_client_server_sockets(
84            server_socket,
85            client_socket
86        ).await;
87    }
88
89    /// Test utility function to send a block using the provided send callback.
90    /// Asserts that the callback is asynchronous and successfully sends the block.
91    pub(crate) async fn test_send_block_async_callback(send_callback: &Option<SendCallback>, block: DXBBlock) {
92        match send_callback {
93            Some(SendCallback::Async(callback)) => {
94                callback.call(block).await.unwrap();
95            }
96            _ => panic!("Expected async send callback"),
97        }
98    }
99
100    /// Test utility function to send a block using the provided send callback.
101    /// Asserts that the callback is asynchronous and successfully sends the block.
102    pub(crate) fn test_send_block_sync_once_callback(send_callback: &Option<SendCallback>, block: DXBBlock) {
103        match send_callback {
104            Some(SendCallback::SyncOnce(callback)) => {
105                callback(block).unwrap();
106            }
107            _ => panic!("Expected sync once callback"),
108        }
109    }
110    
111    /// Test utility function to receive a block from the provided socket data iterator.
112    /// Asserts that the received block matches the provided block.
113    pub(crate) async fn test_receive_block(block_iterator: &mut SocketDataIterator, matches_block: DXBBlock) {
114        let received_block = async_next_pin_box(block_iterator).await;
115        assert!(received_block.is_some());
116        let received_block = received_block.unwrap();
117        assert!(received_block.is_ok());
118        let received_data = received_block.unwrap();
119        assert_eq!(received_data, matches_block.to_bytes());
120    }
121}