Merge branch 'gen_id' into 'master'
Add a gen_id method for plugins to generate ids See merge request !19
This commit is contained in:
commit
4be13c1c01
4 changed files with 14 additions and 5 deletions
|
@ -8,6 +8,8 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
use std::sync::{RwLock, Arc};
|
use std::sync::{RwLock, Arc};
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
|
||||||
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
@ -73,6 +75,7 @@ pub struct PluginProxyBinding {
|
||||||
dispatcher: Arc<Dispatcher>,
|
dispatcher: Arc<Dispatcher>,
|
||||||
plugin_container: Arc<PluginContainer>,
|
plugin_container: Arc<PluginContainer>,
|
||||||
jid: Jid,
|
jid: Jid,
|
||||||
|
next_id: Arc<AtomicUsize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PluginProxyBinding {
|
impl PluginProxyBinding {
|
||||||
|
@ -81,6 +84,7 @@ impl PluginProxyBinding {
|
||||||
dispatcher: dispatcher,
|
dispatcher: dispatcher,
|
||||||
plugin_container: plugin_container,
|
plugin_container: plugin_container,
|
||||||
jid: jid,
|
jid: jid,
|
||||||
|
next_id: Arc::new(ATOMIC_USIZE_INIT),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,6 +156,13 @@ impl PluginProxy {
|
||||||
binding.jid.clone()
|
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.
|
/// A trait whch all plugins should implement.
|
||||||
|
|
|
@ -67,8 +67,7 @@ impl MessagingPlugin {
|
||||||
from: None,
|
from: None,
|
||||||
to: Some(to.clone()),
|
to: Some(to.clone()),
|
||||||
type_: MessageType::Chat,
|
type_: MessageType::Chat,
|
||||||
// TODO: always use an id.
|
id: Some(self.proxy.gen_id()),
|
||||||
id: None,
|
|
||||||
bodies: {
|
bodies: {
|
||||||
let mut bodies = BTreeMap::new();
|
let mut bodies = BTreeMap::new();
|
||||||
bodies.insert(String::new(), String::from(body));
|
bodies.insert(String::new(), String::from(body));
|
||||||
|
|
|
@ -53,8 +53,7 @@ impl PingPlugin {
|
||||||
self.proxy.send(Iq {
|
self.proxy.send(Iq {
|
||||||
from: None,
|
from: None,
|
||||||
to: Some(to),
|
to: Some(to),
|
||||||
// TODO: use a generic way to generate ids.
|
id: Some(self.proxy.gen_id()),
|
||||||
id: Some(String::from("id")),
|
|
||||||
payload: IqType::Get(IqPayload::Ping(Ping).into()),
|
payload: IqType::Get(IqPayload::Ping(Ping).into()),
|
||||||
}.into());
|
}.into());
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl PresencePlugin {
|
||||||
let presence = Presence {
|
let presence = Presence {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: None,
|
id: Some(self.proxy.gen_id()),
|
||||||
type_: type_,
|
type_: type_,
|
||||||
show: show,
|
show: show,
|
||||||
priority: 0i8,
|
priority: 0i8,
|
||||||
|
|
Loading…
Reference in a new issue