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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------

use {
    crate::{
        database_call,
        rdfox_api::{
            CServerConnection,
            CServerConnection_newServerConnection,
            CServer_createFirstLocalServerRole,
            CServer_getNumberOfLocalServerRoles,
            CServer_startLocalServer,
            CServer_stopLocalServer,
        },
        server_connection::ServerConnection,
        Parameters,
        RoleCreds,
    },
    rdf_store_rs::{
        consts::LOG_TARGET_DATABASE,
        RDFStoreError::{self, CouldNotConnectToServer},
    },
    std::{
        ffi::CString,
        ptr,
        sync::{
            atomic::{AtomicBool, Ordering},
            Arc,
        },
    },
};

#[derive(Debug)]
pub struct Server {
    default_role_creds: RoleCreds,
    running:            AtomicBool,
}

impl Drop for Server {
    fn drop(&mut self) { self.stop(); }
}

impl std::fmt::Display for Server {
    // noinspection RsUnreachableCode
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "server {self:p}",)
    }
}

impl Server {
    pub fn is_running(&self) -> bool { self.running.load(Ordering::Relaxed) }

    pub fn start(role_creds: RoleCreds) -> Result<Arc<Self>, RDFStoreError> {
        Self::start_with_parameters(role_creds, None)
    }

    pub fn start_with_parameters(
        role_creds: RoleCreds,
        params: Option<Parameters>,
    ) -> Result<Arc<Self>, RDFStoreError> {
        if let Some(params) = params {
            database_call!(
                "Starting a local RDFFox server",
                CServer_startLocalServer(params.inner.cast_const())
            )?;
        } else {
            let params = Parameters::empty()?;
            database_call!(
                "Starting a local RDFFox server with default parameters",
                CServer_startLocalServer(params.inner.cast_const())
            )?;
        };
        let server = Server {
            default_role_creds: role_creds,
            running:            AtomicBool::new(true),
        };

        if server.get_number_of_local_server_roles()? == 0 {
            server.create_role(&server.default_role_creds)?;
        }

        tracing::debug!(
            target: LOG_TARGET_DATABASE,
            "Local RDFox server has been started"
        );
        Ok(Arc::new(server))
    }

    pub fn create_role(&self, role_creds: &RoleCreds) -> Result<(), RDFStoreError> {
        let c_role_name = CString::new(role_creds.role_name.as_str()).unwrap();
        let c_password = CString::new(role_creds.password.as_str()).unwrap();
        let msg = format!(
            "Creating server role named [{}]",
            role_creds.role_name
        );
        database_call!(
            msg.as_str(),
            CServer_createFirstLocalServerRole(c_role_name.as_ptr(), c_password.as_ptr())
        )
    }

    pub fn get_number_of_local_server_roles(&self) -> Result<u16, RDFStoreError> {
        let mut number_of_roles = 0_usize;
        database_call!(
            "Getting the number of local server roles",
            CServer_getNumberOfLocalServerRoles(&mut number_of_roles)
        )?;
        Ok(number_of_roles as u16)
    }

    pub fn connection_with_default_role(
        self: &Arc<Self>,
    ) -> Result<Arc<ServerConnection>, RDFStoreError> {
        let role_creds = &self.default_role_creds;
        self.connection(role_creds.clone())
    }

    pub fn connection(
        self: &Arc<Self>,
        role_creds: RoleCreds,
    ) -> Result<Arc<ServerConnection>, RDFStoreError> {
        let c_role_name = CString::new(role_creds.role_name.as_str()).unwrap();
        let c_password = CString::new(role_creds.password.as_str()).unwrap();
        let mut server_connection_ptr: *mut CServerConnection = ptr::null_mut();
        database_call!(
            "Creating a server connection",
            CServerConnection_newServerConnection(
                c_role_name.as_ptr(),
                c_password.as_ptr(),
                &mut server_connection_ptr,
            )
        )?;
        if server_connection_ptr.is_null() {
            tracing::error!(
                target: LOG_TARGET_DATABASE,
                "Could not establish connection to {self}"
            );
            return Err(CouldNotConnectToServer)
        }
        Ok(Arc::new(ServerConnection::new(
            role_creds,
            self.clone(),
            server_connection_ptr,
        )))
    }

    pub fn stop(&mut self) {
        *self.running.get_mut() = false;
        tracing::trace!(
            target: LOG_TARGET_DATABASE,
            server = format!("{self:p}"),
            "Stopping local RDFox server"
        );
        unsafe {
            CServer_stopLocalServer();
        }
        tracing::trace!(
            target: LOG_TARGET_DATABASE,
            server = format!("{self:p}"),
            "Stopped local RDFox server"
        );
    }
}