Skip to content

Commit

Permalink
Fix client no-framework compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyla Hellyer committed Sep 27, 2017
1 parent 71f709d commit 1d4ecb2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 32 deletions.
77 changes: 51 additions & 26 deletions src/client/bridge/gateway/shard_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use typemap::ShareMap;
use framework::Framework;

pub struct ShardManager {
#[cfg(feature = "framework")]
runners: Arc<ParkingLotMutex<HashMap<ShardId, ShardRunnerInfo>>>,
/// The index of the first shard to initialize, 0-indexed.
shard_index: u64,
Expand Down Expand Up @@ -47,31 +46,57 @@ impl ShardManager {

let runners = Arc::new(ParkingLotMutex::new(HashMap::new()));

let mut shard_queuer = feature_framework! {{
ShardQueuer {
data: data.clone(),
event_handler: event_handler.clone(),
framework: framework.clone(),
last_start: None,
manager_tx: thread_tx.clone(),
runners: runners.clone(),
rx: shard_queue_rx,
token: token.clone(),
ws_url: ws_url.clone(),
}
} else {
ShardQueuer {
data: data.clone(),
event_handler: event_handler.clone(),
last_start: None,
manager_tx: thread_tx.clone(),
runners: runners.clone(),
rx: shard_queue_rx,
rx: shard_queue_rx,
token: token.clone(),
ws_url: ws_url.clone(),
}
}};
let mut shard_queuer = ShardQueuer {
data: data.clone(),
event_handler: event_handler.clone(),
framework: framework.clone(),
last_start: None,
manager_tx: thread_tx.clone(),
runners: runners.clone(),
rx: shard_queue_rx,
token: token.clone(),
ws_url: ws_url.clone(),
};

thread::spawn(move || {
shard_queuer.run();
});

Self {
shard_queuer: shard_queue_tx,
thread_rx: thread_rx,
runners,
shard_index,
shard_init,
shard_total,
}
}

#[cfg(not(feature = "framework"))]
pub fn new<H>(
shard_index: u64,
shard_init: u64,
shard_total: u64,
ws_url: Arc<Mutex<String>>,
token: Arc<Mutex<String>>,
data: Arc<ParkingLotMutex<ShareMap>>,
event_handler: Arc<H>,
) -> Self where H: EventHandler + Send + Sync + 'static {
let (thread_tx, thread_rx) = mpsc::channel();
let (shard_queue_tx, shard_queue_rx) = mpsc::channel();

let runners = Arc::new(ParkingLotMutex::new(HashMap::new()));

let mut shard_queuer = ShardQueuer {
data: data.clone(),
event_handler: event_handler.clone(),
last_start: None,
manager_tx: thread_tx.clone(),
runners: runners.clone(),
rx: shard_queue_rx,
token: token.clone(),
ws_url: ws_url.clone(),
};

thread::spawn(move || {
shard_queuer.run();
Expand Down
4 changes: 3 additions & 1 deletion src/client/bridge/gateway/shard_queuer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use framework::Framework;
use gateway::Shard;
use internal::prelude::*;
use parking_lot::Mutex as ParkingLotMutex;
Expand All @@ -17,6 +16,9 @@ use super::{
};
use typemap::ShareMap;

#[cfg(feature = "framework")]
use framework::Framework;

/// The shard queuer is a simple loop that runs indefinitely to manage the
/// startup of shards.
///
Expand Down
29 changes: 24 additions & 5 deletions src/client/bridge/gateway/shard_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ use internal::ws_impl::ReceiverExt;
use model::event::{Event, GatewayEvent};
use parking_lot::Mutex as ParkingLotMutex;
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use super::super::super::{EventHandler, dispatch};
use super::{LockedShard, ShardId, ShardManagerMessage};
use typemap::ShareMap;
use websocket::WebSocketError;

#[cfg(feature = "framework")]
use framework::Framework;
#[cfg(feature = "framework")]
use std::sync::Mutex;

pub struct ShardRunner<H: EventHandler + 'static> {
data: Arc<ParkingLotMutex<ShareMap>>,
Expand All @@ -24,6 +26,7 @@ pub struct ShardRunner<H: EventHandler + 'static> {
}

impl<H: EventHandler + 'static> ShardRunner<H> {
#[cfg(feature = "framework")]
pub fn new(shard: LockedShard,
manager_tx: Sender<ShardManagerMessage>,
framework: Arc<Mutex<Option<Box<Framework + Send>>>>,
Expand All @@ -42,6 +45,23 @@ impl<H: EventHandler + 'static> ShardRunner<H> {
}
}

#[cfg(not(feature = "framework"))]
pub fn new(shard: LockedShard,
manager_tx: Sender<ShardManagerMessage>,
data: Arc<ParkingLotMutex<ShareMap>>,
event_handler: Arc<H>) -> Self {
let (tx, rx) = mpsc::channel();

Self {
runner_rx: rx,
runner_tx: tx,
data,
event_handler,
manager_tx,
shard,
}
}

pub fn run(&mut self) -> Result<()> {
loop {
{
Expand Down Expand Up @@ -86,10 +106,9 @@ impl<H: EventHandler + 'static> ShardRunner<H> {
&self.event_handler);
} else {
dispatch(event,
&info.shard,
&info.data,
&info.event_handler,
&handle);
&self.shard,
&self.data,
&self.event_handler);
}}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> {
self.token.clone(),
self.data.clone(),
self.event_handler.clone(),
#[cfg(feature = "framework")]
self.framework.clone(),
);

Expand Down

0 comments on commit 1d4ecb2

Please sign in to comment.