From 138877f5f7d424abc837e594ab6b22bfffda4665 Mon Sep 17 00:00:00 2001 From: Jordan Hall Date: Fri, 21 Jun 2013 00:25:01 +0100 Subject: [PATCH] Placed repeated inbox and sent SQL operations into appropriate helper functions --- src/bitmessagemain.py | 68 +++++++++++-------------------------------- src/helper_inbox.py | 22 ++++++++++++++ src/helper_sent.py | 11 +++++++ 3 files changed, 50 insertions(+), 51 deletions(-) create mode 100644 src/helper_inbox.py create mode 100644 src/helper_sent.py diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index d2d92a61..8e336fdc 100644 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -56,6 +56,8 @@ from class_singleCleaner import * # Helper Functions import helper_startup import helper_bootstrap +import helper_inbox +import helper_sent # For each stream to which we connect, several outgoingSynSender threads # will exist and will collectively create 8 connections with peers. @@ -742,15 +744,11 @@ class receiveDataThread(threading.Thread): toAddress = '[Broadcast subscribers]' if messageEncodingType != 0: - shared.sqlLock.acquire() + t = (self.inventoryHash, toAddress, fromAddress, subject, int( time.time()), body, 'inbox', messageEncodingType, 0) - shared.sqlSubmitQueue.put( - '''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() + helper_inbox.insert(t) + shared.UISignalQueue.put(('displayNewInboxMessage', ( self.inventoryHash, toAddress, fromAddress, subject, body))) @@ -902,15 +900,11 @@ class receiveDataThread(threading.Thread): toAddress = '[Broadcast subscribers]' if messageEncodingType != 0: - shared.sqlLock.acquire() + t = (self.inventoryHash, toAddress, fromAddress, subject, int( time.time()), body, 'inbox', messageEncodingType, 0) - shared.sqlSubmitQueue.put( - '''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() + helper_inbox.insert(t) + shared.UISignalQueue.put(('displayNewInboxMessage', ( self.inventoryHash, toAddress, fromAddress, subject, body))) @@ -1226,15 +1220,10 @@ class receiveDataThread(threading.Thread): body = 'Unknown encoding type.\n\n' + repr(message) subject = '' if messageEncodingType != 0: - shared.sqlLock.acquire() t = (self.inventoryHash, toAddress, fromAddress, subject, int( time.time()), body, 'inbox', messageEncodingType, 0) - shared.sqlSubmitQueue.put( - '''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() + helper_inbox.insert(t) + shared.UISignalQueue.put(('displayNewInboxMessage', ( self.inventoryHash, toAddress, fromAddress, subject, body))) @@ -1269,15 +1258,10 @@ class receiveDataThread(threading.Thread): 32) # We don't actually need the ackdata for acknowledgement since this is a broadcast message but we can use it to update the user interface when the POW is done generating. toAddress = '[Broadcast subscribers]' ripe = '' - shared.sqlLock.acquire() + t = ('', toAddress, ripe, fromAddress, subject, message, ackdata, int( time.time()), 'broadcastqueued', 1, 1, 'sent', 2) - shared.sqlSubmitQueue.put( - '''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() + helper_sent.insert(t) shared.UISignalQueue.put(('displayNewSentMessage', ( toAddress, '[Broadcast subscribers]', fromAddress, subject, message, ackdata))) @@ -4046,15 +4030,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if len(params) == 0: return 'API Error 0000: I need parameters!' msgid = params[0].decode('hex') - t = (msgid,) - shared.sqlLock.acquire() - shared.sqlSubmitQueue.put( - '''UPDATE inbox SET folder='trash' WHERE msgid=?''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() - shared.UISignalQueue.put(('removeInboxRowByMsgid',msgid)) + helper_inbox.trash(msgid) return 'Trashed inbox message (assuming message existed).' elif method == 'trashSentMessage': if len(params) == 0: @@ -4126,15 +4102,10 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0014: Your fromAddress is disabled. Cannot send.' ackdata = OpenSSL.rand(32) - shared.sqlLock.acquire() + t = ('', toAddress, toRipe, fromAddress, subject, message, ackdata, int( time.time()), 'msgqueued', 1, 1, 'sent', 2) - shared.sqlSubmitQueue.put( - '''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() + helper_sent.insert(t) toLabel = '' t = (toAddress,) @@ -4195,15 +4166,10 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): toAddress = '[Broadcast subscribers]' ripe = '' - shared.sqlLock.acquire() + t = ('', toAddress, ripe, fromAddress, subject, message, ackdata, int( time.time()), 'broadcastqueued', 1, 1, 'sent', 2) - shared.sqlSubmitQueue.put( - '''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - shared.sqlSubmitQueue.put(t) - shared.sqlReturnQueue.get() - shared.sqlSubmitQueue.put('commit') - shared.sqlLock.release() + helper_sent.insert(t) toLabel = '[Broadcast subscribers]' shared.UISignalQueue.put(('displayNewSentMessage', ( diff --git a/src/helper_inbox.py b/src/helper_inbox.py new file mode 100644 index 00000000..fc420825 --- /dev/null +++ b/src/helper_inbox.py @@ -0,0 +1,22 @@ +import shared + +def insert(t): + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put( + '''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + +def trash(msgid): + t = (msgid,) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put( + '''UPDATE inbox SET folder='trash' WHERE msgid=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.UISignalQueue.put(('removeInboxRowByMsgid',msgid)) + diff --git a/src/helper_sent.py b/src/helper_sent.py new file mode 100644 index 00000000..dcfe844e --- /dev/null +++ b/src/helper_sent.py @@ -0,0 +1,11 @@ +import shared + +def insert(t): + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put( + '''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() +