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
use crate::{
    config::ConfigManager,
    types::{ConnectionID, Difficulties, Difficulty, DifficultySettings},
    Miner, MinerList, Result, SessionID,
};
use extended_primitives::Buffer;
use parking_lot::{Mutex, RwLock};
use serde::Serialize;
use std::{
    sync::Arc,
    time::{Duration, Instant, SystemTime},
};
use tokio::sync::mpsc::UnboundedSender;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error};
use uuid::Uuid;

//@todo remove this excessive_bools
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub struct SessionInfo {
    pub agent: bool,
    pub authorized: bool,
    pub subscribed: bool,
    pub client: Option<String>,
    pub session_start: SystemTime,
    // pub state: SessionState,
    pub is_long_timeout: bool,
}

impl Default for SessionInfo {
    fn default() -> Self {
        Self::new()
    }
}

impl SessionInfo {
    pub fn new() -> Self {
        SessionInfo {
            agent: false,
            authorized: false,
            subscribed: false,
            client: None,
            session_start: SystemTime::now(),
            // state: SessionState::Connected,
            is_long_timeout: false,
        }
    }
}

#[derive(PartialEq, Eq, Debug)]
pub enum SessionState {
    Connected,
    Disconnected,
}

#[derive(Debug)]
pub enum SendInformation {
    Json(String),
    Text(String),
    Raw(Buffer),
}

//@todo thought process -> Rather than have this boolean variables that slowly add up over time, we
//should add a new type of "SessionType". This will allow us to also incorporate other types of
//connections that are developed in the future or that are already here and enables a lot easier
//pattern matching imo.

//@todo also think about these enums for connectgion sttatus like authenticated/subscribed etc.
#[derive(Clone)]
pub struct Session<State> {
    inner: Arc<Inner<State>>,
    config_manager: ConfigManager,

    cancel_token: CancellationToken,
    miner_list: MinerList,
    shared: Arc<Mutex<Shared>>,
    difficulty_settings: Arc<RwLock<DifficultySettings>>,
}

struct Inner<State> {
    pub id: ConnectionID,
    pub session_id: SessionID,
    pub state: State,
}

//@todo I think we need to have a few more Mutex's here otherwise we run the risk of deadlocks.
pub(crate) struct Shared {
    //@todo possibly turn this into an Atomic
    status: SessionState,
    //@todo change this (But Later)
    sender: UnboundedSender<SendInformation>,
    needs_ban: bool,
    last_active: Instant,
    //@todo wrap this in a RwLock I believe
    info: SessionInfo,
}

impl<State: Clone> Session<State> {
    pub fn new(
        id: ConnectionID,
        session_id: SessionID,
        sender: UnboundedSender<SendInformation>,
        config_manager: ConfigManager,
        cancel_token: CancellationToken,
        state: State,
    ) -> Result<Self> {
        let config = config_manager.current_config();

        let shared = Shared {
            status: SessionState::Connected,
            last_active: Instant::now(),
            needs_ban: false,
            sender,
            info: SessionInfo::new(),
        };

        let inner = Inner {
            id,
            session_id,
            state,
        };

        Ok(Session {
            config_manager,
            cancel_token,
            miner_list: MinerList::new(),
            shared: Arc::new(Mutex::new(shared)),
            inner: Arc::new(inner),
            difficulty_settings: Arc::new(RwLock::new(DifficultySettings {
                default: Difficulty::from(config.difficulty.initial_difficulty),
                minimum: Difficulty::from(config.difficulty.minimum_difficulty),
            })),
        })
    }

    #[must_use]
    pub fn is_disconnected(&self) -> bool {
        self.shared.lock().status == SessionState::Disconnected
    }

    pub fn send<T: Serialize>(&self, message: T) -> Result<()> {
        let shared = self.shared.lock();

        if shared.last_active.elapsed()
            > Duration::from_secs(
                self.config_manager
                    .current_config()
                    .connection
                    .active_timeout,
            )
        {
            error!(
                "Session: {} not active for 10 minutes. Disconnecting",
                self.inner.id,
            );
            drop(shared);

            self.ban();

            //@todo return Error here instead
            return Ok(());
        }

        debug!("Sending message: {}", serde_json::to_string(&message)?);

        //@todo implement Display on SendInformation.
        let msg = SendInformation::Json(serde_json::to_string(&message)?);

        //@todo it may make sense to keep the sender inside of session here - not sure why it's in
        //connection like the way it is.
        //@todo this feels inefficient, maybe we do send bytes here.
        shared.sender.send(msg)?;

        Ok(())
    }

    pub fn send_raw(&self, message: Buffer) -> Result<()> {
        let shared = self.shared.lock();

        shared.sender.send(SendInformation::Raw(message))?;

        Ok(())
    }

    pub fn shutdown(&self) {
        if !self.is_disconnected() {
            self.disconnect();

            self.cancel_token.cancel();
        }
    }

    pub fn disconnect(&self) {
        self.shared.lock().status = SessionState::Disconnected;
    }

    pub fn ban(&self) {
        self.shared.lock().needs_ban = true;
        self.shutdown();
    }

