cloudi 2.0.7

Rust CloudI API
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
//-*-Mode:rust;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
//ex: set ft=rust fenc=utf-8 sts=4 ts=4 sw=4 et nomod:

//! # Rust CloudI API

// MIT License
//
// Copyright (c) 2023 Michael Truog <mjtruog at protonmail dot com>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#![crate_name = "cloudi"]
#![crate_type = "lib"]

extern crate erlang;

use std::backtrace::Backtrace;
use std::collections::HashMap;
use std::collections::LinkedList;
use std::io::Read;
use std::io::Write;
use std::net::TcpStream;
use std::net::UdpSocket;
use std::os::fd::FromRawFd;
use std::os::raw::c_int;
use std::time::Duration;
use std::time::Instant;
use erlang::OtpErlangTerm;

pub type Result<T> = std::result::Result<T, Error>;

/// provided when handling a service request
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum RequestType {
    ASYNC = 1,
    SYNC = -1,
}
/// the timeout value in milliseconds
pub type Timeout = u32;
/// the service request priority when queued
pub type Priority = i8;
/// the Erlang pid that is the source of the service request
pub type Source = OtpErlangTerm;

/// service request callback function return type
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum Response {
    Response(Vec<u8>),
    ResponseInfo(Vec<u8>, Vec<u8>),
    Forward(String, Vec<u8>, Vec<u8>),
    Forward_(String, Vec<u8>, Vec<u8>, Timeout, Priority),
    Null(),
    NullError(&'static str),
}

impl From<Error> for Response {
    fn from(error: Error) -> Self {
        match error.kind {
            ErrorKind::Terminate {..} => {
                Response::Null()
            },
            _ => {
                eprintln!("{:#?}", error);
                std::process::abort()
            },
        }
    }
}

/// unwrap macro (like ?) for handling the Terminate error in a callback
#[macro_export]
macro_rules! unwrap {
    ($expr:expr $(,)?) => {
        match $expr {
            std::result::Result::Ok(value) => {
                value
            },
            std::result::Result::Err(error) => {
                return cloudi::Response::from(error);
            },
        }
    };
}

/// service request transaction id
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct TransId {
    id: [u8; 16],
}

impl TransId {
    pub fn new(id: &[u8]) -> Self {
        TransId {
            id: id.try_into().unwrap(),
        }
    }

    pub fn null() -> Self {
        TransId {
            id: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        }
    }

    pub fn is_timeout(&self) -> bool {
        self == &TransId::null()
    }

    pub fn as_slice(&self) -> &[u8] {
        self.id.as_slice()
    }

    pub fn to_vec(&self) -> Vec<u8> {
        self.id.to_vec()
    }
}

/// a function pointer to handle a service request
pub type Callback<'s, S> = fn(&RequestType,
                              &str,
                              &str,
                              &[u8],
                              &[u8],
                              Timeout,
                              Priority,
                              &TransId,
                              &Source,
                              &mut S,
                              &mut API<'s, S>) -> Response;

/// fatal error macro for using panic!
#[macro_export]
macro_rules! fatal {
    ($($arg:tt)*) => {{
        panic!($($arg)*);
    }};
}

/// Error description
#[derive(Debug, Eq, PartialEq)]
pub enum ErrorKind {
    Terminate { timeout: Timeout },
    InvalidInputError(),
    MessageDecodingError(),
    UnexpectedError(),
}

/// Error data
#[derive(Debug)]
pub struct Error {
    pub kind: ErrorKind,
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
    _backtrace: Backtrace,
}

impl Error {
    fn new<E>(error: E) -> Self
    where E: Into<Box<dyn std::error::Error + Send + Sync>>, {
        Error {
            kind: ErrorKind::UnexpectedError(),
            source: Some(error.into()),
            _backtrace: Backtrace::capture(),
        }
    }
}

impl PartialEq<Error> for Error {
    fn eq(&self, other: &Error) -> bool {
        self.kind == (*other).kind
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.kind)
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.source {
            None => None,
            Some(e) => Some(&**e),
        }
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Error {
            kind,
            source: None,
            _backtrace: Backtrace::capture(),
        }
    }
}

impl From<erlang::Error> for Error {
    fn from(error: erlang::Error) -> Self {
        Error::new(error)
    }
}

impl From<std::env::VarError> for Error {
    fn from(error: std::env::VarError) -> Self {
        Error::new(error)
    }
}

impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Self {
        Error::new(error)
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(error: std::num::ParseIntError) -> Self {
        Error::new(error)
    }
}

impl From<std::string::FromUtf8Error> for Error {
    fn from(error: std::string::FromUtf8Error) -> Self {
        Error::new(error)
    }
}

