diff --git a/clients/main-qt4.cpp b/clients/main-qt4.cpp index a6846f5f..0b792497 100644 --- a/clients/main-qt4.cpp +++ b/clients/main-qt4.cpp @@ -25,13 +25,67 @@ #include #include "qt4/qt4client.h" +#define WAIT_ENGINE 10000 //wait 10 seconds for engine to halt + using namespace TelEngine; -static QtDriver qtdriver; +class EngineThread; + +static QtDriver qtdriver(false); +static EngineThread* s_engineThread = 0; + +class EngineThread : public Thread +{ +public: + inline EngineThread() + : Thread("Engine") + { } + virtual void run(); + virtual void cleanup(); +}; + +void EngineThread::run() +{ + Engine::engineRun(); + Debug(DebugAll,"Engine stopped running"); +} + +void EngineThread::cleanup() +{ + Debug(DebugAll,"EngineThread::cleanup() [%p]",this); + s_engineThread = 0; +} + extern "C" int main(int argc, const char** argv, const char** envp) { TelEngine::Engine::extraPath("qt4"); - return TelEngine::Engine::main(argc,argv,envp,TelEngine::Engine::Client); + // parse arguments + int retcode = TelEngine::Engine::main(argc,argv,envp,TelEngine::Engine::ClientMainThread); + if (retcode) + return retcode; + + // create engine from this thread + Engine::self(); + s_engineThread = new EngineThread; + if (!s_engineThread->startup()) + return EINVAL; + + // build client if the driver didn't + if (!QtClient::self()) + new QtClient(); + // run the client + QtClient::self()->run(); + // the client finished running, do cleanup + QtClient::self()->cleanup(); + + // wait for the engine to halt + Engine::halt(0); + unsigned long count = WAIT_ENGINE / Thread::idleMsec(); + while (s_engineThread && count--) + Thread::idle(); + Thread::killall(); + + return retcode; } /* vi: set ts=8 sw=4 sts=4 noet: */ diff --git a/engine/Engine.cpp b/engine/Engine.cpp index 9ab487ad..b3eddab7 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -735,7 +735,7 @@ static bool logFileOpen() return false; } -static int engineRun() +int Engine::engineRun() { time_t t = ::time(0); s_startMsg << "Yate (" << ::getpid() << ") is starting " << ::ctime(&t); @@ -1837,6 +1837,9 @@ int Engine::main(int argc, const char** argv, const char** env, RunMode mode, bo bool daemonic = false; bool supervised = false; #endif + bool noStart = (mode == ClientMainThread); + if (noStart) + mode = Client; bool client = (mode == Client); Debugger::Formatting tstamp = Debugger::None; bool colorize = false; @@ -2287,7 +2290,7 @@ int Engine::main(int argc, const char** argv, const char** env, RunMode mode, bo } else #endif - retcode = engineRun(); + retcode = noStart ? 0 : engineRun(); return retcode; } diff --git a/yatengine.h b/yatengine.h index fb0400ca..c79de35f 100644 --- a/yatengine.h +++ b/yatengine.h @@ -821,6 +821,7 @@ public: Server = 2, Client = 3, ClientProxy = 4, + ClientMainThread = 5, }; enum CallAccept { @@ -1152,6 +1153,12 @@ public: */ static void clearEvents(const String& type); + /** + * Start running the engine + * @return The code with which the engine has stopped + */ + static int engineRun(); + protected: /** * Destroys the engine and everything. You must not call it directly,