stream.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file stream.cpp
00024  * \version $Id: stream.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Object representing a Tor stream
00026  */
00027 
00028 #include <QStringList>
00029  
00030 #include "stream.h"
00031 
00032 
00033 /** Default constructor. */
00034 Stream::Stream()
00035 {
00036   _streamId  = 0;
00037   _status    = Unknown;
00038   _circuitId = 0;
00039   _target    = QString();
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   _target    = target;
00049 }
00050 
00051 /** Parses the given string for stream information, given in Tor control
00052  * protocol format. The format is:
00053  *
00054  *     StreamID SP StreamStatus SP CircID SP Target
00055  */
00056 Stream
00057 Stream::fromString(QString stream)
00058 {
00059   QStringList parts = stream.split(" ");
00060   if (parts.size() >= 4) { 
00061     /* Get the stream ID */
00062     quint64 streamId = (quint64)parts.at(0).toULongLong();
00063     /* Get the stream status value */
00064     Stream::Status status = Stream::toStatus(parts.at(1));
00065     /* Get the ID of the circuit on which this stream travels */
00066     quint64 circId = (quint64)parts.at(2).toULongLong();
00067     /* Get the target address for this stream */
00068     QString target = parts.at(3);
00069     
00070     return Stream(streamId, status, circId, target);
00071   }
00072   return Stream();
00073 }
00074 
00075 /** Converts a string description of a stream's status to its enum value */
00076 Stream::Status
00077 Stream::toStatus(QString strStatus)
00078 {
00079   Status status;
00080   strStatus = strStatus.toUpper();
00081   if (strStatus == "NEW") {
00082     status = New;
00083   } else if (strStatus == "NEWRESOLVE") {
00084     status = NewResolve;
00085   } else if (strStatus == "SENTCONNECT") {
00086     status = SentConnect;
00087   } else if (strStatus == "SENTRESOLVE") {
00088     status = SentResolve;
00089   } else if (strStatus == "SUCCEEDED") {
00090     status = Succeeded;
00091   } else if (strStatus == "FAILED") {
00092     status = Failed;
00093   } else if (strStatus == "CLOSED") {
00094     status = Closed;
00095   } else if (strStatus == "DETACHED") {
00096     status = Detached;
00097   } else {
00098     status = Unknown;
00099   }
00100   return status;
00101 }
00102 
00103 /** Returns a human-understandable string representation of this 
00104  * stream's status. */
00105 QString
00106 Stream::statusString()
00107 {
00108   QString status;
00109   switch (_status) {
00110     case New:           status = tr("New"); break;
00111     case NewResolve:    
00112     case SentResolve:   status = tr("Resolving"); break;
00113     case SentConnect:   status = tr("Connecting"); break;
00114     case Succeeded:     status = tr("Open"); break;
00115     case Failed:        status = tr("Failed"); break;
00116     case Closed:        status = tr("Closed"); break;
00117     case Detached:      status = tr("Retrying"); break;
00118     default:            status = tr("Unknown"); break;
00119   }
00120   return status;
00121 }
00122 
00123 /** Returns true if all fields in this Stream object are empty. */
00124 bool
00125 Stream::isEmpty()
00126 {
00127   return (!_streamId && !_circuitId && 
00128           (_status == Unknown) && _target.isEmpty());
00129 }
00130 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0