adborc 0.1.0

Orchestrator for a network of distributed Android devices
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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
/// Request and response objects used for communication with the System node and
/// between System nodes.
pub mod request;

mod consumer;
mod marketmaker;
mod supplier;

use crate::net::{CommandServer, PortForwardMode, PortForwarder, ProcessFn, TCPClient};
use crate::util::{
    adb_utils::{self, AdbVersionInfo, DeviceInfo, ScrcpyVersionInfo},
    SysStateDefaultConfig, ADBORC_VERSION, HEARTBEAT_INTERVAL, MIN_ADB_REV, MIN_ADB_VER,
    MIN_SCRCPY_VER, UNDERTAKER_INTERVAL,
};
use blake2::{digest::consts::U16, Blake2s, Digest};
use lazy_static::lazy_static;
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use serde_json;
use snow::Keypair;
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter, Result, Write};
use std::io::{self, Error, ErrorKind};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::Duration;

use consumer::{Consumer, ConsumerState};
use marketmaker::MarketMaker;
use request::*;
use supplier::{Supplier, SupplierState};
use tokio::task;

pub(crate) struct SystemKeypair {
    keypair: Option<Keypair>,
}

lazy_static! {
    static ref SYSTEM_KEYPAIR: RwLock<SystemKeypair> = RwLock::new(SystemKeypair { keypair: None });
}

impl SystemKeypair {
    pub fn set_keypair(keypair: Keypair) {
        let mut system_keypair = SYSTEM_KEYPAIR.write().unwrap();
        system_keypair.keypair = Some(keypair);
    }

    pub fn is_none() -> bool {
        SYSTEM_KEYPAIR.read().unwrap().keypair.is_none()
    }

    pub fn get_private_key() -> Option<Vec<u8>> {
        let system_keypair = SYSTEM_KEYPAIR.read().unwrap();
        system_keypair
            .keypair
            .as_ref()
            .map(|keypair| keypair.private.clone())
    }

    pub fn get_public_key() -> Option<Vec<u8>> {
        let system_keypair = SYSTEM_KEYPAIR.read().unwrap();
        system_keypair
            .keypair
            .as_ref()
            .map(|keypair| keypair.public.clone())
    }
}

pub(crate) type Key = Vec<u8>;

struct System;

/// This is a struct used to start the system and keep track of the modes running
/// in the current system. For example, if a system starts the network by running in
/// MarketMaker mode, then the MarketMaker mode will be added to the state. Now, if
/// the system decides to supply devices to the network, then the Supplier mode
/// will be added to the state.
pub struct SysState {
    market_maker: Option<MarketMaker>,
    supplier: Option<Supplier>,
    consumer: Option<Consumer>,
    initialized: bool,
}

/// System state information for currently operational mode.
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct SysStateMin {
    pub market_maker: bool,
    pub supplier: bool,
    pub consumer: bool,
    pub initialized: bool,
}

impl Display for SysStateMin {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            r"System status:
    Initialized : {}
    MarketMaker : {}
    Supplier    : {}
    Consumer    : {}",
            self.initialized, self.market_maker, self.supplier, self.consumer
        )
    }
}

lazy_static! {
    /// Creates a new uninitialized SysState struct.
    /// This struct is used to persist the system state.
    static ref SYS_STATE: RwLock<SysState> = RwLock::new(SysState::default());
}

impl Default for SysState {
    /// Creates a new uninitialized SysState struct.
    fn default() -> SysState {
        SysState {
            market_maker: None,
            supplier: None,
            consumer: None,
            initialized: false,
        }
    }
}

// Keeps accesses to the SYS_STATE static variable contained in small functions.
// This is to prevent deadlocks when accessing the SYS_STATE RwLock in multiple places.
// We should not need to lock the SYS_STATE RwLock outside of these functions.
// Note: Eager unwraps are used here because we should never be in a situation where
// the lock is poisoned. If the lock is poisoned, then the thread should panic.
impl SysState {
    // Write functions...

    /// Starts the system listener in uninitialized state.
    /// This function should only be called once.
    /// To initialize the system, send a [`request`] to the system listener
    /// to start any of the modes.
    #[inline(always)]
    #[tokio::main]
    pub async fn start_system() -> io::Result<()> {
        let mut listener = CommandServer {
            host: SysStateDefaultConfig::BIND_HOST.to_string(),
            port: SysStateDefaultConfig::BIND_PORT,
        };
        listener.start(System::process_command).await
    }

    #[inline(always)]
    fn reset_state() {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        *state = SysState::default();
    }

