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
use std::marker::PhantomData;

use dfir_rs::bytes::Bytes;
use serde::de::DeserializeOwned;
use serde::Serialize;

use super::{Location, LocationId, NoTick};
use crate::builder::FlowState;
use crate::ir::{HydroNode, HydroSource};
use crate::staging_util::Invariant;
use crate::{Stream, Unbounded};

pub struct ExternalBytesPort {
    #[cfg_attr(
        not(feature = "build"),
        expect(unused, reason = "unused without feature")
    )]
    pub(crate) process_id: usize,
    #[cfg_attr(
        not(feature = "build"),
        expect(unused, reason = "unused without feature")
    )]
    pub(crate) port_id: usize,
}

pub struct ExternalBincodeSink<T: Serialize> {
    #[cfg_attr(
        not(feature = "build"),
        expect(unused, reason = "unused without feature")
    )]
    pub(crate) process_id: usize,
    #[cfg_attr(
        not(feature = "build"),
        expect(unused, reason = "unused without feature")
    )]
    pub(crate) port_id: usize,
    pub(crate) _phantom: PhantomData<T>,
}

pub struct ExternalBincodeStream<T: DeserializeOwned> {
    #[cfg_attr(
        not(feature = "build"),
        expect(unused, reason = "unused without feature")
    )]
    pub(crate) process_id: usize,
    #[cfg_attr(
        not(feature = "build"),
        expect(unused, reason = "unused without feature")
    )]
    pub(crate) port_id: usize,
    pub(crate) _phantom: PhantomData<T>,
}

pub struct ExternalProcess<'a, P> {
    pub(crate) id: usize,

    pub(crate) flow_state: FlowState,

    pub(crate) _phantom: Invariant<'a, P>,
}

impl<P> Clone for ExternalProcess<'_, P> {
    fn clone(&self) -> Self {
        ExternalProcess {
            id: self.id,
            flow_state: self.flow_state.clone(),
            _phantom: PhantomData,
        }
    }
}

impl<'a, P> Location<'a> for ExternalProcess<'a, P> {
    type Root = Self;

    fn root(&self) -> Self::Root {
        self.clone()
    }

    fn id(&self) -> LocationId {
        LocationId::ExternalProcess(self.id)
    }

    fn flow_state(&self) -> &FlowState {
        &self.flow_state
    }

    fn is_top_level() -> bool {
        true
    }
}

impl<'a, P> ExternalProcess<'a, P> {
    pub fn source_external_bytes<L: Location<'a> + NoTick>(
        &self,
        to: &L,
    ) -> (ExternalBytesPort, Stream<Bytes, L, Unbounded>) {
        let next_external_port_id = {
            let mut flow_state = self.flow_state.borrow_mut();
            let id = flow_state.next_external_out;
            flow_state.next_external_out += 1;
            id
        };

        let deser_expr: syn::Expr = syn::parse_quote!(|b| b.unwrap().freeze());

        (
            ExternalBytesPort {
                process_id: self.id,
                port_id: next_external_port_id,
            },
            Stream::new(
                to.clone(),
                HydroNode::Persist(Box::new(HydroNode::Network {
                    from_location: LocationId::ExternalProcess(self.id),
                    from_key: Some(next_external_port_id),
                    to_location: to.id(),
                    to_key: None,
                    serialize_fn: None,
                    instantiate_fn: crate::ir::DebugInstantiate::Building(),
                    deserialize_fn: Some(deser_expr.into()),
                    input: Box::new(HydroNode::Source {
                        source: HydroSource::ExternalNetwork(),
                        location_kind: LocationId::ExternalProcess(self.id),
                    }),
                })),
            ),
        )
    }

    pub fn source_external_bincode<L: Location<'a> + NoTick, T: Serialize + DeserializeOwned>(
        &self,
        to: &L,
    ) -> (ExternalBincodeSink<T>, Stream<T, L, Unbounded>) {
        let next_external_port_id = {
            let mut flow_state = self.flow_state.borrow_mut();
            let id = flow_state.next_external_out;
            flow_state.next_external_out += 1;
            id
        };

        (
            ExternalBincodeSink {
                process_id: self.id,
                port_id: next_external_port_id,
                _phantom: PhantomData,
            },
            Stream::new(
                to.clone(),
                HydroNode::Persist(Box::new(HydroNode::Network {
                    from_location: LocationId::ExternalProcess(self.id),
                    from_key: Some(next_external_port_id),
                    to_location: to.id(),
                    to_key: None,
                    serialize_fn: None,
                    instantiate_fn: crate::ir::DebugInstantiate::Building(),
                    deserialize_fn: Some(crate::stream::deserialize_bincode::<T>(None).into()),
                    input: Box::new(HydroNode::Source {
                        source: HydroSource::ExternalNetwork(),
                        location_kind: LocationId::ExternalProcess(self.id),
                    }),
                })),
            ),
        )
    }
}