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
use std::{
    ffi::CString,
    ops::{Deref, DerefMut},
    ptr,
};

use num_traits::FromPrimitive;
use solace_rs_sys as ffi;

use crate::{Session, SessionError, SolClientReturnCode};

pub struct CacheSession<'session> {
    // Pointer to session
    // This pointer must never be allowed to leave the struct
    pub(crate) _cache_session_pt: ffi::solClient_opaqueCacheSession_pt,
    pub(crate) session: Session<'session>,
}

unsafe impl Send for CacheSession<'_> {}
unsafe impl Sync for CacheSession<'_> {}

impl<'session> Deref for CacheSession<'session> {
    type Target = Session<'session>;

    fn deref(&self) -> &Self::Target {
        &self.session
    }
}

impl DerefMut for CacheSession<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.session
    }
}

impl<'session> CacheSession<'session> {
    pub(crate) fn new<N>(
        session: Session<'session>,
        cache_name: N,
        max_message: Option<u64>,
        max_age: Option<u64>,
        timeout_ms: Option<u64>,
    ) -> Result<Self, SessionError>
    where
        N: Into<Vec<u8>>,
    {
        let c_cache_name = CString::new(cache_name)?;
        let c_max_message = CString::new(max_message.unwrap_or(1).to_string())?;
        let c_max_age = CString::new(max_age.unwrap_or(0).to_string())?;
        let c_timeout_ms = CString::new(timeout_ms.unwrap_or(10000).to_string())?;

        let cache_session_props = [
            ffi::SOLCLIENT_CACHESESSION_PROP_CACHE_NAME.as_ptr(),
            c_cache_name.as_ptr() as *const u8,
            ffi::SOLCLIENT_CACHESESSION_PROP_DEFAULT_MAX_MSGS.as_ptr(),
            c_max_message.as_ptr() as *const u8,
            ffi::SOLCLIENT_CACHESESSION_PROP_MAX_AGE.as_ptr(),
            c_max_age.as_ptr() as *const u8,
            ffi::SOLCLIENT_CACHESESSION_PROP_REQUESTREPLY_TIMEOUT_MS.as_ptr(),
            c_timeout_ms.as_ptr() as *const u8,
            ptr::null(),
        ]
        .as_mut_ptr() as *mut *const i8;

        let mut cache_session_pt: ffi::solClient_opaqueCacheSession_pt = ptr::null_mut();

        let cache_create_result = unsafe {
            ffi::solClient_session_createCacheSession(
                cache_session_props,
                session._session_pt,
                &mut cache_session_pt,
            )
        };

        if SolClientReturnCode::from_i32(cache_create_result) != Some(SolClientReturnCode::Ok) {
            return Err(SessionError::InitializationFailure);
        }

        Ok(CacheSession {
            session,
            _cache_session_pt: cache_session_pt,
        })
    }

    pub fn blocking_cache_request<T>(&self, topic: T, request_id: u64) -> Result<(), SessionError>
    where
        T: Into<Vec<u8>>,
    {
        let c_topic = CString::new(topic)?;
        let flags = ffi::SOLCLIENT_CACHEREQUEST_FLAGS_LIVEDATA_FLOWTHRU;

        let request_result = unsafe {
            ffi::solClient_cacheSession_sendCacheRequest(
                self._cache_session_pt,
                c_topic.as_ptr(),
                request_id,
                None,
                ptr::null_mut(),
                flags,
                0,
            )
        };

        if SolClientReturnCode::from_i32(request_result) != Some(SolClientReturnCode::Ok) {
            return Err(SessionError::CacheRequestFailure);
        }

        Ok(())
    }
}