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
use std::net::TcpListener;
use tungstenite::server::accept;

#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub struct ServerState {
    ip_v4: &'static str,
}
/// Create an isntation of the server that will communicate with the client
///
impl ServerState {
    pub fn new(ip_v4: &'static str) -> Self {
        Self { ip_v4 }
    }
    /// The function that constitutes the core of the library, sends the code to the client with the given "name" and returns the result.    
    /// The first argument is the code you want to send, the second is the identifier ("name") and the language in which the code was written.
    ///
    ///
    pub fn run_server(
        self,
        message: &'static str,
        identification_string: &'static str,
        leanguage: &'static str,
    ) -> Result<String, tungstenite::Error> {
        let mut get_message = String::from(" ");
        'socket_loop: loop {
            let server = TcpListener::bind(self.ip_v4)?;

            'main_loop: for stream in server.incoming() {
                let mut websocket = match accept(stream?) {
                    Ok(s) => s,
                    Err(error) => {
                        println!("Error: {:?} ", error);
                        continue 'socket_loop;
                    }
                };

                loop {
                    let msg = websocket.read_message()?;

                    #[cfg(feature = "socket_debug")]
                    println!("{:?}", msg);

                    get_message = msg.to_text()?.to_string();
                    if msg.is_binary() || msg.is_text() {
                        if msg.to_text()? == identification_string.to_string() {
                            websocket
                                .write_message(tungstenite::Message::Text(leanguage.to_string()))?;
                            websocket
                                .write_message(tungstenite::Message::Text(message.to_string()))?;
                        } else {
                            break 'main_loop;
                        }
                    }
                }
            }

            if get_message != identification_string.to_string() {
                if get_message.find(identification_string) == Some(0) {
                    let (_, now) = get_message.split_at(identification_string.len());
                    get_message = now.to_string();

                    break 'socket_loop;
                }
            }
        }
        Ok(get_message)
    }
    /// run the file with the name given to the variable file_name with the extension .wasm
    ///
    pub fn run_server_wasm(
        self,
        identification_string: &'static str,
        file_name: &'static str,
    ) -> Result<String, tungstenite::Error> {
        let mut get_message = String::from(" ");
        'socket_loop: loop {
            let server = TcpListener::bind(self.ip_v4)?;

            'main_loop: for stream in server.incoming() {
                let mut websocket = match accept(stream?) {
                    Ok(s) => s,
                    Err(error) => {
                        println!("Error: {:?} ", error);
                        continue 'socket_loop;
                    }
                };

                loop {
                    let msg = websocket.read_message()?;

                    #[cfg(feature = "socket_debug")]
                    println!("{:?}", msg);

                    get_message = msg.to_text()?.to_string();
                    if msg.is_binary() || msg.is_text() {
                        if msg.to_text()? == identification_string.to_string() {
                            websocket
                                .write_message(tungstenite::Message::Text("wasm".to_string()))?;
                            websocket
                                .write_message(tungstenite::Message::Text(file_name.to_string()))?;
                        } else {
                            break 'main_loop;
                        }
                    }
                }
            }

            if get_message != identification_string.to_string() {
                if get_message.find(identification_string) == Some(0) {
                    let (_, now) = get_message.split_at(identification_string.len());
                    get_message = now.to_string();

                    break 'socket_loop;
                }
            }
        }
        Ok(get_message)
    }

    ///
    /// first argument: program code
    /// secound argument: device identification string
    /// third arguments: program language
    pub fn run_server_queue(
        self,
        arguments_vec: &mut Vec<(&str, &str, &str)>,
    ) -> Result<Vec<String>, tungstenite::Error> {
        let server = TcpListener::bind(self.ip_v4)?;
        let mut get_message = String::from(" ");
        let mut return_vec = Vec::new();
        let mut message;
        let mut identification_string;
        let mut leanguage;

        for buff in arguments_vec.iter_mut() {
            message = buff.0;
            identification_string = buff.1;
            leanguage = buff.2;

            'socket_loop: loop {
                'main: for stream in server.incoming() {
                    // spawn(move || -> ! {
                    let mut websocket = match accept(stream?) {
                        Ok(s) => s,
                        Err(error) => {
                            println!("Kiss error: {:?} ", error);
                            continue 'socket_loop;
                        }
                    };

                    loop {
                        let msg = websocket.read_message()?;

                        #[cfg(feature = "socket_debug")]
                        println!("{:?}", msg);

                        get_message = msg.to_text()?.to_string();

                        if msg.is_binary() || msg.is_text() {
                            if msg.to_text()? == identification_string.to_string() {
                                websocket.write_message(tungstenite::Message::Text(
                                    leanguage.to_string(),
                                ))?;
                                websocket.write_message(tungstenite::Message::Text(
                                    message.to_string(),
                                ))?;
                            } else {
                                break 'main;
                            }
                        }
                    }
                }

                if get_message != identification_string.to_string() {
                    if get_message.find(identification_string) == Some(0) {
                        let (_, now) = get_message.split_at(identification_string.len());
                        get_message = now.to_string();
                        return_vec.push(get_message.clone());

                        break 'socket_loop;
                    } else {
                        continue 'socket_loop;
                    }
                }
            }
        }
        Ok(return_vec)
    }
    ///
    ///
    /// first argument: device identification string
    /// secound argument: name filr with program code
    pub fn run_server_queue_wasm(
        self,
        arguments_vec: &mut Vec<(&str, &str)>,
    ) -> Result<Vec<String>, tungstenite::Error> {
        let server = TcpListener::bind(self.ip_v4)?;
        let mut get_message = String::from(" ");
        let mut return_vec = Vec::new();
        let mut file_name;
        let mut identification_string;

        for buff in arguments_vec.iter_mut() {
            identification_string = buff.0;
            file_name = buff.1;

            'socket_loop: loop {
                'main: for stream in server.incoming() {
                    let mut websocket = match accept(stream?) {
                        Ok(s) => s,
                        Err(error) => {
                            println!("Kiss error: {:?} ", error);
                            continue 'socket_loop;
                        }
                    };

                    loop {
                        let msg = websocket.read_message()?;

                        #[cfg(feature = "socket_debug")]
                        println!("{:?}", msg);

                        get_message = msg.to_text()?.to_string();
                        if msg.is_binary() || msg.is_text() {
                            if msg.to_text()? == identification_string.to_string() {
                                websocket.write_message(tungstenite::Message::Text(
                                    "wasm".to_string(),
                                ))?;
                                websocket.write_message(tungstenite::Message::Text(
                                    file_name.to_string(),
                                ))?;
                            } else {
                                break 'main;
                            }
                        }
                    }
                }

                if get_message != identification_string.to_string() {
                    if get_message.find(identification_string) == Some(0) {
                        let (_, now) = get_message.split_at(identification_string.len());
                        get_message = now.to_string();
                        return_vec.push(get_message.clone());

                        break 'socket_loop;
                    } else {
                        continue 'socket_loop;
                    }
                }
            }
        }
        Ok(return_vec)
    }
}