Ticket #6249: TrafficColumn.patch

File TrafficColumn.patch, 14.8 KB (added by cypherpunks, 8 years ago)
  • torcontrol/Stream.cpp

    diff -rupN src-orig/torcontrol/Stream.cpp src/torcontrol/Stream.cpp
    old new Stream::isValidStreamId(const StreamId & 
    8989
    9090  for (int i = 0; i < length; i++) {
    9191    char c = streamId[i].toAscii();
    92     if (c < '0' && c > '9' && c < 'A' && c > 'Z' && c < 'a' && c > 'z')
     92    if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')))
    9393      return false;
    9494  }
    9595  return true;
  • torcontrol/Stream.h

    diff -rupN src-orig/torcontrol/Stream.h src/torcontrol/Stream.h
    old new  
    2626
    2727/** Stream IDs contains 1-16 alphanumeric ASCII characters. */
    2828typedef QString StreamId;
     29Q_DECLARE_METATYPE(StreamId)
    2930
    3031
    3132class Stream
  • torcontrol/TorControl.cpp

    diff -rupN src-orig/torcontrol/TorControl.cpp src/torcontrol/TorControl.cpp
    old new TorControl::TorControl(ControlMethod::Me 
    4141  RELAY_SIGNAL(_eventHandler, SIGNAL(bandwidthUpdate(quint64, quint64)));
    4242  RELAY_SIGNAL(_eventHandler, SIGNAL(circuitStatusChanged(Circuit)));
    4343  RELAY_SIGNAL(_eventHandler, SIGNAL(streamStatusChanged(Stream)));
     44  RELAY_SIGNAL(_eventHandler,
     45               SIGNAL(streamBandwidthUpdate(StreamId, quint64, quint64)));
    4446  RELAY_SIGNAL(_eventHandler, SIGNAL(newDescriptors(QStringList)));
    4547  RELAY_SIGNAL(_eventHandler, SIGNAL(logMessage(tc::Severity, QString)));
    4648  RELAY_SIGNAL(_eventHandler, SIGNAL(dangerousPort(quint16, bool)));
  • torcontrol/TorControl.h

    diff -rupN src-orig/torcontrol/TorControl.h src/torcontrol/TorControl.h
    old new signals: 
    262262   */
    263263  void circuitStatusChanged(const Circuit &circuit);
    264264
     265  /** Emitted when Tor sends a bandwidth usage update for a stream.
     266   * <b>bytesReceived</b> is the number of bytes read on the stream over
     267   * the previous second and <b>bytesWritten</b> is the number of bytes
     268   * sent over the same interval.
     269   */
     270  void streamBandwidthUpdate(const StreamId &streamId,
     271                             quint64 bytesReceived, quint64 bytesSent);
     272
    265273  /** Emitted when Tor has mapped the address <b>from</b> to the address
    266274   * <b>to</b>. <b>expires</b> indicates the time at which when the address
    267275   * mapping will no longer be considered valid.
  • torcontrol/TorEvents.cpp

    diff -rupN src-orig/torcontrol/TorEvents.cpp src/torcontrol/TorEvents.cpp
    old new TorEvents::TorEvents(QObject *parent) 
    4141  qRegisterMetaType<BootstrapStatus>("BootstrapStatus");
    4242  qRegisterMetaType<Circuit>("Circuit");
    4343  qRegisterMetaType<Stream>("Stream");
     44  qRegisterMetaType<StreamId>("StreamId");
    4445
    4546  qRegisterMetaType<QHostAddress>("QHostAddress");
    4647  qRegisterMetaType<QDateTime>("QDateTime");
    TorEvents::toString(Event e) 
    6566    case GeneralStatus:   event = "STATUS_GENERAL"; break;
    6667    case ClientStatus:    event = "STATUS_CLIENT"; break;
    6768    case ServerStatus:    event = "STATUS_SERVER"; break;
     69    case StreamBandwidth: event = "STREAM_BW"; break;
    6870    default: event = "UNKNOWN"; break;
    6971  }
    7072  return event;
    TorEvents::toTorEvent(const QString &eve 
    101103    e = ClientStatus;
    102104  } else if (event == "STATUS_SERVER") {
    103105    e = ServerStatus;
     106  } else if (event == "STREAM_BW") {
     107    e = StreamBandwidth;
    104108  } else {
    105109    e = Unknown;
    106110  }
    TorEvents::handleEvent(const ControlRepl 
    128132      case Bandwidth:      handleBandwidthUpdate(line); break;
    129133      case CircuitStatus:  handleCircuitStatus(line); break;
    130134      case StreamStatus:   handleStreamStatus(line); break;
     135      case StreamBandwidth:handleStreamBandwidthUpdate(line); break;
    131136      case NewDescriptor:  handleNewDescriptor(line); break;
    132137      case AddressMap:     handleAddressMap(line); break;
    133138
    TorEvents::handleStreamStatus(const Repl 
    221226  }
    222227}
    223228
     229/** Handle a stream bandwidth update event. The format of this message is:
     230 *
     231 *    "650" SP "STREAM_BW" SP StreamID SP BytesWritten SP BytesRead
     232 *     BytesWritten = 1*DIGIT
     233 *     BytesRead = 1*DIGIT
     234 */
     235void
     236TorEvents::handleStreamBandwidthUpdate(const ReplyLine &line)
     237{
     238  QStringList msg = line.getMessage().split(" ");
     239  if (msg.size() >= 4) {
     240    StreamId streamId = msg.at(1);
     241    quint64 bytesSent = (quint64)msg.at(2).toULongLong();
     242    quint64 bytesReceived = (quint64)msg.at(3).toULongLong();
     243
     244    /* Post the event to each of the interested targets */
     245    if (Stream::isValidStreamId(streamId))
     246      emit streamBandwidthUpdate(streamId, bytesReceived, bytesSent);
     247  }
     248}
     249
    224250/** Handle a log message event. The format of this message is:
    225251 *  The syntax is:
    226252 *
  • torcontrol/TorEvents.h

    diff -rupN src-orig/torcontrol/TorEvents.h src/torcontrol/TorEvents.h
    old new  
    2525
    2626class Circuit;
    2727class Stream;
     28typedef QString StreamId;
    2829class BootstrapStatus;
    2930class ControlReply;
    3031class ReplyLine;
    class TorEvents : public QObject 
    4142public:
    4243  /** Asynchronous events sent from Tor to the controller */
    4344  enum Event {
    44     Unknown       = 0,
    45     Bandwidth     = (1u << 0),
    46     LogDebug      = (1u << 1),
    47     LogInfo       = (1u << 2),
    48     LogNotice     = (1u << 3),
    49     LogWarn       = (1u << 4),
    50     LogError      = (1u << 5),
    51     CircuitStatus = (1u << 6),
    52     StreamStatus  = (1u << 7),
    53     OrConnStatus  = (1u << 8),
    54     NewDescriptor = (1u << 9),
    55     AddressMap    = (1u << 10),
    56     GeneralStatus = (1u << 11),
    57     ClientStatus  = (1u << 12),
    58     ServerStatus  = (1u << 13)
     45    Unknown         = 0,
     46    Bandwidth       = (1u << 0),
     47    LogDebug        = (1u << 1),
     48    LogInfo         = (1u << 2),
     49    LogNotice       = (1u << 3),
     50    LogWarn         = (1u << 4),
     51    LogError        = (1u << 5),
     52    CircuitStatus   = (1u << 6),
     53    StreamStatus    = (1u << 7),
     54    OrConnStatus    = (1u << 8),
     55    NewDescriptor   = (1u << 9),
     56    AddressMap      = (1u << 10),
     57    GeneralStatus   = (1u << 11),
     58    ClientStatus    = (1u << 12),
     59    ServerStatus    = (1u << 13),
     60    StreamBandwidth = (1u << 14),
    5961  };
    6062  static const Event EVENT_MIN = TorEvents::Bandwidth;
    61   static const Event EVENT_MAX = TorEvents::ServerStatus;
     63  static const Event EVENT_MAX = TorEvents::StreamBandwidth;
    6264  Q_DECLARE_FLAGS(Events, Event);
    6365
    6466  /** Default Constructor */
    signals: 
    9193   */
    9294  void circuitStatusChanged(const Circuit &circuit);
    9395
     96  /** Emitted when Tor sends a bandwidth usage update for a stream.
     97   * <b>bytesReceived</b> is the number of bytes read on the stream over
     98   * the previous second and <b>bytesWritten</b> is the number of bytes
     99   * sent over the same interval.
     100   */
     101  void streamBandwidthUpdate(const StreamId &streamId,
     102                             quint64 bytesReceived, quint64 bytesSent);
     103
    94104  /** Emitted when Tor has mapped the address <b>from</b> to the address
    95105   * <b>to</b>. <b>expires</b> indicates the time at which when the address
    96106   * mapping will no longer be considered valid.
    private: 
    223233  void handleCircuitStatus(const ReplyLine &line);
    224234  /** Handle a stream status event */
    225235  void handleStreamStatus(const ReplyLine &line);
     236  /** Handle a stream bandwidth event */
     237  void handleStreamBandwidthUpdate(const ReplyLine &line);
    226238  /** Handle a log message event */
    227239  void handleLogMessage(const ReplyLine &line);
    228240  /** Handle an OR connection status event. */
  • vidalia/network/CircuitListWidget.cpp

    diff -rupN src-orig/vidalia/network/CircuitListWidget.cpp src/vidalia/network/CircuitListWidget.cpp
    old new CircuitListWidget::CircuitListWidget(QWi 
    3434: QTreeWidget(parent)
    3535{
    3636  /* Create and initialize columns */
    37   setHeaderLabels(QStringList() << tr("Connection") << tr("Status"));
     37  setHeaderLabels(QStringList() << tr("Connection") << tr("Status") << tr("Traffic"));
    3838
    3939  /* Find out when a circuit has been selected */
    4040  connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
    CircuitListWidget::CircuitListWidget(QWi 
    5252void
    5353CircuitListWidget::retranslateUi()
    5454{
    55   setHeaderLabels(QStringList() << tr("Connection") << tr("Status"));
     55  setHeaderLabels(QStringList() << tr("Connection") << tr("Status") << tr("Traffic"));
    5656  for (int i = 0; i < topLevelItemCount(); i++) {
    5757    CircuitItem *circuitItem = dynamic_cast<CircuitItem *>(topLevelItem(i));
    5858    circuitItem->update(circuitItem->circuit());
    5959
    6060    foreach (StreamItem *streamItem, circuitItem->streams()) {
    6161      streamItem->update(streamItem->stream());
     62      streamItem->setTraffic(streamItem->bytesReceived(), streamItem->bytesSent());
    6263    }
    6364  }
    6465}
    CircuitListWidget::addStream(const Strea 
    193194  }
    194195}
    195196
     197/** Updates the traffic totals for a stream with the given delta values. */
     198void
     199CircuitListWidget::onStreamBandwidthUpdate(const StreamId &streamId,
     200                                           quint64 bytesReceived,
     201                                           quint64 bytesSent)
     202{
     203  StreamItem *item = findStreamItem(streamId);
     204  if (item) {
     205    item->setTraffic(
     206      item->bytesReceived() + bytesReceived,
     207      item->bytesSent() + bytesSent);
     208  }
     209}
     210
    196211/** Schedules the given circuit to be removed after the specified timeout. */
    197212void
    198213CircuitListWidget::scheduleCircuitRemoval(CircuitItem *circuit, int delay)
  • vidalia/network/CircuitListWidget.h

    diff -rupN src-orig/vidalia/network/CircuitListWidget.h src/vidalia/network/CircuitListWidget.h
    old new public: 
    3434  /** Circuit list columns. */
    3535  enum Columns {
    3636    ConnectionColumn = 0, /**< Column for either the circuit or stream */
    37     StatusColumn = 1      /**< Status of the connection. */
     37    StatusColumn = 1,     /**< Status of the connection. */
     38    TrafficColumn = 2,    /**< Amount of data received and sent by the stream. */
    3839  };
    3940
    4041  /** Default constructor */
    signals: 
    6667public slots:
    6768  /** Clears all circuits and streams from the list. */
    6869  void clearCircuits();
     70  /** Updates the traffic totals for a stream with the given delta values. */
     71  void onStreamBandwidthUpdate(const StreamId &streamId,
     72                               quint64 bytesReceived, quint64 bytesSent);
    6973
    7074private slots:
    7175  /** Removes the first circuit scheduled to be removed.*/
  • vidalia/network/NetViewer.cpp

    diff -rupN src-orig/vidalia/network/NetViewer.cpp src/vidalia/network/NetViewer.cpp
    old new NetViewer::NetViewer(QWidget *parent) 
    6969  connect(_torControl, SIGNAL(streamStatusChanged(Stream)),
    7070          this, SLOT(addStream(Stream)));
    7171
     72  _torControl->setEvent(TorEvents::StreamBandwidth);
     73  connect(_torControl, SIGNAL(streamBandwidthUpdate(StreamId, quint64, quint64)),
     74          ui.treeCircuitList, SLOT(onStreamBandwidthUpdate(StreamId, quint64, quint64)));
     75
    7276  _torControl->setEvent(TorEvents::AddressMap);
    7377  connect(_torControl, SIGNAL(addressMapped(QString, QString, QDateTime)),
    7478          this, SLOT(addressMapped(QString, QString, QDateTime)));
    NetViewer::NetViewer(QWidget *parent) 
    8387  ui.treeRouterList->header()->
    8488    resizeSection(RouterListWidget::CountryColumn, 25);
    8589  ui.treeCircuitList->header()->
    86     resizeSection(CircuitListWidget::ConnectionColumn, 235);
     90    resizeSection(CircuitListWidget::ConnectionColumn, 240);
     91  ui.treeCircuitList->header()->
     92    resizeSection(CircuitListWidget::StatusColumn, 80);
    8793
    8894  /* Create the TorMapWidget and add it to the dialog */
    8995#if defined(USE_MARBLE)
  • vidalia/network/StreamItem.cpp

    diff -rupN src-orig/vidalia/network/StreamItem.cpp src/vidalia/network/StreamItem.cpp
    old new  
    1919
    2020/** Constructor */
    2121StreamItem::StreamItem(const Stream &stream)
     22  : _bytesReceived(0), _bytesSent(0)
    2223{
    2324  /* Set the stream's displayed target information */
    2425  setText(CircuitListWidget::ConnectionColumn, stream.target());
    StreamItem::update(const Stream &stream) 
    3738  setToolTip(CircuitListWidget::StatusColumn, stream.statusString());
    3839}
    3940
     41/** Formats a count of bytes appropriately as bytes, kilobytes,
     42 * megabytes, or gigabytes. */
     43static
     44QString formatBytes(quint64 bytes) {
     45  const int K = 1024, M = K * K, G = K * M;
     46  double d = bytes;
     47  /*if (bytes < 1 * K) return QString("%1 B").arg(bytes);*/
     48  if (bytes < 10 * K) return QString("%1 KB").arg(d / K, 0, 'f', 1);
     49  if (bytes < 1 * M) return QString("%1 KB").arg(d / K, 0, 'f', 0);
     50  if (bytes < 100 * M) return QString("%1 MB").arg(d / M, 0, 'f', 1);
     51  if (bytes < 1 * G) return QString("%1 MB").arg(d / M, 0, 'f', 0);
     52  return QString("%1 GB").arg(d / G, 0, 'f', 1);
     53}
     54
     55/** Updates the traffic totals of this stream item. */
     56void
     57StreamItem::setTraffic(quint64 bytesReceived, quint64 bytesSent)
     58{
     59  _bytesReceived = bytesReceived;
     60  _bytesSent = bytesSent;
     61 
     62  QString str;
     63  if (bytesReceived | bytesSent) {
     64    str = QString("%1 down / %2 up").arg(
     65      formatBytes(bytesReceived),
     66      formatBytes(bytesSent));
     67  } else {
     68    str = QString();
     69  }
     70 
     71  setText(CircuitListWidget::TrafficColumn, str);
     72  setToolTip(CircuitListWidget::TrafficColumn, str);
     73}
  • vidalia/network/StreamItem.h

    diff -rupN src-orig/vidalia/network/StreamItem.h src/vidalia/network/StreamItem.h
    old new public: 
    3232  void update(const Stream &stream);
    3333  /** Returns the ID of the stream associated with this tree item. */
    3434  StreamId id() const { return _stream.id(); }
     35 
     36  /** Updates the traffic totals of this stream item. */
     37  void setTraffic(quint64 bytesReceived, quint64 bytesSent);
     38  quint64 bytesReceived() const { return _bytesReceived; }
     39  quint64 bytesSent() const { return _bytesSent; }
    3540
    3641private:
    3742  Stream _stream;
     43  quint64 _bytesReceived;
     44  quint64 _bytesSent;
    3845};
    3946
    4047#endif