bc: Implement message subscription channels
This commit is contained in:
1 parent
0b8484b9b6
commit
5500ca4b28
5 files changed
+115
-27
No files matched your search
Generated
+82
-1
@@ -102,6 +102,66 @@ version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f21b581d2f0cb891554812435667bb9610d74feb1a4c6415bf09c28ff0381d"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-channel",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-queue",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cced8691919c02aac3cb0a1bc2e9b73d89e832bf9a06fc579d4e71b68a2da061"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
"maybe-uninit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285"
|
||||
dependencies = [
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
"maybe-uninit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0",
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
"lazy_static",
|
||||
"maybe-uninit",
|
||||
"memoffset",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.7.2"
|
||||
@@ -673,6 +733,12 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "maybe-uninit"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
|
||||
|
||||
[[package]]
|
||||
name = "md5"
|
||||
version = "0.7.0"
|
||||
@@ -685,6 +751,15 @@ version = "2.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "muldiv"
|
||||
version = "0.2.1"
|
||||
@@ -697,7 +772,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"backoff",
|
||||
"cookie-factory",
|
||||
"crossbeam-utils",
|
||||
"crossbeam",
|
||||
"env_logger",
|
||||
"err-derive",
|
||||
"glib",
|
||||
@@ -1020,6 +1095,12 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
backoff = "0.1"
|
||||
cookie-factory = "0.3"
|
||||
crossbeam-utils = "0.7"
|
||||
crossbeam = "0.7"
|
||||
env_logger = "*"
|
||||
err-derive = "0.2"
|
||||
glib = "*"
|
||||
|
||||
@@ -6,7 +6,6 @@ use md5;
|
||||
use self::connection::BcConnection;
|
||||
use std::io::Write;
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::time::Duration;
|
||||
|
||||
use Md5Trunc::*;
|
||||
|
||||
@@ -14,8 +13,6 @@ mod connection;
|
||||
|
||||
pub struct BcCamera {
|
||||
address: SocketAddr,
|
||||
|
||||
bc_context: BcContext,
|
||||
connection: Option<BcConnection>,
|
||||
logged_in: bool,
|
||||
}
|
||||
@@ -70,7 +67,6 @@ impl BcCamera {
|
||||
address,
|
||||
connection: None,
|
||||
logged_in: false,
|
||||
bc_context: BcContext::new(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
use crate::bc;
|
||||
use crate::bc::{model::*};
|
||||
use crate::bc::model::*;
|
||||
use err_derive::Error;
|
||||
use log::*;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::btree_map::Entry;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::mpsc::{channel, Sender, Receiver};
|
||||
use std::net::{TcpStream, SocketAddr};
|
||||
|
||||
/// A shareable connection to a camera. Handles serialization of messages.
|
||||
pub struct BcConnection {
|
||||
connection: TcpStream,
|
||||
context: BcContext,
|
||||
subscribers: Mutex<BTreeMap<u32, Sender<Bc>>>,
|
||||
connection: Arc<TcpStream>,
|
||||
subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>,
|
||||
}
|
||||
|
||||
pub struct BcSubscription<'a> {
|
||||
pub rx: Receiver<Bc>,
|
||||
pub msg_id: u32,
|
||||
conn: &'a BcConnection,
|
||||
msg_id: u32,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -26,21 +26,30 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
pub enum Error {
|
||||
#[error(display="Communication error")]
|
||||
CommunicationError(#[error(source)] std::io::Error),
|
||||
|
||||
#[error(display="Deserialization error")]
|
||||
DeserializationError(#[error(source)] bc::de::Error),
|
||||
|
||||
#[error(display="Serialization error")]
|
||||
SerializationError(#[error(source)] bc::ser::Error),
|
||||
}
|
||||
|
||||
impl BcConnection {
|
||||
pub fn new(addr: SocketAddr) -> Result<BcConnection> {
|
||||
let connection = TcpStream::connect(addr)?;
|
||||
let bc_conn = BcConnection {
|
||||
connection: Arc::new(TcpStream::connect(addr)?),
|
||||
subscribers: Default::default(),
|
||||
};
|
||||
|
||||
Ok(BcConnection {
|
||||
connection,
|
||||
context: BcContext::new(),
|
||||
subscribers: Mutex::new(BTreeMap::new()),
|
||||
})
|
||||
let mut conn = bc_conn.connection.clone();
|
||||
let mut subs = bc_conn.subscribers.clone();
|
||||
|
||||
let rx_poller = std::thread::spawn(move || {
|
||||
let mut context = BcContext::new();
|
||||
while let Ok(_) = BcConnection::poll(&mut context, &mut conn, &mut subs) {}
|
||||
});
|
||||
|
||||
Ok(bc_conn)
|
||||
}
|
||||
|
||||
pub fn subscribe(&self, msg_id: u32) -> Result<BcSubscription> {
|
||||
@@ -52,12 +61,16 @@ impl BcConnection {
|
||||
Ok(BcSubscription { rx, conn: self, msg_id })
|
||||
}
|
||||
|
||||
pub fn poll(&mut self) -> Result<()> {
|
||||
fn poll(
|
||||
context: &mut BcContext,
|
||||
connection: &mut Arc<TcpStream>,
|
||||
subscribers: &mut Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>
|
||||
) -> Result<()> {
|
||||
// TODO if the connection hangs up, hang up on all subscribers and delete our TcpStream
|
||||
let response = Bc::deserialize(&mut self.context, &self.connection)?;
|
||||
let response = Bc::deserialize(context, &**connection)?;
|
||||
let msg_id = response.meta.msg_id;
|
||||
|
||||
let mut locked_subs = self.subscribers.lock().unwrap();
|
||||
let mut locked_subs = subscribers.lock().unwrap();
|
||||
match locked_subs.entry(msg_id) {
|
||||
Entry::Occupied(mut occ) => {
|
||||
if let Err(_) = occ.get_mut().send(response) {
|
||||
@@ -78,7 +91,7 @@ impl BcConnection {
|
||||
impl<'a> BcSubscription<'a> {
|
||||
pub fn send(&self, bc: Bc) -> Result<()> {
|
||||
// TODO Use channel for writing, too
|
||||
bc.serialize(&self.conn.connection)?;
|
||||
bc.serialize(&*self.conn.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+3
-5
@@ -10,7 +10,6 @@ use backoff::{ExponentialBackoff, Operation};
|
||||
use bc_protocol::BcCamera;
|
||||
use config::{Config, CameraConfig};
|
||||
use cmdline::Opt;
|
||||
use crossbeam_utils::thread;
|
||||
use err_derive::Error;
|
||||
use gst::RtspServer;
|
||||
use std::fs;
|
||||
@@ -34,7 +33,7 @@ fn main() -> Result<(), Error> {
|
||||
|
||||
let rtsp = &RtspServer::new();
|
||||
|
||||
thread::scope(|s| {
|
||||
crossbeam::scope(|s| {
|
||||
for camera in config.cameras {
|
||||
s.spawn(move |_| {
|
||||
// TODO handle these errors
|
||||
@@ -70,13 +69,12 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(
|
||||
let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?;
|
||||
|
||||
println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr);
|
||||
|
||||
camera.connect()?;
|
||||
|
||||
camera.login(&camera_config.username, camera_config.password.as_deref())?;
|
||||
|
||||
println!("{}: Connected to camera, starting video stream", camera_config.name);
|
||||
|
||||
camera.start_video(output)?;
|
||||
|
||||
Ok(())
|
||||
unreachable!()
|
||||
}
|
||||
Reference in new issue
Block a user