1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::marker::PhantomData;
use std::process::Output;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use dashmap::DashMap;
use futures::future::{BoxFuture, join_all, select_all};
use futures::FutureExt;
use tokio::sync::broadcast::Receiver;
use tokio::sync::oneshot::error::RecvError;
use tokio::sync::watch::Ref;
use tokio::sync::{broadcast, mpsc, oneshot, watch};
use tracing::info;

use cosmic_hyperlane::{
    HyperClient, HyperConnectionDetails, HyperConnectionErr, HyperGate, HyperGateSelector,
    HyperRouter, Hyperway, HyperwayEndpoint, HyperwayEndpointFactory, HyperwayInterchange,
    HyperwayStub, InterchangeGate, LayerTransform, LocalHyperwayGateJumper,
    LocalHyperwayGateUnlocker, MountInterchangeGate, SimpleGreeter,
    TokenAuthenticatorWithRemoteWhitelist,
};
use cosmic_space::artifact::asynch::{ArtifactApi, ArtifactFetcher, ReadArtifactFetcher};
use cosmic_space::err::SpaceErr;
use cosmic_space::hyper::{InterchangeKind, Knock};
use cosmic_space::kind::StarSub;
use cosmic_space::loc::{
    ConstellationName, Layer, MachineName, StarHandle, StarKey, Surface, ToPoint, ToSurface,
};
use cosmic_space::log::{PointLogger, RootLogger};
use cosmic_space::particle::{Status, Stub};
use cosmic_space::point::Point;
use cosmic_space::settings::Timeouts;
use cosmic_space::substance::{Bin, Substance};
use cosmic_space::wave::core::cmd::CmdMethod;
use cosmic_space::wave::exchange::asynch::Exchanger;
use cosmic_space::wave::exchange::SetStrategy;
use cosmic_space::wave::{Agent, DirectedProto, HyperWave, Pong, UltraWave, Wave};

use crate::err::HyperErr;
use crate::reg::{Registry, RegistryApi};
use crate::star::{HyperStar, HyperStarApi, HyperStarSkel, HyperStarTx, StarCon, StarTemplate};
use crate::{Cosmos, DriversBuilder};

#[derive(Clone)]
pub struct MachineApi<P>
where
    P: Cosmos,
{
    tx: mpsc::Sender<MachineCall<P>>,
}

impl<P> MachineApi<P>
where
    P: Cosmos,
{
    pub fn new(tx: mpsc::Sender<MachineCall<P>>) -> Self {
        Self { tx }
    }

    pub async fn endpoint_factory(
        &self,
        from: StarKey,
        to: StarKey,
    ) -> Result<Box<dyn HyperwayEndpointFactory>, P::Err> {
        let (rtn, mut rtn_rx) = oneshot::channel();
        self.tx
            .send(MachineCall::EndpointFactory { from, to, rtn })
            .await;
        Ok(rtn_rx.await?)
    }

    pub async fn add_interchange(
        &self,
        kind: InterchangeKind,
        gate: Arc<dyn HyperGate>,
    ) -> Result<(), SpaceErr> {
        let (rtn, rtn_rx) = oneshot::channel();
        self.tx
            .send(MachineCall::AddGate { kind, gate, rtn })
            .await?;
        rtn_rx.await?
    }

    pub async fn knock(&self, knock: Knock) -> Result<HyperwayEndpoint, SpaceErr> {
        let (rtn, rtn_rx) = oneshot::channel();
        self.tx.send(MachineCall::Knock { knock, rtn }).await;
        rtn_rx.await?
    }

    pub fn terminate(&self) {
        self.tx.try_send(MachineCall::Terminate);
    }

    pub async fn wait_ready(&self) {
        let (tx, mut rx) = oneshot::channel();
        self.tx.send(MachineCall::WaitForReady(tx)).await;
        rx.await;
    }

    pub async fn await_termination(&self) -> Result<(), P::Err> {
        let (tx, mut rx) = oneshot::channel();
        self.tx.send(MachineCall::AwaitTermination(tx)).await;
        let mut rx = match rx.await {
            Ok(rx) => rx,
            Err(err) => {
                return Err(P::Err::new(err.to_string()));
            }
        };
        match rx.recv().await {
            Ok(result) => result,
            Err(err) => {
                return Err(P::Err::new(err.to_string()));
            }
        }
    }

    #[cfg(test)]
    pub async fn get_machine_star(&self) -> Result<HyperStarApi<P>, SpaceErr> {
        let (tx, mut rx) = oneshot::channel();
        self.tx.send(MachineCall::GetMachineStar(tx)).await;
        Ok(rx.await?)
    }

    #[cfg(test)]
    pub async fn get_star(&self, key: StarKey) -> Result<HyperStarApi<P>, SpaceErr> {
        let (rtn, mut rtn_rx) = oneshot::channel();
        self.tx.send(MachineCall::GetStar { key, rtn }).await;
        rtn_rx.await?
    }
}