    #[inline(always)]
    fn set_market_maker(mm: MarketMaker) {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.market_maker = Some(mm);
    }

    #[inline(always)]
    fn set_supplier(supplier: Supplier) {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.supplier = Some(supplier);
    }

    #[inline(always)]
    fn set_consumer(consumer: Consumer) {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.consumer = Some(consumer);
    }

    #[inline(always)]
    fn set_initialized() {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.initialized = true;
    }

    #[inline(always)]
    fn reset_market_maker() {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.market_maker = None;
    }

    #[inline(always)]
    fn reset_supplier() {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.supplier = None;
    }

    #[inline(always)]
    fn reset_consumer() {
        if SYS_STATE.is_poisoned() {
            error!("SYS_STATE lock is poisoned");
            return;
        }
        let mut state = SYS_STATE.write().unwrap();
        state.consumer = None;
    }

    #[inline(always)]
    fn update_initialized() -> bool {
        let mut state = SYS_STATE.write().unwrap();
        state.initialized =
            state.market_maker.is_some() || state.supplier.is_some() || state.consumer.is_some();
        state.initialized
    }

    #[inline(always)]
    #[tokio::main]
    async fn stop_system() {
        let listener = CommandServer {
            host: SysStateDefaultConfig::BIND_HOST.to_string(),
            port: SysStateDefaultConfig::BIND_PORT,
        };
        listener.stop().await;
    }

    // Read functions...

    #[inline(always)]
    fn get_min_state() -> SysStateMin {
        let state = SYS_STATE.read().unwrap();
        SysStateMin {
            market_maker: state.market_maker.is_some(),
            supplier: state.supplier.is_some(),
            consumer: state.consumer.is_some(),
            initialized: state.initialized,
        }
    }

    #[inline(always)]
    fn is_initialized() -> bool {
        let state = SYS_STATE.read().unwrap();
        state.initialized
    }

    #[inline(always)]
    fn market_maker_is_some() -> bool {
        let state = SYS_STATE.read().unwrap();
        state.market_maker.is_some()
    }

    #[inline(always)]
    fn supplier_is_some() -> bool {
        let state = SYS_STATE.read().unwrap();
        state.supplier.is_some()
    }

    #[inline(always)]
    fn consumer_is_some() -> bool {
        let state = SYS_STATE.read().unwrap();
        state.consumer.is_some()
    }
}

impl System {
    /// Initializes the system state in a MarketMaker mode.
    pub fn start_market_maker() -> io::Result<()> {
        if !SysState::is_initialized() {
            match MarketMaker::new() {
                Ok(mm) => {
                    SysState::set_market_maker(mm);
                    SysState::set_initialized();
                    info!("Market maker started");
                }
                Err(e) => {
                    error!("Error starting market maker: {}", e);
                    return Err(e);
                }
            }
        } else {
            error!("System is already initialized");
            return Err(Error::new(
                ErrorKind::Other,
                "System is already initialized",
            ));
        }
        Ok(())
    }

    /// Stops the MarketMaker, if currently running.
    /// Returns true if the MarketMaker was stopped, false otherwise.
    pub fn stop_market_maker() -> bool {
        if SysState::market_maker_is_some() {
            MarketMaker::terminate();
            SysState::reset_market_maker();
            info!("Market maker stopped");
            if !SysState::update_initialized() {
                info!("System is uninitialized");
            }
            true
        } else {
            error!("Market maker is not running");
            false
        }
    }

    /// Initializes the system state in a Supplier mode.
    pub fn start_supplier_and_connect(
        mm_host: &str,
        mm_port: u16,
        name: Option<String>,
        secure_comms: bool,
    ) -> io::Result<()> {
        if SysState::supplier_is_some() {
            error!("Error starting supplier: Supplier is already running");
            return Err(Error::new(ErrorKind::Other, "Supplier is already running"));
        }
        match Supplier::new(mm_host.to_string(), mm_port, name, secure_comms) {
            Ok(supplier) => {
                SysState::set_supplier(supplier);
                SysState::set_initialized();
                info!("Supplier started");
            }
            Err(e) => {
                error!("Error starting supplier: {}", e);
                return Err(e);
            }
        }
        Ok(())
    }

    /// Stops the Supplier, if currently running.
    /// Returns true if the Supplier was stopped, false otherwise.
    pub fn stop_supplier(from_market_maker: bool) -> bool {
        if SysState::supplier_is_some() {
            if from_market_maker {
                Supplier::market_maker_terminate();
            } else {
                Supplier::terminate();
            }
            SysState::reset_supplier();
            info!("Supplier stopped");
            if !SysState::update_initialized() {
                info!("System is uninitialized");
            }
            true
        } else {
            warn!("Supplier is not running");
            false
        }
    }

