program.h 1.06 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
#ifndef PROGRAM_H
#define PROGRAM_H

#include "thread_util.h"

#include <string>
Sam Moore's avatar
Sam Moore committed
7
#include <unistd.h> //Needed to check permissions
Sam Moore's avatar
Sam Moore committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

/**
 * A wrapping class for an external program, which can exchange messages with the current process through stdin/stdout
 * Useful for attaching control of an operation to an external process - for example, AI for a game
 */
class Program
{
	public:
		Program(const char * executeablePath); //Constructor
		virtual ~Program(); //Destructor




		bool SendMessage(const char * print, ...); //Sends a formated message (NOTE: Prints a newline)
		bool SendMessage(const std::string & buffer) {return SendMessage(buffer.c_str());} //Sends a C++ string message
		bool GetMessage(std::string & buffer, double timeout=-1); //Retrieves a message, or waits for a timeout (if positive)

		bool Running() const;

28
29
		

Sam Moore's avatar
Sam Moore committed
30
31
32
33
34
35
	protected:
		FILE * input;	//Stream used for sending information TO the process
		FILE * output; //Stream used for retrieving information FROM the process

	private:
		pid_t pid; //Process ID of the program wrapped
36
		
Sam Moore's avatar
Sam Moore committed
37
38
39
40
41
};

#endif //PROGRAM_H