#[derive(Clone)]
pub struct MachineSkel<P>
where
    P: Cosmos,
{
    pub name: MachineName,
    pub cosmos: P,
    pub registry: Registry<P>,
    pub artifacts: ArtifactApi,
    pub logger: RootLogger,
    pub timeouts: Timeouts,
    pub api: MachineApi<P>,
    pub status_rx: watch::Receiver<MachineStatus>,
    pub status_tx: mpsc::Sender<MachineStatus>,
    pub machine_star: Surface,
    pub global: Surface,
}

pub struct Machine<P>
where
    P: Cosmos + 'static,
{
    pub skel: MachineSkel<P>,
    pub stars: Arc<HashMap<Point, HyperStarApi<P>>>,
    pub machine_star: HyperStarApi<P>,
    pub gate_selector: Arc<HyperGateSelector>,
    pub call_tx: mpsc::Sender<MachineCall<P>>,
    pub call_rx: mpsc::Receiver<MachineCall<P>>,
    pub termination_broadcast_tx: broadcast::Sender<Result<(), P::Err>>,
    pub logger: PointLogger,
}

impl<P> Machine<P>
where
    P: Cosmos + 'static,
{
    pub fn new(platform: P) -> MachineApi<P> {
        let (call_tx, call_rx) = mpsc::channel(1024);
        let machine_api = MachineApi::new(call_tx.clone());
        tokio::spawn(async move { Machine::init(platform, call_tx, call_rx).await });

        machine_api
    }

    async fn init(
        platform: P,
        call_tx: mpsc::Sender<MachineCall<P>>,
        call_rx: mpsc::Receiver<MachineCall<P>>,
    ) -> Result<MachineApi<P>, P::Err> {
        let template = platform.machine_template();
        let machine_name = platform.machine_name();
        let machine_api = MachineApi::new(call_tx.clone());
        let (mpsc_status_tx, mut mpsc_status_rx) = mpsc::channel(128);
        let (watch_status_tx, watch_status_rx) = watch::channel(MachineStatus::Init);
        tokio::spawn(async move {
            while let Some(status) = mpsc_status_rx.recv().await {
                watch_status_tx.send(status);
            }
        });

        let machine_star = StarKey::machine(machine_name.clone())
            .to_point()
            .to_surface()
            .with_layer(Layer::Gravity);
        let logger = platform.logger().point(machine_star.point.clone());
        let global = machine_star
            .point
            .push("global")
            .unwrap()
            .to_surface()
            .with_layer(Layer::Core);
        let skel = MachineSkel {
            name: machine_name.clone(),
            machine_star,
            registry: platform.global_registry().await?,
            artifacts: platform.artifact_hub(),
            logger: platform.logger(),
            timeouts: Timeouts::default(),
            cosmos: platform.clone(),
            api: machine_api.clone(),
            status_tx: mpsc_status_tx,
            status_rx: watch_status_rx,
            global,
        };

        let mut stars = HashMap::new();
        let mut gates = Arc::new(DashMap::new());
        let star_templates = template.with_machine_star(machine_name);

        for star_template in star_templates {
            let star_point = star_template.key.clone().to_point();
            let star_port = star_point.clone().to_surface().with_layer(Layer::Core);

            let drivers_point = star_point.push("drivers".to_string()).unwrap();
            let logger = skel.logger.point(drivers_point.clone());

            let mut star_tx: HyperStarTx<P> = HyperStarTx::new(star_point.clone());
            let star_skel =
                HyperStarSkel::new(star_template.clone(), skel.clone(), &mut star_tx).await;

            let mut drivers = platform.drivers_builder(&star_template.kind);

            let mut interchange =
                HyperwayInterchange::new(logger.push_point("interchange").unwrap());

            let star_hop = star_point.clone().to_surface().with_layer(Layer::Gravity);

            let mut hyperway = Hyperway::new(star_hop.clone(), Agent::HyperUser, logger.clone());
            hyperway.transform_inbound(Box::new(LayerTransform::new(Layer::Gravity)));

            let hyperway_endpoint = hyperway.hyperway_endpoint_far(None).await;
            interchange.add(hyperway).await;
            interchange.singular_to(star_hop.clone());

            let interchange = Arc::new(interchange);
            let auth = skel.cosmos.star_auth(&star_template.key)?;
            let greeter = SimpleGreeter::new(star_hop.clone(), star_port.clone());
            let gate: Arc<dyn HyperGate> = Arc::new(MountInterchangeGate::new(
                auth,
                greeter,
                interchange.clone(),
                logger.clone(),
            ));

            for con in star_template.connections.iter() {
                match con {
                    StarCon::Receiver(remote) => {
                        let star = remote
                            .key
                            .clone()
                            .to_point()
                            .to_surface()
                            .with_layer(Layer::Gravity);
                        let hyperway = Hyperway::new(star, Agent::HyperUser, logger.clone());
                        interchange.add(hyperway).await;
                    }
                    StarCon::Connector(remote) => {
                        let star = remote
                            .key
                            .clone()
                            .to_point()
                            .to_surface()
                            .with_layer(Layer::Gravity);
                        let hyperway = Hyperway::new(star, Agent::HyperUser, logger.clone());
                        interchange.add(hyperway).await;
                    }
                }
            }

            gates.insert(InterchangeKind::Star(star_template.key.clone()), gate);
            let star_api = HyperStar::new(
                star_skel.clone(),
                drivers,
                hyperway_endpoint,
                interchange.clone(),
                star_tx,
            )
            .await?;
            stars.insert(star_point.clone(), star_api);
        }

        let mut gate_selector = Arc::new(HyperGateSelector::new(gates));
        skel.cosmos.start_services(&gate_selector).await;
        let gate: Arc<dyn HyperGate> = gate_selector.clone();

        let (machine_point, machine_star) = stars
            .iter()
            .find(|(k, v)| v.kind == StarSub::Machine)
            .map(|(k, v)| (k.clone(), v.clone()))
            .expect("expected Machine Star");

        {
            let machine_api = skel.api.clone();
            ctrlc::set_handler(move || {
                machine_api.terminate();
            });
        }

        let logger = skel.logger.point(machine_point);

        let (term_tx, _) = broadcast::channel(1);
        let stars = Arc::new(stars);
        {
            let mut star_statuses_rx: Vec<watch::Receiver<Status>> =
                stars.values().map(|s| s.status_rx.clone()).collect();
            let status_tx = skel.status_tx.clone();
            let star_count = stars.len();
            tokio::spawn(async move {
                loop {
                    let mut readies = 0;
                    let mut inits = 0;
                    let mut panics = 0;
                    let mut fatals = 0;
                    for status_rx in star_statuses_rx.iter_mut() {
                        match status_rx.borrow().clone() {
                            Status::Unknown => {}
                            Status::Pending => {}
                            Status::Init => {
                                inits = inits + 1;
                            }
                            Status::Ready => {
                                readies = readies + 1;
                            }
                            Status::Paused => {}
                            Status::Resuming => {}
                            Status::Panic => {
                                panics = panics + 1;
                            }
                            Status::Fatal => {
                                fatals = fatals + 1;
                            }
                            Status::Done => {}
                        }
                    }

                    if readies == star_count {
                        status_tx.send(MachineStatus::Ready).await;
                    } else if fatals > 0 {
                        status_tx.send(MachineStatus::Fatal).await;
                    } else if panics > 0 {
                        status_tx.send(MachineStatus::Panic).await;
                    } else if inits > 0 {
                        status_tx.send(MachineStatus::Init).await;
                    }

                    let boxed_status_rx: Vec<BoxFuture<Result<(), watch::error::RecvError>>> =
                        star_statuses_rx
                            .iter_mut()
                            .map(|s| s.changed().boxed())
                            .collect();
                    select_all(boxed_status_rx).await;
                }
            });
        }

        let mut machine = Self {
            skel: skel.clone(),
            logger: logger.clone(),
            machine_star,
            stars,
            gate_selector,
            call_tx,
            call_rx,
            termination_broadcast_tx: term_tx,
        };

        /// SETUP ARTIFAC
        let factory = MachineApiExtFactory {
            machine_api: machine_api.clone(),
            logger: logger.clone(),
        };
        let exchanger = Exchanger::new(
            Point::from_str("artifact").unwrap().to_surface(),
            Timeouts::default(),
            logger.clone(),
        );
        let client =
            HyperClient::new_with_exchanger(Box::new(factory), Some(exchanger), logger.clone())
                .unwrap();

        let fetcher = Arc::new(ClientArtifactFetcher::new(client, skel.registry.clone()));
        skel.artifacts.set_fetcher(fetcher).await;

        machine.start().await;
        Ok(machine_api)
    }

    async fn init0(&self) {
        let logger = self.logger.span();
        let mut inits = vec![];
        for star in self.stars.values() {
            inits.push(star.init().boxed());
        }

        join_all(inits).await;
    }

    async fn start(mut self) -> Result<(), P::Err> {
        self.call_tx
            .send(MachineCall::Init)
            .await
            .unwrap_or_default();

        while let Some(call) = self.call_rx.recv().await {
            match call {
                MachineCall::Init => {
                    self.init0().await;
                }
                MachineCall::Terminate => {
                    self.termination_broadcast_tx.send(Ok(()));
                    return Ok(());
                }
                MachineCall::AwaitTermination(tx) => {
                    tx.send(self.termination_broadcast_tx.subscribe());
                }
                MachineCall::WaitForReady(rtn) => {
                    let mut status_rx = self.skel.status_rx.clone();
                    tokio::spawn(async move {
                        loop {
                            if MachineStatus::Ready == status_rx.borrow().clone() {
                                rtn.send(());
                                break;
                            }
                            match status_rx.changed().await {
                                Ok(_) => {}
                                Err(err) => {
                                    rtn.send(());
                                    break;
                                }
                            }
                        }
                    });
                }
                MachineCall::AddGate { kind, gate, rtn } => {
                    rtn.send(self.gate_selector.add(kind.clone(), gate));
                }
                MachineCall::Knock { knock, rtn } => {
                    let gate_selector = self.gate_selector.clone();
                    let logger = self.skel.logger.point(self.skel.machine_star.point.clone());
                    tokio::spawn(async move {
                        rtn.send(
                            logger
                                .result_ctx("MachineCall::Knock", gate_selector.knock(knock).await),
                        );
                    });
                }
                MachineCall::EndpointFactory { from, to, rtn } => {
                    let factory = Box::new(MachineHyperwayEndpointFactory::new(
                        from,
                        to,
                        self.call_tx.clone(),
                    ));
                    rtn.send(factory).unwrap_or_default();
                }
                #[cfg(test)]
                MachineCall::GetMachineStar(rtn) => {
                    rtn.send(self.machine_star.clone());
                }
                #[cfg(test)]
                MachineCall::GetStar { key, rtn } => {
                    rtn.send(
                        self.stars
                            .get(&key.to_point())
                            .ok_or(format!("could not find star: {}", key.to_string()).into())
                            .cloned(),
                    )
                    .unwrap_or_default();
                }
                #[cfg(test)]
                MachineCall::GetRegistry(rtn) => {
                    rtn.send(self.skel.registry.clone());
                }
            }

            self.termination_broadcast_tx
                .send(Err(P::Err::new("machine quit unexpectedly.")));
        }

        Ok(())
    }
}

