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
use futures::channel::mpsc::{Receiver, Sender};
use futures::future::join_all;
use futures::future::Either;
use futures::prelude::*;
use futures::FutureExt;
use std::any::Any;
use std::fmt;
use std::future::Future;
use std::pin::Pin;

use crate::anyhow::{Context, Result};
use crate::runtime::BlockDescription;
use crate::runtime::BlockMessage;
use crate::runtime::BlockMeta;
use crate::runtime::Error;
use crate::runtime::FlowgraphMessage;
use crate::runtime::MessageIo;
use crate::runtime::MessageOutput;
use crate::runtime::Pmt;
use crate::runtime::PortId;
use crate::runtime::StreamInput;
use crate::runtime::StreamIo;
use crate::runtime::StreamOutput;

/// Work IO
///
/// Communicate between `work()` and the runtime.
pub struct WorkIo {
    /// Call block immediately again
    pub call_again: bool,
    /// Mark block as finished
    pub finished: bool,
    /// Block on future
    ///
    /// The block will be called (1) if somehting happens or (2) if the future resolves
    pub block_on: Option<Pin<Box<dyn Future<Output = ()> + Send>>>,
}

impl WorkIo {
    /// Helper to set the future of the Work IO
    pub fn block_on<F: Future<Output = ()> + Send + 'static>(&mut self, f: F) {
        self.block_on = Some(Box::pin(f));
    }
}

impl fmt::Debug for WorkIo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("WorkIo")
            .field("call_again", &self.call_again)
            .field("finished", &self.finished)
            .finish()
    }
}

/// Kernal
///
/// Central trait to implement a block
#[async_trait]
pub trait Kernel: Send {
    /// Processes stream data
    async fn work(
        &mut self,
        _io: &mut WorkIo,
        _s: &mut StreamIo,
        _m: &mut MessageIo<Self>,
        _b: &mut BlockMeta,
    ) -> Result<()> {
        Ok(())
    }
    /// Initialize kernel
    async fn init(
        &mut self,
        _s: &mut StreamIo,
        _m: &mut MessageIo<Self>,
        _b: &mut BlockMeta,
    ) -> Result<()> {
        Ok(())
    }
    /// De-initialize kernel
    async fn deinit(
        &mut self,
        _s: &mut StreamIo,
        _m: &mut MessageIo<Self>,
        _b: &mut BlockMeta,
    ) -> Result<()> {
        Ok(())
    }
}

#[async_trait]
pub trait BlockT: Send + Any {
    // ##### BLOCK
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
    async fn run(
        &mut self,
        block_id: usize,
        main_inbox: Sender<FlowgraphMessage>,
        inbox: Receiver<BlockMessage>,
    );

    // ##### META
    fn instance_name(&self) -> Option<&str>;
    fn set_instance_name(&mut self, name: &str);
    fn type_name(&self) -> &str;
    fn is_blocking(&self) -> bool;

    // ##### STREAM IO
    #[allow(clippy::type_complexity)]
    fn set_tag_propagation(
        &mut self,
        f: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
    );
    fn stream_inputs(&self) -> &Vec<StreamInput>;
    fn stream_input(&self, id: usize) -> &StreamInput;
    fn stream_input_name_to_id(&self, name: &str) -> Option<usize>;
    fn stream_outputs(&self) -> &Vec<StreamOutput>;
    fn stream_output(&self, id: usize) -> &StreamOutput;
    fn stream_output_name_to_id(&self, name: &str) -> Option<usize>;

    // ##### MESSAGE IO
    fn message_input_name_to_id(&self, name: &str) -> Option<usize>;
    fn message_outputs(&self) -> &Vec<MessageOutput>;
    fn message_output_name_to_id(&self, name: &str) -> Option<usize>;
}

/// Typed Block
pub struct TypedBlock<T> {
    /// Block metadata
    pub meta: BlockMeta,
    /// Stream IO
    pub sio: StreamIo,
    /// Message IO
    pub mio: MessageIo<T>,
    /// Kernel
    pub kernel: T,
}

impl<T: Kernel + Send + 'static> TypedBlock<T> {
    /// Create Typed Block
    pub fn new(meta: BlockMeta, sio: StreamIo, mio: MessageIo<T>, kernel: T) -> Self {
        Self {
            meta,
            sio,
            mio,
            kernel,
        }
    }
}

