orchestra_toolkit/
session.rs

1/* Copyright 2024-2025 LEDR Technologies Inc.
2* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
3*
4* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
5*
6* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7*
8* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
9*
10* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
11*/
12
13use std::path::PathBuf;
14use thiserror::Error;
15
16use crate::{
17    constants::IANA_PORT, hgtp::*, AvesterraError, ConvertValueError, String255TooLongError,
18};
19
20/// Configuration for the session
21/// You can use the default configuration by calling [`SessionConfig::default`],
22///
23/// ```
24/// use orchestra_toolkit::*;
25///
26/// /// see [`Session`]
27/// fn connect() -> anyhow::Result<Session> {
28///     let mut config = SessionConfig::default();
29///     config.address = "prod.ledr.io".to_string();
30///     Session::initialize(config)
31/// }
32///
33/// /// see [`SessionAsync`]
34/// async fn connect_async() -> anyhow::Result<SessionAsync> {
35///     let mut config = SessionConfig::default();
36///     config.address = "prod.ledr.io".to_string();
37///     SessionAsync::initialize(config).await
38/// }
39///
40/// ```
41///
42pub struct SessionConfig {
43    pub address: String,
44    pub port: u16,
45    pub pem_filepath: PathBuf,
46    /// Maximum number of connection channels that can be openend at the same time
47    pub max_channels: u32,
48
49    /// Artificial latency to simulate network delay in milliseconds
50    /// Useful for testing purposes
51    #[cfg(feature = "dangerous_simulated_latency")]
52    pub simulated_latency_ms: u64,
53}
54
55impl Default for SessionConfig {
56    fn default() -> Self {
57        Self {
58            address: "127.0.0.1".to_string(),
59            port: IANA_PORT,
60            pem_filepath: PathBuf::from("/AvesTerra/Certificates/avesterra.pem"),
61            max_channels: 64,
62
63            #[cfg(feature = "dangerous_simulated_latency")]
64            simulated_latency_ms: 500,
65        }
66    }
67}
68
69pub trait SessionTrait: Sized + Clone {
70    fn get_async_session(&self) -> &SessionAsync;
71    fn get_socket_pool(&self) -> &HGTPPool;
72}
73
74mod asynchronous;
75pub use asynchronous::SessionAsync;
76mod synchronous;
77pub use synchronous::Session;
78
79#[derive(Error, Debug)]
80pub enum CallError {
81    /// Error communicating with the server
82    #[error(transparent)]
83    IO(#[from] std::io::Error),
84
85    /// Invalid response from the server
86    #[error(transparent)]
87    InvalidResponse(#[from] InvalidResponse),
88
89    /// Error returned by the server
90    #[error(transparent)]
91    Avesterra(#[from] AvesterraError),
92}
93
94impl From<UnpackError> for CallError {
95    fn from(e: UnpackError) -> Self {
96        Self::InvalidResponse(e.into())
97    }
98}
99
100impl From<ConvertValueError> for CallError {
101    fn from(e: ConvertValueError) -> Self {
102        Self::InvalidResponse(e.into())
103    }
104}
105
106#[derive(Error, Debug)]
107pub enum InvalidResponse {
108    #[error("received invalid HGTP frame: {0}")]
109    HGTPFrame(#[from] UnpackError),
110
111    #[error("received invalid value: {0}")]
112    ValueTag(#[from] ConvertValueError),
113
114    #[error("failed to deserialize value: {0}")]
115    ValueDeser(#[from] serde_json::Error),
116
117    #[error("received invalid name: {0}")]
118    Name(#[source] String255TooLongError),
119
120    #[error("received invalid key: {0}")]
121    Key(#[source] String255TooLongError),
122
123    #[error("received attribute value not in taxonomy: {0}")]
124    Attribute(i64),
125}
126
127mod api;
128pub use api::*;