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
//! Session Listener

use async_trait::async_trait;
use fe2o3_amqp_types::{
    definitions::{self, ConnectionError},
    performatives::{Attach, Begin, Detach, Disposition, End, Flow, Transfer},
    states::SessionState,
};
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;

use crate::{
    connection::AllocSessionError,
    control::{ConnectionControl, SessionControl},
    endpoint::{
        self, IncomingChannel, InputHandle, LinkFlow, OutgoingChannel, OutputHandle, Session,
    },
    link::{LinkFrame, LinkRelay},
    session::{
        self,
        engine::SessionEngine,
        frame::{SessionFrame, SessionIncomingItem, SessionOutgoingItem},
        error::{AllocLinkError, BeginError, Error, SessionInnerError}, SessionHandle, 
        DEFAULT_SESSION_CONTROL_BUFFER_SIZE,
    },
    util::Initialized,
    Payload,
};

use super::{builder::Builder, IncomingSession, ListenerConnectionHandle};

cfg_transaction! {
    use fe2o3_amqp_types::{messaging::Accepted, transaction::TransactionError};
    
    use crate::transaction::{manager::TransactionManager, session::TxnSession, AllocTxnIdError};
}


/// An empty marker trait that acts as a constraint for session engine
pub trait ListenerSessionEndpoint {}

impl ListenerSessionEndpoint for ListenerSession {}

cfg_transaction! {
    impl ListenerSessionEndpoint for TxnSession<ListenerSession> {}
}

type SessionBuilder = crate::session::Builder;

/// Type alias for listener session handle
pub type ListenerSessionHandle = SessionHandle<mpsc::Receiver<Attach>>;

impl ListenerSessionHandle {
    /// Waits for the next incoming link
    pub async fn next_incoming_attach(&mut self) -> Option<Attach> {
        self.link_listener.recv().await
    }
}

pub(crate) async fn allocate_incoming_link(
    control: &mpsc::Sender<SessionControl>,
    link_name: String,
    link_relay: LinkRelay<()>,
    input_handle: InputHandle,
) -> Result<OutputHandle, AllocLinkError> {
    let (responder, resp_rx) = oneshot::channel();

    control
        .send(SessionControl::AllocateIncomingLink {
            link_name,
            link_relay,
            input_handle,
            responder,
        })
        .await
        // The `SendError` could only happen when the receiving half is
        // dropped, meaning the `SessionEngine::event_loop` has stopped.
        // This would also mean the `Session` is Unmapped, and thus it
        // may be treated as illegal state
        .map_err(|_| AllocLinkError::IllegalSessionState)?;
    resp_rx
        .await
        // The error could only occur when the sending half is dropped,
        // indicating the `SessionEngine::even_loop` has stopped or
        // unmapped. Thus it could be considered as illegal state
        .map_err(|_| AllocLinkError::IllegalSessionState)?
}

/// An acceptor for incoming session
///
/// This is simply a wrapper around the session builder since there is not
/// much else that you can configure. The wrapper is here for consistency in terms of API desgin.
///
/// # Accepts incoming session with default configuration
///
/// ```rust,ignore
/// use crate::acceptor::SessionAcceptor;
///
/// let mut connection: ListenerConnectionHandle = connection_acceptor.accept(stream).await.unwrap();
/// let session_acceptor = SessionAcceptor::new();
/// let session = session_acceptor.accept(&mut connection).await.unwrap();
/// ```
///
/// ## Default configuration
///
/// The default configuration is the same as that of `crate::Session`.
///
/// | Field | Default Value |
/// |-------|---------------|
/// |`next_outgoing_id`| 0 |
/// |`incoming_window`| [`crate::session::DEFAULT_WINDOW`] |
/// |`outgoing_window`| [`crate::session::DEFAULT_WINDOW`] |
/// |`handle_max`| `u32::MAX` |
/// |`offered_capabilities` | `None` |
/// |`desired_capabilities`| `None` |
/// |`Properties`| `None` |
///
/// # Customize the acceptor
///
/// The acceptor can be customized using the builder pattern or by modifying the field
/// directly after the acceptor is built.
///
/// ```rust
/// use crate::acceptor::SessionAcceptor;
///
/// let session_acceptor = SessionAcceptor::builder()
///     .handle_max(16)
///     .build();
/// ```
#[derive(Debug)]
pub struct SessionAcceptor(pub SessionBuilder);

impl Default for SessionAcceptor {
    fn default() -> Self {
        Self::builder().build()
    }
}

