Vidalia  0.2.15
SendCommandEvent.cpp
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If 
00004 **  you did not receive the LICENSE file with this file, you may obtain it
00005 **  from the Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file SendEommandEvent.cpp
00013 ** \brief An event posted to a socket living in another thread, indicating
00014 ** that it should send the given control command.
00015 */
00016 
00017 #include "SendCommandEvent.h"
00018 
00019 #include <QMutexLocker>
00020 
00021 
00022 SendCommandEvent::SendCommandEvent(const ControlCommand &cmd, SendWaiter *w)
00023   : QEvent(QEvent::User)
00024 {
00025   _cmd    = cmd;
00026   _waiter = w;
00027 }
00028 
00029 /** Sets the result of the send operation. */
00030 void
00031 SendCommandEvent::SendWaiter::setResult(bool success, const QString &errmsg)
00032 {
00033   _mutex.lock();
00034   _status = (success ? Success : Failed);
00035   _errmsg = errmsg;
00036   _mutex.unlock();
00037   _waitCond.wakeAll();
00038 }
00039 
00040 /** Waits for and gets the result of the send operation. */
00041 bool 
00042 SendCommandEvent::SendWaiter::getResult(QString *errmsg)
00043 {
00044   forever {
00045     _mutex.lock();
00046     if (_status == Waiting) {
00047       _waitCond.wait(&_mutex);
00048       _mutex.unlock();
00049     } else {
00050       _mutex.unlock();
00051       break;
00052     }
00053   }
00054   if (errmsg) {
00055     *errmsg = _errmsg;
00056   }
00057   return (_status == Success);
00058 }
00059 
00060 /** Returns the SendWaiter's current SenderStatus value. */
00061 SendCommandEvent::SendWaiter::SenderStatus
00062 SendCommandEvent::SendWaiter::status()
00063 {
00064   QMutexLocker locker(&_mutex);
00065   return _status;
00066 }