/// an instance of the CloudI API
pub struct API<'s, S> {
    state: &'s mut S,
    callbacks: HashMap<String, LinkedList<Callback<'s, S>>>,
    buffer_recv: Vec<u8>,
    buffer_size: usize,
    socket_kind: SocketKind,
    process_index: u32,
    process_count: u32,
    process_count_min: u32,
    process_count_max: u32,
    prefix: String,
    timeout_initialize: u32,
    timeout_async: u32,
    timeout_sync: u32,
    timeout_terminate: u32,
    priority_default: i8,
    use_header: bool,
    initialization_complete: bool,
    terminate: bool,
    response: Option<(Vec<u8>, Vec<u8>, TransId)>,
    trans_id: Option<TransId>,
    trans_ids: Option<Vec<TransId>>,
    subscribe_count: Option<u32>,
}

/// the number of threads to create per operating system process
pub fn thread_count() -> Result<u32> {
    getenv_to_u32("CLOUDI_API_INIT_THREAD_COUNT")
}

impl<'s, S: 's> API<'s, S> {
    /// creates an instance of the CloudI API
    pub fn new(thread_index: u32,
               state: &'s mut S) -> Result<Self> {
        let protocol = match std::env::var("CLOUDI_API_INIT_PROTOCOL") {
            Ok(protocol_str) => protocol_str,
            Err(_) => {
                eprintln!("CloudI service execution must occur in CloudI");
                return Err(ErrorKind::InvalidInputError().into());
            },
        };
        let buffer_size =
            getenv_to_u32("CLOUDI_API_INIT_BUFFER_SIZE")? as usize;
        let buffer_recv = Vec::with_capacity(buffer_size);
        let fd: c_int = (thread_index + 3) as i32;
        let (socket_kind, use_header) = match protocol.as_str() {
            "tcp" | "local" => {
                (SocketKind::TCP(unsafe { TcpStream::from_raw_fd(fd) }), true)
            },
            "udp" => {
                (SocketKind::UDP(unsafe { UdpSocket::from_raw_fd(fd) }), false)
            },
            _ => {
                return Err(ErrorKind::InvalidInputError().into());
            },
        };
        let mut api = API {
            state,
            callbacks: HashMap::new(),
            buffer_recv,
            buffer_size,
            socket_kind,
            process_index: 0,
            process_count: 0,
            process_count_min: 0,
            process_count_max: 0,
            prefix: String::new(),
            timeout_initialize: 0,
            timeout_async: 0,
            timeout_sync: 0,
            timeout_terminate: 10, // TIMEOUT_TERMINATE_MIN
            priority_default: 0,
            use_header,
            initialization_complete: false,
            terminate: false,
            response: None,
            trans_id: None,
            trans_ids: None,
            subscribe_count: None,
        };
        api.send(&OtpErlangTerm::OtpErlangAtomUTF8(b"init".to_vec()))?;
        let _ = api.poll_request(&None, false)?;
        Ok(api)
    }

    /// subscribes to a service name pattern with a function pointer callback
    /// (or a closure callback without a context)
    pub fn subscribe(&mut self, pattern: &str,
                     f: Callback<'s, S>) -> Result<()> {
        let key = self.prefix.clone() + pattern;
        match self.callbacks.get_mut(&key) {
            None => {
                let mut callback_list = LinkedList::new();
                callback_list.push_back(f);
                let _ = self.callbacks.insert(key, callback_list);
            },
            Some(callback_list) => {
                callback_list.push_back(f);
            },
        };
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"subscribe".to_vec()),
            OtpErlangTerm::OtpErlangString(pattern.into()),
        ]))
    }

    /// determine how many service name pattern subscriptions have occurred
    pub fn subscribe_count(&mut self, pattern: &str) -> Result<u32> {
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"subscribe_count".to_vec()),
            OtpErlangTerm::OtpErlangString(pattern.into()),
        ]))?;
        let _ = self.poll_request(&None, false)?;
        Ok(self.subscribe_count.take().unwrap())
    }

    /// unsubscribes from a service name pattern
    pub fn unsubscribe(&mut self, pattern: &str) -> Result<()> {
        let key = self.prefix.clone() + pattern;
        let callback_list = self.callbacks.get_mut(&key).unwrap();
        let _ = callback_list.pop_front().unwrap();
        if callback_list.is_empty() {
            self.callbacks.remove(&key).unwrap();
        }
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"unsubscribe".to_vec()),
            OtpErlangTerm::OtpErlangString(pattern.into()),
        ]))
    }

    /// asynchronous point-to-point communication to a service
    pub fn send_async(&mut self, name: &str, request: &Vec<u8>,
                      timeout_opt: Option<u32>,
                      request_info_opt: Option<Vec<u8>>,
                      priority_opt: Option<i8>) -> Result<TransId> {
        let timeout = timeout_opt.unwrap_or(self.timeout_async);
        let request_info = request_info_opt.unwrap_or(b"".to_vec());
        let priority = priority_opt.unwrap_or(self.priority_default);
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"send_async".to_vec()),
            OtpErlangTerm::OtpErlangString(name.into()),
            OtpErlangTerm::OtpErlangBinary(request_info),
            OtpErlangTerm::OtpErlangBinary(request.clone()),
            OtpErlangTerm::OtpErlangInteger(timeout as i32),
            OtpErlangTerm::OtpErlangInteger(priority as i32),
        ]))?;
        let _ = self.poll_request(&None, false)?;
        Ok(self.trans_id.take().unwrap())
    }

    /// synchronous point-to-point communication to a service
    pub fn send_sync(&mut self, name: &str, request: &Vec<u8>,
                     timeout_opt: Option<u32>,
                     request_info_opt: Option<Vec<u8>>,
                     priority_opt: Option<i8>) ->
                     Result<(Vec<u8>, Vec<u8>, TransId)> {
        let timeout = timeout_opt.unwrap_or(self.timeout_sync);
        let request_info = request_info_opt.unwrap_or(b"".to_vec());
        let priority = priority_opt.unwrap_or(self.priority_default);
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"send_sync".to_vec()),
            OtpErlangTerm::OtpErlangString(name.into()),
            OtpErlangTerm::OtpErlangBinary(request_info),
            OtpErlangTerm::OtpErlangBinary(request.clone()),
            OtpErlangTerm::OtpErlangInteger(timeout as i32),
            OtpErlangTerm::OtpErlangInteger(priority as i32),
        ]))?;
        let _ = self.poll_request(&None, false)?;
        Ok(self.response.take().unwrap())
    }

    /// asynchronous point-multicast communication to services
    pub fn mcast_async(&mut self, name: &str, request: &Vec<u8>,
                       timeout_opt: Option<u32>,
                       request_info_opt: Option<Vec<u8>>,
                       priority_opt: Option<i8>) -> Result<Vec<TransId>> {
        let timeout = timeout_opt.unwrap_or(self.timeout_async);
        let request_info = request_info_opt.unwrap_or(b"".to_vec());
        let priority = priority_opt.unwrap_or(self.priority_default);
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"mcast_async".to_vec()),
            OtpErlangTerm::OtpErlangString(name.into()),
            OtpErlangTerm::OtpErlangBinary(request_info),
            OtpErlangTerm::OtpErlangBinary(request.clone()),
            OtpErlangTerm::OtpErlangInteger(timeout as i32),
            OtpErlangTerm::OtpErlangInteger(priority as i32),
        ]))?;
        let _ = self.poll_request(&None, false)?;
        Ok(self.trans_ids.take().unwrap())
    }

    /// asynchronously receive a response
    pub fn recv_async(&mut self, timeout_opt: Option<u32>,
                      trans_id_opt: Option<TransId>,
                      consume_opt: Option<bool>) ->
                      Result<(Vec<u8>, Vec<u8>, TransId)> {
        let timeout = timeout_opt.unwrap_or(self.timeout_sync);
        let trans_id = trans_id_opt.unwrap_or(TransId::null());
        let consume = consume_opt.unwrap_or(true);
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"recv_async".to_vec()),
            OtpErlangTerm::OtpErlangInteger(timeout as i32),
            OtpErlangTerm::OtpErlangBinary(trans_id.to_vec()),
            OtpErlangTerm::OtpErlangAtomBool(consume),
        ]))?;
        let _ = self.poll_request(&None, false)?;
        Ok(self.response.take().unwrap())
    }

    /// the 0-based index of this process in the service instance
    pub fn process_index(&self) -> u32 {
        self.process_index
    }

    /// the current process count based on the service configuration
    pub fn process_count(&self) -> u32 {
        self.process_count
    }

    /// the count_process_dynamic maximum count based on the
    /// service configuration
    pub fn process_count_max(&self) -> u32 {
        self.process_count_max
    }

    /// the count_process_dynamic minimum count based on the
    /// service configuration
    pub fn process_count_min(&self) -> u32 {
        self.process_count_min
    }

    /// the service name pattern prefix from the service configuration
    pub fn prefix(&self) -> String {
        self.prefix.clone()
    }

    /// the service initialization timeout from the service configuration
    pub fn timeout_initialize(&self) -> u32 {
        self.timeout_initialize
    }

    /// the default asynchronous service request send timeout from
    /// the service configuration
    pub fn timeout_async(&self) -> u32 {
        self.timeout_async
    }

    /// the default synchronous service request send timeout from
    /// the service configuration
    pub fn timeout_sync(&self) -> u32 {
        self.timeout_sync
    }

    /// the service termination timeout based on the service configuration
    pub fn timeout_terminate(&self) -> u32 {
        self.timeout_terminate
    }

    /// the default service request send priority from the
    /// service configuration
    pub fn priority_default(&self) -> i8 {
        self.priority_default
    }

    fn callback(&mut self, request_type: &RequestType,
                name: &str, pattern: &str,
                request_info: &[u8], request: &[u8],
                timeout: u32, priority: i8, trans_id: &[u8],
                source: OtpErlangTerm) -> Result<()> {
        // To avoid overhead and maintain the same CloudI service
        // functionality as the other CloudI API implementations,
        // it is necessary to not support closures with contexts in Rust
        // (std::ops::{Fn, FnMut, FnOnce}).
        // The callback function needs to exist in self.callbacks during the
        // callback execution and self needs to be provided as a
        // mutable reference, so the callback needs to be a
        // function pointer (or a closure without a context).
        let callback_f = match self.callbacks.get_mut(pattern) {
            None => {
                null_response as Callback<'s, S>
            },
            Some(callback_list) => {
                let f = callback_list.pop_front().unwrap();
                callback_list.push_back(f);
                f
            },
        };
        let state_p = self.state as *mut S;
        let callback_result = match callback_f(request_type,
                                               name, pattern,
                                               request_info, request,
                                               timeout, priority,
                                               &TransId::new(trans_id),
                                               &source,
                                               unsafe { &mut *state_p },
                                               self) {
            Response::Response(response) => {
                CallbackResult::ReturnI(b"".to_vec(), response)
            },
            Response::ResponseInfo(response_info, response) => {
                CallbackResult::ReturnI(response_info, response)
            },
            Response::Forward(name_next, response_info, response) => {
                CallbackResult::ForwardI(name_next, response_info, response,
                                         timeout, priority)
            },
            Response::Forward_(name_next, response_info, response,
                               timeout_next, priority_next) => {
                CallbackResult::ForwardI(name_next, response_info, response,
                                         timeout_next, priority_next)
            },
            Response::Null() => {
                CallbackResult::ReturnI(b"".to_vec(), b"".to_vec())
            },
            Response::NullError(error) => {
                eprintln!("{error}");
                CallbackResult::ReturnI(b"".to_vec(), b"".to_vec())
            },
        };
        match callback_result {
            CallbackResult::ReturnI(response_info, response) => {
                let message_type = match request_type {
                    RequestType::ASYNC => b"return_async".to_vec(),
                    RequestType::SYNC => b"return_sync".to_vec(),
                };
                self.send(&OtpErlangTerm::OtpErlangTuple(vec![
                    OtpErlangTerm::OtpErlangAtom(message_type),
                    OtpErlangTerm::OtpErlangString(name.into()),
                    OtpErlangTerm::OtpErlangString(pattern.into()),
                    OtpErlangTerm::OtpErlangBinary(response_info),
                    OtpErlangTerm::OtpErlangBinary(response),
                    OtpErlangTerm::OtpErlangInteger(timeout as i32),
                    OtpErlangTerm::OtpErlangBinary(trans_id.to_vec()),
                    source,
                ]))
            },
            CallbackResult::ForwardI(name_next, request_info_next, request_next,
                                     timeout_next, priority_next) => {
                let message_type = match request_type {
                    RequestType::ASYNC => b"forward_async".to_vec(),
                    RequestType::SYNC => b"forward_sync".to_vec(),
                };
                self.send(&OtpErlangTerm::OtpErlangTuple(vec![
                    OtpErlangTerm::OtpErlangAtom(message_type),
                    OtpErlangTerm::OtpErlangString(name_next.into_bytes()),
                    OtpErlangTerm::OtpErlangBinary(request_info_next),
                    OtpErlangTerm::OtpErlangBinary(request_next),
                    OtpErlangTerm::OtpErlangInteger(timeout_next as i32),
                    OtpErlangTerm::OtpErlangInteger(priority_next as i32),
                    OtpErlangTerm::OtpErlangBinary(trans_id.to_vec()),
                    source,
                ]))
            },
        }
    }

    fn handle_events(&mut self, external: bool, data: &[u8], data_size: usize,
                     i: &mut usize, mut command: u32) -> Result<bool> {
        if command == 0 {
            command = unpack_incoming_u32(i, data)?;
        }
        loop {
            match command {
                MESSAGE_TERM => {
                    self.terminate = true;
                    if external {
                        return Ok(false);
                    }
                    else {
                        let error = ErrorKind::Terminate{
                            timeout: self.timeout_terminate,
                        };
                        return Err(error.into());
                    }
                },
                MESSAGE_REINIT => {
                    self.process_count = unpack_incoming_u32(i, data)?;
                    self.timeout_async = unpack_incoming_u32(i, data)?;
                    self.timeout_sync = unpack_incoming_u32(i, data)?;
                    self.priority_default = unpack_incoming_i8(i, data)?;
                    let fatal_exceptions = unpack_incoming_bool(i, data)?;
                    if fatal_exceptions == false {
                        return Err(ErrorKind::InvalidInputError().into());
                    }
                },
                MESSAGE_KEEPALIVE => {
                    self.send(&OtpErlangTerm::OtpErlangAtomUTF8(
                        b"keepalive".to_vec()))?;
                },
                _ => {
                    return Err(ErrorKind::MessageDecodingError().into());
                },
            }
            if *i == data_size {
                return Ok(true);
            }
            command = unpack_incoming_u32(i, data)?;
        }
    }

    fn poll_request(&mut self, timeout: &Option<Duration>,
                    external: bool) -> Result<bool> {
        if self.terminate {
            if external {
                return Ok(false);
            }
            else {
                let error = ErrorKind::Terminate{
                    timeout: self.timeout_terminate,
                };
                return Err(error.into());
            }
        }
        else if external && ! self.initialization_complete {
            self.send(&OtpErlangTerm::OtpErlangAtomUTF8(b"polling".to_vec()))?;
            self.initialization_complete = true;
        }
        let poll_timer = match timeout {
            Some(_) => Some(Instant::now()),
            None => None,
        };
        let mut buffer = self.recv(timeout)?;
        let mut data_size = buffer.len();
        if data_size == 0 {
            return Ok(true);
        }
        let mut i_value: usize = 0;
        let i = &mut i_value;
        let mut data: &[u8] = buffer.as_slice();
        loop {
            let command = unpack_incoming_u32(i, data)?;
            match command {
                MESSAGE_INIT => {
                    self.process_index = unpack_incoming_u32(i, data)?;
                    self.process_count = unpack_incoming_u32(i, data)?;
                    self.process_count_max = unpack_incoming_u32(i, data)?;
                    self.process_count_min = unpack_incoming_u32(i, data)?;
                    let prefix_size =
                        unpack_incoming_u32(i, data)? as usize;
                    self.prefix = (unpack_incoming_str(i, prefix_size,
                                                       data)?).to_string();
                    self.timeout_initialize = unpack_incoming_u32(i, data)?;
                    self.timeout_async = unpack_incoming_u32(i, data)?;
                    self.timeout_sync = unpack_incoming_u32(i, data)?;
                    self.timeout_terminate = unpack_incoming_u32(i, data)?;
                    self.priority_default = unpack_incoming_i8(i, data)?;
                    let fatal_exceptions = unpack_incoming_bool(i, data)?;
                    if fatal_exceptions == false {
                        return Err(ErrorKind::InvalidInputError().into());
                    }
                    let bind = unpack_incoming_i32(i, data)?;
                    if bind >= 0 {
                        return Err(ErrorKind::InvalidInputError().into());
                    }
                    if *i != data_size {
                        assert_eq!(false, external);
                        let _ = self.handle_events(external, data, data_size,
                                                   i, 0)?;
                    }
                    return Ok(false);
                },
                MESSAGE_SEND_ASYNC |
                MESSAGE_SEND_SYNC => {
                    let request_type = if command == MESSAGE_SEND_ASYNC {
                        RequestType::ASYNC
                    }
                    else { 
                        assert_eq!(MESSAGE_SEND_SYNC, command);
                        RequestType::SYNC
                    };
                    let name_size = unpack_incoming_u32(i, data)? as usize;
                    let name = unpack_incoming_str(i, name_size, data)?;
                    let pattern_size = unpack_incoming_u32(i, data)? as usize;
                    let pattern = unpack_incoming_str(i, pattern_size, data)?;
                    let request_info_size =
                        unpack_incoming_u32(i, data)? as usize;
                    let request_info =
                        unpack_incoming_bytes_1(i, request_info_size, data)?;
                    let request_size = unpack_incoming_u32(i, data)? as usize;
                    let request =
                        unpack_incoming_bytes_1(i, request_size, data)?;
                    let request_timeout = unpack_incoming_u32(i, data)?;
                    let priority = unpack_incoming_i8(i, data)?;
                    let trans_id = unpack_incoming_bytes(i, 16, data)?;
                    let source_size = unpack_incoming_u32(i, data)? as usize;
                    let source = unpack_incoming_pid(i, source_size, data)?;
                    if *i != data_size {
                        assert_eq!(true, external);
                        if ! self.handle_events(external, data, data_size,
                                                i, 0)? {
                            return Ok(false);
                        }
                    }
                    self.callback(&request_type, name, pattern,
                                  request_info, request,
                                  request_timeout, priority,
                                  trans_id, source)?;
                    if self.terminate {
                        return Ok(false);
                    }
                },
                MESSAGE_RECV_ASYNC |
                MESSAGE_RETURN_SYNC => {
                    let response_info_size =
                        unpack_incoming_u32(i, data)? as usize;
                    let response_info =
                        unpack_incoming_bytes_1(i, response_info_size, data)?;
                    let response_size = unpack_incoming_u32(i, data)? as usize;
                    let response =
                        unpack_incoming_bytes_1(i, response_size, data)?;
                    let trans_id = unpack_incoming_bytes(i, 16, data)?;
                    if *i != data_size {
                        assert_eq!(false, external);
                        let _ = self.handle_events(external, data, data_size,
                                                   i, 0)?;
                    }
                    self.response = Some((response_info.to_vec(),
                                          response.to_vec(),
                                          TransId::new(trans_id)));
                    return Ok(false);
                },
                MESSAGE_RETURN_ASYNC => {
                    let trans_id = unpack_incoming_bytes(i, 16, data)?;
                    if *i != data_size {
                        assert_eq!(false, external);
                        let _ = self.handle_events(external, data, data_size,
                                                   i, 0)?;
                    }
                    self.trans_id = Some(TransId::new(trans_id));
                    return Ok(false);
                },
                MESSAGE_RETURNS_ASYNC => {
                    let trans_id_count =
                        unpack_incoming_u32(i, data)? as usize;
                    let mut trans_ids: Vec<TransId> = Vec::new();
                    for _ in 0..trans_id_count {
                        let trans_id = unpack_incoming_bytes(i, 16, data)?;
                        trans_ids.push(TransId::new(trans_id));
                    }
                    self.trans_ids = Some(trans_ids);
                    return Ok(false);
                },
                MESSAGE_SUBSCRIBE_COUNT => {
                    let count = unpack_incoming_u32(i, data)?;
                    self.subscribe_count = Some(count);
                    if *i != data_size {
                        assert_eq!(false, external);
                        let _ = self.handle_events(external, data, data_size,
                                                   i, 0)?;
                    }
                    return Ok(false);
                },
                MESSAGE_TERM => {
                    if ! self.handle_events(external, data, data_size,
                                            i, command)? {
                        return Ok(false);
                    }
                    assert!(false);
                },
                MESSAGE_REINIT => {
                    self.process_count = unpack_incoming_u32(i, data)?;
                    self.timeout_async = unpack_incoming_u32(i, data)?;
                    self.timeout_sync = unpack_incoming_u32(i, data)?;
                    self.priority_default = unpack_incoming_i8(i, data)?;
                    let fatal_exceptions = unpack_incoming_bool(i, data)?;
                    if fatal_exceptions == false {
                        return Err(ErrorKind::InvalidInputError().into());
                    }
                    if *i != data_size {
                        continue
                    }
                },
                MESSAGE_KEEPALIVE => {
                    self.send(&OtpErlangTerm::OtpErlangAtomUTF8(
                        b"keepalive".to_vec()))?;
                    if *i != data_size {
                        continue
                    }
                },
                _ => {
                    return Err(ErrorKind::MessageDecodingError().into());
                },
            };
            let timeout_value = match timeout {
                Some(duration) => {
                    let timeout_value_new = 
                        duration.checked_sub(poll_timer.unwrap().elapsed());
                    if timeout_value_new == None {
                        return Ok(true);
                    }
                    timeout_value_new
                },
                None => None,
            };
            buffer = self.recv(&timeout_value)?;
            data_size = buffer.len();
            if data_size == 0 {
                return Ok(true);
            }
            *i = 0;
            data = buffer.as_slice();
        }
    }

    /// blocks to process incoming CloudI service requests
    pub fn poll(&mut self, timeout: i32) -> Result<bool> {
        let timeout_opt: Option<Duration> = if timeout < 0 {
            None
        }
        else if timeout == 0 {
            Some(Duration::from_millis(1))
        }
        else {
            Some(Duration::from_millis(timeout as u64))
        };
        self.poll_request(&timeout_opt, true)
    }

    /// shutdown the service successfully
    pub fn shutdown(&mut self, reason_opt: Option<String>) -> Result<()> {
        let reason = reason_opt.unwrap_or("".to_string());
        self.send(&OtpErlangTerm::OtpErlangTuple(vec![
            OtpErlangTerm::OtpErlangAtom(b"shutdown".to_vec()),
            OtpErlangTerm::OtpErlangString(reason.into_bytes()),
        ]))
    }

    fn send(&mut self, command: &OtpErlangTerm) -> Result<()> {
        let data = erlang::term_to_binary(command)?;
        if self.use_header {
            let length = data.len() as u32;
            self.send_data(&length.to_be_bytes())?;
        }
        self.send_data(data.as_slice())
    }

    fn send_data(&mut self, data: &[u8]) -> Result<()> {
        match &mut self.socket_kind {
            SocketKind::TCP(socket) => {
                Ok(socket.write_all(data)?)
            },
            SocketKind::UDP(socket) => {
                let _ = socket.send(data)?;
                Ok(())
            },
        }
    }

    fn recv(&mut self, timeout: &Option<Duration>) -> Result<Vec<u8>> {
        let mut size = self.buffer_recv.len();
        assert_eq!(0, size);
        match &mut self.socket_kind {
            SocketKind::TCP(socket) => {
                socket.set_read_timeout(*timeout)?;
                assert_eq!(true, self.use_header);
                let mut header: [u8; 4] = [0; 4];
                socket.read_exact(&mut header)?;
                let length = u32::from_be_bytes(header);
                self.buffer_recv.resize(size + length as usize, 0);
                let mut data: &mut [u8] = &mut self.buffer_recv[size..];
                socket.read_exact(&mut data)?;
            },
            SocketKind::UDP(socket) => {
                socket.set_read_timeout(*timeout)?;
                assert_eq!(false, self.use_header);
                self.buffer_recv.resize(size + self.buffer_size, 0);
                let mut data: &mut [u8] = &mut self.buffer_recv[size..];
                let mut length = socket.recv(&mut data)?;
                while length == self.buffer_size {
                    size += length;
                    self.buffer_recv.resize(size + self.buffer_size, 0);
                    data = &mut self.buffer_recv[size..];
                    length = socket.recv(&mut data)?;
                }
                self.buffer_recv.resize(size + length, 0);
            },
        };
        let buffer = self.buffer_recv.to_vec();
        self.buffer_recv.clear();
        Ok(buffer)
    }
}