    /// Initializes the system state in a Consumer mode.
    pub fn start_consumer_and_connect(
        mm_host: &str,
        mm_port: u16,
        name: Option<String>,
    ) -> io::Result<()> {
        if SysState::consumer_is_some() {
            error!("Error starting consumer: Consumer is already running");
            return Err(Error::new(ErrorKind::Other, "Consumer is already running"));
        }
        match Consumer::new(mm_host.to_string(), mm_port, name) {
            Ok(consumer) => {
                SysState::set_consumer(consumer);
                SysState::set_initialized();
                info!("Consumer started");
            }
            Err(e) => {
                error!("Error starting consumer: {}", e);
                return Err(e);
            }
        }
        Ok(())
    }

    /// Stops the Consumer, if currently running.
    /// Returns true if the Consumer was stopped, false otherwise.
    pub fn stop_consumer(from_market_maker: bool) -> bool {
        if SysState::consumer_is_some() {
            if from_market_maker {
                Consumer::market_maker_terminate();
            } else {
                Consumer::terminate();
            }
            SysState::reset_consumer();
            info!("Consumer stopped");
            if !SysState::update_initialized() {
                info!("System is uninitialized");
            }
            true
        } else {
            warn!("Consumer is not running");
            false
        }
    }

    fn server_shutdown() {
        info!("Shutting down system state server");
        System::stop_market_maker();
        System::stop_supplier(false);
        System::stop_consumer(false);
    }

    fn check_system() -> (SupplierCheck, ConsumerCheck) {
        let adb_version = adb_utils::get_adb_version().unwrap_or_else(|e| {
            error!("Error getting adb version: {}", e);
            AdbVersionInfo::default()
        });
        let scrcpy_version = adb_utils::get_scrcpy_version().unwrap_or_else(|e| {
            error!("Error getting scrcpy version: {}", e);
            ScrcpyVersionInfo::default()
        });
        let consumer_info = ConsumerVerInfo {
            adb_info: adb_version.clone(),
            scrcpy_info: scrcpy_version,
        };
        let supplier_check = adb_version.into();
        let consumer_check = consumer_info.into();
        (supplier_check, consumer_check)
    }

    fn process_command(command: String, peer_addr: SocketAddr, peer_id: Arc<Key>) -> String {
        debug!(
            "Processing command: {}\tfrom peer: {}",
            command,
            base64::encode(peer_id.as_ref())
        );

        let request = serde_json::from_str::<Request>(&command);
        if request.is_err() {
            return serde_json::to_string(&SysStateResponse::InvalidRequest { request: command })
                .unwrap();
        }

        let request = request.unwrap();

        // Unwrapping of serialing/deserializing is safe, because we use request/response objects
        // that are known to be serializable/deserializable.

        let is_supplier_mm =
            || SysState::supplier_is_some() && SupplierState::verify_market_maker(&peer_id);
        let is_consumer_mm =
            || SysState::consumer_is_some() && ConsumerState::verify_market_maker(&peer_id);

        match request {
            Request::System(SysStateRequest::SupplierMarketMakerTerminating)
                if is_supplier_mm() =>
            {
                thread::spawn(|| System::stop_supplier(true));
                serde_json::to_string(&SysStateResponse::TerminationAcknowledged).unwrap()
            }
            Request::System(SysStateRequest::ConsumerMarketMakerTerminating)
                if is_consumer_mm() =>
            {
                thread::spawn(|| System::stop_consumer(true));
                serde_json::to_string(&SysStateResponse::TerminationAcknowledged).unwrap()
            }
            Request::System(request) => System::process_request(request, peer_addr),
            Request::MarketMaker(request) if SysState::market_maker_is_some() => {
                MarketMaker::process_request(request, peer_addr, peer_id)
            }
            Request::Supplier(request) if SysState::supplier_is_some() => {
                Supplier::process_request(request, peer_addr, peer_id)
            }
            Request::Consumer(request) if SysState::consumer_is_some() => {
                Consumer::process_request(request, peer_addr, peer_id)
            }
            _ => serde_json::to_string(&SysStateResponse::RequestNotAllowed).unwrap(),
        }
    }

