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 1796 2007-07-03 01:28:52Z 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   _port      = 0;
00040 }
00041 
00042 /** Constructor */
00043 Stream::Stream(quint64 streamId, Status status, quint64 circuitId, 
00044                QString address, quint16 port)
00045 {
00046   _streamId  = streamId;
00047   _status    = status;
00048   _circuitId = circuitId;
00049   _address   = address;
00050   _port      = port;
00051 }
00052 
00053 /** Constructor */
00054 Stream::Stream(quint64 streamId, Status status, quint64 circuitId, QString target)
00055 {
00056   _streamId  = streamId;
00057   _status    = status;
00058   _circuitId = circuitId;
00059   _port      = 0;
00060 
00061   int i = target.indexOf(":");
00062   if (i >= 0)
00063     _address = target.mid(0, i);
00064   if (i + 1 < target.length()) 
00065     _port = target.mid(i+1).toUInt();
00066 }
00067 
00068 /** Parses the given string for stream information, given in Tor control
00069  * protocol format. The format is:
00070  *
00071  *     StreamID SP StreamStatus SP CircID SP Target
00072  */
00073 Stream
00074 Stream::fromString(QString stream)
00075 {
00076   QStringList parts = stream.split(" ");
00077   if (parts.size() >= 4) { 
00078     /* Get the stream ID */
00079     quint64 streamId = (quint64)parts.at(0).toULongLong();
00080     /* Get the stream status value */
00081     Stream::Status status = Stream::toStatus(parts.at(1));
00082     /* Get the ID of the circuit on which this stream travels */
00083     quint64 circId = (quint64)parts.at(2).toULongLong();
00084     /* Get the target address for this stream */
00085     QString target = parts.at(3);
00086     
00087     return Stream(streamId, status, circId, target);
00088   }
00089   return Stream();
00090 }
00091 
00092 /** Converts a string description of a stream's status to its enum value */
00093 Stream::Status
00094 Stream::toStatus(QString strStatus)
00095 {
00096   Status status;
00097   strStatus = strStatus.toUpper();
00098   if (strStatus == "NEW") {
00099     status = New;
00100   } else if (strStatus == "NEWRESOLVE") {
00101     status = NewResolve;
00102   } else if (strStatus == "SENTCONNECT") {
00103     status = SentConnect;
00104   } else if (strStatus == "SENTRESOLVE") {
00105     status = SentResolve;
00106   } else if (strStatus == "SUCCEEDED") {
00107     status = Succeeded;
00108   } else if (strStatus == "FAILED") {
00109     status = Failed;
00110   } else if (strStatus == "CLOSED") {
00111     status = Closed;
00112   } else if (strStatus == "DETACHED") {
00113     status = Detached;
00114   } else {
00115     status = Unknown;
00116   }
00117   return status;
00118 }
00119 
00120 /** Returns a human-understandable string representation of this 
00121  * stream's status. */
00122 QString
00123 Stream::statusString()
00124 {
00125   QString status;
00126   switch (_status) {
00127     case New:           status = tr("New"); break;
00128     case NewResolve:    
00129     case SentResolve:   status = tr("Resolving"); break;
00130     case SentConnect:   status = tr("Connecting"); break;
00131     case Succeeded:     status = tr("Open"); break;
00132     case Failed:        status = tr("Failed"); break;
00133     case Closed:        status = tr("Closed"); break;
00134     case Detached:      status = tr("Retrying"); break;
00135     default:            status = tr("Unknown"); break;
00136   }
00137   return status;
00138 }
00139 
00140 /** Returns true if all fields in this Stream object are empty. */
00141 bool
00142 Stream::isEmpty()
00143 {
00144   return (!_streamId && !_circuitId && 
00145           (_status == Unknown) && _address.isEmpty() && !_port);
00146 }
00147 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3