Logo Search packages:      
Sourcecode: yate version File versions  Download package

int Engine::run ( void   ) 

Run the engine.

Returns:
Error code, 0 for success

Definition at line 215 of file Engine.cpp.

References TelEngine::NamedList::addParam(), TelEngine::String::c_str(), TelEngine::ObjList::count(), TelEngine::Debug(), TelEngine::MessageDispatcher::dequeue(), TelEngine::GenObject::destruct(), dispatch(), enqueue(), initPlugins(), install(), TelEngine::Thread::killall(), loadPlugins(), TelEngine::Mutex::locks(), TelEngine::Message::msgTime(), TelEngine::ObjList::next(), TelEngine::Time::now(), TelEngine::Output(), TelEngine::Message::retValue(), TelEngine::Time::sec(), TelEngine::Time::usec(), usedPlugins(), and TelEngine::Thread::yield().

Referenced by main().

{
    Debug(DebugAll,"Engine::run()");
    install(new EngineStatusHandler);
    loadPlugins();
    Debug(DebugInfo,"plugins.count() = %d",plugins.count());
    if (s_super_handle >= 0) {
      install(new EngineSuperHandler);
      if (s_restarts)
          s_restarts = 1000000 * s_restarts + Time::now();
    }
    else if (s_restarts) {
      Debug(DebugWarn,"No supervisor - disabling automatic restarts");
      s_restarts = 0;
    }
    initPlugins();
    ::signal(SIGINT,sighandler);
    ::signal(SIGTERM,sighandler);
    Debug(DebugInfo,"Engine dispatching start message");
    dispatch("engine.start");
    unsigned long corr = 0;
    ::signal(SIGHUP,sighandler);
    ::signal(SIGQUIT,sighandler);
    ::signal(SIGPIPE,SIG_IGN);
    Output("Yate engine is initialized and starting up");
    while (s_haltcode == -1) {
      if (s_cmds) {
          Output("Executing initial commands");
          for (ObjList* c = s_cmds; c; c=c->next()) {
            String* s = static_cast<String*>(c->get());
            if (s) {
                Message m("engine.command");
                m.addParam("line",*s);
                if (dispatch(m)) {
                  if (m.retValue())
                      Output("%s",m.retValue().c_str());
                }
                else
                  Debug(DebugWarn,"Unrecognized command '%s'",s->c_str());
            }
          }
          s_cmds->destruct();
          s_cmds = 0;
      }

      if (s_init) {
          s_init = false;
          initPlugins();
      }

      // Create worker thread if we didn't hear about any of them in a while
      if (s_makeworker && (EnginePrivate::count < s_maxworkers)) {
          Debug(DebugInfo,"Creating new message dispatching thread (%d running)",EnginePrivate::count);
          EnginePrivate *prv = new EnginePrivate;
          prv->startup();
      }
      else
          s_makeworker = true;

      if (s_restarts && (Time::now() >= s_restarts)) {
          if (!(usedPlugins() || dispatch("engine.busy"))) {
            s_haltcode = 128;
            break;
          }
          // If we cannot restart now try again in 10s
          s_restarts = Time::now() + 10000000;
      }

      // Attempt to sleep until the next full second
      unsigned long t = (Time::now() + corr) % 1000000;
      ::usleep(1000000 - t);
      Message *m = new Message("engine.timer");
      m->addParam("time",String((int)m->msgTime().sec()));
      // Try to fine tune the ticker
      t = m->msgTime().usec() % 1000000;
      if (t > 500000)
          corr -= (1000000-t)/10;
      else
          corr += t/10;
      enqueue(m);
      Thread::yield();
    }
    s_haltcode &= 0xff;
    Output("Yate engine is shutting down with code %d",s_haltcode);
    dispatch("engine.halt");
    ::usleep(200000);
    m_dispatcher.dequeue();
    Thread::killall();
    m_dispatcher.dequeue();
    ::signal(SIGINT,SIG_DFL);
    ::signal(SIGTERM,SIG_DFL);
    ::signal(SIGHUP,SIG_DFL);
    ::signal(SIGQUIT,SIG_DFL);
    delete this;
    Debug(DebugInfo,"Exiting with %d locked mutexes",Mutex::locks());
    return s_haltcode;
}


Generated by  Doxygen 1.6.0   Back to index