    fn process_request(request: SysStateRequest, peer_addr: SocketAddr) -> String {
        // Only requests from localhost client are allowed.
        if !(peer_addr.ip().is_loopback()) {
            return serde_json::to_string(&SysStateResponse::RequestNotAllowed).unwrap();
        }
        match request {
            SysStateRequest::GetState => {
                let state = SysState::get_min_state();
                serde_json::to_string(&SysStateResponse::CurrentSysState { state }).unwrap()
            }
            SysStateRequest::GetPeerId => {
                let pub_key = SystemKeypair::get_public_key();
                if pub_key.is_none() {
                    return serde_json::to_string(&SysStateResponse::GetPeerIdFailure).unwrap();
                }
                serde_json::to_string(&SysStateResponse::PeerId {
                    peer_id: base64::encode(pub_key.unwrap()),
                })
                .unwrap()
            }
            SysStateRequest::SystemCheck => {
                let (supplier_check, consumer_check) = System::check_system();
                serde_json::to_string(&SysStateResponse::SystemCheck {
                    supplier_check,
                    consumer_check,
                })
                .unwrap()
            }
            SysStateRequest::SetAdbPath { adb_path } => {
                let adb_path = PathBuf::from(adb_path);
                let result = adb_utils::set_adb_path(adb_path);
                if result.is_err() {
                    return serde_json::to_string(&SysStateResponse::SetAdbPathFailure {
                        reason: result.err().unwrap().to_string(),
                    })
                    .unwrap();
                }
                serde_json::to_string(&SysStateResponse::SetAdbPathSuccess).unwrap()
            }
            SysStateRequest::SetScrcpyPath { scrcpy_path } => {
                let scrcpy_path = PathBuf::from(scrcpy_path);
                let result = adb_utils::set_scrcpy_path(scrcpy_path);
                if result.is_err() {
                    return serde_json::to_string(&SysStateResponse::SetScrcpyPathFailure {
                        reason: result.err().unwrap().to_string(),
                    })
                    .unwrap();
                }
                serde_json::to_string(&SysStateResponse::SetScrcpyPathSuccess).unwrap()
            }
            SysStateRequest::Shutdown => {
                System::server_shutdown();
                thread::spawn(|| {
                    // Wait for 1 second to allow the server to send the response.
                    // The cleanup is effectively done in the server shutdown handler.
                    thread::sleep(Duration::from_secs(1));
                    SysState::reset_state();
                    SysState::stop_system();
                });
                serde_json::to_string(&SysStateResponse::ShutDownSuccess).unwrap()
            }
            SysStateRequest::StartMarketMaker => match System::start_market_maker() {
                Ok(_) => serde_json::to_string(&SysStateResponse::StartMarketMakerSuccess).unwrap(),
                Err(e) => serde_json::to_string(&SysStateResponse::StartMarketMakerFailed {
                    reason: e.to_string(),
                })
                .unwrap(),
            },
            SysStateRequest::StopMarketMaker => {
                if System::stop_market_maker() {
                    serde_json::to_string(&SysStateResponse::StopMarketMakerSuccess).unwrap()
                } else {
                    serde_json::to_string(&SysStateResponse::StopMarketMakerFailed).unwrap()
                }
            }
            SysStateRequest::StartSupplier {
                mm_host,
                mm_port,
                name,
                secure_comms,
            } => match System::start_supplier_and_connect(&mm_host, mm_port, name, secure_comms) {
                Ok(_) => serde_json::to_string(&SysStateResponse::StartSupplierSuccess).unwrap(),
                Err(e) => serde_json::to_string(&SysStateResponse::StartSupplierFailed {
                    reason: e.to_string(),
                })
                .unwrap(),
            },
            SysStateRequest::StopSupplier => {
                if System::stop_supplier(false) {
                    serde_json::to_string(&SysStateResponse::StopSupplierSuccess).unwrap()
                } else {
                    serde_json::to_string(&SysStateResponse::StopSupplierFailed).unwrap()
                }
            }
            SysStateRequest::StartConsumer {
                mm_host,
                mm_port,
                name,
            } => match System::start_consumer_and_connect(&mm_host, mm_port, name) {
                Ok(_) => serde_json::to_string(&SysStateResponse::StartConsumerSuccess).unwrap(),
                Err(e) => serde_json::to_string(&SysStateResponse::StartConsumerFailed {
                    reason: e.to_string(),
                })
                .unwrap(),
            },
            SysStateRequest::StopConsumer => {
                if System::stop_consumer(false) {
                    serde_json::to_string(&SysStateResponse::StopConsumerSuccess).unwrap()
                } else {
                    serde_json::to_string(&SysStateResponse::StopConsumerFailed).unwrap()
                }
            }
            _ => serde_json::to_string(&SysStateResponse::RequestNotAllowed).unwrap(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct MarketMakerSpec;

/// Information related to a Supplier on the network.
/// This information is exchanged with the MarketMaker.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct SupplierSpec {
    /// The name of the Supplier on the network.
    pub name: String,
    /// The IP address of the Supplier on the network.
    pub bind_host: String,
    /// The port of the Supplier on the network.
    pub bind_port: u16,
    /// List of Devices supplied by the Supplier.
    pub devices: Vec<DeviceSpec>,
    /// Version info of the Supplier.
    ver_info: SupplierCheck,
    /// Public key of the Supplier.
    pub pub_key: String,
    /// Whether the Supplier forces secure communication for devices.
    pub secure_comms: bool,
    /// `adborc` version.
    pub adborc_version: String,
}

impl Default for SupplierSpec {
    fn default() -> SupplierSpec {
        SupplierSpec {
            name: String::new(),
            bind_host: String::new(),
            bind_port: SysStateDefaultConfig::BIND_PORT,
            devices: Vec::new(),
            ver_info: SupplierCheck::default(),
            pub_key: SystemKeypair::get_public_key().map_or(String::new(), base64::encode),
            secure_comms: false,
            adborc_version: ADBORC_VERSION.to_string(),
        }
    }
}

impl Display for SupplierSpec {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            r"SupplierSpec:
    name: {}   bind_host: {}   bind_port: {}
    
    devices: {:#?}
    
    ver_info: {}",
            self.name, self.bind_host, self.bind_port, self.devices, self.ver_info
        )
    }
}

/// Information related to a Consumer on the network.
/// This information is exchanged with the MarketMaker.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ConsumerSpec {
    /// The name of the Consumer on the network.
    pub name: String,
    /// The IP address of the Consumer on the network.
    pub bind_host: String,
    /// The port of the Consumer on the network.
    pub bind_port: u16,
    /// Version info of the Consumer.
    ver_info: ConsumerCheck,
    /// Public key of the Consumer.
    pub pub_key: String,
    /// `adborc` version.
    pub adborc_version: String,
}

impl Default for ConsumerSpec {
    fn default() -> ConsumerSpec {
        ConsumerSpec {
            name: String::new(),
            bind_host: String::new(),
            bind_port: SysStateDefaultConfig::BIND_PORT,
            ver_info: ConsumerCheck::default(),
            pub_key: SystemKeypair::get_public_key().map_or(String::new(), base64::encode),
            adborc_version: ADBORC_VERSION.to_string(),
        }
    }
}

impl Display for ConsumerSpec {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            r"ConsumerSpec:
    name: {}   bind_host: {}   bind_port: {}
    
