예제 #1
0
// QueryExpression
QueryExpression::QueryExpression(const std::string &query, XmlQueryContext &context, Transaction *txn, bool debug)
	: query_(query),
	  context_(context),
	  qec_(context_, /*debugging*/false),
	  conf_(context, txn, &ci_),
	  xqContext_(XQilla::createContext(XQilla::XQUERY_UPDATE, &conf_, Globals::defaultMemoryManager)),
	  expr_(0)
{
	((Manager &)((QueryContext &)getContext()).getManager())
		.log(Log::C_OPTIMIZER, Log::L_INFO, "Started parse");

	HighResTimer t;
	t.start();

	conf_.setMinder(&minder_);
	conf_.setQueryExecutionContext(&qec_);
	conf_.setProjectionInfo(&pi_);

	try {
		((QueryContext&)context_).startQuery();
		expr_ = XQilla::parse(UTF8ToXMLCh(getQuery()).str(), xqContext_, 0,
			XQilla::NO_STATIC_RESOLUTION | XQilla::NO_ADOPT_CONTEXT);

		ScopedPtr<Optimizer> optimizer(createOptimizer(xqContext_, minder_, debug));
		optimizer->startOptimize(expr_);
	}
	catch(const QueryInterruptedException &) {
		delete expr_;
		throw XmlException(XmlException::OPERATION_INTERRUPTED,
			"Query was interrupted by the application");
	}
	catch(const QueryTimeoutException &) {
		delete expr_;
		throw XmlException(XmlException::OPERATION_TIMEOUT,
			"Query timed out");
	}
	catch(const XQException &e) {
		delete expr_;
		throw XmlException(XmlException::QUERY_PARSER_ERROR, e);
	}
	catch(...) {
		delete expr_;
		throw;
	}
	t.stop();

	if(Log::isLogEnabled(Log::C_OPTIMIZER, Log::L_INFO)) {
		ostringstream s;
		s << "Finished parse, time taken = " << (t.durationInSeconds() * 1000) << "ms";
		((Manager &)((QueryContext &)getContext()).getManager())
			.log(Log::C_OPTIMIZER, Log::L_INFO, s);
	}
}
예제 #2
0
void Server::beginServer ()
{
  if (_log) _log->write ("Server starting");

  if (_daemon)
  {
    daemonize ();  // Only the child returns.
    writePidFile ();
  }

  signal (SIGHUP,  signal_handler);  // Graceful stop
  signal (SIGUSR1, signal_handler);  // Config reload
  signal (SIGUSR2, signal_handler);

  TLSServer server;
  if (_config)
  {
    server.debug (_config->getInteger ("debug.tls"));

    std::string ciphers = _config->get ("ciphers");
    if (ciphers != "")
    {
      server.ciphers (ciphers);
      if (_log) _log->format ("Using ciphers: %s", ciphers.c_str ());
    }

    std::string trust = _config->get ("trust");
    if (trust == "allow all")
      server.trust (TLSServer::allow_all);
    else if (trust == "strict")
      server.trust (TLSServer::strict);
    else if (_log)
      _log->format ("Invalid 'trust' setting value of '%s'", trust.c_str ());
  }

  server.init (_ca_file,        // CA
               _crl_file,       // CRL
               _cert_file,      // Cert
               _key_file);      // Key
  server.queue (_queue_size);
  server.bind (_host, _port, _family);
  server.listen ();

  if (_log) _log->write ("Server ready");

  _request_count = 0;
  while (1)
  {
    try
    {
      TLSTransaction tx;
      tx.trust (server.trust ());
      server.accept (tx);

      if (_sighup)
        throw "SIGHUP shutdown.";

      // Get client address and port, for logging.
      if (_log_clients)
        tx.getClient (_client_address, _client_port);

      // Metrics.
      HighResTimer timer;
      timer.start ();

      std::string input;
      tx.recv (input);

      // Handle the request.
      ++_request_count;

      // Call the derived class handler.
      std::string output;
      handler (input, output);
      if (output.length ())
        tx.send (output);

      if (_log)
      {
        timer.stop ();
        _log->format ("[%d] Serviced in %.6fs", _request_count, timer.total ());
      }
    }

    catch (std::string& e) { if (_log) _log->write (std::string ("Error: ") + e); }
    catch (char* e)        { if (_log) _log->write (std::string ("Error: ") + e); }
    catch (...)            { if (_log) _log->write ("Error: Unknown exception"); }
  }
}