18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.
This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
37
piprocess.h
Executable file → Normal file
37
piprocess.h
Executable file → Normal file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Process
|
||||
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -28,6 +28,18 @@
|
||||
# include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
/// events:
|
||||
/// execStarted(PIString program)
|
||||
/// execFinished(PIString program, int exit_code)
|
||||
///
|
||||
/// handlers:
|
||||
/// bool exec(const PIString & program)
|
||||
/// bool exec(const PIString & program, const PIString & arg1)
|
||||
/// bool exec(const PIString & program, const PIString & arg1, const PIString & arg2)
|
||||
/// bool exec(const PIString & program, const PIString & arg1, const PIString & arg2, const PIString & arg3)
|
||||
/// bool exec(const PIString & program, const PIStringList & args)
|
||||
/// void terminate()
|
||||
/// bool waitForFinish(int timeout_msecs = 60000)
|
||||
class PIProcess: private PIThread
|
||||
{
|
||||
public:
|
||||
@@ -53,17 +65,18 @@ public:
|
||||
PIString workingDirectory() const {return wd;}
|
||||
void setWorkingDirectory(const PIString & path) {wd = path;}
|
||||
void resetWorkingDirectory() {wd.clear();}
|
||||
void exec(const PIString & program) {args.clear(); args << program; exec_();}
|
||||
void exec(const PIString & program, const PIString & arg) {args.clear(); args << program << arg; exec_();}
|
||||
void exec(const PIString & program, const PIString & arg1, const PIString & arg2) {args.clear(); args << program << arg1 << arg2; exec_();}
|
||||
void exec(const PIString & program, const PIString & arg1, const PIString & arg2, const PIString & arg3) {args.clear(); args << program << arg1 << arg2 << arg3; exec_();}
|
||||
void exec(const PIString & program, const PIStringList & args_) {args << program << args_; exec_();}
|
||||
EVENT_HANDLER1(PIProcess, void, exec, const PIString & , program) {args.clear(); args << program; exec_();}
|
||||
EVENT_HANDLER2(PIProcess, void, exec, const PIString & , program, const PIString & , arg) {args.clear(); args << program << arg; exec_();}
|
||||
EVENT_HANDLER3(PIProcess, void, exec, const PIString & , program, const PIString & , arg1, const PIString & , arg2) {args.clear(); args << program << arg1 << arg2; exec_();}
|
||||
EVENT_HANDLER4(PIProcess, void, exec, const PIString & , program, const PIString & , arg1, const PIString & , arg2, const PIString & , arg3) {args.clear(); args << program << arg1 << arg2 << arg3; exec_();}
|
||||
EVENT_HANDLER2(PIProcess, void, exec, const PIString & , program, const PIStringList & , args_) {args << program << args_; exec_();}
|
||||
#ifdef WINDOWS
|
||||
void terminate() {if (is_exec) if (!TerminateProcess(pi.hProcess, 0)) return; pi.dwProcessId = 0;}
|
||||
EVENT_HANDLER(PIProcess, void, terminate) {if (is_exec) if (!TerminateProcess(pi.hProcess, 0)) return; pi.dwProcessId = 0;}
|
||||
#else
|
||||
void terminate() {if (is_exec) kill(pid, SIGKILL); pid = 0;}
|
||||
EVENT_HANDLER(PIProcess, void, terminate) {if (is_exec) kill(pid, SIGKILL); pid = 0;}
|
||||
#endif
|
||||
bool waitForFinish(int timeout_msecs = 60000) {return PIThread::waitForFinish(timeout_msecs);}
|
||||
EVENT_HANDLER(PIProcess, bool, waitForFinish) {return waitForFinish(60000);}
|
||||
EVENT_HANDLER1(PIProcess, bool, waitForFinish, int, timeout_msecs) {return PIThread::waitForFinish(timeout_msecs);}
|
||||
PIByteArray readOutput() {f_out.open(PIIODevice::ReadOnly); return f_out.readAll();}
|
||||
PIByteArray readError() {f_err.open(PIIODevice::ReadOnly); return f_err.readAll();}
|
||||
|
||||
@@ -79,6 +92,9 @@ public:
|
||||
static int currentPID() {return getpid();}
|
||||
#endif
|
||||
|
||||
EVENT1(PIProcess, execStarted, PIString, program)
|
||||
EVENT2(PIProcess, execFinished, PIString, program, int, exit_code)
|
||||
|
||||
private:
|
||||
virtual void run();
|
||||
void exec_();
|
||||
@@ -94,7 +110,8 @@ private:
|
||||
#else
|
||||
pid_t pid;
|
||||
#endif
|
||||
int exit_code, sz;
|
||||
FILE * tf_in, * tf_out, * tf_err;
|
||||
int exit_code, sz, as;
|
||||
bool is_exec;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user