Merge branch 'new-event-system' into 'master'

Adapt new event system for component

See merge request !8
This commit is contained in:
lumi 2017-05-27 13:21:05 +00:00
commit cdf665454f
3 changed files with 54 additions and 54 deletions

View file

@ -2,7 +2,6 @@ extern crate xmpp;
use xmpp::jid::Jid; use xmpp::jid::Jid;
use xmpp::component::ComponentBuilder; use xmpp::component::ComponentBuilder;
use xmpp::plugins::messaging::{MessagingPlugin, MessageEvent};
use std::env; use std::env;
@ -17,11 +16,5 @@ fn main() {
.port(port) .port(port)
.connect() .connect()
.unwrap(); .unwrap();
component.register_plugin(MessagingPlugin::new()); component.main().unwrap();
loop {
let event = component.next_event().unwrap();
if let Some(evt) = event.downcast::<MessageEvent>() {
println!("{:?}", evt);
}
}
} }

View file

@ -1,9 +1,10 @@
use xml;
use jid::Jid; use jid::Jid;
use transport::{Transport, PlainTransport}; use transport::{Transport, PlainTransport};
use error::Error; use error::Error;
use ns; use ns;
use plugin::{Plugin, PluginProxyBinding}; use plugin::{Plugin, PluginInit, PluginProxyBinding};
use event::AbstractEvent; use event::{Dispatcher, ReceiveElement};
use connection::{Connection, Component2S}; use connection::{Connection, Component2S};
use sha_1::{Sha1, Digest}; use sha_1::{Sha1, Digest};
@ -12,7 +13,11 @@ use minidom::Element;
use xml::reader::XmlEvent as ReaderEvent; use xml::reader::XmlEvent as ReaderEvent;
use std::fmt::Write; use std::fmt::Write;
use std::sync::mpsc::{Receiver, channel}; use std::sync::{Mutex, Arc};
use std::collections::HashMap;
use std::any::TypeId;
/// A builder for `Component`s. /// A builder for `Component`s.
pub struct ComponentBuilder { pub struct ComponentBuilder {
@ -56,15 +61,14 @@ impl ComponentBuilder {
let host = &self.host.unwrap_or(self.jid.domain.clone()); let host = &self.host.unwrap_or(self.jid.domain.clone());
let mut transport = PlainTransport::connect(host, self.port)?; let mut transport = PlainTransport::connect(host, self.port)?;
Component2S::init(&mut transport, &self.jid.domain, "stream_opening")?; Component2S::init(&mut transport, &self.jid.domain, "stream_opening")?;
let (sender_out, sender_in) = channel(); let dispatcher = Arc::new(Mutex::new(Dispatcher::new()));
let (dispatcher_out, dispatcher_in) = channel(); let transport = Arc::new(Mutex::new(transport));
let mut component = Component { let mut component = Component {
jid: self.jid, jid: self.jid,
transport: transport, transport: transport,
plugins: Vec::new(), plugins: HashMap::new(),
binding: PluginProxyBinding::new(sender_out, dispatcher_out), binding: PluginProxyBinding::new(dispatcher.clone()),
sender_in: sender_in, dispatcher: dispatcher,
dispatcher_in: dispatcher_in,
}; };
component.connect(self.secret)?; component.connect(self.secret)?;
Ok(component) Ok(component)
@ -74,11 +78,10 @@ impl ComponentBuilder {
/// An XMPP component. /// An XMPP component.
pub struct Component { pub struct Component {
jid: Jid, jid: Jid,
transport: PlainTransport, transport: Arc<Mutex<PlainTransport>>,
plugins: Vec<Box<Plugin>>, plugins: HashMap<TypeId, Arc<Box<Plugin>>>,
binding: PluginProxyBinding, binding: PluginProxyBinding,
sender_in: Receiver<Element>, dispatcher: Arc<Mutex<Dispatcher>>,
dispatcher_in: Receiver<AbstractEvent>,
} }
impl Component { impl Component {
@ -88,53 +91,57 @@ impl Component {
} }
/// Registers a plugin. /// Registers a plugin.
pub fn register_plugin<P: Plugin + 'static>(&mut self, mut plugin: P) { pub fn register_plugin<P: Plugin + PluginInit + 'static>(&mut self, mut plugin: P) {
plugin.bind(self.binding.clone()); let binding = self.binding.clone();
self.plugins.push(Box::new(plugin)); plugin.bind(binding);
let p = Arc::new(Box::new(plugin) as Box<Plugin>);
{
let mut disp = self.dispatcher.lock().unwrap();
P::init(&mut disp, p.clone());
}
if self.plugins.insert(TypeId::of::<P>(), p).is_some() {
panic!("registering a plugin that's already registered");
}
} }
/// Returns the plugin given by the type parameter, if it exists, else panics. /// Returns the plugin given by the type parameter, if it exists, else panics.
pub fn plugin<P: Plugin>(&self) -> &P { pub fn plugin<P: Plugin>(&self) -> &P {
for plugin in &self.plugins { self.plugins.get(&TypeId::of::<P>())
let any = plugin.as_any(); .expect("the requested plugin was not registered")
if let Some(ret) = any.downcast_ref::<P>() { .as_any()
return ret; .downcast_ref::<P>()
} .expect("plugin downcast failure (should not happen!!)")
}
panic!("plugin does not exist!");
} }
/// Returns the next event and flush the send queue. /// Returns the next event and flush the send queue.
pub fn next_event(&mut self) -> Result<AbstractEvent, Error> { pub fn main(&mut self) -> Result<(), Error> {
self.flush_send_queue()?; self.dispatcher.lock().unwrap().flush_all();
loop { loop {
if let Ok(evt) = self.dispatcher_in.try_recv() { let elem = self.read_element()?;
return Ok(evt); {
let mut disp = self.dispatcher.lock().unwrap();
disp.dispatch(ReceiveElement(elem));
disp.flush_all();
} }
let elem = self.transport.read_element()?;
for plugin in self.plugins.iter_mut() {
plugin.handle(&elem);
// TODO: handle plugin return
}
self.flush_send_queue()?;
} }
} }
/// Flushes the send queue, sending all queued up stanzas. fn read_element(&self) -> Result<Element, Error> {
pub fn flush_send_queue(&mut self) -> Result<(), Error> { // TODO: not sure how great of an self.transport.lock().unwrap().read_element()
// idea it is to flush in this
// manner…
while let Ok(elem) = self.sender_in.try_recv() {
self.transport.write_element(&elem)?;
} }
Ok(())
fn write_element(&self, elem: &Element) -> Result<(), Error> {
self.transport.lock().unwrap().write_element(elem)
}
fn read_event(&self) -> Result<xml::reader::XmlEvent, Error> {
self.transport.lock().unwrap().read_event()
} }
fn connect(&mut self, secret: String) -> Result<(), Error> { fn connect(&mut self, secret: String) -> Result<(), Error> {
// TODO: this is very ugly
let mut sid = String::new(); let mut sid = String::new();
loop { loop {
let e = self.transport.read_event()?; let e = self.read_event()?;
match e { match e {
ReaderEvent::StartElement { attributes, .. } => { ReaderEvent::StartElement { attributes, .. } => {
for attribute in attributes { for attribute in attributes {
@ -158,9 +165,9 @@ impl Component {
.ns(ns::COMPONENT_ACCEPT) .ns(ns::COMPONENT_ACCEPT)
.build(); .build();
elem.append_text_node(handshake); elem.append_text_node(handshake);
self.transport.write_element(&elem)?; self.write_element(&elem)?;
loop { loop {
let n = self.transport.read_element()?; let n = self.read_element()?;
if n.is("handshake", ns::COMPONENT_ACCEPT) { if n.is("handshake", ns::COMPONENT_ACCEPT) {
return Ok(()); return Ok(());
} }

View file

@ -14,7 +14,7 @@ pub mod ns;
pub mod transport; pub mod transport;
pub mod error; pub mod error;
pub mod client; pub mod client;
//pub mod component; pub mod component;
pub mod plugin; pub mod plugin;
#[macro_use] pub mod plugin_macro; #[macro_use] pub mod plugin_macro;
pub mod event; pub mod event;