/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#ifndef NMM_FORK_HPP
#define NMM_FORK_HPP NMM_FORK_HPP

#include "nmm/nmm_prefix.hpp"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>


#include <string>
#include <vector>

#include "nmm/utils/Singleton.hpp"
#include "nmm/utils/thread/ThreadedObject.hpp"
#include "nmm/utils/thread/ThreadMutex.hpp"
#include "nmm/utils/thread/MutexGuard.hpp"

using namespace std;

namespace NMM{

  class Fork : public ThreadedObject{
    friend class CreateUsingNew<Fork>;
    
  public:

  private:
    ThreadMutex executemutex;

    //int-array for the 2 file-descriptors
    int sv[2];
    //pipe to get the Child-ID
    int childPid;    
     
    void start();


  private:
    Fork();
    Fork(const Fork&);
    ~Fork();
    
  public:
    
    /**
     *Geth the read-socket used
     */
    int getReadSocket();
    /**
     *Get the write-socket used
     */
    int getWriteSocket();
    
    /**
     *Exceute the command given as parameter
     *@param command to execute
     *@return int result of the execution 
     */
    int executeCommand(string command);
    /**
     *kills the child-process created
     */
    int killall();

    /**
     *set the priority of the child-process. the command wich will be executed inherits this priority
     *@param priority of the child-process
     *@return -1 if setting of priority failed
     *@return 0 if setting of priority was successful
     */
    int setChildPriority(int prio);
protected:
    void run();
  };    
    
  /* Typdef to access the ForkHelper */
  typedef Singleton<Fork, CreateUsingNew> SFork;
}

#include "nmm/nmm_suffix.hpp"

#endif