    ver_info: {}",
            self.name, self.bind_host, self.bind_port, self.ver_info
        )
    }
}

/// Information related to a Device on the network.
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub struct DeviceSpec {
    /// Device ID of the Device on the network.
    pub device_id: String,
    /// Android Serial of the device.
    pub android_serial: String,
    /// Details of the device.
    /// Contains device name, brand and model.
    pub device_details: String,
    /// Network ID of the Supplier for the device.
    available_at: String,
    /// Name of the Supplier for the device.
    available_at_name: String,
    /// IP address of the Supplier for the device.
    available_at_host: String,
    /// Port of the Supplier where the device is available.
    pub available_at_port: u16,
    /// Network ID of the Consumer for the device.
    used_by: String,
    /// Name of the Consumer for the device.
    used_by_name: String,
    // IP address of the Consumer for the device.
    used_by_host: String,
    /// Port of the Consumer where the device is used.
    pub used_by_port: u16,
    /// Whether the device uses secure tunnels for communication.
    pub secure_comms: bool,
}

impl Display for DeviceSpec {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            r"device_id: {}   android_serial: {}   {}",
            self.device_id, self.android_serial, self.device_details
        )
    }
}

/// Filters available to apply while searching for devices.
/// Actual filter used is a vector of this enum. See [`DeviceFilterVec`] for more details.
#[derive(Serialize, Deserialize, Debug)]
pub enum DeviceFilter {
    // Filter only available devices (or only reserved devices).
    IsAvailable(bool),
    // Device ID(s) to filter.
    DeviceIds(HashSet<String>),
    // Device names to filter.
    DeviceNames(HashSet<String>),
    // Device models to filter.
    DeviceModels(HashSet<String>),
    // Filter devices by supplier id.
    SupplierIds(HashSet<String>),
    // Filter devices by supplier name.
    SupplierNames(HashSet<String>),
    // Filter devices by supplier host.
    SupplierHosts(HashSet<String>),
    // Filter devices by consumer id.
    ConsumerIds(HashSet<String>),
    // Filter devices by consumer name.
    ConsumerNames(HashSet<String>),
    // Filter devices by consumer host.
    ConsumerHosts(HashSet<String>),
}

