Source Code (Use browser search to find items of interest.)

Class Index

kioslave'Request (./kdebase/kioslave/ldap/kldap.h:367)

  class Request : public LDAPBase
  {
  public:

    /**
     * The mode to run the request.
     *
     * LDAP operations can be done in two ways: Synchronous requests
     * block the calling process until the result is available.
     * Asynchronous request return immediately and require the 
     * programm to retrieve the result later.
     *
     * As LDAP operations connect to a (remote) server, nonblocking
     * operation is probably the method of choice.
     *
     */
    enum RunMode { Synchronous, Asynchronous };

    /**
     * Constructor. Initializes an empty request.
     *
     * Note that the default mode is asynchronous.
     *
     */
    Request(Connection &c, RunMode m=Asynchronous);

    /// Destructor.
    virtual ~Request();

    /**
     * Execute the request.
     *
     * This method executes the request, i.e. it sends the request
     * to the server. If the run mode is "Synchronous", this method
     * will block until the result is available, or the timeout
     * has been exceeded.
     *
     * @see Request#setTimeout
     *
     * @return TRUE, if the request was initiated, else FALSE.
     *
     */
    virtual bool execute();

    /**
     * Finish the request.
     *
     * This method retrieves the result of the request. 
     * If the mode is "Asynchronous", this will block until the
     * result is available, or the timeout has been exceeded.
     *
     * @see Request#setTimeout
     *
     * @return TRUE, if the result is available, else FALSE.
     *
     */
    virtual bool finish();

    /**
     * Abandon a running request.
     *
     * This method cancels a running asynchronous operation.
     *
     * @return TRUE, if the operation was cancelled, else FALSE.
     *
     */
    virtual bool abandon();
    
    /**
     * Set timeout usage.
     *
     * Decides if a timeout is used in retrieving the result.
     *
     * The timeout is only used if you "setUseTimeout(TRUE);". This
     * is to allow a timeout of zero for polling the result.
     *
     * @see Request#setTimeout
     *
     * @param use TRUE, if timeout should be used, else FALSE.
     */
    void setUseTimeout(bool use) { use_timeout=use; };

    /// Returns timeout usage.
    bool useTimeout() { return use_timeout; };

    /**
     * Set timeout.
     *
     * Allows to set the time to wait for a request to finish.
     *
     * You can use a timeout of 0 seconds, 0 microseconds to 
     * implement a polling. 
     *
     * Note that the timeout is ignored until you call
     * setUseTimeout(TRUE).
     * 
     * @see Request#setUseTimeout
     *
     * @param sec seconds to wait
     * @param usec microseconds to wait
     *
     */
    void setTimeout(int sec, int usec=0) { _timeout.tv_sec=sec; _timeout.tv_usec=usec; };
    
    /**
     * Returns the timeout.
     *
     * Returns the value of the currently set timeout.
     *
     * @param sec Variable to store the seconds.
     * @param usec Variable to store the microseconds.
     *
     */
    void timeout(int &sec, int &usec) { sec=_timeout.tv_sec; usec=_timeout.tv_usec; };

  protected:

    int     expected;
    RunMode mode;
    bool    running;

    int id;
    int all;

    LDAPMessage    *req_result;
    struct timeval _timeout;
    bool           use_timeout;

  };

  /**
   * A class for search requests.
   *
   * This class enapsulates LDAP search requests, the most common
   * form of LDAP requests.
   *
   */ 

kioslave'Request::Request() (./kdebase/kioslave/ldap/kldap.cpp:93)

Request::Request(Connection &c, RunMode m)
  : LDAPBase(), mode(m), running(FALSE), id(0), all(1), 
    req_result(0), use_timeout(FALSE)
{
  _timeout.tv_sec = 0;
  _timeout.tv_usec = 0;
  
  _handle = c.handle();
  expected = -2;
}



kioslave'Request::~Request() (./kdebase/kioslave/ldap/kldap.cpp:105)

Request::~Request()
{
  if (req_result)
    ldap_msgfree(req_result);
}



kioslave'Request::execute() (./kdebase/kioslave/ldap/kldap.cpp:112)

bool Request::execute()
{
  // if there was already a request: stop it
  if (running)
    abandon();

  // mark the request as running
  running = TRUE;

  return TRUE;
}



kioslave'Request::finish() (./kdebase/kioslave/ldap/kldap.cpp:125)

bool Request::finish()
{
  if (!handle())
    return FALSE;

  debug << "finish request" << endl;

  // if sync, the result is already there
  // if not: get the result
  if (mode == Asynchronous)
    {
      int retval;

      // was there really a request?
      if (!id)
	return FALSE;

      // delete previous result
      if (req_result)
	ldap_msgfree(req_result);

      // get the result
      if (use_timeout)
	retval = ldap_result(handle(), id, all, &_timeout, &req_result);
      else
	retval = ldap_result(handle(), id, all, 0, &req_result);
      
      // check if there was an error
      if (retval == -1)
	{
	  running = FALSE;
	  id = 0;
	  return check(handle()->ld_errno);
	}

      // check if the timeout was exceeded
      if (retval == 0)
	return FALSE;
	
      // check if the result was the one expected
      if (retval != expected)
	return FALSE;
    }

  // the result should be ready now
  if (!req_result)
    return FALSE;

  return TRUE;
}



kioslave'Request::abandon() (./kdebase/kioslave/ldap/kldap.cpp:177)

bool Request::abandon()
{
  if (!handle())
    return FALSE;

  // check only if async
  if (mode == Asynchronous)
    {
      // was there really a request?
      if (!id)
	return FALSE;

      // cancel the request
      id = 0;
      running = FALSE;
      return ldap_abandon(handle(), id);
    }

  return TRUE;
}