My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
ReceiverControl.h
Go to the documentation of this file.
1 // $Id: ReceiverControl.h,v 1.1.1.1 2009/07/06 23:57:23 eml Exp $
2 
3 #ifndef GCP_MEDIATOR_RECEIVERCONTROL_H
4 #define GCP_MEDIATOR_RECEIVERCONTROL_H
5 
16 #include <iostream>
17 #include <fstream>
18 #include <list>
19 
20 
21 #include "gcp/util/common/GenericTask.h"
22 
23 #include "gcp/mediator/specific/ReceiverControlMsg.h"
24 
25 namespace gcp {
26 
27  namespace util {
28  class CoProc;
29  class Port;
30  }
31 
32  namespace mediator {
33 
38  class Control;
39 
43  class ReceiverControl :
44  public gcp::util::GenericTask<ReceiverControlMsg> {
45 
46  public:
47 
48 
49  struct Script {
50 
51  enum Status {
52  NONE,
53  FAILED,
54  OK
55  };
56 
57  enum State {
58  DONE,
59  START,
60  BODY,
61  END,
62  };
63 
64  enum Type {
65  ERROR,
66  MESSAGE,
67  STATUS,
68  };
69 
70  // The process associated with this script
71 
72  gcp::util::CoProc* proc_;
73 
74  // A file descriptor associated with the output file for this
75  // script
76 
77  std::ofstream outFile_;
78 
79  gcp::util::Port* stdOut_;
80  gcp::util::Port* stdErr_;
81 
82  // Variables for handling messages received over the stderr
83  // pipe
84 
85  unsigned tagCount_;
86 
87  std::ostringstream startTag_;
88  std::ostringstream message_;
89  std::ostringstream endTag_;
90 
91  State messageState_;
92  Type messageType_;
93 
94  // The seq id number associated with this script
95 
96  unsigned seq_;
97 
98  // A return status from this script
99 
100  Status status_;
101 
102  bool stdOutWasClosed_;
103  bool stdErrWasClosed_;
104 
105  std::string script_;
106 
107  //------------------------------------------------------------
108  // Methods
109  //------------------------------------------------------------
110 
111  // Constructor will be called with the command name to execute
112 
113  Script(std::string script, unsigned seq, std::string dir);
114 
115  ~Script();
116 
117  // Methods to return the stdout/stderr fds for this process
118 
119  int stdInFd();
120  int stdOutFd();
121  int stdErrFd();
122 
123  void open(std::string fileName);
124  void close();
125 
126  // Copy data from the stdout pipe of the process to the output
127  // file
128 
129  unsigned copyFromStdoutToFile();
130 
131  // Read the next string from stderr
132 
133  unsigned readFromStdErr();
134 
135  // Process a message received on stderr
136 
137  void processMessage();
138 
139  // Set the message type from the start tag that was read
140 
141  void setMessageType();
142 
143  };
144 
148  ReceiverControl(Control* parent);
149 
153  virtual ~ReceiverControl();
154 
155  // Forward a script message to the receiver control task.
156 
157  void sendScript(std::string script, unsigned seq);
158 
162  void run() {
164  }
165 
166  private:
167 
171  friend class Control;
172 
173  Control* parent_;
174 
175  // The output directory
176 
177  std::string outputDir_;
178 
182  std::list<Script*> scripts_;
183 
184  // The list of scripts that finished on the last loop of select()
185 
186  std::list<Script*> finishedScripts_;
187 
188  // A logfile object for managing output file names
189 
190  gcp::util::LogFile logFile_;
191 
195  void serviceMsgQ();
196 
202  void processMsg(ReceiverControlMsg* taskMsg);
203 
204  // Register receipt of a command to execute a new script
205 
206  void installNewScript(char* script, unsigned seq);
207 
208  // Respond to data on the stdout pipe for this script
209 
210  void processStdOut(Script* script);
211 
212  // Respond to data on the stderr pipe for this script
213 
214  void processStdErr(Script* script);
215 
216  // Respond to data on the stdout pipe for this script
217 
218  void registerStdOutClosed(Script* script);
219 
220  // Respond to data on the stdout pipe for this script
221 
222  void registerStdErrClosed(Script* script);
223 
224  // Respond to data on the stdout pipe for this script
225 
226  void checkCompletionStatus(Script* script);
227 
228  // Register an error installing a new script
229 
230  void registerError(std::string message, unsigned seq);
231 
232  void removeFinishedScriptsFromList();
233 
234  // Send a script completion message to the control program
235 
236  void sendScriptCompletedMsg(unsigned seq);
237 
238  }; // End class ReceiverControl
239 
240  } // End namespace mediator
241 } // End namespace gcp
242 
243 
244 
245 #endif // End #ifndef GCP_MEDIATOR_RECEIVERCONTROL_H
void close()
Definition: ReceiverControl.cc:333
Definition: CoProc.h:27
virtual void run(void)
Definition: GenericTask.h:358
Definition: ReceiverControl.h:43
Definition: ReceiverControl.h:49
unsigned copyFromStdoutToFile()
Definition: ReceiverControl.cc:376
Definition: Port.h:19
void run()
Definition: ReceiverControl.h:162
Definition: LogFile.h:20
unsigned readFromStdErr()
Definition: ReceiverControl.cc:415
void open(std::string fileName)
Definition: ReceiverControl.cc:344
ReceiverControl(Control *parent)
Definition: ReceiverControl.cc:22
Definition: Control.h:53
void sendScript(std::string script, unsigned seq)
Definition: ReceiverControl.cc:255
virtual ~ReceiverControl()
Definition: ReceiverControl.cc:55
Definition: ReceiverControlMsg.h:21
Definition: GenericTask.h:33
void processMessage()
Definition: ReceiverControl.cc:525