impl Display for DeviceFilter {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            Self::IsAvailable(is_available) => {
                if *is_available {
                    write!(f, "available")
                } else {
                    write!(f, "reserved")
                }
            }
            Self::DeviceIds(device_ids) => write!(f, "device_ids: {:?}", device_ids),
            Self::DeviceNames(device_names) => write!(f, "device_names: {:?}", device_names),
            Self::DeviceModels(device_models) => write!(f, "device_models: {:?}", device_models),
            Self::SupplierIds(supplier_ids) => write!(f, "supplier_ids: {:?}", supplier_ids),
            Self::SupplierNames(supplier_names) => {
                write!(f, "supplier_names: {:?}", supplier_names)
            }
            Self::SupplierHosts(supplier_hosts) => {
                write!(f, "supplier_hosts: {:?}", supplier_hosts)
            }
            Self::ConsumerIds(consumer_ids) => write!(f, "consumer_ids: {:?}", consumer_ids),
            Self::ConsumerNames(consumer_names) => {
                write!(f, "consumer_names: {:?}", consumer_names)
            }
            Self::ConsumerHosts(consumer_hosts) => {
                write!(f, "consumer_hosts: {:?}", consumer_hosts)
            }
        }
    }
}

impl DeviceFilter {
    fn filter(&self, device: &DeviceSpec) -> bool {
        match self {
            Self::IsAvailable(is_available) => *is_available == device.used_by.is_empty(),
            Self::DeviceIds(device_ids) => device_ids.contains(&device.device_id),
            Self::DeviceNames(device_names) => {
                let device_name = DeviceInfo::from(device.device_details.clone()).name;
                device_names.contains(&device_name)
            }
            Self::DeviceModels(device_models) => {
                let device_model = DeviceInfo::from(device.device_details.clone()).model;
                device_models.contains(&device_model)
            }
            Self::SupplierIds(supplier_ids) => supplier_ids.contains(&device.available_at),
            Self::SupplierNames(supplier_names) => {
                supplier_names.contains(&device.available_at_name)
            }
            Self::SupplierHosts(supplier_hosts) => {
                supplier_hosts.contains(&device.available_at_host)
            }
            Self::ConsumerIds(consumer_ids) => consumer_ids.contains(&device.used_by),
            Self::ConsumerNames(consumer_names) => consumer_names.contains(&device.used_by_name),
            Self::ConsumerHosts(consumer_hosts) => consumer_hosts.contains(&device.used_by_host),
        }
    }
}

/// A filter composed of one or more [`DeviceFilter`] used to
/// filter devices on the network. Useful for searching for
/// devices on the network that satisfy certain properties.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct DeviceFilterVec {
    pub filters: Vec<DeviceFilter>,
}

impl Display for DeviceFilterVec {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let mut availability_str = String::new();
        let mut filter_str = String::new();

        for filter in self.filters.iter() {
            match filter {
                DeviceFilter::IsAvailable(is_available) => {
                    availability_str = if *is_available {
                        "available".to_string()
                    } else {
                        "reserved".to_string()
                    }
                }
                _ => {
                    if filter_str.is_empty() {
                        writeln!(filter_str, "   with {}", filter).unwrap()
                    } else {
                        writeln!(filter_str, "   and {}", filter).unwrap()
                    }
                }
            }
        }

        write!(
            f,
            "Fetch {}devices in the network\n{}",
            availability_str, filter_str
        )
    }
}

/// Support level for `supplier` mode on the system node.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum SupplierCheck {
    /// System is supported to run in Supplier mode
    Supported { ver_info: AdbVersionInfo },
    /// Supplier mode is NOT supported due to ADB version/revision conflict
    AdbNotSupported { ver_info: AdbVersionInfo },
    /// Supplier mode is NOT supported due to ADB not being found
    AdbNotFound { ver_info: AdbVersionInfo },
}

impl Default for SupplierCheck {
    fn default() -> SupplierCheck {
        SupplierCheck::AdbNotFound {
            ver_info: AdbVersionInfo::default(),
        }
    }
}

impl Display for SupplierCheck {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            SupplierCheck::Supported { .. } => write!(f, "Supported"),
            SupplierCheck::AdbNotSupported { ver_info } => {
                write!(f, "ADB not supported\n{}", ver_info)
            }
            SupplierCheck::AdbNotFound { .. } => write!(f, "ADB not found"),
        }
    }
}

