add a get_own_jid method to plugins

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-29 03:14:26 +01:00
parent f10080cba5
commit b09eb078f4
3 changed files with 16 additions and 5 deletions

View file

@ -81,9 +81,9 @@ impl ClientBuilder {
let transport = Arc::new(Mutex::new(transport));
let plugin_container = Arc::new(PluginContainer::new());
let mut client = Client {
jid: self.jid,
jid: self.jid.clone(),
transport: transport.clone(),
binding: PluginProxyBinding::new(dispatcher.clone(), plugin_container.clone()),
binding: PluginProxyBinding::new(dispatcher.clone(), plugin_container.clone(), self.jid),
plugin_container: plugin_container,
dispatcher: dispatcher,
};

View file

@ -61,9 +61,9 @@ impl ComponentBuilder {
let transport = Arc::new(Mutex::new(transport));
let plugin_container = Arc::new(PluginContainer::new());
let mut component = Component {
jid: self.jid,
jid: self.jid.clone(),
transport: transport.clone(),
binding: PluginProxyBinding::new(dispatcher.clone(), plugin_container.clone()),
binding: PluginProxyBinding::new(dispatcher.clone(), plugin_container.clone(), self.jid),
plugin_container: plugin_container,
dispatcher: dispatcher,
};

View file

@ -18,6 +18,8 @@ use std::mem;
use minidom::Element;
use jid::Jid;
pub struct PluginContainer {
plugins: RwLock<HashMap<TypeId, Arc<Plugin>>>,
}
@ -70,13 +72,15 @@ impl<P: Plugin> AsRef<P> for PluginRef<P> {
pub struct PluginProxyBinding {
dispatcher: Arc<Dispatcher>,
plugin_container: Arc<PluginContainer>,
jid: Jid,
}
impl PluginProxyBinding {
pub fn new(dispatcher: Arc<Dispatcher>, plugin_container: Arc<PluginContainer>) -> PluginProxyBinding {
pub fn new(dispatcher: Arc<Dispatcher>, plugin_container: Arc<PluginContainer>, jid: Jid) -> PluginProxyBinding {
PluginProxyBinding {
dispatcher: dispatcher,
plugin_container: plugin_container,
jid: jid,
}
}
}
@ -141,6 +145,13 @@ impl PluginProxy {
pub fn send(&self, elem: Element) {
self.dispatch(SendElement(elem));
}
/// Get our own JID.
pub fn get_own_jid(&self) -> Jid {
self.with_binding(|binding| {
binding.jid.clone()
})
}
}
/// A trait whch all plugins should implement.