wip dispatcher

This commit is contained in:
Brandon Thomas committed 2021-04-28 23:23:05 -04:00
1 parent ef937d8097
commit 982a4ea0f6
5 files changed
+66 -14

No files matched your search

Generated
+1
View File
@@ -1158,6 +1158,7 @@ dependencies = [
"anyhow",
"easyenv",
"google_maps",
"lazy_static",
"log",
"prost",
"prost-build",
+1
View File
@@ -14,6 +14,7 @@ path = "src/main.rs"
[dependencies]
anyhow = "1.0.38"
easyenv = "0.0.4"
lazy_static = "1.4.0"
log = "0.4.14"
prost = "0.7.0"
regex = "1.4.6"
+39
View File
@@ -0,0 +1,39 @@
use crate::protos::protos;
use lazy_static::lazy_static;
use log::{info, warn};
use regex::Regex;
pub struct Dispatcher {
}
impl Dispatcher {
pub fn new() -> Self {
Self {}
}
pub fn handle_message(&self, message: protos::TwitchMessage) {
lazy_static! {
static ref COMMAND_REGEX : Regex = Regex::new(r"^\s*(\w+)\s+(.*)$").expect("Regex should work");
}
info!("Handling Proto: {:?}", message);
//for cap in re.captures_iter(text) {
// println!("Month: {} Day: {} Year: {}", &cap[2], &cap[3], &cap[1]);
//}
let message_ref = match message.message_contents {
None => return,
Some(ref m) => m,
};
let captures = COMMAND_REGEX.captures(&message_ref).unwrap();
info!("Captures: {:?}", captures);
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_types() {
}
}
+18 -12
View File
@@ -1,25 +1,25 @@
#[macro_use]
extern crate serde_derive;
use anyhow::{Context, Error};
use anyhow::anyhow;
use redis::aio::Connection;
use redis::{AsyncCommands, RedisResult};
pub type AnyhowResult<T> = anyhow::Result<T>;
use std::thread;
use std::time::Duration;
mod dispatcher;
mod protos;
mod redis_client;
mod redis_subscriber;
mod secrets;
use anyhow::anyhow;
use anyhow::{Context, Error};
use log::{warn, info};
use redis::aio::Connection;
use redis::{AsyncCommands, RedisResult};
use std::thread;
use std::time::Duration;
use crate::secrets::Secrets;
use crate::redis_client::RedisClient;
use crate::redis_subscriber::RedisSubscribeClient;
use crate::dispatcher::Dispatcher;
pub type AnyhowResult<T> = anyhow::Result<T>;
const ENV_SUBSCRIBE_TOPIC : &'static str = "SUBSCRIBE_TOPIC";
const ENV_SUBSCRIBE_TOPIC_DEFAULT : &'static str = "twitch";
@@ -33,7 +33,13 @@ async fn main() -> anyhow::Result<()> {
let secrets = Secrets::from_file("secrets.toml")?;
let mut redis_client = RedisSubscribeClient::new(&secrets.redis);
let dispatcher = Dispatcher::new();
let mut redis_client = RedisSubscribeClient::new(
&secrets.redis,
dispatcher
);
redis_client.connect().await?;
loop {
+7 -2
View File
@@ -1,5 +1,6 @@
use anyhow::anyhow;
use crate::AnyhowResult;
use crate::dispatcher::Dispatcher;
use crate::protos::protos;
use crate::secrets::RedisSecrets;
use log::{info, warn};
@@ -18,14 +19,18 @@ pub struct RedisSubscribeClient {
connection: Option<PubSub>,
connection_failure_count: u32,
/// Event dispatcher that handles routing and passing to handlers.
dispatcher: Dispatcher,
}
impl RedisSubscribeClient {
pub fn new(secrets: &RedisSecrets) -> Self {
pub fn new(secrets: &RedisSecrets, dispatcher: Dispatcher) -> Self {
Self {
secrets: secrets.clone(),
connection: None,
connection_failure_count: 0,
dispatcher,
}
}
@@ -85,7 +90,7 @@ impl RedisSubscribeClient {
},
};
info!("Proto: {:?}", message_proto);
self.dispatcher.handle_message(message_proto);
}
thread::sleep(Duration::from_millis(250));