eruption_sdk/
connection.rs

1/*
2    This file is part of Eruption.
3
4    Eruption is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    Eruption is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with Eruption.  If not, see <http://www.gnu.org/licenses/>.
16
17    Copyright (c) 2019-2022, The Eruption Development Team
18*/
19
20use crate::canvas::Canvas;
21use crate::hardware::HotplugInfo;
22use crate::transport::{LocalTransport, ServerStatus, Transport};
23use crate::Result;
24use parking_lot::Mutex;
25use std::sync::Arc;
26
27#[derive(Clone)]
28pub struct Connection {
29    con: Arc<Mutex<dyn Transport>>,
30}
31
32impl Connection {
33    pub fn new(connection_type: ConnectionType) -> Result<Self> {
34        Ok(Self {
35            con: Arc::new(Mutex::new(make_transport(&connection_type)?)),
36        })
37    }
38
39    pub fn connect(&self) -> Result<()> {
40        self.con.lock().connect()
41    }
42
43    pub fn disconnect(&self) -> Result<()> {
44        self.con.lock().disconnect()
45    }
46
47    pub fn submit_canvas(&self, canvas: &Canvas) -> Result<()> {
48        self.con.lock().submit_canvas(canvas)
49    }
50
51    pub fn get_server_status(&self) -> Result<ServerStatus> {
52        self.con.lock().get_server_status()
53    }
54
55    pub fn notify_device_hotplug(&self, hotplug_info: &HotplugInfo) -> Result<()> {
56        self.con.lock().notify_device_hotplug(hotplug_info)
57    }
58}
59
60impl Drop for Connection {
61    fn drop(&mut self) {
62        let _ = self.disconnect();
63    }
64}
65
66/// The type of the connection
67#[derive(Debug, Clone)]
68pub enum ConnectionType {
69    /// Unknown connection type
70    Unknown,
71
72    /// Local transport
73    Local,
74
75    /// Type REMOTE is currently not implemented
76    Remote,
77}
78
79fn make_transport(_connection_type: &ConnectionType) -> Result<impl Transport> {
80    LocalTransport::new()
81}