From f107b0fbd9e2bb301f9725f5bd84b0f6996be9ee Mon Sep 17 00:00:00 2001
From: Yuri <yuri@tsoft.com>
Date: Mon, 7 Jul 2014 13:30:23 -0700
Subject: [PATCH] Addition of bytes received/sent counts on the network
 information tab.

---
 src/bitmessageqt/__init__.py     | 10 ++++++++++
 src/bitmessageqt/bitmessageui.py | 18 +++++++++++++-----
 src/bitmessageqt/bitmessageui.ui | 32 +++++++++++++++++++++++++++++---
 src/class_receiveDataThread.py   |  4 +++-
 src/class_sendDataThread.py      | 14 +++++++++-----
 src/shared.py                    |  2 ++
 6 files changed, 66 insertions(+), 14 deletions(-)

diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py
index 9543503f..35a4b563 100644
--- a/src/bitmessageqt/__init__.py
+++ b/src/bitmessageqt/__init__.py
@@ -1459,14 +1459,23 @@ class MyForm(QtGui.QMainWindow):
     def updateNumberOfMessagesProcessed(self):
         self.ui.labelMessageCount.setText(_translate(
             "MainWindow", "Processed %1 person-to-person messages.").arg(str(shared.numberOfMessagesProcessed)))
+        self.updateNumberOfBytes()
 
     def updateNumberOfBroadcastsProcessed(self):
         self.ui.labelBroadcastCount.setText(_translate(
             "MainWindow", "Processed %1 broadcast messages.").arg(str(shared.numberOfBroadcastsProcessed)))
+        self.updateNumberOfBytes()
 
     def updateNumberOfPubkeysProcessed(self):
         self.ui.labelPubkeyCount.setText(_translate(
             "MainWindow", "Processed %1 public keys.").arg(str(shared.numberOfPubkeysProcessed)))
+        self.updateNumberOfBytes()
+
+    def updateNumberOfBytes(self):
+        self.ui.labelBytesRecvCount.setText(_translate(
+            "MainWindow", "Received %1 bytes.").arg(str(locale.format("%d", shared.numberOfBytesReceived, grouping=True))))
+        self.ui.labelBytesSentCount.setText(_translate(
+            "MainWindow", "Sent %1 bytes.").arg(str(locale.format("%d", shared.numberOfBytesSent, grouping=True))))
 
     def updateNetworkStatusTab(self):
         # print 'updating network status tab'
@@ -1521,6 +1530,7 @@ class MyForm(QtGui.QMainWindow):
         self.ui.labelLookupsPerSecond.setText(_translate(
             "MainWindow", "Inventory lookups per second: %1").arg(str(shared.numberOfInventoryLookupsPerformed/2)))
         shared.numberOfInventoryLookupsPerformed = 0
+        self.updateNumberOfBytes()
 
     # Indicates whether or not there is a connection to the Bitmessage network
     connected = False
diff --git a/src/bitmessageqt/bitmessageui.py b/src/bitmessageqt/bitmessageui.py
index b80367dd..279bc6c7 100644
--- a/src/bitmessageqt/bitmessageui.py
+++ b/src/bitmessageqt/bitmessageui.py
@@ -2,8 +2,8 @@
 
 # Form implementation generated from reading ui file 'bitmessageui.ui'
 #
-# Created: Sat Nov  2 18:01:09 2013
-#      by: PyQt4 UI code generator 4.10
+# Created: Mon Jul  7 12:26:18 2014
+#      by: PyQt4 UI code generator 4.10.3
 #
 # WARNING! All changes made in this file will be lost!
 
@@ -431,15 +431,21 @@ class Ui_MainWindow(object):
         self.labelBroadcastCount.setGeometry(QtCore.QRect(350, 150, 351, 16))
         self.labelBroadcastCount.setObjectName(_fromUtf8("labelBroadcastCount"))
         self.labelLookupsPerSecond = QtGui.QLabel(self.networkstatus)
-        self.labelLookupsPerSecond.setGeometry(QtCore.QRect(320, 210, 291, 16))
+        self.labelLookupsPerSecond.setGeometry(QtCore.QRect(320, 230, 291, 16))
         self.labelLookupsPerSecond.setObjectName(_fromUtf8("labelLookupsPerSecond"))
