dope-manifold 0.35.0

Owned application and service composition for dope
Documentation
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
use std::marker;

use dope_core::{
    driver::{
        self,
        route::{Identity, table::conn::Capacity},
        schedule,
        schedule::{app::Permit, clock},
    },
    io::socket,
};
use dope_net::{
    link::{
        egress,
        pool::{change::opening, recv::input},
    },
    wire::{self, domain, open, receive::Chunk, terminal},
};
use drain::Mode as _;
use o3::{
    base::{Call, cell::region, time::Positive},
    mem::rc,
};

use crate::{
    app::{
        Park, creation,
        receive::{self, limits},
    },
    client::{
        self,
        app::{self, Resumed, defer::Controlled, request::Turn, resume},
        attempt, close, conn,
        session::{
            self,
            app::{drain, ingress, parser, request, retire},
            codec::Codec,
            ingress::Source,
            policy::{Compatible, end, start},
            response::flow::Policy as _,
            turns,
        },
    },
};

pub struct State<'w, 'd: 'w, ID: Identity, N: session::Session<'d, ID>, W: 'd> {
    pub(super) session: N,
    pub(super) ingress:
        ingress::Ingress<'d, <<N as session::Session<'d, ID>>::Codec as Codec>::Staging>,
    wire: marker::PhantomData<(&'w (), *const W)>,
}

