Files
adcs_api
cbor_protocol
channel_protocol
clyde_3g_eps_api
clyde_3g_eps_service
comms_service
db_test
eps_api
example_rust_c_service
example_rust_service
extern_lib
file_protocol
file_service
iobc_supervisor_service
isis_ants
isis_ants_api
isis_ants_service
isis_imtq_api
isis_iobc_supervisor
kubos_app
kubos_app_service
kubos_build_helper
kubos_file_client
kubos_service
kubos_shell_client
kubos_system
kubos_telemetry_db
large_download
large_upload
local_comms_service
mai400
mai400_api
mai400_service
monitor_service
novatel_oem6_api
novatel_oem6_service
nsl_duplex_d2
nsl_duplex_d2_comms_service
obc_hs
radio_api
rust_i2c
rust_mission_app
rust_uart
scheduler_service
serial_comms_service
shell_protocol
shell_service
telemetry_service
uart_comms_client
udp_client
utils
  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
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#![allow(clippy::block_in_if_condition_stmt)]

use channel_protocol::{ChannelMessage, ChannelProtocol};
use failure::bail;
use kubos_system::Config as ServiceConfig;
use log::{error, info, warn};
use shell_protocol::{ProcessHandler, ProtocolError, ShellMessage, ShellProtocol};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

#[derive(Debug)]
struct ThreadProcess {
    pub sender: Sender<(ChannelMessage, SocketAddr)>,
    pub pid: u32,
    pub path: String,
}

// Create process list and send back to requester
fn list_processes(
    channel_id: u32,
    host: &str,
    remote: &str,
    threads: &Arc<Mutex<HashMap<u32, ThreadProcess>>>,
) -> Result<(), failure::Error> {
    let proc_list: HashMap<u32, (String, u32)> = threads
        .lock()
        .map_err(|err| {
            error!("Failed to get threads mutex: {:?}", err);
            err
        })
        .unwrap()
        .iter()
        .map(|(channel_id, data)| (*channel_id, (data.path.to_owned(), data.pid)))
        .collect();

    let chan_proto =
        channel_protocol::ChannelProtocol::new(host, remote, shell_protocol::CHUNK_SIZE);

    chan_proto.send(&shell_protocol::messages::list::to_cbor(
        channel_id,
        Some(proc_list),
    )?)?;

    Ok(())
}

// Spawn new process and spin up thread for handling it
fn spawn_process(
    channel_id: u32,
    command: &str,
    args: Option<Vec<String>>,
    host_addr: &str,
    remote_addr: &str,
    timeout: Duration,
    shared_threads: Arc<Mutex<HashMap<u32, ThreadProcess>>>,
) -> Result<(u32, Sender<(ChannelMessage, SocketAddr)>), failure::Error> {
    #[allow(clippy::type_complexity)]
    let (sender, receiver): (
        Sender<(ChannelMessage, SocketAddr)>,
        Receiver<(ChannelMessage, SocketAddr)>,
    ) = mpsc::channel();

    let proc_handle = match ProcessHandler::spawn(command, args) {
        Ok(p) => p,
        Err(e) => {
            bail!("Failed to spawn {:?}", e);
        }
    };
    let pid = proc_handle.id();

    let channel_protocol = ChannelProtocol::new(host_addr, remote_addr, shell_protocol::CHUNK_SIZE);

    channel_protocol.send(&shell_protocol::messages::pid::to_cbor(
        channel_id,
        proc_handle.id(),
    )?)?;

    thread::spawn(move || {
        thread_body(
            channel_protocol,
            channel_id,
            timeout,
            proc_handle,
            &shared_threads,
            &receiver,
        )
    });

    Ok((pid, sender))
}

// Main function of process handling thread
fn thread_body(
    channel_protocol: ChannelProtocol,
    channel_id: u32,
    timeout: Duration,
    proc_handle: ProcessHandler,
    shared_threads: &Arc<Mutex<HashMap<u32, ThreadProcess>>>,
    receiver: &Receiver<(ChannelMessage, SocketAddr)>,
) {
    let mut s_protocol = ShellProtocol::new(channel_protocol, channel_id, Box::new(proc_handle));

    // Receive and react to incoming shell protocol messages
    if let Err(e) = s_protocol.message_engine(
        |d| match receiver.recv_timeout(d) {
            Ok((v, s)) => Ok((v, s)),
            Err(RecvTimeoutError::Timeout) => Err(ProtocolError::ReceiveTimeout),
            Err(e) => Err(ProtocolError::ReceiveError {
                err: format!("Error {:?}", e),
            }),
        },
        timeout,
    ) {
        warn!("Encountered errors while processing transaction: {}", e);
    }

    // Remove ourselves from threads list once we are finished
    shared_threads
        .lock()
        .map_err(|err| {
            error!("Failed to get threads mutex: {:?}", err);
            err
        })
        .unwrap()
        .remove(&channel_id);
}

