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
use std::collections::HashMap;
use crate::encoding::*;

#[derive(Debug)]
pub enum MessageType {
    // Request,
    // BatchRequest,
    Reply(Header, ReplyData),
    ValidateConnection(Header),
    // CloseConnection
}

#[derive(Debug)]
pub struct Header {
    pub magic: String,
    pub protocol_major: u8,
    pub protocol_minor: u8,
    pub encoding_major: u8,
    pub encoding_minor: u8,
    pub message_type: u8,
    pub compression_status: u8,
    pub message_size: i32
}

#[derive(Debug, IceDerive)]
pub struct Identity {
    pub name: String,
    pub category: String
}

impl Identity {
    pub fn new(ident: &str) -> Identity {
        match ident.find("/") {
            Some(_) => {
                let split = ident.split("/").collect::<Vec<&str>>();
                Identity {
                    name: String::from(split[1]),
                    category: String::from(split[0])
                }
            }
            None => Identity {
                name: String::from(ident),
                category: String::new()
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct Encapsulation {
    pub size: i32,
    pub major: u8,
    pub minor: u8,
    pub data: Vec<u8>
}

#[derive(Debug, IceDerive)]
pub struct RequestData {
    pub request_id: i32,
    pub id: Identity,
    pub facet: Vec<String>,
    pub operation: String,
    pub mode: u8,
    pub context: HashMap<String, String>,
    pub params: Encapsulation
}

#[derive(Debug, IceEncode)]
pub struct ReplyData {
    pub request_id: i32,
    pub status: u8,
    pub body: Encapsulation
}

#[derive(Debug, IceDerive)]
pub struct Version
{
    pub major: u8,
    pub minor: u8
}

#[derive(Debug, IceDerive)]
pub struct ProxyData {
    pub id: String,
    pub facet: Vec<String>,
    pub mode: u8,
    pub secure: bool,
    pub protocol: Version,
    pub encoding: Version
}

#[derive(Debug)]
pub enum EndPointType {
    WellKnownObject(String),
    TCP(EndpointData),
    SSL(EndpointData),
}

#[derive(Debug)]
pub struct LocatorResult {
    pub proxy_data: ProxyData,
    pub size: IceSize,
    pub endpoint: EndPointType
}

#[derive(Debug, IceDerive)]
pub struct EndpointData
{
    pub host: String,
    pub port: i32,
    pub timeout: i32,
    pub compress: bool
}

impl Header {
    pub fn new(message_type: u8, message_size: i32) -> Header {
        Header {
            magic: String::from("IceP"),
            protocol_major: 1,
            protocol_minor: 0,
            encoding_major: 1,
            encoding_minor: 0,
            message_type: message_type,
            compression_status: 0,
            message_size: message_size
        }
    }
}

impl Encapsulation {
    pub fn empty() -> Encapsulation {
        Encapsulation {
            size: 6,
            major: 1,
            minor: 1,
            data: vec![]
        }
    }

    pub fn from(bytes: Vec<u8>) -> Encapsulation {
        Encapsulation {
            size: 6 + bytes.len() as i32,
            major: 1,
            minor: 1,
            data: bytes
        }
    }
}