/** \file
 *
 *  Contains the LRAUVController class declaration.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef LRAUVCONTROLLER_H_
#define LRAUVCONTROLLER_H_

#include <signal.h>

#include <cstdio>
#include <string>
#include <thread>
#include <vector>

/// Handler for an MBARI's LRAUV controller process.
class LRAUVController
{
public:
    ~LRAUVController();

/// Whether the controller process is still running or not.
/// \throws std::system_error if a system call fails.
public:
    bool IsRunning();

/// Kill controller process if running.
/// \param[in] signal Signal to be sent.
/// \return true if the signal was delivered, false otherwise.
/// \throws std::system_error if a system call fails.
public:
    bool Kill( int signal );

/// Wait for controller process to exit.
/// \return process exit status.
/// \throws std::system_error if a system call fails.
public:
    int Wait();

/// Start an LRAUV controller process to run `_commands`.
/// \param[in] _commands A sequence of LRAUV commands to execute.
/// \return handler instance for underlying controller process.
/// \throws std::system_error if a system call fails.
public:
    static LRAUVController Execute( const std::vector<std::string> &_commands );

// Constructor.
// \param[in] pid LRAUV controller process ID.
// \param[in] _stdout Read end of a pipe connected to
// the LRAUV controller process standard output.
private:
    LRAUVController( int _pid, FILE *_stdout );

private:
    std::thread backgroundThread;

private:
    int pid;
};

#endif // LRAUVCONTROLLER_H_
