Vidalia  0.3.1
SendCommandEvent.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file SendEommandEvent.cpp
13 ** \brief An event posted to a socket living in another thread, indicating
14 ** that it should send the given control command.
15 */
16 
17 #include "SendCommandEvent.h"
18 
19 #include <QMutexLocker>
20 
21 
23  : QEvent(QEvent::User)
24 {
25  _cmd = cmd;
26  _waiter = w;
27 }
28 
29 /** Sets the result of the send operation. */
30 void
31 SendCommandEvent::SendWaiter::setResult(bool success, const QString &errmsg)
32 {
33  _mutex.lock();
34  _status = (success ? Success : Failed);
35  _errmsg = errmsg;
36  _mutex.unlock();
37  _waitCond.wakeAll();
38 }
39 
40 /** Waits for and gets the result of the send operation. */
41 bool
43 {
44  forever {
45  _mutex.lock();
46  if (_status == Waiting) {
47  _waitCond.wait(&_mutex);
48  _mutex.unlock();
49  } else {
50  _mutex.unlock();
51  break;
52  }
53  }
54  if (errmsg) {
55  *errmsg = _errmsg;
56  }
57  return (_status == Success);
58 }
59 
60 /** Returns the SendWaiter's current SenderStatus value. */
63 {
64  QMutexLocker locker(&_mutex);
65  return _status;
66 }
SendCommandEvent::SendWaiter::setResult
void setResult(bool success, const QString &errmsg=QString())
Definition: SendCommandEvent.cpp:31
SendCommandEvent::SendWaiter::_status
enum SendCommandEvent::SendWaiter::SenderStatus _status
SendCommandEvent::_waiter
SendWaiter * _waiter
Definition: SendCommandEvent.h:57
SendCommandEvent::SendCommandEvent
SendCommandEvent(const ControlCommand &cmd, SendWaiter *w=0)
Definition: SendCommandEvent.cpp:22
SendCommandEvent::SendWaiter::_mutex
QMutex _mutex
Definition: SendCommandEvent.h:43
SendCommandEvent::SendWaiter::Success
@ Success
Definition: SendCommandEvent.h:33
ControlCommand
Definition: ControlCommand.h:22
SendCommandEvent::SendWaiter::status
SenderStatus status()
Definition: SendCommandEvent.cpp:62
SendCommandEvent.h
SendCommandEvent::SendWaiter::getResult
bool getResult(QString *errmsg=0)
Definition: SendCommandEvent.cpp:42
SendCommandEvent::SendWaiter::_waitCond
QWaitCondition _waitCond
Definition: SendCommandEvent.h:44
SendCommandEvent::SendWaiter::SenderStatus
SenderStatus
Definition: SendCommandEvent.h:33
SendCommandEvent::SendWaiter::_errmsg
QString _errmsg
Definition: SendCommandEvent.h:45
SendCommandEvent::_cmd
ControlCommand _cmd
Definition: SendCommandEvent.h:56
SendCommandEvent::SendWaiter
Definition: SendCommandEvent.h:30
SendCommandEvent::SendWaiter::Failed
@ Failed
Definition: SendCommandEvent.h:33