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
use super::{L2rUser, Options, Result};
use super::{PeerConstructor, ProgramState};
use std;
use std::cell::RefCell;
use std::rc::Rc;

pub enum ClassMessageBoundaryStatus {
    StreamOriented,
    MessageOriented,
    MessageBoundaryStatusDependsOnInnerType,
}

pub enum ClassMulticonnectStatus {
    MultiConnect,
    SingleConnect,
    MulticonnectnessDependsOnInnerType,
}

/// A trait for a each specified type's accompanying object
///
/// Don't forget to register each instance at the `list_of_all_specifier_classes` macro.
pub trait SpecifierClass: std::fmt::Debug {
    /// The primary name of the class
    fn get_name(&self) -> &'static str;
    /// Names to match command line parameters against, with a `:` colon if needed
    fn get_prefixes(&self) -> Vec<&'static str>;
    /// --long-help snippet about this specifier
    fn help(&self) -> &'static str;
    /// Given the command line text, construct the specifier
    /// arg is what comes after the colon (e.g. `//echo.websocket.org` in `ws://echo.websocket.org`)
    fn construct(&self, arg: &str) -> Result<Rc<dyn Specifier>>;
    /// Given the inner specifier, construct this specifier.
    fn construct_overlay(&self, inner: Rc<dyn Specifier>) -> Result<Rc<dyn Specifier>>;
    /// Returns if this specifier is an overlay
    fn is_overlay(&self) -> bool;
    /// True if it is not expected to preserve message boundaries on reads
    fn message_boundary_status(&self) -> ClassMessageBoundaryStatus;

    fn multiconnect_status(&self) -> ClassMulticonnectStatus;
    /// If it is Some then is_overlay, construct and most other things are ignored and prefix get replaced...
    fn alias_info(&self) -> Option<&'static str>;
}

macro_rules! specifier_alias {
    (name=$n:ident,
            prefixes=[$($p:expr),*],
            alias=$x:expr,
            help=$h:expr) => {
        #[derive(Debug,Default)]
        pub struct $n;
        impl $crate::SpecifierClass for $n {
            fn get_name(&self) -> &'static str { stringify!($n) }
            fn get_prefixes(&self) -> Vec<&'static str> { vec![$($p),*] }
            fn help(&self) -> &'static str { $h }
            fn message_boundary_status(&self) -> $crate::ClassMessageBoundaryStatus {
                panic!("Error: message_boundary_status called on alias class")
            }
            fn multiconnect_status(&self) -> $crate::ClassMulticonnectStatus {
                panic!("Error: multiconnect_status called on alias class")
            }
            fn is_overlay(&self) -> bool {
                false
            }
            fn construct(&self, _arg:&str) -> $crate::Result<Rc<dyn Specifier>> {
                panic!("Error: construct called on alias class")
            }
            fn construct_overlay(&self, _inner : Rc<dyn Specifier>) -> $crate::Result<Rc<dyn Specifier>> {
                panic!("Error: construct_overlay called on alias class")
            }
            fn alias_info(&self) -> Option<&'static str> { Some($x) }
        }
    };
}