// Retrieves and parses next shell message
fn get_message(
    cbor_proto: &cbor_protocol::Protocol,
) -> Result<
    (
        channel_protocol::ChannelMessage,
        shell_protocol::ShellMessage,
        std::net::SocketAddr,
    ),
    failure::Error,
> {
    let (source, message) = cbor_proto.recv_message_peer()?;

    let channel_message = channel_protocol::parse_message(message)?;

    let shell_message = shell_protocol::parse_message(&channel_message.clone())?;

    Ok((channel_message, shell_message, source))
}

// Starts and runs the main loop receiving new shell protocol messages
pub fn recv_loop(config: &ServiceConfig) -> Result<(), failure::Error> {
    // Get and bind our UDP listening socket
    let host = config
        .hosturl()
        .ok_or_else(|| failure::format_err!("Unable to fetch addr for service"))?;

    // Extract our local IP address so we can spawn child sockets later
    let mut host_parts = host.split(':').map(|val| val.to_owned());
    let host_addr = host_parts
        .next()
        .ok_or_else(|| failure::format_err!("Failed to parse service IP address"))?;

    let c_protocol =
        cbor_protocol::Protocol::new(&host.clone(), shell_protocol::CHUNK_SIZE as usize);

    let timeout = config
        .get("timeout")
        .and_then(|val| val.as_integer().map(|num| Duration::from_secs(num as u64)))
        .unwrap_or(Duration::from_millis(2));

    // Setup map of channel IDs to thread channels
    let raw_threads: HashMap<u32, ThreadProcess> = HashMap::new();
    // Create thread sharable wrapper
    let threads = Arc::new(Mutex::new(raw_threads));

    loop {
        let (channel_message, shell_message, message_source) = match get_message(&c_protocol) {
            Ok((c, s, m)) => (c, s, m),
            Err(e) => {
                warn!("Failed to get next message: {}", e);
                continue;
            }
        };
        let channel_id = channel_message.channel_id;
        let remote_addr = format!("{}", message_source);

        match shell_message {
            // Gather and send back list of processes
            ShellMessage::List {
                channel_id,
                process_list: None,
            } => {
                info!("<- {{ {}, list }}", channel_id);
                list_processes(
                    channel_id,
                    &host_addr,
                    &format!("{}", message_source),
                    &threads,
                )?;
                continue;
            }
            // Spawn up a new process & thread
            ShellMessage::Spawn {
                channel_id,
                command,
                args,
            } => {
                info!("<- {{ {}, spawn, {}, {:?} }}", channel_id, command, args);
                if !threads
                    .lock()
                    .map_err(|err| {
                        error!("Failed to get threads mutex: {:?}", err);
                        err
                    })
                    .unwrap()
                    .contains_key(&channel_id)
                {
                    if let Ok((pid, sender)) = spawn_process(
                        channel_id,
                        &command,
                        args,
                        &host_addr,
                        &remote_addr,
                        timeout,
                        threads.clone(),
                    ) {
                        threads
                            .lock()
                            .map_err(|err| {
                                error!("Failed to get threads mutex: {:?}", err);
                                err
                            })
                            .unwrap()
                            .insert(
                                channel_id,
                                ThreadProcess {
                                    sender: sender.clone(),
                                    pid,
                                    path: command.to_owned(),
                                },
                            );
                    }
                } else {
                    warn!("Process on channel {} already exists", channel_id);
                }
            }
            // Pass along the message to existing process
            _ => {
                if let Some(process_handle) = threads
                    .lock()
                    .map_err(|err| {
                        error!("Failed to get threads mutex: {:?}", err);
                        err
                    })
                    .unwrap()
                    .get(&channel_id)
                {
                    if let Err(e) = process_handle
                        .sender
                        .send((channel_message, message_source))
                    {
                        warn!("Error when sending to channel {}: {:?}", channel_id, e);
                    }
                }

                if !threads
                    .lock()
                    .map_err(|err| {
                        error!("Failed to get threads mutex: {:?}", err);
                        err
                    })
                    .unwrap()
                    .contains_key(&channel_id)
                {
                    warn!("No session found for {}", channel_id);
                    let channel_protocol =
                        ChannelProtocol::new(&host_addr, &remote_addr, shell_protocol::CHUNK_SIZE);

                    channel_protocol.send(&shell_protocol::messages::error::to_cbor(
                        channel_id,
                        &format!("No session found on channel {}", channel_id),
                    )?)?;
                    threads
                        .lock()
                        .map_err(|err| {
                            error!("Failed to get threads mutex: {:?}", err);
                            err
                        })
                        .unwrap()
                        .remove(&channel_id);
                }
            }
        }
    }
}