Skip to content

add a simple event listener feature #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion examples/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ use freeswitch_esl::{Esl, EslError};
async fn main() -> Result<(), EslError> {
let addr = "localhost:8021"; // Freeswitch host
let password = "ClueCon";
let inbound = Esl::inbound(addr, password).await?;
let (tx, mut rx) = tokio::sync::mpsc::channel(100);
let inbound = Esl::inbound(addr, password, Some(tx)).await?;

let reloadxml = inbound.api("reloadxml").await?;
println!("reloadxml response : {:?}", reloadxml);

let reloadxml = inbound.bgapi("reloadxml").await?;
println!("reloadxml response : {:?}", reloadxml);

let subscribe = inbound.subscribe(vec!["all"]).await?;
println!("subscribe all response : {:?}", subscribe);

loop {
match rx.recv().await {
Some(ev) => println!("received event: {:#?}", ev),
_ => break,
}
}
Ok(())
}
11 changes: 10 additions & 1 deletion src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tokio::io::WriteHalf;
use tokio::net::{TcpStream, ToSocketAddrs};
use tokio::sync::{
oneshot::{channel, Sender},
mpsc,
Mutex,
};
use tokio_stream::StreamExt;
Expand Down Expand Up @@ -60,6 +61,7 @@ impl EslConnection {
stream: TcpStream,
password: impl ToString,
connection_type: EslConnectionType,
listener: Option<mpsc::Sender<HashMap<String, Value>>>,
) -> Result<Self, EslError> {
// let sender = Arc::new(sender);
let commands = Arc::new(Mutex::new(VecDeque::new()));
Expand Down Expand Up @@ -128,10 +130,16 @@ impl EslConnection {
}
trace!("continued");
trace!("got channel execute complete");
continue;
}
}
}
}
if let Some(ref listener) = listener {
if let Err(e) = listener.send(event_body).await {
trace!("got error forwarding event event to listener: {}", e);
}
}
continue;
}
_ => {
Expand Down Expand Up @@ -186,9 +194,10 @@ impl EslConnection {
socket: impl ToSocketAddrs,
password: impl ToString,
connection_type: EslConnectionType,
listener: Option<mpsc::Sender<HashMap<String, Value>>>,
) -> Result<Self, EslError> {
let stream = TcpStream::connect(socket).await?;
Self::with_tcpstream(stream, password, connection_type).await
Self::with_tcpstream(stream, password, connection_type, listener).await
}
pub(crate) async fn auth(&self) -> Result<String, EslError> {
let auth_response = self
Expand Down
5 changes: 4 additions & 1 deletion src/esl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use tokio::net::ToSocketAddrs;

use crate::{connection::EslConnection, outbound::Outbound, EslError};
use std::collections::HashMap;
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum EslConnectionType {
Inbound,
Expand All @@ -13,8 +15,9 @@ impl Esl {
pub async fn inbound(
addr: impl ToSocketAddrs,
password: impl ToString,
listener: Option<tokio::sync::mpsc::Sender<HashMap<String, Value>>>,
) -> Result<EslConnection, EslError> {
EslConnection::new(addr, password, EslConnectionType::Inbound).await
EslConnection::new(addr, password, EslConnectionType::Inbound, listener).await
}

/// Creates new server for outbound connection
Expand Down
2 changes: 1 addition & 1 deletion src/outbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Outbound {
pub async fn accept(&self) -> Result<(EslConnection, SocketAddr), EslError> {
let (stream, addr) = self.listener.accept().await?;
let connection =
EslConnection::with_tcpstream(stream, "None", EslConnectionType::Outbound).await?;
EslConnection::with_tcpstream(stream, "None", EslConnectionType::Outbound, None).await?;
Ok((connection, addr))
}
}