impl<'w, 'd: 'w, ID: Identity, N: session::Session<'d, ID>, W: wire::Spec + 'd>
    State<'w, 'd, ID, N, W>
{
    pub fn try_new<S>(
        setup: session::Setup<'d, N, S>,
        limit: Capacity,
        driver: &mut driver::Context<'_, 'd>,
    ) -> Result<Self, creation::Error>
    where
        S: Source<'d>,
    {
        let (inner, limits) = setup.into_parts();
        let plan = Codec::staging(inner.codec());
        let ingress = ingress::Ingress::try_new(limits, limit, plan, driver)?;
        Ok(Self {
            session: inner,
            ingress,
            wire: marker::PhantomData,
        })
    }

    pub const fn session(&self) -> &N {
        &self.session
    }

    pub(crate) fn session_mut(&mut self) -> &mut N {
        &mut self.session
    }
}
impl<'w, 'd: 'w, ID: Identity, N, W> app::Application<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID>,
    W: input::Execution + 'd,
{
    type Conn = session::Connection<'w, 'd, ID, N, W>;
    type Connect = N::Connect;
    type Deferred = Controlled;
    type Failure = N::Failure;
    type Wire = W;
    type Send = N::Send;
    type Input = receive::Owned<N::Receipt>;
    type Requests = N::Requests;

    fn try_conn(&self) -> Result<Self::Conn, rc::Error> {
        session::Connection::try_new(&self.session, self.ingress.pool(), *self.ingress.budget())
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Receive<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Retirement<'d, ID> + session::Scheduling<'d, ID>,
    W: input::Execution + 'd,
{
    type Continuation = resume::Retained;

    fn receipt(
        &self,
        id: conn::Id<'d, ID>,
        conn: &Self::Conn,
        region: &region::Token<region::Identity<'d>>,
    ) -> N::Receipt {
        self.session.receipt(id, &conn.conn_state, region)
    }

    fn withdraw(&mut self, id: conn::Id<'d, ID>, conn: &mut Self::Conn) -> app::Fence {
        self.session.withdraw(id, &mut conn.conn_state)
    }

    fn quiesce<'a>(
        &self,
        barrier: app::Barrier<'a, 'd, ID>,
        driver: &mut driver::Context<'_, 'd>,
    ) -> app::Drain<'a, 'd, ID> {
        N::Drain::select(&self.session, barrier, driver.region_token())
    }

    fn eof<'e, O>(
        &mut self,
        entry: conn::Ctx<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        _: receive::End,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        driver: &mut driver::Context<'_, 'd>,
    ) -> app::Eof {
        let (id, conn, close, ready) = entry.into_io();
        if !matches!(
            self.session.mode(id, &conn.conn_state),
            session::ReceiveMode::Duplex
        ) {
            return app::Eof::Close;
        }
        self.session.eof(&mut turns::Context::new(
            id,
            &mut conn.conn_state,
            egress,
            ready,
            driver.reborrow(),
            close,
        ))
    }

    fn fence<O>(
        &self,
        conn: conn::Ref<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        driver: &mut driver::Context<'_, 'd>,
    ) -> app::Fence {
        self.session
            .fence(conn.id(), &conn.state().conn_state, driver.region_token())
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Resumable<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Retirement<'d, ID> + session::Scheduling<'d, ID>,
    W: input::Execution + 'd,
{
    fn resident(&self) -> domain::Resident {
        self.ingress.budget().available().into()
    }

    fn wake(&mut self, conn: &mut Self::Conn) -> app::Wake {
        conn.continuation.wake()
    }

    fn inbound<O>(
        &self,
        conn: conn::Ref<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        base: Positive,
        driver: &mut driver::Context<'_, 'd>,
    ) -> app::Inbound {
        self.session.inbound(
            conn.id(),
            &conn.state().conn_state,
            base,
            driver.region_token(),
        )
    }

    fn resume<'e, 'turn, O>(
        &mut self,
        permit: Permit<'turn, 'd>,
        conn: conn::Ctx<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        driver: &mut driver::Context<'_, 'd>,
    ) -> <Self::Continuation as resume::Mode>::Outcome {
        let (id, conn, close, work, ready) = conn.into_parts();
        let session::Connection {
            ingress: input,
            conn_state: state,
            continuation: flow,
            ..
        } = conn;
        let mut ctx = turns::Context::new(id, state, egress, ready, driver.reborrow(), close);
        let parser = parser::Parser::new(&mut self.session, input, self.ingress.budget(), &mut ctx);
        let next = parser.run_admitted(flow, work, permit);
        next.finish(&mut ctx)
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Owned<'w, 'd, ID, N::Receipt> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Retirement<'d, ID> + session::Scheduling<'d, ID>,
    W: input::Execution + 'd,
{
    const RETENTION: limits::Retention = limits::Retention::new(1, 0);

    fn chunk<'e, O>(
        &mut self,
        conn: conn::Ctx<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        chunk: Chunk<'w, 'd, Self::Wire>,
        receipt: N::Receipt,
        driver: &mut driver::Context<'_, 'd>,
    ) -> <Self::Continuation as resume::Mode>::Outcome {
        let (id, conn, close, work, ready) = conn.into_parts();
        let session::Connection {
            ingress: input,
            conn_state: state,
            continuation: flow,
            ..
        } = conn;
        let has_prefix = !input.is_empty();
        if input.try_push(chunk).is_err() {
            return Resumed::Complete(app::ChunkOutcome::failed(receive::Failure::Contract));
        }
        let mut ctx = turns::Context::new(id, state, egress, ready, driver.reborrow(), close);
        self.session.received(receipt, has_prefix, &mut ctx);
        let next = parser::Parser::new(&mut self.session, input, self.ingress.budget(), &mut ctx)
            .run_retained(flow, work);
        next.finish(&mut ctx)
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Lifecycle<'w, 'd, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID>
        + session::Output<'d, ID>
        + session::Retirement<'d, ID>
        + session::Scheduling<'d, ID>
        + Compatible<'w, 'd, W, ID>,
    W: input::Execution + 'd,
{
    fn take_close(&self, conn: conn::Id<'d, ID>) -> Option<close::Directive> {
        self.session.take_close(conn)
    }

    fn wire_fail<'e, O>(
        &mut self,
        conn: conn::Io<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        _: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        cause: terminal::Error<'w, Self::Wire>,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        end::Sink::fail(
            self.session.terminal(),
            conn.id(),
            cause,
            driver.region_token(),
        );
    }

    fn io_fail<'e, O>(
        &mut self,
        conn: conn::Io<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        error: dope_core::Errno,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        let (id, conn, close, ready) = conn.into_parts();
        let mut ctx = turns::Context::new(
            id,
            &mut conn.conn_state,
            egress,
            ready,
            driver.reborrow(),
            close,
        );
        session::Retirement::io_fail(&mut self.session, error, &mut ctx);
    }

    fn prepare<'e, O>(
        &mut self,
        entry: conn::Ctx<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        queued: bool,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        let Some(flush) = N::flush() else {
            return;
        };
        let (id, conn, close, ready) = entry.into_io();
        let input = conn.input(queued);
        flush.call((
            &mut self.session,
            &mut turns::Context::new(
                id,
                &mut conn.conn_state,
                egress,
                ready,
                driver.reborrow(),
                close,
            ),
            input,
        ));
    }

    fn sent(
        &mut self,
        conn: conn::Id<'d, ID>,
        pending: egress::Pending,
        token: &mut region::Token<region::Identity<'d, 1>>,
    ) {
        N::sent().call((&mut self.session, conn, pending, token));
    }

    fn cancel(&mut self, payload: Self::Send, token: &mut region::Token<region::Identity<'d, 1>>) {
        N::cancel().call((&mut self.session, payload, token));
    }

    fn close<'e, O>(
        &mut self,
        conn: conn::Ctx<'_, 'd, ID, Self::Wire, Self::Conn, O>,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        reason: close::Reason,
        driver: &mut driver::Context<'_, 'd>,
    ) -> app::CloseOutcome {
        retire::run(self, conn, egress, reason, driver)
    }

    fn drained(
        &mut self,
        proof: conn::Retired<'d, ID, Self>,
        conn: &mut Self::Conn,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        self.session.disconnect(turns::Retired::new(
            proof,
            &mut conn.conn_state,
            driver.reborrow(),
        ));
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Opened<'w, 'd, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + Compatible<'w, 'd, W, ID>,
    W: input::Execution + 'd,
{
    fn open(
        &mut self,
        key: attempt::Completion<'_, 'd, ID>,
        cause: open::Error<'w, W>,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        start::Sink::fail(self.session.open(), key, cause, driver);
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Connected<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Connected<'d, ID>,
    W: input::Execution + 'd,
{
    type Activation = app::admit::Registered;

    fn connected<'e, O>(
        &mut self,
        key: attempt::Completion<'_, 'd, ID>,
        peer: socket::Addr,
        entry: conn::Established<'_, 'd, ID, W, Self::Conn, O>,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        let (lifetime, conn, close, ready) = entry.into_io();
        let state = conn.activate(self.session.codec());
        self.session.connect(
            key,
            peer,
            turns::Startup {
                lifetime,
                state,
                sink: egress,
                ready,
                driver: driver.reborrow(),
                close,
            },
        );
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Peer<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Peer<'d, ID>,
    W: input::Execution + 'd,
{
    type Addr = N::Addr;

    fn connected<'e, O>(
        &mut self,
        peer: Self::Addr,
        entry: conn::Ctx<'_, 'd, ID, W, Self::Conn, O>,
        egress: egress::Queue<'_, 'e, 'd, { client::IOV_CAP }, Self::Send>,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        let (id, conn, close, ready) = entry.into_io();
        let state = conn.activate(self.session.codec());
        self.session.connect(
            peer,
            &mut turns::Context::new(id, state, egress, ready, driver.reborrow(), close),
        );
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Exhausted<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Exhausted<'d, ID>,
    W: input::Execution + 'd,
{
    fn exhaust(
        &mut self,
        key: attempt::Completion<'_, 'd, ID>,
        cause: opening::Exhaustion,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        self.session.exhaust(key, cause, driver);
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Failed<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Failed<'d, ID> + session::Exhausted<'d, ID>,
    W: input::Execution + 'd,
{
    fn failed(
        &mut self,
        key: attempt::Completion<'_, 'd, ID>,
        cause: attempt::ConnectFault,
        driver: &mut driver::Context<'_, 'd>,
    ) {
        self.session.failed(key, cause, driver);
    }
}

impl<'w, 'd: 'w, ID: Identity, N: request::Source<'d, ID>, W: input::Execution + 'd>
    app::Requests<'d, ID> for State<'w, 'd, ID, N, W>
{
    fn drain<'e>(&self, turn: &mut Turn<'_, 'e, 'd, Self::Conn, Self::Send, ID>) {
        <N as request::Source<'d, ID>>::drain(&self.session, turn);
    }
}

impl<'w, 'd: 'w, ID: Identity, N, W> app::Scheduling<'d, ID> for State<'w, 'd, ID, N, W>
where
    N: session::Parser<'d, ID> + session::Scheduling<'d, ID>,
    W: input::Execution + 'd,
{
    fn park() -> Option<impl for<'a, 'b, 'c, 'e> Call<(&'a mut Self, &'b mut Park<'c, 'e, 'd>)>> {
        let call = N::park()?;
        Some(move |(this, park): (&mut Self, &mut Park<'_, '_, 'd>)| {
            call.call((&mut this.session, park));
        })
    }

    fn shutdown() -> Option<impl for<'a> Call<&'a mut Self>> {
        let call = N::shutdown()?;
        Some(move |this: &mut Self| call.call(&mut this.session))
    }

    fn progress(
        &self,
        region: &region::Token<region::Identity<'d>>,
        clock: clock::Clock<'_, 'd>,
    ) -> schedule::Progress<'d> {
        self.session.progress(region, clock)
    }
}