LCOV - code coverage report
Current view: top level - libs/desim/src - node_os.rs (source / functions) Coverage Total Hit
Test: 8b13a09a5c233d98abd4a0d3e59157e7db16d6fd.info Lines: 100.0 % 21 21
Test Date: 2024-11-21 10:53:51 Functions: 100.0 % 7 7

            Line data    Source code
       1              : use std::sync::Arc;
       2              : 
       3              : use rand::Rng;
       4              : 
       5              : use crate::proto::NodeEvent;
       6              : 
       7              : use super::{
       8              :     chan::Chan,
       9              :     network::TCP,
      10              :     world::{Node, NodeId, World},
      11              : };
      12              : 
      13              : /// Abstraction with all functions (aka syscalls) available to the node.
      14              : #[derive(Clone)]
      15              : pub struct NodeOs {
      16              :     world: Arc<World>,
      17              :     internal: Arc<Node>,
      18              : }
      19              : 
      20              : impl NodeOs {
      21        18403 :     pub fn new(world: Arc<World>, internal: Arc<Node>) -> NodeOs {
      22        18403 :         NodeOs { world, internal }
      23        18403 :     }
      24              : 
      25              :     /// Get the node id.
      26         9521 :     pub fn id(&self) -> NodeId {
      27         9521 :         self.internal.id
      28         9521 :     }
      29              : 
      30              :     /// Opens a bidirectional connection with the other node. Always successful.
      31        33688 :     pub fn open_tcp(&self, dst: NodeId) -> TCP {
      32        33688 :         self.world.open_tcp(dst)
      33        33688 :     }
      34              : 
      35              :     /// Returns a channel to receive node events (socket Accept and internal messages).
      36        18950 :     pub fn node_events(&self) -> Chan<NodeEvent> {
      37        18950 :         self.internal.node_events()
      38        18950 :     }
      39              : 
      40              :     /// Get current time.
      41       310307 :     pub fn now(&self) -> u64 {
      42       310307 :         self.world.now()
      43       310307 :     }
      44              : 
      45              :     /// Generate a random number in range [0, max).
      46        32427 :     pub fn random(&self, max: u64) -> u64 {
      47        32427 :         self.internal.rng.lock().gen_range(0..max)
      48        32427 :     }
      49              : 
      50              :     /// Append a new event to the world event log.
      51        26819 :     pub fn log_event(&self, data: String) {
      52        26819 :         self.internal.log_event(data)
      53        26819 :     }
      54              : }
        

Generated by: LCOV version 2.1-beta