6WLPW4L4PN6ANYYW3I6AOCN3SXQP37RQTUOM52IVXL3SRZD7UHAAC
F3IDYEIHR2FA7SCE2MYVQSUQ5SOPGJ5IKBTS2V3HLM52SWGQ322AC
LSQ7OPL7Z5WJDO7ZZS3D4UPMEEWUUF2LNQ3FZUWWFUVZEXA7CIOAC
RPECDMZ4O7MMU32H6Z7EW75GRVEQ6IJYNPWSV54HUPTCX5H7I26QC
BQFOYJRTLAFIBZ3SOERSUB2ZXMXJUAROXOGMGWHJW4CVBRT4R5QAC
AYQYCR7JKWTAZASLQTY5DPVI6A5MN6X4XMPC3GBS5KJRH3EGRPIQC
VODZCAIINDFQAU6RUV5VBRGJSMCWNMYAQ3ZE56SNBYU2WOSHFOMAC
KWR2TS2D7YN4LCGMQZ35V4WBLQDHAVSZJV3SZAIR5HQRSJAWLWCQC
LKAUS67VU5SLG5O5MTSO6N4VVT5OB2X4VNR6HYLIJ3ZOAXRQ4Q2QC
WX2TUKTGV5XK5F5GMEUDU2IHT3LRW2IQRE2BRAH76CTJIVUFMQCAC
LBG7DYYI7WCROF4GRYVXDXIV2465E5KPGQVZTFUPTZQBNZZ3LNYAC
R5ZNUPT57LKKPGEWOAHLSRE4VA47SDZD33OXCFFGVIXJLWXI5Y7AC
7RMAZDLQ6FEEQGUUAIVEM4X2VME7IUUZMEJMMV3H46U3UKO4BODQC
CPONUGXG2EFQI5VTBCP74GUGHWNORQ7FCKIOVXOIKGIYWZE66UEAC
K3I54HXNNCTEA7L6UGWIVBFB5TPZVZZXDBRSR2YZDIKREXQVWRIQC
pub struct Elemeld<H, N> where
H: io::HostInterface,
N: io::NetInterface,
{
host: H,
net: N,
state: State,
use mio;
use std::net;
const HOST_EVENT: mio::Token = mio::Token(0);
const NET_EVENT: mio::Token = mio::Token(1);
pub struct Elemeld<'a> {
config: &'a Config,
impl<H, N> Elemeld<H, N> where
H: io::HostInterface,
N: io::NetInterface,
{
pub fn new(host: H, net: N) -> Self {
impl<'a> Elemeld<'a> {
pub fn new(event_loop: &mut mio::EventLoop<Self>, config: &'a Config) -> Self {
// Setup host interface
let host = X11Interface::open();
event_loop.register(&host,
HOST_EVENT,
mio::EventSet::readable(),
mio::PollOpt::level()).unwrap();
// Setup cluster
State::Connected => match self.cluster.filter_host_event(&self.host) {
Some(event) => {
match event {
io::NetEvent::Focus(focus) => {
self.net.send_to_all(&[event]);
},
event => {
let addr = self.cluster.focused_screen().default_route();
self.net.send_to(&[event], addr);
}
}
State::Connected => match self.cluster.process_host_event(&self.host, event) {
Some(event) => match event {
// Global events
io::NetEvent::Focus(focus) => {
self.net.send_to_all(&[event]);
},
// Focused events
event => {
let addr = self.cluster.focused_screen().default_route();
self.net.send_to(&[event], addr);
},
pub fn net_event(&mut self) {
match self.net.recv_from() {
Some((events, addr)) => for event in events {
match event {
// Initialization events
io::NetEvent::Connect(cluster) => {
self.cluster.merge(cluster);
self.net.send_to(&[io::NetEvent::Cluster(self.cluster.clone())], &addr);
self.state = State::Connected;
},
io::NetEvent::Cluster(cluster) => {
self.cluster.replace(&self.host, cluster);
self.state = State::Connected;
},
pub fn net_event(&mut self, event: io::NetEvent, addr: net::SocketAddr) {
match event {
// Initialization events
io::NetEvent::Connect(cluster) => {
self.cluster.merge(cluster);
self.net.send_to(&[io::NetEvent::Cluster(self.cluster.clone())], &addr);
self.state = State::Connected;
},
io::NetEvent::Cluster(cluster) => {
self.cluster.replace(&self.host, cluster);
self.state = State::Connected;
},
// Global events
io::NetEvent::Focus(focus) => {
self.cluster.refocus(&self.host, focus);
},
// Focued events
event => match self.cluster.process_net_event(event) {
Some(event) => { self.host.send_event(event); },
None => (),
},
}
}
}
// Global events
io::NetEvent::Focus(focus) => {
self.cluster.refocus(&self.host, focus);
},
event => match self.cluster.filter_net_event(event) {
Some(event) => { self.host.send_event(event); },
impl<'a> mio::Handler for Elemeld<'a> {
type Timeout = ();
type Message = ();
fn ready(&mut self,
event_loop: &mut mio::EventLoop<Self>,
token: mio::Token, events: mio::EventSet)
{
match token {
HOST_EVENT => {
if events.is_readable() {
match self.host.recv_event() {
Some(event) => self.host_event(event),
match self.state {
State::Connecting => {
self.net.send_to_all(&[io::NetEvent::Connect(self.cluster.clone())]);
self.state = State::Waiting;
if events.is_writable() {
match self.state {
State::Connecting => {
self.net.send_to_all(&[io::NetEvent::Connect(self.cluster.clone())]);
self.state = State::Waiting;
event_loop.reregister(&self.net,
NET_EVENT,
mio::EventSet::readable(),
mio::PollOpt::level()).unwrap();
},
_ => ()
}
}
socket.join_multicast(&mio::IpAddr::V4(config.multicast_addr)).unwrap();
socket.bind(&SocketAddr::V4(
SocketAddrV4::new(config.server_addr, config.port)
)).unwrap();
socket.join_multicast(&config.multicast_addr).unwrap();
socket.bind(&match config.server_addr {
mio::IpAddr::V4(addr) => net::SocketAddr::V4((net::SocketAddrV4::new(addr, config.port))),
mio::IpAddr::V6(addr) => net::SocketAddr::V6((net::SocketAddrV6::new(addr, config.port, 0, 0))),
}).unwrap();
let multicast_addr = SocketAddr::V4(SocketAddrV4::new(self.config.multicast_addr, self.config.port));
self.send_to(events, &multicast_addr)
let addr = match self.config.multicast_addr {
mio::IpAddr::V4(addr) => net::SocketAddr::V4((net::SocketAddrV4::new(addr, self.config.port))),
mio::IpAddr::V6(addr) => net::SocketAddr::V6((net::SocketAddrV6::new(addr, self.config.port, 0, 0))),
};
self.send_to(events, &addr)
let mut event_loop = mio::EventLoop::new().unwrap();
let mut manager = EventManager::new(&mut event_loop, Config {
server_addr: mio::Ipv4Addr::new(0, 0, 0, 0),
multicast_addr: mio::Ipv4Addr::new(239, 255, 80, 80),
let config = Config {
server_addr: mio::IpAddr::V4(net::Ipv4Addr::new(0, 0, 0, 0)),
multicast_addr: mio::IpAddr::V4(net::Ipv4Addr::new(239, 255, 80, 80)),
});
event_loop.run(&mut manager).unwrap();
}
struct EventManager {
elemeld: Elemeld<X11Interface, IpInterface>,
}
impl EventManager {
fn new(event_loop: &mut mio::EventLoop<Self>, config: Config) -> Self {
// Setup host interface
let host = X11Interface::open();
event_loop.register(&host,
HOST_EVENT,
mio::EventSet::readable(),
mio::PollOpt::level()).unwrap();
// Setup net interface
let net = IpInterface::open(config);
event_loop.register(&net,
NET_EVENT,
mio::EventSet::readable() |
mio::EventSet::writable(),
mio::PollOpt::level()).unwrap();
};
EventManager { elemeld: Elemeld::new(host, net) }
}
let mut event_loop = mio::EventLoop::new().unwrap();
let mut elemeld = Elemeld::new(&mut event_loop, &config);
event_loop.run(&mut elemeld).unwrap();
impl mio::Handler for EventManager {
type Timeout = ();
type Message = ();
fn ready(&mut self,
event_loop: &mut mio::EventLoop<Self>,
token: mio::Token, events: mio::EventSet)
{
match token {
HOST_EVENT => self.elemeld.host_event(),
NET_EVENT => self.elemeld.net_event(),
_ => unreachable!(),
}
}
}