macro_rules! specifier_class {
    (name=$n:ident,
            target=$t:ident,
            prefixes=[$($p:expr),*],
            arg_handling=$c:tt,
            overlay=$o:expr,
            $so:expr,
            $ms:expr,
            help=$h:expr) => {
        #[derive(Debug,Default)]
        pub struct $n;
        impl $crate::SpecifierClass for $n {
            fn get_name(&self) -> &'static str { stringify!($n) }
            fn get_prefixes(&self) -> Vec<&'static str> { vec![$($p),*] }
            fn help(&self) -> &'static str { $h }
            fn message_boundary_status(&self) -> $crate::ClassMessageBoundaryStatus {
                use $crate::ClassMessageBoundaryStatus::*;
                $so
            }
            fn multiconnect_status(&self) -> $crate::ClassMulticonnectStatus {
                use $crate::ClassMulticonnectStatus::*;
                $ms
            }
            fn is_overlay(&self) -> bool {
                $o
            }
            specifier_class!(construct target=$t $c);
        }
    };
    (construct target=$t:ident noarg) => {
        fn construct(&self, just_arg:&str) -> $crate::Result<Rc<dyn Specifier>> {
            if just_arg != "" {
                Err(format!("{}-specifer requires no parameters. `{}` is not needed",
                    self.get_name(), just_arg))?;
            }
            Ok(Rc::new($t))
        }
        fn construct_overlay(&self, _inner : Rc<dyn Specifier>) -> $crate::Result<Rc<dyn Specifier>> {
            panic!("Error: construct_overlay called on non-overlay specifier class")
        }
        fn alias_info(&self) -> Option<&'static str> { None }
    };
    (construct target=$t:ident into) => {
        fn construct(&self, just_arg:&str) -> $crate::Result<Rc<dyn Specifier>> {
            Ok(Rc::new($t(just_arg.into())))
        }
        fn construct_overlay(&self, _inner : Rc<dyn Specifier>) -> $crate::Result<Rc<dyn Specifier>> {
            panic!("Error: construct_overlay called on non-overlay specifier class")
        }
        fn alias_info(&self) -> Option<&'static str> { None }
    };
    (construct target=$t:ident parse) => {
        fn construct(&self, just_arg:&str) -> $crate::Result<Rc<dyn Specifier>> {
            Ok(Rc::new($t(just_arg.parse()?)))
        }
        fn construct_overlay(&self, _inner : Rc<dyn Specifier>) -> $crate::Result<Rc<dyn Specifier>> {
            panic!("Error: construct_overlay called on non-overlay specifier class")
        }
        fn alias_info(&self) -> Option<&'static str> { None }
    };
    (construct target=$t:ident parseresolve) => {
        fn construct(&self, just_arg:&str) -> $crate::Result<Rc<dyn Specifier>> {
            use std::net::ToSocketAddrs;
            info!("Resolving hostname to IP addresses");
            let addrs : Vec<std::net::SocketAddr> = just_arg.to_socket_addrs()?.collect();
            if addrs.is_empty() {
                Err("Failed to resolve this hostname to IP")?;
            }
            for addr in &addrs {
                info!("Got IP: {}", addr);
            }
            Ok(Rc::new($t(addrs)))
        }
        fn construct_overlay(&self, _inner : Rc<dyn Specifier>) -> $crate::Result<Rc<dyn Specifier>> {
            panic!("Error: construct_overlay called on non-overlay specifier class")
        }
        fn alias_info(&self) -> Option<&'static str> { None }
    };
    (construct target=$t:ident subspec) => {
        fn construct(&self, just_arg:&str) -> $crate::Result<Rc<dyn Specifier>> {
            Ok(Rc::new($t($crate::spec(just_arg)?)))
        }
        fn construct_overlay(&self, _inner : Rc<dyn Specifier>) -> $crate::Result<Rc<dyn Specifier>> {
            Ok(Rc::new($t(_inner)))
        }
        fn alias_info(&self) -> Option<&'static str> { None }
    };
    (construct target=$t:ident {$($x:tt)*}) => {
        $($x)*
        fn alias_info(&self) -> Option<&'static str> { None }
    };
}


#[derive(Debug)]
pub struct SpecifierNode {
    pub cls: Rc<dyn SpecifierClass>,
    //pub opt: Rc<std::any::Any>,
}

#[derive(Debug)]
pub struct SpecifierStack {
    pub addr: String,
    pub addrtype: SpecifierNode,
    pub overlays: Vec<SpecifierNode>,
}

#[derive(Clone)]
pub struct ConstructParams {
    pub global_state: Rc<RefCell<ProgramState>>,
    pub program_options: Rc<Options>,
    pub left_to_right: L2rUser,
}