impl From<AdbVersionInfo> for SupplierCheck {
    fn from(ver_info: AdbVersionInfo) -> Self {
        if ver_info.path.is_empty() {
            return SupplierCheck::AdbNotFound { ver_info };
        }
        let ver_number = ver_info
            .version
            .split('.')
            .last()
            .unwrap()
            .parse::<u8>()
            .unwrap_or(0);
        let rev_number = ver_info
            .revision
            .split('.')
            .next()
            .unwrap()
            .parse::<u8>()
            .unwrap_or(0);
        if ver_number >= MIN_ADB_VER && rev_number >= MIN_ADB_REV {
            SupplierCheck::Supported { ver_info }
        } else {
            SupplierCheck::AdbNotSupported { ver_info }
        }
    }
}

/// `ADB` and `SCRCPY` version information for the system node.
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub struct ConsumerVerInfo {
    pub adb_info: AdbVersionInfo,
    pub scrcpy_info: ScrcpyVersionInfo,
}

impl Display for ConsumerVerInfo {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            r"Consumer version info:
    ADB Information
    ---------------

{}

    SCRCPY Information
    ------------------
{}",
            self.adb_info, self.scrcpy_info
        )
    }
}

impl ConsumerVerInfo {
    fn get() -> Self {
        let adb_info = adb_utils::get_adb_version().unwrap_or_default();
        let scrcpy_info = adb_utils::get_scrcpy_version().unwrap_or_default();
        ConsumerVerInfo {
            adb_info,
            scrcpy_info,
        }
    }
}

/// Support level for `consumer` mode on the system node.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum ConsumerCheck {
    /// System is supported to run in Consumer mode.
    /// Both ADB and SCRCPY are supported for the Consumer.
    FullSupport { ver_info: ConsumerVerInfo },
    /// System is supported to run in Consumer mode.
    /// Only ADB is supported for the Consumer.
    /// SCRCPY is NOT supported for the Consumer because
    /// of version conflict.
    ScrcpyNotSupported { ver_info: ConsumerVerInfo },
    /// System is supported to run in Consumer mode.
    /// Only ADB is supported for the Consumer.
    /// SCRCPY is NOT supported for the Consumer because
    /// it is not found.
    ScrcpyNotFound { ver_info: ConsumerVerInfo },
    /// System is NOT supported to run in Consumer mode.
    /// ADB is not supported for the Consumer.
    /// SCRCPY may or may not be supported.
    AdbNotSupported { ver_info: ConsumerVerInfo },
    /// System is NOT supported to run in Consumer mode.
    /// ADB is not found for the Consumer.
    /// SCRCPY may or may not be supported.
    AdbNotFound { ver_info: ConsumerVerInfo },
}

impl Default for ConsumerCheck {
    fn default() -> ConsumerCheck {
        ConsumerCheck::AdbNotFound {
            ver_info: ConsumerVerInfo::default(),
        }
    }
}

impl Display for ConsumerCheck {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            ConsumerCheck::FullSupport { ver_info } => {
                write!(f, "ADB and SCRCPY supported\n{}", ver_info)
            }
            ConsumerCheck::ScrcpyNotSupported { ver_info } => {
                write!(f, "ADB supported, SCRCPY not supported\n{}", ver_info)
            }
            ConsumerCheck::ScrcpyNotFound { ver_info } => {
                write!(f, "ADB supported, SCRCPY not found\n{}", ver_info)
            }
            ConsumerCheck::AdbNotSupported { ver_info } => {
                write!(f, "ADB not supported\n{}", ver_info)
            }
            ConsumerCheck::AdbNotFound { .. } => write!(f, "ADB not found"),
        }
    }
}

impl From<ConsumerVerInfo> for ConsumerCheck {
    fn from(ver_info: ConsumerVerInfo) -> Self {
        if ver_info.adb_info.path.is_empty() {
            return ConsumerCheck::AdbNotFound { ver_info };
        }
        if ConsumerCheck::check_adb(&ver_info.adb_info) {
            if ver_info.scrcpy_info.path.is_empty() {
                return ConsumerCheck::ScrcpyNotFound { ver_info };
            }
            if ConsumerCheck::check_scrcpy(&ver_info.scrcpy_info) {
                ConsumerCheck::FullSupport { ver_info }
            } else {
                ConsumerCheck::ScrcpyNotSupported { ver_info }
            }
        } else {
            ConsumerCheck::AdbNotSupported { ver_info }
        }
    }
}