    #[must_use]
    pub fn needs_ban(&self) -> bool {
        self.shared.lock().needs_ban
    }

    #[must_use]
    pub fn id(&self) -> &ConnectionID {
        &self.inner.id
    }

    pub fn register_worker(
        &self,
        session_id: SessionID,
        client: Option<String>,
        worker_name: Option<String>,
        worker_id: Uuid,
    ) {
        //@todo has to be an easier way to reuse worker_name here
        debug!(id = ?self.inner.id, "Registered Worker {worker_id} ({}) Session ID: {session_id}", worker_name.clone().unwrap_or(String::new()));

        let worker = Miner::new(
            self.id().clone(),
            worker_id,
            session_id,
            client,
            worker_name,
            self.config_manager.clone(),
            self.difficulty_settings.read().clone(),
        );

        self.miner_list.add_miner(session_id, worker);
    }

    #[must_use]
    pub fn unregister_worker(&self, session_id: SessionID) -> Option<(SessionID, Miner)> {
        self.miner_list.remove_miner(session_id)
    }

    #[must_use]
    pub fn get_miner_list(&self) -> MinerList {
        self.miner_list.clone()
    }

    #[must_use]
    pub fn get_worker_by_session_id(&self, session_id: SessionID) -> Option<Miner> {
        self.miner_list.get_miner_by_id(session_id)
    }

    //@todo I think we can remove this
    pub fn update_worker_by_session_id(&self, session_id: SessionID, miner: Miner) {
        self.miner_list
            .update_miner_by_session_id(session_id, miner);
    }

    // ===== Worker Helper functions ===== //

    pub fn set_client(&self, client: &str) {
        let mut agent = false;
        let mut long_timeout = false;
        //@todo check these equal to just STATIC CONSTS for the various things we need to know.
        //ClientType Enum
        //@todo we need to do some checking/pruning etc of this client string.

        if client.starts_with("btccom-agent/") {
            //Agent
            agent = true;
            long_timeout = true;
        }

        let mut shared = self.shared.lock();
        shared.info.agent = agent;
        shared.info.client = Some(client.to_string());
        shared.info.is_long_timeout = long_timeout;
    }

    #[must_use]
    pub fn get_connection_info(&self) -> SessionInfo {
        self.shared.lock().info.clone()
    }

    #[must_use]
    pub fn is_long_timeout(&self) -> bool {
        self.shared.lock().info.is_long_timeout
    }

    // Returns the current timeout
    #[must_use]
    pub fn timeout(&self) -> Duration {
        let shared = self.shared.lock();

        if shared.info.is_long_timeout {
            // One Week
            Duration::from_secs(86400 * 7)
        } else if shared.info.subscribed && shared.info.authorized {
            // Ten Minutes
            Duration::from_secs(600)
        } else {
            //@todo let's play with this -> I think 15 might be too short, but if it works lets do
            //it.
            Duration::from_secs(15)
        }
    }

    #[must_use]
    pub fn get_session_id(&self) -> SessionID {
        self.inner.session_id
    }

    #[must_use]
    pub fn authorized(&self) -> bool {
        self.shared.lock().info.authorized
    }

    pub fn authorize(&self) {
        self.shared.lock().info.authorized = true;
    }

    #[must_use]
    pub fn subscribed(&self) -> bool {
        self.shared.lock().info.subscribed
    }

    pub fn subscribe(&self) {
        self.shared.lock().info.subscribed = true;
    }

    #[must_use]
    pub fn is_agent(&self) -> bool {
        self.shared.lock().info.agent
    }

    pub fn set_difficulty(&self, session_id: SessionID, difficulty: Difficulty) {
        if let Some(miner) = self.miner_list.get_miner_by_id(session_id) {
            miner.set_difficulty(difficulty);
        }
    }

    pub fn set_default_difficulty(&self, difficulty: Difficulty) {
        self.difficulty_settings.write().default = difficulty;
    }

    //@todo we need to test this
    pub fn set_minimum_difficulty(&self, difficulty: Difficulty) {
        //We only want to set the minimum difficulty if it is greater than or equal to the Global
        //minimum difficulty
        if difficulty.as_u64() >= self.config_manager.difficulty_config().minimum_difficulty {
            self.difficulty_settings.write().minimum = difficulty;
        }
    }

    #[must_use]
    pub fn get_difficulties(&self, session_id: SessionID) -> Option<Difficulties> {
        self.miner_list
            .get_miner_by_id(session_id)
            .map(|miner| miner.difficulties())
    }

    //@todo double check that a reference here will work
    #[must_use]
    pub fn state(&self) -> &State {
        &self.inner.state
    }

    #[must_use]
    pub fn update_difficulty(&self, session_id: SessionID) -> Option<Difficulty> {
        if let Some(miner) = self.miner_list.get_miner_by_id(session_id) {
            miner.update_difficulty()
        } else {
            None
        }
    }

    pub(crate) fn active(&self) {
        self.shared.lock().last_active = Instant::now();
    }
}

#[cfg(feature = "test-utils")]
impl<State: Clone> Session<State> {
    pub fn mock(state: State) -> Session<State> {}
}