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
//! # Services
//!
//! Interlink uses services to represent asynchronous tasks that can hold their
//! own state and communicate between each-other using messages and handlers.
//!
//! You can derive the service trait using its derive macro or implement it
//! manually to get access to the [`Service::started`] and [`Service::stopping`]
//! functions.
//!
//! With derive macro:
//! ```
//! use interlink::prelude::*;
//!
//! #[derive(Service)]
//! struct MyService {
//!     value: String,
//! }
//! ```
//!
//! Without derive macro. Both the started and stopping functions are optional
//! ```
//! use interlink::prelude::*;
//!
//! struct MyService {
//!     value: String,
//! }
//!
//! impl Service for MyService {
//!     fn started(&mut self, ctx: &mut ServiceContext<Self>) {
//!         println!("My service is started")
//!     }
//! }
//! ```
//!
//! Then you can start your service using the [`Service::start`] function which will
//! start the service in a tokio task and return you a [`Link`] to the service. Alternatively
//! if you need access to the service context while creating your service you can use the
//! [`Service::create`] function which provides the context.
//!
//! See [`Link`] for what to do from here

use std::time::Duration;

use crate::envelope::{ServiceAction, ServiceMessage};
use crate::link::Link;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio::time::sleep;

/// Trait implemented by structures that can be spawned as
/// services and used by the app
#[allow(unused_variables)]
pub trait Service: Sized + Send + 'static {
    /// Handler called before the service starts processing messages
    ///
    /// `ctx` The service context
    fn started(&mut self, ctx: &mut ServiceContext<Self>) {}

    /// Start an already created service and provides a link for
    /// communicating with the service
    ///
    /// ```
    /// use interlink::prelude::*;
    ///
    /// #[derive(Service)]
    /// struct MyService;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     // Create the service
    ///     let service: MyService = MyService {};
    ///     // Start the service and obtain a link to it
    ///     let addr: Link<MyService> = service.start();
    /// }
    /// ```
    fn start(self) -> Link<Self> {
        let ctx = ServiceContext::new();
        let link = ctx.link();
        ctx.spawn(self);
        link
    }

    /// Alternative way of creating a service where the service may
    /// rely on the context an example of this is an associated service
    /// which requires a link to the service but is also stored on the
    /// service struct
    ///
    /// ```
    /// use interlink::prelude::*;
    ///
    /// #[derive(Service)]
    /// struct First {
    ///     /// Link to spawned service
    ///     second: Link<Second>,
    /// }
    ///
    /// /// Some other service which requires a link to our service
    /// #[derive(Service)]
    /// struct Second {
    ///     /// Link to the service that owns this service
    ///     owner: Link<First>
    /// }
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     // Provide a closure which takes in the ctx    
    ///     let link: Link<First> = First::create(|ctx| {
    ///     
    ///         // Create second which references the context
    ///         let second: Link<Second> = Second {
    ///             owner: ctx.link()
    ///         }
    ///         .start();
    ///     
    ///         // Can now use the spawned value
    ///         First { second }    
    ///     });
    /// }
    /// ```
    fn create<F>(action: F) -> Link<Self>
    where
        F: FnOnce(&mut ServiceContext<Self>) -> Self,
    {
        let mut ctx = ServiceContext::new();
        let service = action(&mut ctx);
        let link = ctx.link();
        ctx.spawn(service);
        link
    }

    /// Handler logic called when the service is stopping
    fn stopping(&mut self) {}
}

/// Backing context for a service which handles storing the
/// reciever for messaging and the original link for spawning
/// copies
pub struct ServiceContext<S: Service> {
    /// Reciever for handling incoming messages for the service
    rx: mpsc::UnboundedReceiver<ServiceMessage<S>>,
    /// The original link cloned to create other links to the service
    link: Link<S>,
}