/// the 0-based index of this process in the service instance
pub fn process_index() -> Result<u32> {
    getenv_to_u32("CLOUDI_API_INIT_PROCESS_INDEX")
}

/// the count_process_dynamic maximum count based on the service configuration
pub fn process_count_max() -> Result<u32> {
    getenv_to_u32("CLOUDI_API_INIT_PROCESS_COUNT_MAX")
}

/// the count_process_dynamic minimum count based on the service configuration
pub fn process_count_min() -> Result<u32> {
    getenv_to_u32("CLOUDI_API_INIT_PROCESS_COUNT_MIN")
}

/// the service initialization timeout from the service configuration
pub fn timeout_initialize() -> Result<u32> {
    getenv_to_u32("CLOUDI_API_INIT_TIMEOUT_INITIALIZE")
}

/// the service termination timeout based on the service configuration
pub fn timeout_terminate() -> Result<u32> {
    getenv_to_u32("CLOUDI_API_INIT_TIMEOUT_TERMINATE")
}

fn text_pairs_parse(text: &[u8]) -> HashMap<String, Vec<String>> {
    let mut pairs: HashMap<String, Vec<String>> = HashMap::new();
    let data: Vec<String> = text.split(|c| *c == 0).map(|element| {
        unsafe {
            std::str::from_utf8_unchecked(element)
        }.to_string()
    }).collect();
    for i in (0..(data.len() - 1)).step_by(2) {
        let key = &data[i];
        let value = &data[i + 1];
        match pairs.get_mut(key) {
            None => {
                let _ = pairs.insert(key.clone(), vec![value.clone()]);
            },
            Some(current) => {
                current.push(value.clone());
            },
        };
    }
    pairs
}

