Send different kinds of events
This commit is contained in:
parent
0c7e8c1dff
commit
3f14151feb
2 changed files with 21 additions and 16 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
use euphoxide::api::packet::ParsedPacket;
|
use euphoxide::api::packet::ParsedPacket;
|
||||||
use euphoxide::api::{Data, Nick, Send};
|
use euphoxide::api::{Data, Nick, Send};
|
||||||
use euphoxide::bot::instance::{ServerConfig, Snapshot};
|
use euphoxide::bot::instance::{Event, ServerConfig, Snapshot};
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
|
@ -161,8 +161,10 @@ async fn main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
while let Some(event) = rx.recv().await {
|
while let Some(event) = rx.recv().await {
|
||||||
if on_packet(event.packet, event.snapshot).await.is_err() {
|
if let Event::Packet(_config, packet, snapshot) = event {
|
||||||
|
if on_packet(packet, snapshot).await.is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -157,11 +157,16 @@ pub struct Snapshot {
|
||||||
pub state: State,
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Most of the time, the largest variant (`Packet`) is sent. The size of this
|
||||||
|
// enum is not critical anyways since it's not constructed that often.
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Event {
|
pub enum Event {
|
||||||
pub config: InstanceConfig,
|
Connecting(InstanceConfig),
|
||||||
pub packet: ParsedPacket,
|
Connected(InstanceConfig, ConnTx),
|
||||||
pub snapshot: Snapshot,
|
Packet(InstanceConfig, ParsedPacket, Snapshot),
|
||||||
|
Disconnected(InstanceConfig),
|
||||||
|
Stopped(InstanceConfig),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A single instance of a bot in a single room.
|
/// A single instance of a bot in a single room.
|
||||||
|
|
@ -176,7 +181,6 @@ pub struct Event {
|
||||||
/// one instance per room.
|
/// one instance per room.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
// TODO Share Arc<InstanceConfig> instead of cloning InstanceConfig everywhere
|
|
||||||
config: InstanceConfig,
|
config: InstanceConfig,
|
||||||
request_tx: mpsc::UnboundedSender<oneshot::Sender<ConnTx>>,
|
request_tx: mpsc::UnboundedSender<oneshot::Sender<ConnTx>>,
|
||||||
}
|
}
|
||||||
|
|
@ -226,7 +230,10 @@ impl Instance {
|
||||||
{
|
{
|
||||||
// TODO Only delay reconnecting if previous reconnect attempt failed
|
// TODO Only delay reconnecting if previous reconnect attempt failed
|
||||||
loop {
|
loop {
|
||||||
|
on_event(Event::Connecting(config.clone()));
|
||||||
Self::run_once::<F>(&config, &on_event, &mut request_rx).await;
|
Self::run_once::<F>(&config, &on_event, &mut request_rx).await;
|
||||||
|
on_event(Event::Disconnected(config.clone()));
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"{}: Waiting {} seconds before reconnecting",
|
"{}: Waiting {} seconds before reconnecting",
|
||||||
config.name,
|
config.name,
|
||||||
|
|
@ -279,6 +286,7 @@ impl Instance {
|
||||||
.await
|
.await
|
||||||
.ok()?;
|
.ok()?;
|
||||||
Self::set_cookies(config, cookies);
|
Self::set_cookies(config, cookies);
|
||||||
|
on_event(Event::Connected(config.clone(), conn.tx().clone()));
|
||||||
|
|
||||||
let conn_tx = conn.tx().clone();
|
let conn_tx = conn.tx().clone();
|
||||||
let result = select! {
|
let result = select! {
|
||||||
|
|
@ -306,13 +314,8 @@ impl Instance {
|
||||||
conn_tx: conn.tx().clone(),
|
conn_tx: conn.tx().clone(),
|
||||||
state: conn.state().clone(),
|
state: conn.state().clone(),
|
||||||
};
|
};
|
||||||
let event = Event {
|
|
||||||
config: config.clone(),
|
|
||||||
packet,
|
|
||||||
snapshot,
|
|
||||||
};
|
|
||||||
|
|
||||||
match &event.packet.content {
|
match &packet.content {
|
||||||
Ok(Data::SnapshotEvent(_)) => {
|
Ok(Data::SnapshotEvent(_)) => {
|
||||||
if let Some(username) = &config.username {
|
if let Some(username) = &config.username {
|
||||||
debug!("{}: Setting nick to username {}", config.name, username);
|
debug!("{}: Setting nick to username {}", config.name, username);
|
||||||
|
|
@ -336,7 +339,7 @@ impl Instance {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
on_event(event);
|
on_event(Event::Packet(config.clone(), packet, snapshot));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue