oxedyne/fe2o3/fe2o3_o3db_sync/src/bots/worker/bot.rs
2.8 KiB, 15 runs
created by r1870400018:749, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::{ |
| 2 | prelude::*, |
| 3 | base::index::WorkerInd, |
| 4 | bots::base::bot::OzoneBot, |
| 5 | comm::{ |
| 6 | channels::ChannelPool, |
| 7 | msg::OzoneMsg, |
| 8 | }, |
| 9 | file::zdir::ZoneDir, |
| 10 | }; |
| 11 | |
| 12 | use oxedyne_fe2o3_core::{ |
| 13 | channels::Simplex, |
| 14 | thread::Semaphore, |
| 15 | }; |
| 16 | use oxedyne_fe2o3_jdat::id::NumIdDat; |
| 17 | |
| 18 | #[macro_export] |
| 19 | macro_rules! workerbot_methods { () => { |
| 20 | fn wind(&self) -> &WorkerInd { &self.wind } |
| 21 | fn wtyp(&self) -> &WorkerType { &self.wtyp } |
| 22 | fn zdir(&self) -> &ZoneDir { &self.zdir } |
| 23 | fn set_zdir(&mut self, zdir: ZoneDir) { |
| 24 | self.zdir = zdir; |
| 25 | } |
| 26 | } } |
| 27 | |
| 28 | pub trait WorkerBot< |
| 29 | const UIDL: usize, |
| 30 | UID: NumIdDat<UIDL> + 'static, |
| 31 | ENC: Encrypter + 'static, |
| 32 | KH: Hasher + 'static, |
| 33 | PR: Hasher, |
| 34 | CS: Checksummer, |
| 35 | >: |
| 36 | OzoneBot<UIDL, UID, ENC, KH, PR, CS> + Send |
| 37 | { |
| 38 | fn wind(&self) -> &WorkerInd; |
| 39 | fn wtyp(&self) -> &WorkerType; |
| 40 | fn zdir(&self) -> &ZoneDir; |
| 41 | fn set_zdir(&mut self, zdir: ZoneDir); |
| 42 | |
| 43 | fn zbot(&self) -> Option<&Simplex<OzoneMsg<UIDL, UID, ENC, KH>>> { |
| 44 | match self.chans().get_zbot(self.wind().zind()) { |
| 45 | Err(e) => { |
| 46 | self.error(e); |
| 47 | None |
| 48 | }, |
| 49 | Ok(chan) => Some(chan), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn cbots(&self) -> Outcome<ChannelPool<UIDL, UID, ENC, KH>> { |
| 54 | self.chans().get_workers_of_type_in_zone(&WorkerType::Cache, self.wind().zind()) |
| 55 | } |
| 56 | fn fbots(&self) -> Outcome<ChannelPool<UIDL, UID, ENC, KH>> { |
| 57 | self.chans().get_workers_of_type_in_zone(&WorkerType::File, self.wind().zind()) |
| 58 | } |
| 59 | fn igbots(&self) -> Outcome<ChannelPool<UIDL, UID, ENC, KH>> { |
| 60 | self.chans().get_workers_of_type_in_zone(&WorkerType::InitGarbage, self.wind().zind()) |
| 61 | } |
| 62 | fn scbots(&self) -> Outcome<ChannelPool<UIDL, UID, ENC, KH>> { |
| 63 | self.chans().get_workers_of_type_in_zone(&WorkerType::Scan, self.wind().zind()) |
| 64 | } |
| 65 | |
| 66 | fn listen_worker(&mut self, msg: OzoneMsg<UIDL, UID, ENC, KH>) -> Option<OzoneMsg<UIDL, UID, ENC, KH>> { |
| 67 | match msg { |
| 68 | OzoneMsg::ZoneDir(_, zdir) => { |
| 69 | self.set_zdir(zdir); |
| 70 | None |
| 71 | }, |
| 72 | _ => Some(msg), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// A way to identify the bots that do most of the work in Ozone. |
| 78 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 79 | pub enum WorkerType { |
| 80 | Cache, |
| 81 | File, |
| 82 | InitGarbage, |
| 83 | Reader, |
| 84 | Scan, |
| 85 | Writer, |
| 86 | } |
| 87 | |
| 88 | pub struct ZoneWorkerInitArgs< |
| 89 | const UIDL: usize, |
| 90 | UID: NumIdDat<UIDL>, |
| 91 | ENC: Encrypter, |
| 92 | KH: Hasher, |
| 93 | PR: Hasher, |
| 94 | CS: Checksummer, |
| 95 | >{ |
| 96 | // Identity |
| 97 | pub wind: WorkerInd, |
| 98 | pub wtyp: WorkerType, |
| 99 | // Bot |
| 100 | pub sem: Semaphore, |
| 101 | pub log_stream_id: String, |
| 102 | // Comms |
| 103 | pub chan_in: Simplex<OzoneMsg<UIDL, UID, ENC, KH>>, |
| 104 | // API |
| 105 | pub api: OzoneApi<UIDL, UID, ENC, KH, PR, CS>, |
| 106 | } |