fn text_pairs_new(pairs: &HashMap<String, Vec<String>>,
                  response: bool) -> Vec<u8> {
    let mut text_segments: Vec<u8> = Vec::new();
    for (key, values) in pairs.iter() {
        for value in values.iter() {
            text_segments.extend_from_slice(key.as_bytes());
            text_segments.push(0);
            text_segments.extend_from_slice(value.as_bytes());
            text_segments.push(0);
        }
    }
    if response && text_segments.is_empty() {
        vec![0]
    }
    else {
        text_segments
    }
}

/// decode service request info key/value data
pub fn info_key_value_parse(info: &[u8]) -> HashMap<String, Vec<String>> {
    text_pairs_parse(info)
}

/// encode service response info key/value data
pub fn info_key_value_new(pairs: &HashMap<String, Vec<String>>,
                          response_opt: Option<bool>) -> Vec<u8> {
    text_pairs_new(pairs, response_opt.unwrap_or(true))
}

const MESSAGE_INIT: u32 = 1;
const MESSAGE_SEND_ASYNC: u32 = 2;
const MESSAGE_SEND_SYNC: u32 = 3;
const MESSAGE_RECV_ASYNC: u32 = 4;
const MESSAGE_RETURN_ASYNC: u32 = 5;
const MESSAGE_RETURN_SYNC: u32 = 6;
const MESSAGE_RETURNS_ASYNC: u32 = 7;
const MESSAGE_KEEPALIVE: u32 = 8;
const MESSAGE_REINIT: u32 = 9;
const MESSAGE_SUBSCRIBE_COUNT: u32 = 10;
const MESSAGE_TERM: u32 = 11;