pub(crate) struct TypedBlockWrapper<T> {
    pub(crate) inner: Option<TypedBlock<T>>,
}

// clippy bug
#[allow(clippy::needless_pass_by_ref_mut)]
impl<T: Kernel + Send + 'static> TypedBlockWrapper<T> {
    async fn call_handler(
        io: &mut WorkIo,
        mio: &mut MessageIo<T>,
        meta: &mut BlockMeta,
        kernel: &mut T,
        id: PortId,
        p: Pmt,
    ) -> std::result::Result<Pmt, Error> {
        let id = match id {
            PortId::Index(i) => {
                if i < mio.inputs().len() {
                    i
                } else {
                    return Err(Error::InvalidHandler(PortId::Index(i)));
                }
            }
            PortId::Name(n) => match mio.input_name_to_id(&n) {
                Some(s) => s,
                None => {
                    return Err(Error::InvalidHandler(PortId::Name(n)));
                }
            },
        };
        if matches!(p, Pmt::Finished) {
            mio.input_mut(id).finish();
        }
        let h = mio.input(id).get_handler();
        let f = (h)(kernel, io, mio, meta, p);
        f.await.or(Err(Error::HandlerError))
    }

    async fn run_impl(
        TypedBlock {
            mut meta,
            mut sio,
            mut mio,
            mut kernel,
        }: TypedBlock<T>,
        block_id: usize,
        mut main_inbox: Sender<FlowgraphMessage>,
        mut inbox: Receiver<BlockMessage>,
    ) -> Result<()> {
        // init work io
        let mut work_io = WorkIo {
            call_again: false,
            finished: false,
            block_on: None,
        };

        // setup phase
        loop {
            match inbox.next().await.context("no msg")? {
                BlockMessage::Initialize => {
                    if let Err(e) = kernel.init(&mut sio, &mut mio, &mut meta).await {
                        error!(
                            "{}: Error during initialization. Terminating.",
                            meta.instance_name().unwrap()
                        );
                        main_inbox
                            .send(FlowgraphMessage::BlockError {
                                block_id,
                                block: Block(Box::new(TypedBlockWrapper {
                                    inner: Some(TypedBlock {
                                        sio,
                                        mio,
                                        meta,
                                        kernel,
                                    }),
                                })),
                            })
                            .await?;
                        return Err(e);
                    } else {
                        main_inbox.send(FlowgraphMessage::Initialized).await?;
                    }
                    break;
                }
                BlockMessage::StreamOutputInit { src_port, writer } => {
                    sio.output(src_port).init(writer);
                }
                BlockMessage::StreamInputInit { dst_port, reader } => {
                    sio.input(dst_port).set_reader(reader);
                }
                BlockMessage::MessageOutputConnect {
                    src_port,
                    dst_port,
                    dst_inbox,
                } => {
                    mio.output_mut(src_port).connect(dst_port, dst_inbox);
                }
                t => warn!(
                    "{} unhandled message during init {:?}",
                    meta.instance_name().unwrap(),
                    t
                ),
            }
        }

        let inbox = inbox.peekable();
        futures::pin_mut!(inbox);

        // main loop
        loop {
            // ================== non blocking
            loop {
                match inbox.next().now_or_never() {
                    Some(Some(BlockMessage::Notify)) => {}
                    Some(Some(BlockMessage::BlockDescription { tx })) => {
                        let stream_inputs: Vec<String> =
                            sio.inputs().iter().map(|x| x.name().to_string()).collect();
                        let stream_outputs: Vec<String> =
                            sio.outputs().iter().map(|x| x.name().to_string()).collect();
                        let message_inputs: Vec<String> = mio.input_names();
                        let message_outputs: Vec<String> =
                            mio.outputs().iter().map(|x| x.name().to_string()).collect();

                        let description = BlockDescription {
                            id: block_id,
                            type_name: meta.type_name().to_string(),
                            instance_name: meta.instance_name().unwrap().to_string(),
                            stream_inputs,
                            stream_outputs,
                            message_inputs,
                            message_outputs,
                            blocking: meta.is_blocking(),
                        };
                        tx.send(description).unwrap();
                    }
                    Some(Some(BlockMessage::StreamInputDone { input_id })) => {
                        sio.input(input_id).finish();
                    }
                    Some(Some(BlockMessage::StreamOutputDone { .. })) => {
                        work_io.finished = true;
                    }
                    Some(Some(BlockMessage::Call { port_id, data })) => {
                        match Self::call_handler(
                            &mut work_io,
                            &mut mio,
                            &mut meta,
                            &mut kernel,
                            port_id,
                            data,
                        )
                        .await
                        {
                            Err(Error::InvalidHandler(port_id)) => {
                                error!(
                                    "{}: BlockMessage::Call -> Invalid Handler {port_id:?}.",
                                    meta.instance_name().unwrap(),
                                );
                            }
                            Err(Error::HandlerError) => {
                                error!(
                                    "{}: BlockMessage::Call -> HandlerError. Terminating.",
                                    meta.instance_name().unwrap(),
                                );
                                main_inbox
                                    .send(FlowgraphMessage::BlockError {
                                        block_id,
                                        block: Block(Box::new(TypedBlockWrapper {
                                            inner: Some(TypedBlock {
                                                sio,
                                                mio,
                                                meta,
                                                kernel,
                                            }),
                                        })),
                                    })
                                    .await?;
                                return Err(Error::HandlerError.into());
                            }
                            _ => {}
                        }
                    }
                    Some(Some(BlockMessage::Callback { port_id, data, tx })) => {
                        match Self::call_handler(
                            &mut work_io,
                            &mut mio,
                            &mut meta,
                            &mut kernel,
                            port_id.clone(),
                            data,
                        )
                        .await
                        {
                            Err(Error::HandlerError) => {
                                error!(
                                    "{}: Error in callback. Terminating.",
                                    meta.instance_name().unwrap(),
                                );
                                let _ = tx.send(Err(Error::InvalidHandler(port_id)));
                                main_inbox
                                    .send(FlowgraphMessage::BlockError {
                                        block_id,
                                        block: Block(Box::new(TypedBlockWrapper {
                                            inner: Some(TypedBlock {
                                                sio,
                                                mio,
                                                meta,
                                                kernel,
                                            }),
                                        })),
                                    })
                                    .await?;
                                return Err(Error::HandlerError.into());
                            }
                            res => {
                                let _ = tx.send(res);
                            }
                        }
                    }
                    Some(Some(BlockMessage::Terminate)) => work_io.finished = true,
                    Some(Some(t)) => warn!("block unhandled message in main loop {:?}", t),
                    _ => break,
                };
                // received at least one message
                work_io.call_again = true;
            }

            // ================== shutdown
            if work_io.finished {
                debug!("{} terminating ", meta.instance_name().unwrap());
                join_all(sio.inputs_mut().iter_mut().map(|i| i.notify_finished())).await;
                join_all(sio.outputs_mut().iter_mut().map(|o| o.notify_finished())).await;
                join_all(mio.outputs_mut().iter_mut().map(|o| o.notify_finished())).await;

                match kernel.deinit(&mut sio, &mut mio, &mut meta).await {
                    Ok(_) => {
                        let _ = main_inbox
                            .send(FlowgraphMessage::BlockDone {
                                block_id,
                                block: Block(Box::new(TypedBlockWrapper {
                                    inner: Some(TypedBlock {
                                        sio,
                                        mio,
                                        meta,
                                        kernel,
                                    }),
                                })),
                            })
                            .await;
                        break;
                    }
                    Err(e) => {
                        error!(
                            "{}: Error in deinit (). Terminating. ({:?})",
                            meta.instance_name().unwrap(),
                            e
                        );
                        main_inbox
                            .send(FlowgraphMessage::BlockError {
                                block_id,
                                block: Block(Box::new(TypedBlockWrapper {
                                    inner: Some(TypedBlock {
                                        sio,
                                        mio,
                                        meta,
                                        kernel,
                                    }),
                                })),
                            })
                            .await?;
                        return Err(e);
                    }
                };
            }

            // ================== blocking
            if !work_io.call_again {
                if let Some(f) = work_io.block_on.take() {
                    let p = inbox.as_mut().peek();

                    match future::select(f, p).await {
                        Either::Left(_) => {
                            work_io.call_again = true;
                        }
                        Either::Right((_, f)) => {
                            work_io.block_on = Some(f);
                            continue;
                        }
                    };
                } else {
                    inbox.as_mut().peek().await;
                    continue;
                }
            }

            // ================== work
            work_io.call_again = false;
            if let Err(e) = kernel
                .work(&mut work_io, &mut sio, &mut mio, &mut meta)
                .await
            {
                error!(
                    "{}: Error in work(). Terminating. ({:?})",
                    meta.instance_name().unwrap(),
                    e
                );
                main_inbox
                    .send(FlowgraphMessage::BlockError {
                        block_id,
                        block: Block(Box::new(TypedBlockWrapper {
                            inner: Some(TypedBlock {
                                sio,
                                mio,
                                meta,
                                kernel,
                            }),
                        })),
                    })
                    .await?;
                return Err(e);
            }
            sio.commit();

            futures_lite::future::yield_now().await;
        }

        Ok(())
    }
}