pub enum MachineCall<P>
where
    P: Cosmos,
{
    Init,
    Terminate,
    AwaitTermination(oneshot::Sender<broadcast::Receiver<Result<(), P::Err>>>),
    WaitForReady(oneshot::Sender<()>),
    AddGate {
        kind: InterchangeKind,
        gate: Arc<dyn HyperGate>,
        rtn: oneshot::Sender<Result<(), SpaceErr>>,
    },
    Knock {
        knock: Knock,
        rtn: oneshot::Sender<Result<HyperwayEndpoint, SpaceErr>>,
    },
    EndpointFactory {
        from: StarKey,
        to: StarKey,
        rtn: oneshot::Sender<Box<dyn HyperwayEndpointFactory>>,
    },
    #[cfg(test)]
    GetMachineStar(oneshot::Sender<HyperStarApi<P>>),
    #[cfg(test)]
    GetStar {
        key: StarKey,
        rtn: oneshot::Sender<Result<HyperStarApi<P>, SpaceErr>>,
    },
    #[cfg(test)]
    GetRegistry(oneshot::Sender<Registry<P>>),
}

#[derive(Clone, Eq, PartialEq, strum_macros::Display)]
pub enum MachineStatus {
    Pending,
    Init,
    Ready,
    Panic,
    Fatal,
}