/// All of those methods are about left_to_right mechanism
impl ConstructParams {
    /// Reset left_to_right to default value.
    pub fn reset_l2r(&mut self) {
        match self.left_to_right {
            L2rUser::FillIn(ref mut x) => {
                *x.borrow_mut() = Default::default();
                //*x = Rc::new(RefCell::new(Default::default()));
            }
            L2rUser::ReadFrom(_) => panic!("ConstructParams::reset_l2r called wrong"),
        }
    }
    /// Clones ConstructParams, changing FillIn to ReadFrom in left_to_right field
    /// and also disassociating it from the original RefCell.
    ///
    /// Panics when called on object with left_to_right set to ReadFrom.
    pub fn reply(&self) -> Self {
        let l2r = match self.left_to_right {
            L2rUser::FillIn(ref x) => Rc::new(x.borrow().clone()),
            L2rUser::ReadFrom(_) => panic!("ConstructParams::reply called wrong"),
        };
        ConstructParams {
            global_state: self.global_state.clone(),
            program_options: self.program_options.clone(),
            left_to_right: L2rUser::ReadFrom(l2r),
        }
    }

    pub fn deep_clone(&self) -> Self {
        let l2r = match self.left_to_right {
            L2rUser::FillIn(ref x) => L2rUser::FillIn(Rc::new(RefCell::new(x.borrow().clone()))),
            L2rUser::ReadFrom(_) => {
                panic!(
                    "You are not supposed to use ConstructParams::deep_clone on ReadFrom things"
                );
            }
        };
        ConstructParams {
            global_state: self.global_state.clone(),
            program_options: self.program_options.clone(),
            left_to_right: l2r,
        }
    }

    /// Access specified-specific global (singleton) data
    pub fn global<T:std::any::Any, F>(&self, def:F) -> std::cell::RefMut<T> 
        where F : FnOnce()->T
    {
        std::cell::RefMut::map(
            self.global_state.borrow_mut(),
            |x|{
                x.0.entry().or_insert(def())
            }
        )
    }
}

/// A parsed command line argument.
/// For example, `ws-listen:tcp-l:127.0.0.1:8080` gets parsed into
/// a `WsUpgrade(TcpListen(SocketAddr))`.
pub trait Specifier: std::fmt::Debug {
    /// Apply the specifier for constructing a "socket" or other connecting device.
    fn construct(&self, p: ConstructParams) -> PeerConstructor;

    // Specified by `specifier_boilerplate!`:
    fn is_multiconnect(&self) -> bool;
    fn uses_global_state(&self) -> bool;
}

impl Specifier for Rc<dyn Specifier> {
    fn construct(&self, p: ConstructParams) -> PeerConstructor {
        (**self).construct(p)
    }

    fn is_multiconnect(&self) -> bool {
        (**self).is_multiconnect()
    }
    fn uses_global_state(&self) -> bool {
        (**self).uses_global_state()
    }
}

macro_rules! specifier_boilerplate {
    (singleconnect $($e:tt)*) => {
        fn is_multiconnect(&self) -> bool { false }
        specifier_boilerplate!($($e)*);
    };
    (multiconnect $($e:tt)*) => {
        fn is_multiconnect(&self) -> bool { true }
        specifier_boilerplate!($($e)*);
    };
    (no_subspec $($e:tt)*) => {
        specifier_boilerplate!($($e)*);
    };
    (has_subspec $($e:tt)*) => {
        specifier_boilerplate!($($e)*);
    };
    () => {
    };
    (globalstate $($e:tt)*) => {
        fn uses_global_state(&self) -> bool { true }
        specifier_boilerplate!($($e)*);
    };
    (noglobalstate $($e:tt)*) => {
        fn uses_global_state(&self) -> bool { false }
        specifier_boilerplate!($($e)*);
    };
}

macro_rules! self_0_is_subspecifier {
    (...) => {
       // removed with old linter
    };
    (proxy_is_multiconnect) => {
        self_0_is_subspecifier!(...);
        fn is_multiconnect(&self) -> bool { self.0.is_multiconnect() }
    };
}