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
50impl Default for SessionConfig {
51    fn default() -> Self {
52        Self {
53            address: "127.0.0.1".to_string(),
54            port: IANA_PORT,
55            pem_filepath: PathBuf::from("/AvesTerra/Certificates/avesterra.pem"),
56            max_channels: 64,
57        }
58    }
59}
60
61pub trait SessionTrait: Sized + Clone {
62    fn get_async_session(&self) -> &SessionAsync;
63    fn get_socket_pool(&self) -> &HGTPPool;
64}
65
66mod asynchronous;
67pub use asynchronous::SessionAsync;
68mod synchronous;
69pub use synchronous::Session;
70
71#[derive(Error, Debug)]
72pub enum CallError {
73    /// Error communicating with the server
74    #[error(transparent)]
75    IO(#[from] std::io::Error),
76
77    /// Invalid response from the server
78    #[error(transparent)]
79    InvalidResponse(#[from] InvalidResponse),
80
81    /// Error returned by the server
82    #[error(transparent)]
83    Avesterra(#[from] AvesterraError),
84}
85
86impl From<UnpackError> for CallError {
87    fn from(e: UnpackError) -> Self {
88        Self::InvalidResponse(e.into())
89    }
90}
91
92impl From<ConvertValueError> for CallError {
93    fn from(e: ConvertValueError) -> Self {
94        Self::InvalidResponse(e.into())
95    }
96}
97
98#[derive(Error, Debug)]
99pub enum InvalidResponse {
100    #[error("received invalid HGTP frame: {0}")]
101    HGTPFrame(#[from] UnpackError),
102
103    #[error("received invalid value: {0}")]
104    ValueTag(#[from] ConvertValueError),
105
106    #[error("failed to deserialize value: {0}")]
107    ValueDeser(#[from] serde_json::Error),
108
109    #[error("received invalid name: {0}")]
110    Name(#[source] String255TooLongError),
111
112    #[error("received invalid key: {0}")]
113    Key(#[source] String255TooLongError),
114
115    #[error("received attribute value not in taxonomy: {0}")]
116    Attribute(i64),
117}
118
119mod api;
120pub use api::*;