impl ConsumerCheck {
    pub fn is_adb_supported(&self) -> bool {
        matches!(
            self,
            ConsumerCheck::FullSupport { .. }
                | ConsumerCheck::ScrcpyNotSupported { .. }
                | ConsumerCheck::ScrcpyNotFound { .. }
        )
    }

    fn check_adb(adb_info: &AdbVersionInfo) -> bool {
        let adb_ver_number = adb_info
            .version
            .split('.')
            .last()
            .unwrap()
            .parse::<u8>()
            .unwrap_or(0);
        adb_ver_number >= MIN_ADB_VER
    }

    fn check_scrcpy(scrcpy_info: &ScrcpyVersionInfo) -> bool {
        let scrcpy_ver_number = scrcpy_info
            .version
            .split('.')
            .last()
            .unwrap()
            .parse::<u8>()
            .unwrap_or(0);
        scrcpy_ver_number >= MIN_SCRCPY_VER
    }
}

struct DeviceKey {
    android_serial: String,
    android_id: String,
    model: String,
}

// Hash output size is 16 bytes.
type Blake2s16 = Blake2s<U16>;

impl DeviceKey {
    fn new(android_serial: &str, device_info: &DeviceInfo) -> Self {
        DeviceKey {
            android_serial: android_serial.to_owned(),
            android_id: device_info.android_id.clone(),
            model: device_info.model.clone(),
        }
    }

    fn get_uuid(&self) -> String {
        let mut hasher = Blake2s16::new();
        hasher.update(self.android_serial.as_bytes());
        hasher.update(self.android_id.as_bytes());
        hasher.update(self.model.as_bytes());
        let res = hasher.finalize();
        base64::encode(res)
    }
}

mod test_utils {
    use super::*;

    // Starts a dummy system server that initializes the SystemKeypair.
    #[allow(dead_code)]
    pub fn start_dummy_system_server(process_command: ProcessFn) {
        task::spawn(async move {
            let mut server = CommandServer {
                host: SysStateDefaultConfig::BIND_HOST.to_string(),
                port: SysStateDefaultConfig::BIND_PORT,
            };

            server.start(process_command).await.unwrap();
        });
    }

    #[allow(dead_code)]
    pub fn get_peer_with_key(key: &Key) -> (SocketAddr, Arc<Key>) {
        let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
        (socket_addr, Arc::new(key.clone()))
    }
}

#[cfg(test)]

mod tests {
    use super::*;
    use crate::net::TCPClient;
    use crate::util::{test_with_logs, SysStateDefaultConfig};
    use request::{MarketMakerRequest, MarketMakerResponse, Request};
    use serial_test::serial;

    #[test]
    #[serial]
    fn init_and_stop_all_modes() {
        test_with_logs();

        assert!(!SysState::is_initialized());

        thread::spawn(|| SysState::start_system().unwrap());

        let client = TCPClient::new("localhost", SysStateDefaultConfig::BIND_PORT).unwrap();

        let data = serde_json::to_string(&Request::System(SysStateRequest::GetState)).unwrap();
        let response = client.send(&data, None).unwrap();
        let expected_response = serde_json::to_string(&SysStateResponse::CurrentSysState {
            state: SysStateMin::default(),
        })
        .unwrap();
        assert_eq!(response, expected_response);

        let data = serde_json::to_string(&Request::System(SysStateRequest::GetPeerId)).unwrap();
        let response = client.send(&data, None).unwrap();
        let pub_key = SystemKeypair::get_public_key().map_or(String::new(), base64::encode);
        let expected_response =
            serde_json::to_string(&SysStateResponse::PeerId { peer_id: pub_key }).unwrap();
        assert_eq!(response, expected_response);

        System::start_market_maker().unwrap();

        let data = serde_json::to_string(&Request::MarketMaker(MarketMakerRequest::Test)).unwrap();
        let response = client.send(&data, None).unwrap();
        let expected_response = serde_json::to_string(&MarketMakerResponse::Test).unwrap();
        assert_eq!(response, expected_response);

        System::start_supplier_and_connect(
            "localhost",
            SysStateDefaultConfig::BIND_PORT,
            None,
            false,
        )
        .unwrap();
        System::start_consumer_and_connect("localhost", SysStateDefaultConfig::BIND_PORT, None)
            .unwrap();

        assert!(SysState::is_initialized());
        assert!(SysState::market_maker_is_some());
        assert!(SysState::supplier_is_some());
        assert!(SysState::consumer_is_some());

        System::server_shutdown();

        assert!(!SysState::is_initialized());
        assert!(!SysState::market_maker_is_some());
        assert!(!SysState::supplier_is_some());
        assert!(!SysState::consumer_is_some());
    }
}