impl SessionAcceptor {
    /// Creates a new acceptor with default configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new builder for [`SessionAcceptor`]
    pub fn builder() -> Builder<Self, Initialized> {
        Builder::<Self, Initialized>::new()
    }

    cfg_not_transaction! {
        #[allow(clippy::too_many_arguments)]
        async fn launch_listener_session_engine<R>(
            &self,
            listener_session: ListenerSession,
            _control_link_outgoing: &mpsc::Sender<LinkFrame>,
            connection: &crate::connection::ConnectionHandle<R>,
            _session_control_tx: &mpsc::Sender<SessionControl>,
            session_control_rx: mpsc::Receiver<SessionControl>,
            incoming: mpsc::Receiver<SessionFrame>,
            outgoing_link_frames: mpsc::Receiver<LinkFrame>,
        ) -> Result<(JoinHandle<()>, oneshot::Receiver<Result<(), Error>>), BeginError> {
            let engine = SessionEngine::begin_listener_session(
                connection.control.clone(),
                listener_session,
                session_control_rx,
                incoming,
                connection.outgoing.clone(),
                outgoing_link_frames,
            )
            .await?;
            Ok(engine.spawn())
        }
    }

    cfg_transaction! {
        #[allow(clippy::too_many_arguments)]
        async fn launch_listener_session_engine<R>(
            &self,
            listener_session: ListenerSession,
            control_link_outgoing: &mpsc::Sender<LinkFrame>,
            connection: &crate::connection::ConnectionHandle<R>,
            session_control_tx: &mpsc::Sender<SessionControl>,
            session_control_rx: mpsc::Receiver<SessionControl>,
            incoming: mpsc::Receiver<SessionFrame>,
            outgoing_link_frames: mpsc::Receiver<LinkFrame>,
        ) -> Result<(JoinHandle<()>, oneshot::Receiver<Result<(), Error>>), BeginError> {
            match self.0.control_link_acceptor.clone() {
                Some(control_link_acceptor) => {
                    let txn_manager =
                        TransactionManager::new(control_link_outgoing.clone(), control_link_acceptor);
                    let listener_session = TxnSession {
                        control: session_control_tx.clone(),
                        session: listener_session,
                        txn_manager,
                    };
    
                    let engine = SessionEngine::begin_listener_session(
                        connection.control.clone(),
                        listener_session,
                        session_control_rx,
                        incoming,
                        connection.outgoing.clone(),
                        outgoing_link_frames,
                    )
                    .await?;
                    Ok(engine.spawn())
                }
                None => {
                    let engine = SessionEngine::begin_listener_session(
                        connection.control.clone(),
                        listener_session,
                        session_control_rx,
                        incoming,
                        connection.outgoing.clone(),
                        outgoing_link_frames,
                    )
                    .await?;
                    Ok(engine.spawn())
                }
            }
        }
    }

    /// Accept an incoming session
    pub async fn accept_incoming_session(
        &self,
        incoming_session: IncomingSession,
        connection: &mut ListenerConnectionHandle,
    ) -> Result<ListenerSessionHandle, BeginError> {
        let local_state = SessionState::Unmapped;
        let (session_control_tx, session_control_rx) =
            mpsc::channel::<SessionControl>(DEFAULT_SESSION_CONTROL_BUFFER_SIZE);
        let (incoming_tx, incoming_rx) = mpsc::channel(self.0.buffer_size);
        let (outgoing_tx, outgoing_rx) = mpsc::channel(self.0.buffer_size);
        let (link_listener_tx, link_listener_rx) = mpsc::channel(self.0.buffer_size);

        // create session in connection::Engine
        let outgoing_channel = match connection.allocate_session(incoming_tx).await {
            Ok(channel) => channel,
            Err(error) => match error {
                AllocSessionError::IllegalState => return Err(BeginError::IllegalConnectionState),
                AllocSessionError::ChannelMaxReached => {
                    // A peer that receives a channel number outside the supported range MUST close the connection
                    // with the framing-error error-code
                    let error = definitions::Error::new(
                        ConnectionError::FramingError,
                        "Exceeding channel-max".to_string(),
                        None,
                    );
                    connection
                        .control
                        .send(ConnectionControl::Close(Some(error)))
                        .await
                        .map_err(|_| BeginError::IllegalConnectionState)?;

                    return Err(BeginError::LocalChannelMaxReached);
                }
            },
        };
        let mut session = self.0.clone().into_session(outgoing_channel, local_state);
        session.on_incoming_begin(
            IncomingChannel(incoming_session.channel),
            incoming_session.begin,
        )?;

        let listener_session = ListenerSession {
            session,
            link_listener: link_listener_tx,
        };

        let (engine_handle, outcome) = self
            .launch_listener_session_engine(
                listener_session,
                &outgoing_tx,
                connection,
                &session_control_tx,
                session_control_rx,
                incoming_rx,
                outgoing_rx,
            )
            .await?;

        let handle = SessionHandle {
            is_ended: false,
            control: session_control_tx,
            engine_handle,
            outcome,
            outgoing: outgoing_tx,
            link_listener: link_listener_rx,
        };
        Ok(handle)
    }

