|
| 1 | +///! Create a conference. |
| 2 | +///! |
| 3 | +///! 1st Group |
| 4 | +///! Staff (5) - Going to organize the event // OK |
| 5 | +///! |
| 6 | +///! 2nd Group |
| 7 | +///! Enthusiasts (50) - interested in participating to the conference (haven't registered yet) // OK |
| 8 | +///! |
| 9 | +///! 3rd Group |
| 10 | +///! Attendees (empty for now) - Participate |
| 11 | +///! |
| 12 | +///! Enthusiast -> Ask one of the staff members "when is the conference going to happen ?" // OK |
| 13 | +///! Broadcast / Question => Answer 0 or 1 Staff members are going to reply eventually? // OK |
| 14 | +///! |
| 15 | +///! Staff -> Send a Leaflet to all of the enthusiasts, letting them know that they can register. // OK |
| 16 | +///! |
| 17 | +///! "hey conference <awesomeconference> is going to happen. will you be there?" |
| 18 | +///! Broadcast / Question -> if people reply with YES => fill the 3rd group |
| 19 | +///! some enthusiasts are now attendees |
| 20 | +///! |
| 21 | +///! Staff -> send the actual schedule and misc infos to Attendees |
| 22 | +///! Broadcast / Statement (Attendees) |
| 23 | +///! |
| 24 | +///! An attendee sends a thank you note to one staff member (and not bother everyone) |
| 25 | +///! One message / Statement (Staff) // OK |
| 26 | +///! |
| 27 | +///! ```rust |
| 28 | +///! let staff = Distributor::named("staff"); |
| 29 | +///! let enthusiasts = Distributor::named("enthusiasts"); |
| 30 | +///! let attendees = Disitributor::named("attendees"); |
| 31 | +///! // Enthusiast -> Ask the whole staff "when is the conference going to happen ?" |
| 32 | +///! ask_one(Message + Clone) -> Result<impl Future<Output = Reply>, CouldNotSendError> |
| 33 | +///! // await_one // await_all |
| 34 | +///! // first ? means "have we been able to send the question?" |
| 35 | +///! // it s in a month |
| 36 | +///! let replies = staff.ask_one("when is the conference going to happen ?")?.await?; |
| 37 | +///! ask_everyone(Message + Clone) -> Result<impl Stream<Item = Reply>, CouldNotSendError> |
| 38 | +///! let participants = enthusiasts.ask_everyone("here's our super nice conference, it s happening people!").await?; |
| 39 | +///! for participant in participants { |
| 40 | +///! // grab the sender and add it to the attendee recipient group |
| 41 | +///! } |
| 42 | +///! // send the schedule |
| 43 | +///! tell_everyone(Message + Clone) -> Result<(), CouldNotSendError> |
| 44 | +///! attendees.tell_everyone("hey there, conf is in a week, here s where and how it s going to happen")?; |
| 45 | +///! // send a thank you note |
| 46 | +///! tell(Message) -> Result<(), CouldNotSendError> |
| 47 | +///! staff.tell_one("thank's it was amazing")?; |
| 48 | +///! children |
| 49 | +///! .with_redundancy(10) |
| 50 | +///! .with_distributor(Distributor::named("staff")) |
| 51 | +///! // We create the function to exec when each children is called |
| 52 | +///! .with_exec(move |ctx: BastionContext| async move { /* ... */ }) |
| 53 | +///! children |
| 54 | +///! .with_redundancy(100) |
| 55 | +///! .with_distributor(Distributor::named("enthusiasts")) |
| 56 | +///! // We create the function to exec when each children is called |
| 57 | +///! .with_exec(move |ctx: BastionContext| async move { /* ... */ }) |
| 58 | +///! children |
| 59 | +///! .with_redundancy(0) |
| 60 | +///! .with_distributor(Distributor::named("attendees")) |
| 61 | +///! // We create the function to exec when each children is called |
| 62 | +///! .with_exec(move |ctx: BastionContext| async move { /* ... */ }) |
| 63 | +///! ``` |
| 64 | +use anyhow::{anyhow, Context, Result as AnyResult}; |
| 65 | +use bastion::distributor::*; |
| 66 | +use bastion::prelude::*; |
| 67 | +use tracing::Level; |
| 68 | + |
| 69 | +// true if the person attends the conference |
| 70 | +#[derive(Debug)] |
| 71 | +struct RSVP { |
| 72 | + attends: bool, |
| 73 | + child_ref: ChildRef, |
| 74 | +} |
| 75 | + |
| 76 | +#[derive(Debug, Clone)] |
| 77 | +struct ConferenceSchedule { |
| 78 | + start: std::time::Duration, |
| 79 | + end: std::time::Duration, |
| 80 | + misc: String, |
| 81 | +} |
| 82 | + |
| 83 | +/// cargo r --features=tokio-runtime distributor |
| 84 | +#[tokio::main] |
| 85 | +async fn main() -> AnyResult<()> { |
| 86 | + let subscriber = tracing_subscriber::fmt() |
| 87 | + .with_max_level(Level::INFO) |
| 88 | + .finish(); |
| 89 | + tracing::subscriber::set_global_default(subscriber).unwrap(); |
| 90 | + |
| 91 | + // Initialize bastion |
| 92 | + Bastion::init(); |
| 93 | + |
| 94 | + // 1st Group |
| 95 | + Bastion::supervisor(|supervisor| { |
| 96 | + supervisor.children(|children| { |
| 97 | + // Iniit staff |
| 98 | + // Staff (5 members) - Going to organize the event |
| 99 | + children |
| 100 | + .with_redundancy(5) |
| 101 | + .with_distributor(Distributor::named("staff")) |
| 102 | + .with_exec(organize_the_event) |
| 103 | + }) |
| 104 | + }) |
| 105 | + // 2nd Group |
| 106 | + .and_then(|_| { |
| 107 | + Bastion::supervisor(|supervisor| { |
| 108 | + supervisor.children(|children| { |
| 109 | + // Enthusiasts (50) - interested in participating to the conference (haven't registered yet) |
| 110 | + children |
| 111 | + .with_redundancy(50) |
| 112 | + .with_distributor(Distributor::named("enthusiasts")) |
| 113 | + .with_exec(be_interested_in_the_conference) |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + .map_err(|_| anyhow!("couldn't setup the bastion"))?; |
| 118 | + |
| 119 | + Bastion::start(); |
| 120 | + |
| 121 | + // Wait a bit until everyone is ready |
| 122 | + // std::thread::sleep(std::time::Duration::from_secs(1)); |
| 123 | + |
| 124 | + let staff = Distributor::named("staff"); |
| 125 | + let enthusiasts = Distributor::named("enthusiasts"); |
| 126 | + let attendees = Distributor::named("attendees"); |
| 127 | + |
| 128 | + // Enthusiast -> Ask one of the staff members "when is the conference going to happen ?" |
| 129 | + let answer = staff.ask_one("when is the next conference going to happen?")?; |
| 130 | + MessageHandler::new( |
| 131 | + answer |
| 132 | + .await |
| 133 | + .expect("coulnd't find out when the next conference is going to happen :("), |
| 134 | + ) |
| 135 | + .on_tell(|reply: String, _sender_addr| { |
| 136 | + tracing::info!("received a reply to my message:\n{}", reply); |
| 137 | + }); |
| 138 | + |
| 139 | + // "hey conference <awesomeconference> is going to happen. will you be there?" |
| 140 | + // Broadcast / Question -> if people reply with YES => fill the 3rd group |
| 141 | + let answers = enthusiasts |
| 142 | + .ask_everyone("hey, the conference is going to happen, will you be there?") |
| 143 | + .expect("couldn't ask everyone"); |
| 144 | + |
| 145 | + for answer in answers.into_iter() { |
| 146 | + MessageHandler::new(answer.await.expect("couldn't receive reply")) |
| 147 | + .on_tell(|rsvp: RSVP, _| { |
| 148 | + if rsvp.attends { |
| 149 | + tracing::info!("{:?} will be there! :)", rsvp.child_ref.id()); |
| 150 | + attendees |
| 151 | + .subscribe(rsvp.child_ref) |
| 152 | + .expect("couldn't subscribe attendee"); |
| 153 | + } else { |
| 154 | + tracing::error!("{:?} won't make it :(", rsvp.child_ref.id()); |
| 155 | + } |
| 156 | + }) |
| 157 | + .on_fallback(|unknown, _sender_addr| { |
| 158 | + tracing::error!( |
| 159 | + "distributor_test: uh oh, I received a message I didn't understand\n {:?}", |
| 160 | + unknown |
| 161 | + ); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + // Ok now that attendees have subscribed, let's send information around! |
| 166 | + tracing::info!("Let's send invitations!"); |
| 167 | + // Staff -> send the actual schedule and misc infos to Attendees |
| 168 | + let total_sent = attendees |
| 169 | + .tell_everyone(ConferenceSchedule { |
| 170 | + start: std::time::Duration::from_secs(60), |
| 171 | + end: std::time::Duration::from_secs(3600), |
| 172 | + misc: "it's going to be amazing!".to_string(), |
| 173 | + }) |
| 174 | + .context("couldn't let everyone know the conference is available!")?; |
| 175 | + |
| 176 | + tracing::error!("total number of attendees: {}", total_sent.len()); |
| 177 | + |
| 178 | + tracing::info!("the conference is running!"); |
| 179 | + tokio::time::sleep(std::time::Duration::from_secs(10)).await; |
| 180 | + |
| 181 | + // An attendee sends a thank you note to one staff member (and not bother everyone) |
| 182 | + staff |
| 183 | + .tell_one("the conference was amazing thank you so much!") |
| 184 | + .context("couldn't thank the staff members :(")?; |
| 185 | + |
| 186 | + tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 187 | + // And we're done! |
| 188 | + Bastion::stop(); |
| 189 | + |
| 190 | + // BEWARE, this example doesn't return |
| 191 | + Bastion::block_until_stopped(); |
| 192 | + |
| 193 | + Ok(()) |
| 194 | +} |
| 195 | + |
| 196 | +async fn organize_the_event(ctx: BastionContext) -> Result<(), ()> { |
| 197 | + loop { |
| 198 | + MessageHandler::new(ctx.recv().await?) |
| 199 | + .on_question(|message: &str, sender| { |
| 200 | + tracing::info!("received a question: \n{}", message); |
| 201 | + sender |
| 202 | + .reply("uh i think it will be next month!".to_string()) |
| 203 | + .unwrap(); |
| 204 | + }) |
| 205 | + .on_tell(|message: &str, _| { |
| 206 | + tracing::info!("received a message: \n{}", message); |
| 207 | + }) |
| 208 | + .on_fallback(|unknown, _sender_addr| { |
| 209 | + tracing::error!( |
| 210 | + "staff: uh oh, I received a message I didn't understand\n {:?}", |
| 211 | + unknown |
| 212 | + ); |
| 213 | + }); |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +async fn be_interested_in_the_conference(ctx: BastionContext) -> Result<(), ()> { |
| 218 | + loop { |
| 219 | + MessageHandler::new(ctx.recv().await?) |
| 220 | + .on_tell(|message: std::sync::Arc<&str>, _| { |
| 221 | + tracing::info!( |
| 222 | + "child {}, received a broadcast message:\n{}", |
| 223 | + ctx.current().id(), |
| 224 | + message |
| 225 | + ); |
| 226 | + }) |
| 227 | + .on_tell(|schedule: ConferenceSchedule, _| { |
| 228 | + tracing::info!( |
| 229 | + "child {}, received broadcast conference schedule!:\n{:?}", |
| 230 | + ctx.current().id(), |
| 231 | + schedule |
| 232 | + ); |
| 233 | + }) |
| 234 | + .on_question(|message: &str, sender| { |
| 235 | + tracing::info!("received a question: \n{}", message); |
| 236 | + // ILL BE THERE! |
| 237 | + sender |
| 238 | + .reply(RSVP { |
| 239 | + attends: rand::random(), |
| 240 | + child_ref: ctx.current().clone(), |
| 241 | + }) |
| 242 | + .unwrap(); |
| 243 | + }); |
| 244 | + } |
| 245 | +} |
0 commit comments