stream.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.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to
00008 **  the terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file stream.cpp
00013 ** \version $Id: stream.cpp 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Object representing a Tor stream
00015 */
00016 
00017 #include <QStringList>
00018  
00019 #include "stream.h"
00020 
00021 
00022 /** Default constructor. */
00023 Stream::Stream()
00024 {
00025   _streamId  = 0;
00026   _status    = Unknown;
00027   _circuitId = 0;
00028   _port      = 0;
00029 }
00030 
00031 /** Constructor */
00032 Stream::Stream(quint64 streamId, Status status, quint64 circuitId, 
00033                QString address, quint16 port)
00034 {
00035   _streamId  = streamId;
00036   _status    = status;
00037   _circuitId = circuitId;
00038   _address   = address;
00039   _port      = port;
00040 }
00041 
00042 /** Constructor */
00043 Stream::Stream(quint64 streamId, Status status, quint64 circuitId, QString target)
00044 {
00045   _streamId  = streamId;
00046   _status    = status;
00047   _circuitId = circuitId;
00048   _port      = 0;
00049 
00050   int i = target.indexOf(":");
00051   if (i >= 0)
00052     _address = target.mid(0, i);
00053   if (i + 1 < target.length()) 
00054     _port = target.mid(i+1).toUInt();
00055 }
00056 
00057 /** Parses the given string for stream information, given in Tor control
00058  * protocol format. The format is:
00059  *
00060  *     StreamID SP StreamStatus SP CircID SP Target
00061  */
00062 Stream
00063 Stream::fromString(QString stream)
00064 {
00065   QStringList parts = stream.split(" ");
00066   if (parts.size() >= 4) { 
00067     /* Get the stream ID */
00068     quint64 streamId = (quint64)parts.at(0).toULongLong();
00069     /* Get the stream status value */
00070     Stream::Status status = Stream::toStatus(parts.at(1));
00071     /* Get the ID of the circuit on which this stream travels */
00072     quint64 circId = (quint64)parts.at(2).toULongLong();
00073     /* Get the target address for this stream */
00074     QString target = parts.at(3);
00075     
00076     return Stream(streamId, status, circId, target);
00077   }
00078   return Stream();
00079 }
00080 
00081 /** Converts a string description of a stream's status to its enum value */
00082 Stream::Status
00083 Stream::toStatus(QString strStatus)
00084 {
00085   Status status;
00086   strStatus = strStatus.toUpper();
00087   if (strStatus == "NEW") {
00088     status = New;
00089   } else if (strStatus == "NEWRESOLVE") {
00090     status = NewResolve;
00091   } else if (strStatus == "SENTCONNECT") {
00092     status = SentConnect;
00093   } else if (strStatus == "SENTRESOLVE") {
00094     status = SentResolve;
00095   } else if (strStatus == "SUCCEEDED") {
00096     status = Succeeded;
00097   } else if (strStatus == "FAILED") {
00098     status = Failed;
00099   } else if (strStatus == "CLOSED") {
00100     status = Closed;
00101   } else if (strStatus == "DETACHED") {
00102     status = Detached; 
00103   } else if (strStatus == "REMAP") {
00104     status = Remap;
00105   } else {
00106     status = Unknown;
00107   }
00108   return status;
00109 }
00110 
00111 /** Returns a human-understandable string representation of this 
00112  * stream's status. */
00113 QString
00114 Stream::statusString() const
00115 {
00116   QString status;
00117   switch (_status) {
00118     case New:           status = tr("New"); break;
00119     case NewResolve:    
00120     case SentResolve:   status = tr("Resolving"); break;
00121     case SentConnect:   status = tr("Connecting"); break;
00122     case Succeeded:     status = tr("Open"); break;
00123     case Failed:        status = tr("Failed"); break;
00124     case Closed:        status = tr("Closed"); break;
00125     case Detached:      status = tr("Retrying"); break;
00126     case Remap:         status = tr("Remapped"); break;
00127     default:            status = tr("Unknown"); break;
00128   }
00129   return status;
00130 }
00131 
00132 /** Returns true if all fields in this Stream object are empty. */
00133 bool
00134 Stream::isEmpty() const
00135 {
00136   return (!_streamId && !_circuitId && 
00137           (_status == Unknown) && _address.isEmpty() && !_port);
00138 }
00139 

Generated on Sat Aug 16 17:31:48 2008 for Vidalia by  doxygen 1.5.6