fifoargs.h

Go to the documentation of this file.
00001 ///
00002 /// \file       fifoargs.h
00003 ///             Class for passing command line arguments via fifo instead
00004 ///             of command line.
00005 ///
00006 
00007 /*
00008     Copyright (C) 2012, Net Direct Inc. (http://www.netdirect.ca/)
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019     See the GNU General Public License in the COPYING file at the
00020     root directory of this project for more details.
00021 */
00022 
00023 #ifndef __BARRY_FIFOARGS_H__
00024 #define __BARRY_FIFOARGS_H__
00025 
00026 #include "dll.h"
00027 #include "pin.h"
00028 #include <iosfwd>
00029 
00030 namespace Barry {
00031 
00032 //
00033 // FifoArgs
00034 //
00035 /// Contains argument variables to be passed through the FIFO.
00036 /// It is no coincidence that there is a close connection to this
00037 /// set of arguments and the arguments given to the pppob program.
00038 ///
00039 struct BXEXPORT FifoArgs
00040 {
00041         Pin m_pin;
00042         std::string m_password;
00043         std::string m_log_filename;
00044         bool m_use_serial_mode;
00045         bool m_verbose;
00046 
00047         FifoArgs()
00048                 : m_use_serial_mode(false)
00049                 , m_verbose(false)
00050         {
00051         }
00052 
00053         std::ostream& Write(std::ostream &os) const;
00054         std::istream& Read(std::istream &is);
00055 
00056         void Clear();
00057 };
00058 
00059 //
00060 // FifoServer
00061 //
00062 /// Accepts a FifoArgs struct, and creates the necessary fifo for transfer.
00063 /// To use, create the object, then execute the program (eg. pppob), and
00064 /// then call Serve() with a given timeout in seconds.
00065 ///
00066 /// This class deletes the fifo in the destructor, or explicitly, with
00067 /// the Cleanup() call.
00068 ///
00069 /// Only arguments that are valid are sent.
00070 ///
00071 class BXEXPORT FifoServer
00072 {
00073         const FifoArgs &m_args;
00074         bool m_created;
00075 
00076 public:
00077         explicit FifoServer(const FifoArgs &args);
00078         ~FifoServer();
00079 
00080         /// Serves the given arguments through the fifo.  Returns
00081         /// false on timeout.
00082         bool Serve(int timeout_sec);
00083 
00084         /// Deletes the fifo.  Called automatically by destructor.
00085         void Cleanup();
00086 };
00087 
00088 //
00089 // FifoClient
00090 //
00091 /// Searches for a fifo and opens and reads it if available.  Use
00092 /// Fetch() with a given timeout to perform the read attempt.
00093 /// Use GetArgs() to access the filled FifoArgs struct.
00094 ///
00095 class BXEXPORT FifoClient
00096 {
00097         FifoArgs m_args;
00098 
00099 public:
00100         FifoClient();
00101 
00102         /// Tries to open the fifo and read the arguments from it.
00103         /// If it fails in any way, or timeout, returns false.
00104         bool Fetch(int timeout_sec);
00105 
00106         const FifoArgs& GetArgs() const { return m_args; }
00107 };
00108 
00109 } // Barry namespace
00110 
00111 #endif
00112