RouterDescriptor.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 RouterDescriptor.cpp
00013 ** \version $Id: RouterDescriptor.cpp 4144 2009-10-14 21:52:34Z edmanm $
00014 ** \brief Parses a blob of router descriptor text from Tor
00015 */
00016 
00017 #include "RouterDescriptor.h"
00018 
00019 #include <QtGlobal>
00020 
00021 
00022 /** Constructor. Just assigns the ID and determines whether the router is
00023  * responsive or not based on the presence of a "!" at the start of the ID.
00024  * See tor-spec.txt for details. */
00025 RouterDescriptor::RouterDescriptor(QStringList descriptor)
00026 {
00027   _status = Online;
00028   parseDescriptor(descriptor);
00029 }
00030 
00031 /** Parses this router's descriptor for relevant information. */
00032 void
00033 RouterDescriptor::parseDescriptor(QStringList descriptor)
00034 {
00035   foreach (QString line, descriptor) {
00036     if (line.startsWith("router ")) {
00037       QStringList parts = line.remove(0,qstrlen("router ")).split(" ");
00038       _name    = parts.at(0);
00039       _ip      = QHostAddress(parts.at(1));
00040       _orPort  = (quint16)parts.at(2).toUInt();
00041       _dirPort = (quint16)parts.at(4).toUInt();
00042     } else if (line.startsWith("platform ")) {
00043       _platform = line.remove(0,qstrlen("platform "));
00044     } else if (line.startsWith("published ")) {
00045       _published = QDateTime::fromString(
00046                                line.remove(0,qstrlen("published ")),
00047                                "yyyy-MM-dd HH:mm:ss");
00048       _published.setTimeSpec(Qt::UTC);
00049     } else if (line.startsWith("opt fingerprint ")) {
00050       _fingerprint = line.remove(0,qstrlen("opt fingerprint "));
00051       _id = _fingerprint.remove(" ");
00052     } else if (line.startsWith("fingerprint ")) {
00053       _fingerprint = line.remove(0,qstrlen("fingerprint "));
00054       _id = _fingerprint.remove(" ");
00055     } else if (line.startsWith("uptime ")) {
00056       _uptime = (quint64)line.remove(0,qstrlen("uptime ")).toULongLong();
00057     } else if (line.startsWith("bandwidth ")) {
00058       QStringList bw = line.remove(0,qstrlen("bandwidth ")).split(" ");
00059       _avgBandwidth      = (quint64)bw.at(0).toULongLong();
00060       _burstBandwidth    = (quint64)bw.at(1).toULongLong();
00061       _observedBandwidth = (quint64)bw.at(2).toULongLong();
00062     } else if (line.startsWith("contact ")) {
00063       _contact = line.remove(0,qstrlen("contact "));
00064     } else if (line.startsWith("hibernating ")) {
00065       if (line.remove(0,qstrlen("hibernating ")).trimmed() == "1") {
00066         _status = Hibernating;
00067       }
00068     }
00069   }
00070 }
00071 
00072 /** Returns a string representation of the status of this router. */
00073 QString
00074 RouterDescriptor::status()
00075 {
00076   if (_status == Online) {
00077     return tr("Online");
00078   } else if (_status == Hibernating) {
00079     return tr("Hibernating");
00080   }
00081   return tr("Offline");
00082 }
00083 

Generated on Mon Aug 30 19:14:02 2010 for Vidalia by  doxygen 1.5.9