2013-06-20 23:23:03 +02:00
import threading
import shared
import time
2013-06-23 21:52:39 +02:00
import sys
2014-01-20 19:45:21 +01:00
import os
2013-09-10 01:26:32 +02:00
import pickle
import tr #anslate
2013-08-27 14:46:57 +02:00
from helper_sql import *
2015-11-24 01:55:17 +01:00
from helper_threading import *
2013-09-10 01:26:32 +02:00
from debug import logger
2013-06-20 23:23:03 +02:00
2014-08-27 09:14:32 +02:00
"""
The singleCleaner class is a timer - driven thread that cleans data structures
to free memory , resends messages when a remote node doesn ' t respond, and
sends pong messages to keep connections alive if the network isn ' t busy.
2013-06-20 23:23:03 +02:00
It cleans these data structures in memory :
2013-10-02 02:14:53 +02:00
inventory ( moves data to the on - disk sql database )
inventorySets ( clears then reloads data out of sql database )
2013-06-20 23:23:03 +02:00
It cleans these tables on the disk :
2014-08-27 09:14:32 +02:00
inventory ( clears expired objects )
2013-10-02 02:14:53 +02:00
pubkeys ( clears pubkeys older than 4 weeks old which we have not used personally )
2013-06-20 23:23:03 +02:00
It resends messages when there has been no response :
2013-11-04 08:05:07 +01:00
resends getpubkey messages in 5 days ( then 10 days , then 20 days , etc . . . )
resends msg messages in 5 days ( then 10 days , then 20 days , etc . . . )
2013-06-20 23:23:03 +02:00
2014-08-27 09:14:32 +02:00
"""
2013-06-20 23:23:03 +02:00
2015-11-24 01:55:17 +01:00
class singleCleaner ( threading . Thread , StoppableThread ) :
2013-06-20 23:23:03 +02:00
def __init__ ( self ) :
2015-11-18 16:22:17 +01:00
threading . Thread . __init__ ( self , name = " singleCleaner " )
2015-11-24 01:55:17 +01:00
self . initStop ( )
2013-06-20 23:23:03 +02:00
def run ( self ) :
timeWeLastClearedInventoryAndPubkeysTables = 0
2013-11-04 08:05:07 +01:00
try :
shared . maximumLengthOfTimeToBotherResendingMessages = ( float ( shared . config . get ( ' bitmessagesettings ' , ' stopresendingafterxdays ' ) ) * 24 * 60 * 60 ) + ( float ( shared . config . get ( ' bitmessagesettings ' , ' stopresendingafterxmonths ' ) ) * ( 60 * 60 * 24 * 365 ) / 12 )
except :
2013-11-06 05:22:51 +01:00
# Either the user hasn't set stopresendingafterxdays and stopresendingafterxmonths yet or the options are missing from the config file.
2013-11-04 08:05:07 +01:00
shared . maximumLengthOfTimeToBotherResendingMessages = float ( ' inf ' )
2013-06-20 23:23:03 +02:00
2015-11-24 01:55:17 +01:00
while shared . shutdown == 0 :
2013-06-20 23:23:03 +02:00
shared . UISignalQueue . put ( (
' updateStatusBar ' , ' Doing housekeeping (Flushing inventory in memory to disk...) ' ) )
2016-03-18 02:01:59 +01:00
shared . inventory . flush ( )
2013-06-20 23:23:03 +02:00
shared . UISignalQueue . put ( ( ' updateStatusBar ' , ' ' ) )
2014-08-27 09:14:32 +02:00
2013-06-20 23:23:03 +02:00
shared . broadcastToSendDataQueues ( (
2013-10-02 02:14:53 +02:00
0 , ' pong ' , ' no data ' ) ) # commands the sendData threads to send out a pong message if they haven't sent anything else in the last five minutes. The socket timeout-time is 10 minutes.
2013-06-20 23:23:03 +02:00
# If we are running as a daemon then we are going to fill up the UI
# queue which will never be handled by a UI. We should clear it to
# save memory.
if shared . safeConfigGetBoolean ( ' bitmessagesettings ' , ' daemon ' ) :
shared . UISignalQueue . queue . clear ( )
if timeWeLastClearedInventoryAndPubkeysTables < int ( time . time ( ) ) - 7380 :
timeWeLastClearedInventoryAndPubkeysTables = int ( time . time ( ) )
2016-03-18 02:01:59 +01:00
shared . inventory . clean ( )
2013-06-20 23:23:03 +02:00
# pubkeys
2013-08-27 14:46:57 +02:00
sqlExecute (
''' DELETE FROM pubkeys WHERE time<? AND usedpersonally= ' no ' ''' ,
int ( time . time ( ) ) - shared . lengthOfTimeToHoldOnToAllPubkeys )
2013-06-20 23:23:03 +02:00
2015-03-09 07:35:32 +01:00
# Let us resend getpubkey objects if we have not yet heard a pubkey, and also msg objects if we have not yet heard an acknowledgement
2013-08-27 14:46:57 +02:00
queryreturn = sqlQuery (
2015-03-09 07:35:32 +01:00
''' select toaddress, ackdata, status FROM sent WHERE ((status= ' awaitingpubkey ' OR status= ' msgsent ' ) AND folder= ' sent ' AND sleeptill<? AND senttime>?) ''' ,
int ( time . time ( ) ) ,
int ( time . time ( ) ) - shared . maximumLengthOfTimeToBotherResendingMessages )
2013-06-20 23:23:03 +02:00
for row in queryreturn :
2015-03-09 07:35:32 +01:00
if len ( row ) < 2 :
2015-11-18 16:22:17 +01:00
logger . error ( ' Something went wrong in the singleCleaner thread: a query did not return the requested fields. ' + repr ( row ) )
2015-11-24 01:55:17 +01:00
self . stop . wait ( 3 )
2013-06-20 23:23:03 +02:00
break
2015-03-09 07:35:32 +01:00
toAddress , ackData , status = row
2013-06-20 23:23:03 +02:00
if status == ' awaitingpubkey ' :
2015-03-09 07:35:32 +01:00
resendPubkeyRequest ( toAddress )
elif status == ' msgsent ' :
resendMsg ( ackData )
2013-11-06 05:22:51 +01:00
2013-09-10 01:26:32 +02:00
# Let us write out the knowNodes to disk if there is anything new to write out.
if shared . needToWriteKnownNodesToDisk :
shared . knownNodesLock . acquire ( )
output = open ( shared . appdata + ' knownnodes.dat ' , ' wb ' )
try :
pickle . dump ( shared . knownNodes , output )
output . close ( )
except Exception as err :
if " Errno 28 " in str ( err ) :
logger . fatal ( ' (while receiveDataThread shared.needToWriteKnownNodesToDisk) Alert: Your disk or data storage volume is full. ' )
2016-04-30 11:07:27 +02:00
shared . UISignalQueue . put ( ( ' alert ' , ( tr . _translate ( " MainWindow " , " Disk full " ) , tr . _translate ( " MainWindow " , ' Alert: Your disk or data storage volume is full. Bitmessage will now exit. ' ) , True ) ) )
2013-09-10 01:26:32 +02:00
if shared . daemon :
os . _exit ( 0 )
shared . knownNodesLock . release ( )
shared . needToWriteKnownNodesToDisk = False
2015-11-24 01:55:17 +01:00
self . stop . wait ( 300 )
2013-11-04 08:05:07 +01:00
2015-03-09 07:35:32 +01:00
def resendPubkeyRequest ( address ) :
logger . debug ( ' It has been a long time and we haven \' t heard a response to our getpubkey request. Sending again. ' )
2013-10-03 16:29:50 +02:00
try :
del shared . neededPubkeys [
2015-03-09 07:35:32 +01:00
address ] # We need to take this entry out of the shared.neededPubkeys structure because the shared.workerQueue checks to see whether the entry is already present and will not do the POW and send the message because it assumes that it has already done it recently.
2013-10-03 16:29:50 +02:00
except :
pass
2013-11-06 05:22:51 +01:00
2013-10-03 16:29:50 +02:00
shared . UISignalQueue . put ( (
' updateStatusBar ' , ' Doing work necessary to again attempt to request a public key... ' ) )
sqlExecute (
2015-03-09 07:35:32 +01:00
''' UPDATE sent SET status= ' msgqueued ' WHERE toaddress=? ''' ,
address )
2013-10-03 16:29:50 +02:00
shared . workerQueue . put ( ( ' sendmessage ' , ' ' ) )
2015-03-09 07:35:32 +01:00
def resendMsg ( ackdata ) :
logger . debug ( ' It has been a long time and we haven \' t heard an acknowledgement to our msg. Sending again. ' )
2013-10-03 16:29:50 +02:00
sqlExecute (
2015-03-09 07:35:32 +01:00
''' UPDATE sent SET status= ' msgqueued ' WHERE ackdata=? ''' ,
ackdata )
2013-10-03 16:29:50 +02:00
shared . workerQueue . put ( ( ' sendmessage ' , ' ' ) )
shared . UISignalQueue . put ( (
' updateStatusBar ' , ' Doing work necessary to again attempt to deliver a message... ' ) )