    /// Waits for incoming session'e Begin performative and then accepts an incoming session
    #[cfg_attr(feature = "tracing", tracing::instrument)]
    pub async fn accept(
        &self,
        connection: &mut ListenerConnectionHandle,
    ) -> Result<ListenerSessionHandle, BeginError> {
        let incoming_session = connection
            .next_incoming_session()
            .await
            .ok_or(BeginError::IllegalConnectionState)?;
        self.accept_incoming_session(incoming_session, connection)
            .await
    }
}

impl<S> SessionEngine<S>
where
    S: ListenerSessionEndpoint + endpoint::SessionEndpoint,
    BeginError: From<S::BeginError>,
{
    pub async fn begin_listener_session(
        conn_control: mpsc::Sender<ConnectionControl>,
        session: S,
        control: mpsc::Receiver<SessionControl>,
        incoming: mpsc::Receiver<SessionIncomingItem>,
        outgoing: mpsc::Sender<SessionFrame>,
        outgoing_link_frames: mpsc::Receiver<LinkFrame>,
    ) -> Result<Self, BeginError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("Instantiating session engine");
        #[cfg(feature = "log")]
        log::trace!("Instantiating session engine");
        let mut engine = Self {
            conn_control,
            session,
            control,
            incoming,
            outgoing,
            outgoing_link_frames,
        };

        // send a begin
        engine.session.send_begin(&engine.outgoing).await?;
        Ok(engine)
    }
}

/// A session on the listener side
#[derive(Debug)]
pub struct ListenerSession {
    pub(crate) session: session::Session,
    pub(crate) link_listener: mpsc::Sender<Attach>,
}

impl endpoint::SessionExt for ListenerSession {}

#[async_trait]
impl endpoint::Session for ListenerSession {
    type AllocError = <session::Session as endpoint::Session>::AllocError;
    type BeginError = <session::Session as endpoint::Session>::BeginError;
    type EndError = <session::Session as endpoint::Session>::EndError;
    type Error = <session::Session as endpoint::Session>::Error;

    type State = <session::Session as endpoint::Session>::State;

    fn local_state(&self) -> &Self::State {
        self.session.local_state()
    }

    fn local_state_mut(&mut self) -> &mut Self::State {
        self.session.local_state_mut()
    }

    fn outgoing_channel(&self) -> OutgoingChannel {
        self.session.outgoing_channel()
    }

    fn allocate_link(
        &mut self,
        link_name: String,
        link_handle: Option<LinkRelay<()>>,
    ) -> Result<OutputHandle, Self::AllocError> {
        self.session.allocate_link(link_name, link_handle)
    }

    fn allocate_incoming_link(
        &mut self,
        link_name: String,
        link_handle: LinkRelay<()>,
        input_handle: InputHandle,
    ) -> Result<OutputHandle, Self::AllocError> {
        self.session
            .allocate_incoming_link(link_name, link_handle, input_handle)
    }

    fn deallocate_link(&mut self, output_handle: OutputHandle) {
        self.session.deallocate_link(output_handle)
    }

    fn on_incoming_begin(
        &mut self,
        channel: IncomingChannel,
        begin: Begin,
    ) -> Result<(), Self::BeginError> {
        self.session.on_incoming_begin(channel, begin)
    }

