[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r2837: Add a SendCommandEvent class that can be used to post an eve (vidalia/trunk/src/torcontrol)
Author: edmanm
Date: 2008-07-06 18:41:50 -0400 (Sun, 06 Jul 2008)
New Revision: 2837
Added:
vidalia/trunk/src/torcontrol/sendcommandevent.cpp
vidalia/trunk/src/torcontrol/sendcommandevent.h
Log:
Add a SendCommandEvent class that can be used to post an event to the control
socket in the correct thread, letting it know it should send a control
command.
Added: vidalia/trunk/src/torcontrol/sendcommandevent.cpp
===================================================================
--- vidalia/trunk/src/torcontrol/sendcommandevent.cpp (rev 0)
+++ vidalia/trunk/src/torcontrol/sendcommandevent.cpp 2008-07-06 22:41:50 UTC (rev 2837)
@@ -0,0 +1,67 @@
+/*
+** This file is part of Vidalia, and is subject to the license terms in the
+** LICENSE file, found in the top level directory of this distribution. If
+** you did not receive the LICENSE file with this file, you may obtain it
+** from the Vidalia source package distributed by the Vidalia Project at
+** http://www.vidalia-project.net/. No part of Vidalia, including this file,
+** may be copied, modified, propagated, or distributed except according to
+** the terms described in the LICENSE file.
+*/
+
+/*
+** \file sendcommandevent.cpp
+** \version $Id$
+** \brief An event posted to a socket living in another thread, indicating
+** that it should send the given control command.
+*/
+
+#include <QMutexLocker>
+
+#include "sendcommandevent.h"
+
+
+SendCommandEvent::SendCommandEvent(const ControlCommand &cmd, SendWaiter *w)
+ : QEvent((QEvent::Type)CustomEventType::SendCommandEvent)
+{
+ _cmd = cmd;
+ _waiter = w;
+}
+
+/** Sets the result of the send operation. */
+void
+SendCommandEvent::SendWaiter::setResult(bool success, const QString &errmsg)
+{
+ _mutex.lock();
+ _status = (success ? Success : Failed);
+ _errmsg = errmsg;
+ _mutex.unlock();
+ _waitCond.wakeAll();
+}
+
+/** Waits for and gets the result of the send operation. */
+bool
+SendCommandEvent::SendWaiter::getResult(QString *errmsg)
+{
+ forever {
+ _mutex.lock();
+ if (_status == Waiting) {
+ _waitCond.wait(&_mutex);
+ _mutex.unlock();
+ } else {
+ _mutex.unlock();
+ break;
+ }
+ }
+ if (errmsg) {
+ *errmsg = _errmsg;
+ }
+ return (_status == Success);
+}
+
+/** Returns the SendWaiter's current SenderStatus value. */
+SendCommandEvent::SendWaiter::SenderStatus
+SendCommandEvent::SendWaiter::status()
+{
+ QMutexLocker locker(&_mutex);
+ return _status;
+}
Property changes on: vidalia/trunk/src/torcontrol/sendcommandevent.cpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: vidalia/trunk/src/torcontrol/sendcommandevent.h
===================================================================
--- vidalia/trunk/src/torcontrol/sendcommandevent.h (rev 0)
+++ vidalia/trunk/src/torcontrol/sendcommandevent.h 2008-07-06 22:41:50 UTC (rev 2837)
@@ -0,0 +1,62 @@
+/*
+** This file is part of Vidalia, and is subject to the license terms in the
+** LICENSE file, found in the top level directory of this distribution. If
+** you did not receive the LICENSE file with this file, you may obtain it
+** from the Vidalia source package distributed by the Vidalia Project at
+** http://www.vidalia-project.net/. No part of Vidalia, including this file,
+** may be copied, modified, propagated, or distributed except according to
+** the terms described in the LICENSE file.
+*/
+
+/*
+** \file sendcommandevent.h
+** \version $Id$
+** \brief An event posted to a socket living in another thread, indicating
+** that it should send the given control command.
+*/
+
+#ifndef _SENDCOMMANDEVENT_H
+#define _SENDCOMMANDEVENT_H
+
+#include <QEvent>
+#include <QMutex>
+#include <QWaitCondition>
+
+#include "eventtype.h"
+#include "controlcommand.h"
+
+
+class SendCommandEvent : public QEvent {
+public:
+ /** Object used to wait for the result of a send operation. */
+ class SendWaiter {
+ public:
+ /** Status of the send SendWaiter. */
+ enum SenderStatus { Waiting, Failed, Success } _status;
+ /** Default constructor. */
+ SendWaiter() { _status = Waiting; }
+ /** Sets the result of the send operation. */
+ void setResult(bool success, const QString &errmsg = QString());
+ /** Waits for and returns the result of the send operation. */
+ bool getResult(QString *errmsg = 0);
+ /** Returns the SendWaiter's current SenderStatus value. */
+ SenderStatus status();
+ private:
+ QMutex _mutex; /**< Mutex around the wait condition. */
+ QWaitCondition _waitCond; /**< Waits for the send to complete. */
+ QString _errmsg; /**< Error message if the send fails. */
+ };
+
+ /** Constructor. */
+ SendCommandEvent(const ControlCommand &cmd, SendWaiter *w = 0);
+ /** Returns the control command to send to Tor. */
+ ControlCommand command() { return _cmd; }
+ /** Returns a SendWaiter (if any) for the result of this send. */
+ SendWaiter* waiter() { return _waiter; }
+
+private:
+ ControlCommand _cmd; /**< Command to send to Tor. */
+ SendWaiter* _waiter; /**< SendWaiter for the result of this event. */
+};
+
+#endif
Property changes on: vidalia/trunk/src/torcontrol/sendcommandevent.h
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native