impl<S> ServiceContext<S>
where
    S: Service,
{
    /// Creates a new service context and the initial link
    pub(crate) fn new() -> ServiceContext<S> {
        let (tx, rx) = mpsc::unbounded_channel();
        let link = Link(tx);

        ServiceContext { rx, link }
    }

    /// Spawns this service into a new tokio task
    /// where it will then begin processing messages
    ///
    /// `service` The service
    pub(crate) fn spawn(mut self, mut service: S) {
        tokio::spawn(async move {
            service.started(&mut self);

            self.process(&mut service).await;

            service.stopping();
        });
    }

    /// Processing loop for the service handles recieved messages and
    /// executing actions from the message handle results
    ///
    /// `service` The service this context is processing for
    async fn process(&mut self, service: &mut S) {
        while let Some(msg) = self.rx.recv().await {
            let action = msg.handle(service, self);
            match action {
                ServiceAction::Stop => break,
                ServiceAction::Continue => continue,
                // Execute tasks that require blocking the processing
                ServiceAction::Execute(fut) => fut.await,
            }
        }
    }

    /// Stop the context directly by closing the reciever the
    /// reciever will drain any existing messages until there
    /// are none remaining
    pub fn stop(&mut self) {
        self.rx.close()
    }

    /// Creates and returns a link to the service
    pub fn link(&self) -> Link<S> {
        self.link.clone()
    }

    /// Returns a reference to the shared link used by this context
    /// for creating new links. You can use this to access the service
    /// link without creating a clone of it
    pub fn shared_link(&self) -> &Link<S> {
        &self.link
    }

    /// Executes the provided `action` function on the service and service context
    /// after the provided `duration` has elapsed. This function returns a [`JoinHandle`]
    /// which you can use to stop the task by calling .abort() on the handle
    ///
    /// ```
    /// use interlink::prelude::*;
    /// use std::time::Duration;
    /// use tokio::time::sleep;
    ///
    /// struct Test {
    ///     value: u32,
    /// }
    ///
    /// impl Service for Test {
    ///     fn started(&mut self, ctx: &mut ServiceContext<Self>) {
    ///         ctx.run_later(Duration::from_secs(1), |service, _ctx| {
    ///             println!("Hello 1 second later from the service: {}", service.value);
    ///             service.value += 1;
    ///         });
    ///     }
    /// }
    ///
    /// #[tokio::test]
    /// async fn test() {
    ///    let link = Test { value: 1 }.start();
    ///
    ///    sleep(Duration::from_secs(5)).await;
    /// }
    /// ```
    pub fn run_later<F>(&self, duration: Duration, action: F) -> JoinHandle<()>
    where
        F: FnOnce(&mut S, &mut ServiceContext<S>) + Send + 'static,
    {
        let link = self.link();
        tokio::spawn(async move {
            sleep(duration).await;
            let _ = link.do_exec(action);
        })
    }

    /// Executes the provided `action` function on the service and service context
    /// every time the `duration` is elapsed. This function returns a [`JoinHandle`]
    /// which you can use to stop the task by calling .abort() on the handle
    ///
    /// ```
    /// use interlink::prelude::*;
    /// use std::time::Duration;
    /// use tokio::time::sleep;
    ///
    /// struct Test {
    ///     value: u32,
    /// }
    ///
    /// impl Service for Test {
    ///     fn started(&mut self, ctx: &mut ServiceContext<Self>) {
    ///         ctx.run_interval(Duration::from_secs(1), |service, _ctx| {
    ///             println!(
    ///                 "Hello at 1 second interval from the service: {}",
    ///                 service.value
    ///              );
    ///             service.value += 1;
    ///         });
    ///     }
    /// }
    ///
    /// #[tokio::test]
    /// async fn test() {
    ///    let link = Test { value: 1 }.start();
    ///
    ///    sleep(Duration::from_secs(15)).await;
    /// }
    /// ```
    pub fn run_interval<F>(&self, duration: Duration, action: F) -> JoinHandle<()>
    where
        F: FnOnce(&mut S, &mut ServiceContext<S>) + Clone + Send + 'static,
    {
        let link = self.link();
        tokio::spawn(async move {
            loop {
                sleep(duration).await;
                if link.do_exec(action.clone()).is_err() {
                    break;
                }
            }
        })
    }
}