    async fn on_incoming_attach(&mut self, attach: Attach) -> Result<(), Self::Error> {
        match self.session.link_by_name.get_mut(&attach.name) {
            Some(link) => match link.take() {
                Some(mut relay) => {
                    // Only Sender need to update the receiver settle mode
                    // because the sender needs to echo a disposition if
                    // rcv-settle-mode is 1
                    if let LinkRelay::Sender {
                        receiver_settle_mode,
                        ..
                    } = &mut relay
                    {
                        *receiver_settle_mode = attach.rcv_settle_mode.clone();
                    }

                    let input_handle = attach.handle.clone().into(); // handle is just a wrapper around u32
                    relay
                        .send(LinkFrame::Attach(attach))
                        .await
                        .map_err(|_| SessionInnerError::UnattachedHandle)?;
                    self.session
                        .link_by_input_handle
                        .insert(input_handle, relay);
                    Ok(())
                }
                None => {
                    self.link_listener.send(attach).await.map_err(|_| {
                        // SessionHandle must have been dropped, then treat it as if the acceptor doesn't exist
                        SessionInnerError::HandleInUse
                    })
                }
            },
            None => {
                // If no such terminus exists, the application MAY
                // choose to create one using the properties supplied by the
                // remote link endpoint. The link endpoint is then mapped
                // to an unused handle, and an attach frame is issued carrying
                // the state of the newly created endpoint.

                self.link_listener.send(attach).await.map_err(|_| {
                    // SessionHandle must have been dropped, then treat it as if the acceptor doesn't exist
                    SessionInnerError::UnattachedHandle
                })
            }
        }
    }

    async fn on_incoming_flow(
        &mut self,
        flow: Flow,
    ) -> Result<Option<SessionOutgoingItem>, Self::Error> {
        self.session.on_incoming_flow(flow).await
    }

    async fn on_incoming_transfer(
        &mut self,
        transfer: Transfer,
        payload: Payload,
    ) -> Result<Option<Disposition>, Self::Error> {
        self.session.on_incoming_transfer(transfer, payload).await
    }

    fn on_incoming_disposition(
        &mut self,
        disposition: Disposition,
    ) -> Result<Option<Vec<Disposition>>, Self::Error> {
        self.session.on_incoming_disposition(disposition)
    }

    async fn on_incoming_detach(&mut self, detach: Detach) -> Result<(), Self::Error> {
        self.session.on_incoming_detach(detach).await
    }

    fn on_incoming_end(
        &mut self,
        channel: IncomingChannel,
        end: End,
    ) -> Result<(), Self::EndError> {
        self.session.on_incoming_end(channel, end)
    }

    // Handling SessionFrames
    async fn send_begin(
        &mut self,
        writer: &mpsc::Sender<SessionFrame>,
    ) -> Result<(), Self::BeginError> {
        self.session.send_begin(writer).await
    }

    async fn send_end(
        &mut self,
        writer: &mpsc::Sender<SessionFrame>,
        error: Option<definitions::Error>,
    ) -> Result<(), Self::EndError> {
        self.session.send_end(writer, error).await
    }

    // Intercepting LinkFrames
    fn on_outgoing_attach(&mut self, attach: Attach) -> Result<SessionFrame, Self::Error> {
        self.session.on_outgoing_attach(attach)
    }

    fn on_outgoing_flow(&mut self, flow: LinkFlow) -> Result<SessionFrame, Self::Error> {
        self.session.on_outgoing_flow(flow)
    }

    fn on_outgoing_transfer(
        &mut self,
        input_handle: InputHandle,
        transfer: Transfer,
        payload: Payload,
    ) -> Result<Option<SessionOutgoingItem>, Self::Error> {
        self.session
            .on_outgoing_transfer(input_handle, transfer, payload)
    }

    fn on_outgoing_disposition(
        &mut self,
        disposition: Disposition,
    ) -> Result<SessionFrame, Self::Error> {
        self.session.on_outgoing_disposition(disposition)
    }

    fn on_outgoing_detach(&mut self, detach: Detach) -> SessionFrame {
        self.session.on_outgoing_detach(detach)
    }
}

cfg_transaction! {
    impl endpoint::HandleDeclare for ListenerSession {
        // This should be unreachable, but an error is probably a better way
        fn allocate_transaction_id(
            &mut self,
        ) -> Result<fe2o3_amqp_types::transaction::TransactionId, AllocTxnIdError> {
            Err(AllocTxnIdError::NotImplemented)
        }
    }
    
    #[async_trait]
    impl endpoint::HandleDischarge for ListenerSession {
        async fn commit_transaction(
            &mut self,
            _txn_id: fe2o3_amqp_types::transaction::TransactionId,
        ) -> Result<Result<Accepted, TransactionError>, Self::Error> {
            // FIXME: This should be impossible
            Ok(Err(TransactionError::UnknownId))
        }
    
        fn rollback_transaction(
            &mut self,
            _txn_id: fe2o3_amqp_types::transaction::TransactionId,
        ) -> Result<Result<Accepted, TransactionError>, Self::Error> {
            // FIXME: This should be impossible
            Ok(Err(TransactionError::UnknownId))
        }
    }
}