aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/sessioninit.rs
blob: e2a29ad3c064806a15a4e612ac4f3ed491c5dffd (plain)
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
use crate::error::ProtocolError;
use crate::message::objects::Identity;
use crate::primitive::{BufferInfo, NetworkId, Variant, VariantList, VariantMap};
use crate::HandshakeSerialize;

/// SessionInit is received along with ClientLoginAck to initialize that user Session
// TODO Replace with proper types
#[derive(Debug, Clone)]
pub struct SessionInit {
    /// List of all configured identities
    pub identities: Vec<Identity>,
    /// List of all existing buffers
    pub buffers: Vec<BufferInfo>, // Vec<Variant::BufferInfo()>
    /// Ids of all networks
    pub network_ids: Vec<NetworkId>,
}

impl From<VariantMap> for SessionInit {
    fn from(input: VariantMap) -> Self {
        let mut state: VariantMap = input.get("SessionState").unwrap().try_into().unwrap();

        log::trace!("sessionstate: {:#?}", state);

        let identities: VariantList = state.remove("Identities").unwrap().try_into().unwrap();
        let buffers: VariantList = state.remove("BufferInfos").unwrap().try_into().unwrap();
        let network_ids: VariantList = state.remove("NetworkIds").unwrap().try_into().unwrap();

        SessionInit {
            identities: identities.into_iter().map(|x| x.try_into().unwrap()).collect(),
            buffers: buffers
                .iter()
                .map(|buffer| match buffer {
                    Variant::BufferInfo(buffer) => buffer.clone(),
                    _ => unimplemented!(),
                })
                .collect(),
            network_ids: network_ids
                .iter()
                .map(|network| match network {
                    Variant::NetworkId(network) => network.clone(),
                    _ => unimplemented!(),
                })
                .collect(),
        }
    }
}

impl HandshakeSerialize for SessionInit {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        let mut values: VariantMap = VariantMap::with_capacity(4);
        values.insert("MsgType".to_string(), Variant::String("SessionInit".to_string()));
        // values.insert(
        //     "Identities".to_string(),
        //     Variant::VariantList(
        //         self.identities
        //             .iter()
        //             .map(|ident| Variant::VariantMap(ident.clone().into()))
        //             .collect(),
        //     ),
        // );
        values.insert(
            "BufferInfos".to_string(),
            Variant::VariantList(
                self.buffers
                    .iter()
                    .map(|buffer| Variant::BufferInfo(buffer.clone()))
                    .collect(),
            ),
        );
        values.insert(
            "NetworkIds".to_string(),
            Variant::VariantList(
                self.network_ids
                    .iter()
                    .map(|id| Variant::NetworkId(id.clone()))
                    .collect(),
            ),
        );
        return HandshakeSerialize::serialize(&values);
    }
}