#[async_trait]
impl<T: Kernel + Send + 'static> BlockT for TypedBlockWrapper<T> {
    // ##### Block
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    // ##### META
    fn instance_name(&self) -> Option<&str> {
        self.inner.as_ref().and_then(|i| i.meta.instance_name())
    }
    fn set_instance_name(&mut self, name: &str) {
        if let Some(i) = self.inner.as_mut() {
            i.meta.set_instance_name(name)
        }
    }
    fn type_name(&self) -> &str {
        self.inner.as_ref().map(|i| i.meta.type_name()).unwrap()
    }
    fn is_blocking(&self) -> bool {
        self.inner.as_ref().map(|i| i.meta.is_blocking()).unwrap()
    }

    // ##### KERNEL
    async fn run(
        &mut self,
        block_id: usize,
        main_inbox: Sender<FlowgraphMessage>,
        inbox: Receiver<BlockMessage>,
    ) {
        let block = self.inner.take().unwrap();

        let instance_name = block
            .meta
            .instance_name()
            .unwrap_or("<broken instance name>")
            .to_string();
        if let Err(e) = Self::run_impl(block, block_id, main_inbox, inbox).await {
            error!("{}: Error in Block.run() {:?}", instance_name, e);
        }
    }

    // ##### STREAM IO
    fn set_tag_propagation(
        &mut self,
        f: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
    ) {
        if let Some(i) = self.inner.as_mut() {
            i.sio.set_tag_propagation(f)
        }
    }
    fn stream_inputs(&self) -> &Vec<StreamInput> {
        self.inner.as_ref().map(|i| i.sio.inputs()).unwrap()
    }
    fn stream_input(&self, id: usize) -> &StreamInput {
        self.inner.as_ref().map(|i| i.sio.input_ref(id)).unwrap()
    }
    fn stream_input_name_to_id(&self, name: &str) -> Option<usize> {
        self.inner
            .as_ref()
            .map(|i| i.sio.input_name_to_id(name))
            .unwrap()
    }
    fn stream_outputs(&self) -> &Vec<StreamOutput> {
        self.inner.as_ref().map(|i| i.sio.outputs()).unwrap()
    }
    fn stream_output(&self, id: usize) -> &StreamOutput {
        self.inner.as_ref().map(|i| i.sio.output_ref(id)).unwrap()
    }
    fn stream_output_name_to_id(&self, name: &str) -> Option<usize> {
        self.inner
            .as_ref()
            .map(|i| i.sio.output_name_to_id(name))
            .unwrap()
    }

    // ##### MESSAGE IO
    fn message_input_name_to_id(&self, name: &str) -> Option<usize> {
        self.inner
            .as_ref()
            .map(|i| i.mio.input_name_to_id(name))
            .unwrap()
    }
    fn message_outputs(&self) -> &Vec<MessageOutput> {
        self.inner.as_ref().map(|i| i.mio.outputs()).unwrap()
    }
    fn message_output_name_to_id(&self, name: &str) -> Option<usize> {
        self.inner
            .as_ref()
            .map(|i| i.mio.output_name_to_id(name))
            .unwrap()
    }
}