enum CallbackResult {
    ReturnI(Vec<u8>, Vec<u8>),
    ForwardI(String, Vec<u8>, Vec<u8>, Timeout, Priority),
}

#[derive(Debug)]
enum SocketKind {
    TCP(TcpStream),
    UDP(UdpSocket),
}

fn null_response<'s, S>(_request_type: &RequestType,
                        _name: &str,
                        _pattern: &str,
                        _request_info: &[u8],
                        _request: &[u8],
                        _timeout: Timeout,
                        _priority: Priority,
                        _trans_id: &TransId,
                        _source: &Source,
                        _state: &mut S,
                        _api: &mut API<'s, S>) -> Response {
    Response::Null()
}

fn getenv_to_u32(name: &'static str) -> Result<u32> {
    match std::env::var(name) {
        Ok(value) => {
            match value.parse::<u32>() {
                Ok(value_u32) => Ok(value_u32),
                Err(_) => Err(ErrorKind::InvalidInputError().into()),
            }
        },
        Err(_) => Err(ErrorKind::InvalidInputError().into()),
    }
}

fn unpack_incoming_str<'s>(i: &mut usize, size: usize,
                           data: &'s [u8]) -> Result<&'s str> {
    let s = unsafe {
        std::str::from_utf8_unchecked(slice_get(data, *i..*i + size - 1)?)
    };
    *i += size;
    Ok(s)
}

