1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// OPCUA for Rust
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2017-2022 Adam Lock

//! Provides diagnostics structures and functions for gathering information about the running
//! state of a server.
use opcua_types::service_types::ServerDiagnosticsSummaryDataType;

use opcua_core::RUNTIME;

use crate::{session::Session, subscriptions::subscription::Subscription};

/// Structure that captures di                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          agnostics information for the server
#[derive(Clone, Serialize, Debug)]
pub struct ServerDiagnostics {
    /// This is a live summary of the server diagnostics
    server_diagnostics_summary: ServerDiagnosticsSummaryDataType,
}

const SERVER_DIAGNOSTICS: &str = "ServerDiagnostics";

impl Default for ServerDiagnostics {
    fn default() -> Self {
        register_runtime_component!(SERVER_DIAGNOSTICS);
        Self {
            server_diagnostics_summary: ServerDiagnosticsSummaryDataType::default(),
        }
    }
}

impl Drop for ServerDiagnostics {
    fn drop(&mut self) {
        deregister_runtime_component!(SERVER_DIAGNOSTICS);
    }
}

impl ServerDiagnostics {
    /// Return a completed summary of the server diagnostics as they stand. This structure
    /// is used to fill the address space stats about the server.
    pub fn server_diagnostics_summary(&self) -> &ServerDiagnosticsSummaryDataType {
        &self.server_diagnostics_summary
    }

    /// Increment the number of requests that were rejected due to security constraints since the server was
    /// started (or restarted). The requests include all Services defined in Part 4, also requests
    /// to create sessions.
    pub(crate) fn on_rejected_security_session(&mut self) {
        self.server_diagnostics_summary
            .security_rejected_session_count += 1;
    }

    /// Increment the number of requests that were rejected since the server was started (or restarted). The
    /// requests include all Services defined in Part 4, also requests to create sessions. This
    /// number includes the securityRejectedRequestsCount.
    pub(crate) fn on_rejected_session(&mut self) {
        self.server_diagnostics_summary.rejected_session_count += 1;
    }

    /// Increment the number of client sessions currently established in the server.
    pub(crate) fn on_create_session(&mut self, _session: &Session) {
        self.server_diagnostics_summary.current_session_count += 1;
        self.server_diagnostics_summary.cumulated_session_count += 1;
        debug!(
            "Incrementing current session count to {}",
            self.server_diagnostics_summary.current_session_count
        );
    }

    /// Decrement the number of client sessions currently established in the server.
    pub(crate) fn on_destroy_session(&mut self, _session: &Session) {
        self.server_diagnostics_summary.current_session_count -= 1;
        debug!(
            "Decrementing current session count to {}",
            self.server_diagnostics_summary.current_session_count
        );
    }

    /// Increment the number of subscriptions currently established in the server.
    pub(crate) fn on_create_subscription(&mut self, _subscription: &Subscription) {
        self.server_diagnostics_summary.current_subscription_count += 1;
        self.server_diagnostics_summary.cumulated_subscription_count += 1;
    }

    /// Decrement the number of subscriptions currently established in the server.
    pub(crate) fn on_destroy_subscription(&mut self, _subscription: &Subscription) {
        self.server_diagnostics_summary.current_subscription_count -= 1;
    }

    /// Increment the number of client sessions that were closed due to timeout since the server was started (or restarted).
    pub(crate) fn on_session_timeout(&mut self) {
        self.server_diagnostics_summary.session_timeout_count += 1;
    }

    // --- These are not yet called by anything

    /*
    /// Increment the number of server-created views in the server.
    pub(crate) fn on_server_view(&mut self, _session: &Session) {
        self.server_diagnostics_summary.server_view_count += 1;
        unimplemented!();
    }

    /// Increment the number of client sessions that were closed due to errors since the server was started (or restarted).
    pub(crate) fn on_session_abort(&mut self, _session: &Session) {
        self.server_diagnostics_summary.session_abort_count += 1;
        unimplemented!()
    }

    /// Increment the number of publishing intervals currently supported in the server.
    pub(crate) fn on_publishing_interval(&mut self) {
        self.server_diagnostics_summary.publishing_interval_count += 1;
        unimplemented!()
    }

    /// Increment the number of requests that were rejected due to security constraints since the server was
    /// started (or restarted). The requests include all Services defined in Part 4, also requests
    /// to create sessions.
    pub fn on_security_rejected_request(&mut self) {
        self.server_diagnostics_summary.security_rejected_requests_count += 1;
        unimplemented!()
    }

    /// Increment the number of requests that were rejected since the server was started (or restarted). The
    /// requests include all Services defined in Part 4, also requests to create sessions. This
    /// number includes the securityRejectedRequestsCount.
    pub(crate) fn on_rejected_request(&mut self) {
        self.server_diagnostics_summary.rejected_requests_count += 1;
        unimplemented!()
    }
    */
}