diff --git a/src/plugin.rs b/src/plugin.rs index b41ab2cf..6053b6cb 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -8,6 +8,8 @@ use std::collections::HashMap; use std::sync::{RwLock, Arc}; +use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; + use std::marker::PhantomData; use std::ops::Deref; @@ -73,6 +75,7 @@ pub struct PluginProxyBinding { dispatcher: Arc, plugin_container: Arc, jid: Jid, + next_id: Arc, } impl PluginProxyBinding { @@ -81,6 +84,7 @@ impl PluginProxyBinding { dispatcher: dispatcher, plugin_container: plugin_container, jid: jid, + next_id: Arc::new(ATOMIC_USIZE_INIT), } } } @@ -152,6 +156,13 @@ impl PluginProxy { binding.jid.clone() }) } + + /// Get a new id. + pub fn gen_id(&self) -> String { + self.with_binding(|binding| { + format!("{}", binding.next_id.fetch_add(1, Ordering::SeqCst)) + }) + } } /// A trait whch all plugins should implement. diff --git a/src/plugins/messaging.rs b/src/plugins/messaging.rs index aa8001f7..dd206516 100644 --- a/src/plugins/messaging.rs +++ b/src/plugins/messaging.rs @@ -67,8 +67,7 @@ impl MessagingPlugin { from: None, to: Some(to.clone()), type_: MessageType::Chat, - // TODO: always use an id. - id: None, + id: Some(self.proxy.gen_id()), bodies: { let mut bodies = BTreeMap::new(); bodies.insert(String::new(), String::from(body)); diff --git a/src/plugins/ping.rs b/src/plugins/ping.rs index dbf21fda..aeca7a21 100644 --- a/src/plugins/ping.rs +++ b/src/plugins/ping.rs @@ -53,8 +53,7 @@ impl PingPlugin { self.proxy.send(Iq { from: None, to: Some(to), - // TODO: use a generic way to generate ids. - id: Some(String::from("id")), + id: Some(self.proxy.gen_id()), payload: IqType::Get(IqPayload::Ping(Ping).into()), }.into()); Ok(()) diff --git a/src/plugins/presence.rs b/src/plugins/presence.rs index 344dd1f6..42ee94c1 100644 --- a/src/plugins/presence.rs +++ b/src/plugins/presence.rs @@ -20,7 +20,7 @@ impl PresencePlugin { let presence = Presence { from: None, to: None, - id: None, + id: Some(self.proxy.gen_id()), type_: type_, show: show, priority: 0i8,