/// Block
///
/// Generic wrapper around a [`TypedBlock`].
#[derive(Debug)]
pub struct Block(pub(crate) Box<dyn BlockT>);

impl Block {
    /// Create Block
    pub fn new<T: Kernel + Send + 'static>(
        meta: BlockMeta,
        sio: StreamIo,
        mio: MessageIo<T>,
        kernel: T,
    ) -> Block {
        Self(Box::new(TypedBlockWrapper {
            inner: Some(TypedBlock {
                meta,
                sio,
                mio,
                kernel,
            }),
        }))
    }
    /// Create block by wrapping a [`TypedBlock`].
    pub fn from_typed<T: Kernel + Send + 'static>(b: TypedBlock<T>) -> Block {
        Self(Box::new(TypedBlockWrapper { inner: Some(b) }))
    }
    /// Try to cast to a given kernel type
    pub fn kernel<T: Kernel + Send + 'static>(&self) -> Option<&T> {
        self.0
            .as_any()
            .downcast_ref::<TypedBlockWrapper<T>>()
            .and_then(|b| b.inner.as_ref())
            .map(|b| &b.kernel)
    }
    /// Try to mutably cast to a given kernel type
    pub fn kernel_mut<T: Kernel + Send + 'static>(&mut self) -> Option<&T> {
        self.0
            .as_any_mut()
            .downcast_mut::<TypedBlockWrapper<T>>()
            .and_then(|b| b.inner.as_mut())
            .map(|b| &b.kernel)
    }

    // ##### META
    /// Get instance name (see [`BlockMeta::instance_name`])
    pub fn instance_name(&self) -> Option<&str> {
        self.0.instance_name()
    }
    /// Set instance name (see [`BlockMeta::set_instance_name`])
    pub fn set_instance_name(&mut self, name: impl AsRef<str>) {
        self.0.set_instance_name(name.as_ref())
    }
    /// Get type name (see [`BlockMeta::type_name`])
    pub fn type_name(&self) -> &str {
        self.0.type_name()
    }
    /// Is block blocking (see [`BlockMeta::is_blocking`])
    pub fn is_blocking(&self) -> bool {
        self.0.is_blocking()
    }

    pub(crate) async fn run(
        mut self,
        block_id: usize,
        main_inbox: Sender<FlowgraphMessage>,
        inbox: Receiver<BlockMessage>,
    ) {
        self.0.run(block_id, main_inbox, inbox).await
    }

    // ##### STREAM IO
    /// Set tag propagation function
    #[allow(clippy::type_complexity)]
    pub fn set_tag_propagation(
        &mut self,
        f: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
    ) {
        self.0.set_tag_propagation(f);
    }
    /// Get stream input ports
    pub fn stream_inputs(&self) -> &Vec<StreamInput> {
        self.0.stream_inputs()
    }
    /// Get stream input port
    pub fn stream_input(&self, id: usize) -> &StreamInput {
        self.0.stream_input(id)
    }
    /// Map stream input port name ot id
    pub fn stream_input_name_to_id(&self, name: &str) -> Option<usize> {
        self.0.stream_input_name_to_id(name)
    }
    /// Get stream output ports
    pub fn stream_outputs(&self) -> &Vec<StreamOutput> {
        self.0.stream_outputs()
    }
    /// Get stream output port
    pub fn stream_output(&self, id: usize) -> &StreamOutput {
        self.0.stream_output(id)
    }
    /// Map stream output port name to id
    pub fn stream_output_name_to_id(&self, name: &str) -> Option<usize> {
        self.0.stream_output_name_to_id(name)
    }

    // ##### MESSAGE IO
    /// Map message input port name to id
    pub fn message_input_name_to_id(&self, name: &str) -> Option<usize> {
        self.0.message_input_name_to_id(name)
    }
    /// Get message output ports
    pub fn message_outputs(&self) -> &Vec<MessageOutput> {
        self.0.message_outputs()
    }
    /// Map message output port name to id
    pub fn message_output_name_to_id(&self, name: &str) -> Option<usize> {
        self.0.message_output_name_to_id(name)
    }
}

impl<T: Kernel + Send + 'static> fmt::Debug for TypedBlockWrapper<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AsyncBlock")
            .field("type_name", &self.type_name().to_string())
            .finish()
    }
}

impl fmt::Debug for dyn BlockT {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BlockT")
            .field("type_name", &self.type_name().to_string())
            .finish()
    }
}