fn unpack_incoming_bytes<'s>(i: &mut usize, size: usize,
                             data: &'s [u8]) -> Result<&'s [u8]> {
    let bytes = slice_get(data, *i..*i + size)?;
    *i += size;
    Ok(bytes)
}

fn unpack_incoming_bytes_1<'s>(i: &mut usize, size: usize,
                               data: &'s [u8]) -> Result<&'s [u8]> {
    let bytes = unpack_incoming_bytes(i, size, data)?;
    // skip C string null termination character
    *i += 1;
    Ok(bytes)
}

fn unpack_incoming_pid(i: &mut usize, size: usize,
                       data: &[u8]) -> Result<OtpErlangTerm> {
    let pid = erlang::binary_to_term(slice_get(data, *i..*i + size)?)?;
    *i += size;
    Ok(pid)
}

fn unpack_incoming_u32(i: &mut usize, data: &[u8]) -> Result<u32> {
    let bytes: [u8; 4] = (slice_get(data, *i..*i + 4)?).try_into().unwrap();
    *i += 4;
    Ok(u32::from_ne_bytes(bytes))
}

fn unpack_incoming_i32(i: &mut usize, data: &[u8]) -> Result<i32> {
    let bytes: [u8; 4] = (slice_get(data, *i..*i + 4)?).try_into().unwrap();
    *i += 4;
    Ok(i32::from_ne_bytes(bytes))
}

fn unpack_incoming_i8(i: &mut usize, data: &[u8]) -> Result<i8> {
    let value: i8 = *slice_get(data, *i)? as i8;
    *i += 1;
    Ok(value)
}

fn unpack_incoming_bool(i: &mut usize, data: &[u8]) -> Result<bool> {
    let value = if *slice_get(data, *i)? == 0 { false } else { true };
    *i += 1;
    Ok(value)
}

fn slice_get<I>(data: &[u8], index: I) ->
Result<&<I as std::slice::SliceIndex<[u8]>>::Output>
where I: std::slice::SliceIndex<[u8]>, {
    match data.get(index) {
        Some(result) => Ok(result),
        None => Err(ErrorKind::MessageDecodingError().into()),
    }
}