pub struct MachineTemplate {
    pub stars: Vec<StarTemplate>,
}

impl MachineTemplate {
    pub fn star_set(&self) -> HashSet<StarKey> {
        let mut rtn = HashSet::new();
        for star in self.stars.iter() {
            rtn.insert(star.key.clone());
        }
        rtn
    }

    pub fn with_machine_star(&self, machine: MachineName) -> Vec<StarTemplate> {
        let mut stars = self.stars.clone();
        let mut machine = StarTemplate::new(StarKey::machine(machine), StarSub::Machine);
        for star in stars.iter_mut() {
            star.connect(machine.to_stub());
            machine.receive(star.to_stub());
        }

        stars.push(machine);

        stars
    }
}

impl Default for MachineTemplate {
    fn default() -> Self {
        let constellation = "central".to_string();

        let mut central = StarTemplate::new(StarKey::central(), StarSub::Central);
        let mut nexus = StarTemplate::new(
            StarKey::new(&constellation, &StarHandle::name("nexus")),
            StarSub::Nexus,
        );
        let mut supe = StarTemplate::new(
            StarKey::new(&constellation, &StarHandle::name("super")),
            StarSub::Super,
        );
        let mut maelstrom = StarTemplate::new(
            StarKey::new(&constellation, &StarHandle::name("maelstrom")),
            StarSub::Maelstrom,
        );
        let mut scribe = StarTemplate::new(
            StarKey::new(&constellation, &StarHandle::name("scribe")),
            StarSub::Scribe,
        );
        let mut jump = StarTemplate::new(
            StarKey::new(&constellation, &StarHandle::name("jump")),
            StarSub::Jump,
        );
        let mut fold = StarTemplate::new(
            StarKey::new(&constellation, &StarHandle::name("fold")),
            StarSub::Fold,
        );

        nexus.receive(central.to_stub());
        nexus.receive(supe.to_stub());
        nexus.receive(maelstrom.to_stub());
        nexus.receive(scribe.to_stub());
        nexus.receive(jump.to_stub());
        nexus.receive(fold.to_stub());

        central.connect(nexus.to_stub());
        supe.connect(nexus.to_stub());
        maelstrom.connect(nexus.to_stub());
        scribe.connect(nexus.to_stub());
        jump.connect(nexus.to_stub());
        fold.connect(nexus.to_stub());

        let mut stars = vec![];
        stars.push(central);
        stars.push(nexus);
        stars.push(supe);
        stars.push(maelstrom);
        stars.push(scribe);
        stars.push(jump);
        stars.push(fold);

        Self { stars }
    }
}

