Further reduce header dependencies
[novacoin.git] / src / qt / qtipcserver.cpp
1 // Copyright (c) 2009-2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <boost/algorithm/string.hpp>
6 #include <boost/interprocess/ipc/message_queue.hpp>
7 #include <boost/tokenizer.hpp>
8 #include <boost/date_time/posix_time/posix_time.hpp>
9
10 #include "ui_interface.h"
11 #include "util.h"
12 #include "qtipcserver.h"
13
14 using namespace boost::interprocess;
15 using namespace boost::posix_time;
16 using namespace boost;
17 using namespace std;
18
19 void ipcShutdown()
20 {
21     message_queue::remove(BITCOINURI_QUEUE_NAME);
22 }
23
24 void ipcThread(void* parg)
25 {
26     message_queue* mq = (message_queue*)parg;
27     char strBuf[257];
28     size_t nSize;
29     unsigned int nPriority;
30     loop
31     {
32         ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(100);
33         if(mq->timed_receive(&strBuf, sizeof(strBuf), nSize, nPriority, d))
34         {
35             ThreadSafeHandleURI(std::string(strBuf, nSize));
36             Sleep(1000);
37         }
38         if (fShutdown)
39         {
40             ipcShutdown();
41             break;
42         }
43     }
44     ipcShutdown();
45 }
46
47 void ipcInit()
48 {
49 #ifdef MAC_OSX
50     // TODO: implement bitcoin: URI handling the Mac Way
51     return;
52 #endif
53 #ifdef WIN32
54     // TODO: THOROUGHLY test boost::interprocess fix,
55     // and make sure there are no Windows argument-handling exploitable
56     // problems.
57     return;
58 #endif
59
60     message_queue* mq;
61     char strBuf[257];
62     size_t nSize;
63     unsigned int nPriority;
64     try {
65         mq = new message_queue(open_or_create, BITCOINURI_QUEUE_NAME, 2, 256);
66
67         // Make sure we don't lose any bitcoin: URIs
68         for (int i = 0; i < 2; i++)
69         {
70             ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(1);
71             if(mq->timed_receive(&strBuf, sizeof(strBuf), nSize, nPriority, d))
72             {
73                 ThreadSafeHandleURI(std::string(strBuf, nSize));
74             }
75             else
76                 break;
77         }
78
79         // Make sure only one bitcoin instance is listening
80         message_queue::remove(BITCOINURI_QUEUE_NAME);
81         mq = new message_queue(open_or_create, BITCOINURI_QUEUE_NAME, 2, 256);
82     }
83     catch (interprocess_exception &ex) {
84         return;
85     }
86     if (!CreateThread(ipcThread, mq))
87     {
88         delete mq;
89     }
90 }