changeset 3384:2da698218c12 draft

Abstract all IPC communication to qtipcserver
author Luke Dashjr <luke-jr+git@utopios.org>
date Fri, 24 Aug 2012 16:48:41 +0000
parents d7511e8afaf9
children d7417ae707c6
files src/qt/bitcoin.cpp src/qt/qtipcserver.cpp src/qt/qtipcserver.h
diffstat 3 files changed, 46 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/src/qt/bitcoin.cpp
+++ b/src/qt/bitcoin.cpp
@@ -20,9 +20,6 @@
 #include <QSplashScreen>
 #include <QLibraryInfo>
 
-#include <boost/interprocess/ipc/message_queue.hpp>
-#include <boost/algorithm/string/predicate.hpp>
-
 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
 #define _BITCOIN_QT_PLUGINS_INCLUDED
 #define __INSURE__
@@ -116,35 +113,8 @@
 #ifndef BITCOIN_QT_TEST
 int main(int argc, char *argv[])
 {
-// TODO: implement URI support on the Mac.
-#if !defined(MAC_OSX)
     // Do this early as we don't want to bother initializing if we are just calling IPC
-    for (int i = 1; i < argc; i++)
-    {
-        if (boost::algorithm::istarts_with(argv[i], "bitcoin:"))
-        {
-            const char *strURI = argv[i];
-            try {
-                boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
-                if (mq.try_send(strURI, strlen(strURI), 0))
-                    // if URI could be sent to the message queue exit here
-                    exit(0);
-                else
-                    // if URI could not be sent to the message queue do a normal Bitcoin-Qt startup
-                    break;
-            }
-            catch (boost::interprocess::interprocess_exception &ex) {
-                // don't log the "file not found" exception, because that's normal for
-                // the first start of the first instance
-                if (ex.get_error_code() != boost::interprocess::not_found_error)
-                {
-                    printf("main() - boost interprocess exception #%d: %s\n", ex.get_error_code(), ex.what());
-                    break;
-                }
-            }
-        }
-    }
-#endif
+    ipcScanRelay(argc, argv);
 
     // Internal string conversion is all UTF-8
     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
@@ -269,29 +239,10 @@
                 {
                     window.show();
                 }
-// TODO: implement URI support on the Mac.
-#if !defined(MAC_OSX)
 
                 // Place this here as guiref has to be defined if we don't want to lose URIs
-                ipcInit();
+                ipcInit(argc, argv);
 
-                // Check for URI in argv
-                for (int i = 1; i < argc; i++)
-                {
-                    if (boost::algorithm::istarts_with(argv[i], "bitcoin:"))
-                    {
-                        const char *strURI = argv[i];
-                        try {
-                            boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
-                            mq.try_send(strURI, strlen(strURI), 0);
-                        }
-                        catch (boost::interprocess::interprocess_exception &ex) {
-                            printf("main() - boost interprocess exception #%d: %s\n", ex.get_error_code(), ex.what());
-                            break;
-                        }
-                    }
-                }
-#endif
                 app.exec();
 
                 window.hide();
--- a/src/qt/qtipcserver.cpp
+++ b/src/qt/qtipcserver.cpp
@@ -7,6 +7,7 @@
 #include "ui_interface.h"
 #include "util.h"
 
+#include <boost/algorithm/string/predicate.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/interprocess/ipc/message_queue.hpp>
 #include <boost/version.hpp>
@@ -24,10 +25,47 @@
 #ifdef MAC_OSX
 // URI handling not implemented on OSX yet
 
-void ipcInit() { }
+void ipcScanRelay(int argc, char *argv[]) { }
+void ipcInit(int argc, char *argv[]) { }
 
 #else
 
+static bool ipcScanCmd(int argc, char *argv[], bool fRelay)
+{
+    // Check for URI in argv
+    bool fSent = false;
+    for (int i = 1; i < argc; i++)
+    {
+        if (boost::algorithm::istarts_with(argv[i], "bitcoin:"))
+        {
+            const char *strURI = argv[i];
+            try {
+                boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
+                if (mq.try_send(strURI, strlen(strURI), 0))
+                    fSent = true;
+                else if (fRelay)
+                    break;
+            }
+            catch (boost::interprocess::interprocess_exception &ex) {
+                // don't log the "file not found" exception, because that's normal for
+                // the first start of the first instance
+                if (ex.get_error_code() != boost::interprocess::not_found_error || !fRelay)
+                {
+                    printf("main() - boost interprocess exception #%d: %s\n", ex.get_error_code(), ex.what());
+                    break;
+                }
+            }
+        }
+    }
+    return fSent;
+}
+
+void ipcScanRelay(int argc, char *argv[])
+{
+    if (ipcScanCmd(argc, argv, true))
+        exit(0);
+}
+
 static void ipcThread(void* pArg)
 {
     IMPLEMENT_RANDOMIZE_STACK(ipcThread(pArg));
@@ -75,7 +113,7 @@
     delete mq;
 }
 
-void ipcInit()
+void ipcInit(int argc, char *argv[])
 {
     message_queue* mq = NULL;
     char buffer[MAX_URI_LENGTH + 1] = "";
@@ -113,6 +151,8 @@
         delete mq;
         return;
     }
+
+    ipcScanCmd(argc, argv, false);
 }
 
 #endif
--- a/src/qt/qtipcserver.h
+++ b/src/qt/qtipcserver.h
@@ -4,6 +4,7 @@
 // Define Bitcoin-Qt message queue name
 #define BITCOINURI_QUEUE_NAME "BitcoinURI"
 
-void ipcInit();
+void ipcScanRelay(int argc, char *argv[]);
+void ipcInit(int argc, char *argv[]);
 
 #endif // QTIPCSERVER_H