pub struct MachineHyperwayEndpointFactory<P>
where
    P: Cosmos,
{
    from: StarKey,
    to: StarKey,
    call_tx: mpsc::Sender<MachineCall<P>>,
}

impl<P> MachineHyperwayEndpointFactory<P>
where
    P: Cosmos,
{
    pub fn new(from: StarKey, to: StarKey, call_tx: mpsc::Sender<MachineCall<P>>) -> Self {
        Self { from, to, call_tx }
    }
}

#[async_trait]
impl<P> HyperwayEndpointFactory for MachineHyperwayEndpointFactory<P>
where
    P: Cosmos,
{
    async fn create(
        &self,
        status_tx: mpsc::Sender<HyperConnectionDetails>,
    ) -> Result<HyperwayEndpoint, SpaceErr> {
        let knock = Knock::new(
            InterchangeKind::Star(self.to.clone()),
            self.from
                .clone()
                .to_point()
                .to_surface()
                .with_layer(Layer::Gravity),
            Substance::Empty,
        );
        let (rtn, mut rtn_rx) = oneshot::channel();
        self.call_tx.send(MachineCall::Knock { knock, rtn }).await;
        tokio::time::timeout(Duration::from_secs(60), rtn_rx).await??
    }
}

pub struct MachineApiExtFactory<P>
where
    P: Cosmos,
{
    pub machine_api: MachineApi<P>,
    pub logger: PointLogger,
}

