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
use std::ops::Deref;

use crate::error::StomperError;

string_id_class!(SubscriptionId);

string_id_class!(DestinationId);

string_id_class!(MessageId);

pub struct InboundMessage {
    pub sender_message_id: Option<MessageId>,
    pub body: Vec<u8>,
}

#[derive(Clone, Debug)]
pub struct OutboundMessage {
    pub destination: DestinationId,
    pub message_id: MessageId,
    pub body: Vec<u8>,
}

pub trait Subscriber: Send + Sync + std::fmt::Debug {
    fn subscribe_callback(
        &self,
        destination: DestinationId,
        suscriber_sub_id: Option<SubscriptionId>,
        result: Result<SubscriptionId, StomperError>,
    );

    fn unsubscribe_callback(
        &self,
        subscriber_sub_id: Option<SubscriptionId>,
        result: Result<SubscriptionId, StomperError>,
    );

    fn send(
        &self,
        subscription: SubscriptionId,
        suscriber_sub_id: Option<SubscriptionId>,
        message: OutboundMessage,
    ) -> Result<(), StomperError>;
}

pub trait Sender: Send + Sync + std::fmt::Debug {
    fn send_callback(
        &self,
        sender_message_id: Option<MessageId>,
        result: Result<MessageId, StomperError>,
    );
}

/// A destinations is a identifiable resource that clients can subscribe to, and send messages to
pub trait Destination: Send + Clone {
    type Client: crate::client::Client;

    fn subscribe<S: Subscriber + 'static, D: Deref<Target = S> + Send + Clone + 'static>(
        &self,
        sender_subscription_id: Option<SubscriptionId>,
        subscriber: D,
        client: &Self::Client,
    );

    fn unsubscribe<S: Subscriber + 'static, D: Deref<Target = S> + Send + Clone + 'static>(
        &self,
        sub_id: SubscriptionId,
        subscriber: D,
        client: &Self::Client,
    );

    fn send<S: Sender + 'static, D: Deref<Target = S> + Send + Clone + 'static>(
        &self,
        message: InboundMessage,
        sender: D,
        client: &Self::Client,
    );

    fn close(&self);
}

pub trait Destinations: Send + Clone {
    type Client: crate::client::Client;

    fn subscribe<S: Subscriber + 'static, D: Deref<Target = S> + Clone + Send + 'static>(
        &self,
        destination: DestinationId,
        sender_subscription_id: Option<SubscriptionId>,
        subscriber: D,
        client: &Self::Client,
    );

    fn unsubscribe<S: Subscriber + 'static, D: Deref<Target = S> + Clone + Send + 'static>(
        &self,
        destination: DestinationId,
        destination_subscription_id: SubscriptionId,
        subscriber: D,
        client: &Self::Client,
    );

    /// Send a message to this destination and, by implication, all clients subscribed to it
    fn send<S: Sender + 'static, D: Deref<Target = S> + Clone + Send + 'static>(
        &self,
        destination: DestinationId,
        message: InboundMessage,
        sender: D,
        client: &Self::Client,
    );
}