+        self.labelBytesRecvCount = QtGui.QLabel(self.networkstatus)
+        self.labelBytesRecvCount.setGeometry(QtCore.QRect(350, 190, 331, 16))
+        self.labelBytesRecvCount.setObjectName(_fromUtf8("labelBytesRecvCount"))
+        self.labelBytesSentCount = QtGui.QLabel(self.networkstatus)
+        self.labelBytesSentCount.setGeometry(QtCore.QRect(350, 210, 331, 16))
+        self.labelBytesSentCount.setObjectName(_fromUtf8("labelBytesSentCount"))
         icon9 = QtGui.QIcon()
         icon9.addPixmap(QtGui.QPixmap(_fromUtf8(":/newPrefix/images/networkstatus.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
         self.tabWidget.addTab(self.networkstatus, icon9, _fromUtf8(""))
         self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
         MainWindow.setCentralWidget(self.centralwidget)
         self.menubar = QtGui.QMenuBar(MainWindow)
-        self.menubar.setGeometry(QtCore.QRect(0, 0, 885, 27))
+        self.menubar.setGeometry(QtCore.QRect(0, 0, 885, 20))
         self.menubar.setObjectName(_fromUtf8("menubar"))
         self.menuFile = QtGui.QMenu(self.menubar)
         self.menuFile.setObjectName(_fromUtf8("menuFile"))
@@ -501,7 +507,7 @@ class Ui_MainWindow(object):
         self.menubar.addAction(self.menuHelp.menuAction())
 
         self.retranslateUi(MainWindow)
-        self.tabWidget.setCurrentIndex(0)
+        self.tabWidget.setCurrentIndex(7)
         QtCore.QObject.connect(self.radioButtonSpecific, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.lineEditTo.setEnabled)
         QtCore.QObject.connect(self.radioButtonSpecific, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.labelSendBroadcastWarning.hide)
         QtCore.QObject.connect(self.radioButtonBroadcast, QtCore.SIGNAL(_fromUtf8("clicked()")), self.labelSendBroadcastWarning.show)
@@ -625,6 +631,8 @@ class Ui_MainWindow(object):
         self.labelPubkeyCount.setText(_translate("MainWindow", "Processed 0 public key.", None))
         self.labelBroadcastCount.setText(_translate("MainWindow", "Processed 0 broadcast.", None))
         self.labelLookupsPerSecond.setText(_translate("MainWindow", "Inventory lookups per second: 0", None))
+        self.labelBytesRecvCount.setText(_translate("MainWindow", "Received 0 bytes.", None))
+        self.labelBytesSentCount.setText(_translate("MainWindow", "Sent 0 bytes.", None))
         self.tabWidget.setTabText(self.tabWidget.indexOf(self.networkstatus), _translate("MainWindow", "Network Status", None))
         self.menuFile.setTitle(_translate("MainWindow", "File", None))
         self.menuSettings.setTitle(_translate("MainWindow", "Settings", None))
diff --git a/src/bitmessageqt/bitmessageui.ui b/src/bitmessageqt/bitmessageui.ui
index e5148ec1..361e8fbe 100644
--- a/src/bitmessageqt/bitmessageui.ui
+++ b/src/bitmessageqt/bitmessageui.ui
@@ -57,7 +57,7 @@
        <enum>QTabWidget::Rounded</enum>
       </property>
       <property name="currentIndex">
-       <number>0</number>
+       <number>7</number>
       </property>
       <widget class="QWidget" name="inbox">
        <attribute name="icon">
@@ -1035,7 +1035,7 @@ p, li { white-space: pre-wrap; }
         <property name="geometry">
          <rect>
           <x>320</x>
-          <y>210</y>
+          <y>230</y>
           <width>291</width>
           <height>16</height>
          </rect>
@@ -1044,6 +1044,32 @@ p, li { white-space: pre-wrap; }
          <string>Inventory lookups per second: 0</string>
         </property>
        </widget>
+       <widget class="QLabel" name="labelBytesRecvCount">
+        <property name="geometry">
+         <rect>
+          <x>350</x>
+          <y>190</y>
+          <width>331</width>
+          <height>16</height>
+         </rect>
+        </property>
+        <property name="text">
+         <string>Received 0 bytes.</string>
+        </property>
+       </widget>
+       <widget class="QLabel" name="labelBytesSentCount">
+        <property name="geometry">
+         <rect>
+          <x>350</x>
+          <y>210</y>
+          <width>331</width>
+          <height>16</height>
+         </rect>
+        </property>
+        <property name="text">
+         <string>Sent 0 bytes.</string>
+        </property>
+       </widget>
       </widget>
      </widget>
     </item>
@@ -1055,7 +1081,7 @@ p, li { white-space: pre-wrap; }
      <x>0</x>
      <y>0</y>
      <width>885</width>
-     <height>27</height>
+     <height>20</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py
index e78a7ae2..761b9e50 100644
--- a/src/class_receiveDataThread.py
+++ b/src/class_receiveDataThread.py
@@ -68,7 +68,9 @@ class receiveDataThread(threading.Thread):
         while True:
             dataLen = len(self.data)
             try:
-                self.data += self.sock.recv(4096)
+                dataRecv = self.sock.recv(4096)
+                self.data += dataRecv
+                shared.numberOfBytesReceived += len(dataRecv)
             except socket.timeout:
                 with shared.printLock:
                     print 'Timeout occurred waiting for data from', self.peer, '. Closing receiveData thread. (ID:', str(id(self)) + ')'
diff --git a/src/class_sendDataThread.py b/src/class_sendDataThread.py
index 5aabaeb5..e88d659c 100644
--- a/src/class_sendDataThread.py
+++ b/src/class_sendDataThread.py
@@ -55,7 +55,7 @@ class sendDataThread(threading.Thread):
             print 'Sending version packet: ', repr(datatosend)
 
         try:
-            self.sock.sendall(datatosend)
+            self.sendBytes(datatosend)
         except Exception as err:
             # if not 'Bad file descriptor' in err:
             with shared.printLock:
@@ -63,6 +63,10 @@ class sendDataThread(threading.Thread):
             
         self.versionSent = 1
 
+    def sendBytes(self, data):
+        self.sock.sendall(data)
+        shared.numberOfBytesSent += len(data)
+
     def run(self):
         while True:
             deststream, command, data = self.sendDataThreadQueue.get()
@@ -114,7 +118,7 @@ class sendDataThread(threading.Thread):
                     datatosend = datatosend + payload
 
                     try:
-                        self.sock.sendall(datatosend)
+                        self.sendBytes(datatosend)
                         self.lastTimeISentData = int(time.time())
                     except:
                         print 'sendaddr: self.sock.sendall failed'
@@ -136,7 +140,7 @@ class sendDataThread(threading.Thread):
                         headerData += pack('>L', len(payload))
                         headerData += hashlib.sha512(payload).digest()[:4]
                         try:
-                            self.sock.sendall(headerData + payload)
+                            self.sendBytes(headerData + payload)
                             self.lastTimeISentData = int(time.time())
                         except:
                             print 'sendinv: self.sock.sendall failed'
@@ -149,7 +153,7 @@ class sendDataThread(threading.Thread):
                             print 'Sending pong to', self.peer, 'to keep connection alive.'
 
                         try:
-                            self.sock.sendall(
+                            self.sendBytes(
                                 '\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35')
                             self.lastTimeISentData = int(time.time())
                         except:
@@ -157,7 +161,7 @@ class sendDataThread(threading.Thread):
                             break
                 elif command == 'sendRawData':
                     try:
-                        self.sock.sendall(data)
+                        self.sendBytes(data)
                         self.lastTimeISentData = int(time.time())
                     except:
                         print 'Sending of data to', self.peer, 'failed. sendDataThread thread', self, 'ending now.' 
diff --git a/src/shared.py b/src/shared.py
index 96b9a7ce..8618c107 100644
--- a/src/shared.py
+++ b/src/shared.py
@@ -70,6 +70,8 @@ numberOfMessagesProcessed = 0
 numberOfBroadcastsProcessed = 0
 numberOfPubkeysProcessed = 0
 numberOfInventoryLookupsPerformed = 0
+numberOfBytesReceived = 0
+numberOfBytesSent = 0
 daemon = False
 inventorySets = {} # key = streamNumer, value = a set which holds the inventory object hashes that we are aware of. This is used whenever we receive an inv message from a peer to check to see what items are new to us. We don't delete things out of it; instead, the singleCleaner thread clears and refills it every couple hours.
 needToWriteKnownNodesToDisk = False # If True, the singleCleaner will write it to disk eventually.