#[async_trait]
impl<P> HyperwayEndpointFactory for MachineApiExtFactory<P>
where
    P: Cosmos,
{
    async fn create(
        &self,
        status_tx: mpsc::Sender<HyperConnectionDetails>,
    ) -> Result<HyperwayEndpoint, SpaceErr> {
        let knock = Knock {
            kind: InterchangeKind::DefaultControl,
            auth: Box::new(Substance::Empty),
            remote: None,
        };
        self.logger
            .result_ctx("machine_api.knock()", self.machine_api.knock(knock).await)
    }
}

pub struct ClientArtifactFetcher<P>
where
    P: Cosmos,
{
    pub registry: Registry<P>,
    pub client: HyperClient,
}

impl<P> ClientArtifactFetcher<P>
where
    P: Cosmos,
{
    pub fn new(client: HyperClient, registry: Registry<P>) -> Self {
        Self { client, registry }
    }
}

#[async_trait]
impl<P> ArtifactFetcher for ClientArtifactFetcher<P>
where
    P: Cosmos,
{
    async fn stub(&self, point: &Point) -> Result<Stub, SpaceErr> {
        let record = self
            .registry
            .record(point)
            .await
            .map_err(|e| e.to_space_err())?;
        Ok(record.details.stub)
    }

    async fn fetch(&self, point: &Point) -> Result<Bin, SpaceErr> {
        let transmitter = self.client.transmitter_builder().await?.build();

        let mut wave = DirectedProto::ping();
        wave.method(CmdMethod::Read);
        wave.to(point.clone().to_surface().with_layer(Layer::Core));
        let pong: Wave<Pong> = transmitter.direct(wave).await?;

        pong.ok_or()?;

        if let Substance::Bin(bin) = pong.variant.core.body {
            Ok(bin)
        } else {
            Err("expecting Bin encountered some other substance when fetching artifact".into())
        }
    }
}