summaryrefslogtreecommitdiffstats
path: root/lib/CommLayer/DBusClientProxy.h
blob: 52ba75d5a4b8cb5e0b46e1478e3859efdb92895c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* 
    Copyright (C) 2009  Jiri Moskovcak (jmoskovc@redhat.com) 
    Copyright (C) 2009  RedHat inc. 
 
    This program is free software; you can redistribute it and/or modify 
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 2 of the License, or 
    (at your option) any later version. 
 
    This program is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    GNU General Public License for more details. 
 
    You should have received a copy of the GNU General Public License along 
    with this program; if not, write to the Free Software Foundation, Inc., 
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
    */
    
#include <dbus-c++/dbus.h>
#include <dbus-c++/glib-integration.h>
#include "DBusCommon.h"
#include <iostream>
#define ABRT_NOT_RUNNING 0
#define ABRT_RUNNING 1

namespace org {
namespace freedesktop {
namespace DBus {

class DaemonWatcher_proxy
 : public ::DBus::InterfaceProxy
{
private:
    void *m_pStateChangeHandler_cb_data;
    void (*m_pStateChangeHandler)(bool running, void* data);
public:

    DaemonWatcher_proxy()
    : ::DBus::InterfaceProxy("org.freedesktop.DBus")
    {
        m_pStateChangeHandler_cb_data = NULL;
        m_pStateChangeHandler = NULL;
        connect_signal(DaemonWatcher_proxy, NameOwnerChanged , _DaemonStateChanged);
    }

    void ConnectStateChangeHandler(void (*pStateChangeHandler)(bool running, void* data), void *cb_data)
    {
        m_pStateChangeHandler_cb_data = cb_data;
        m_pStateChangeHandler = pStateChangeHandler;
    }
private:

    /* unmarshalers (to unpack the DBus message before calling the actual signal handler)
     */
    void _DaemonStateChanged(const ::DBus::SignalMessage &sig)
    {
        ::DBus::MessageIter ri = sig.reader();
        std::string name;
        std::string old_owner;
        std::string new_owner;
        ri >> name;
        ri >> old_owner;
        ri >> new_owner;
        if(name.compare("com.redhat.abrt") == 0){ 
            if(new_owner.length() > 0)
            {
                if(m_pStateChangeHandler)
                {
                    m_pStateChangeHandler(true,m_pStateChangeHandler_cb_data);
                }
                else
                {
                    std::cout << "Daemon appeared!" << std::endl;
                }
            }
            if(new_owner.length() == 0)
            {
                if(m_pStateChangeHandler)
                {
                    m_pStateChangeHandler(false, m_pStateChangeHandler_cb_data);
                }
                else
                {
                    std::cout << "Daemon dissapeared!" << std::endl;
                }
            }
        }
    }
};

} } }

class DaemonWatcher
: public org::freedesktop::DBus::DaemonWatcher_proxy,
  public DBus::IntrospectableProxy,
  public DBus::ObjectProxy
{
public:

    DaemonWatcher(DBus::Connection &connection, const char *path, const char *name)
    : ::DBus::ObjectProxy(connection, path, name)
    {
    }
    ~DaemonWatcher()
    {
        std::cout << "~DaemonWatcher" << std::endl;
    }
};
        

class CDBusClient_proxy
 : public DBus::InterfaceProxy
{
private:
    bool m_bJobDone;
    uint64_t m_iPendingJobID;
    GMainLoop *gloop;
    std::string m_sConnName;
public:

    
    CDBusClient_proxy()
    : DBus::InterfaceProxy(CC_DBUS_IFACE)
    {
        connect_signal(CDBusClient_proxy, Crash, _Crash_stub);
        connect_signal(CDBusClient_proxy, JobDone, _JobDone_stub);
        m_sConnName = "";
    }
    
    CDBusClient_proxy(::DBus::Connection &pConnection)
    : DBus::InterfaceProxy(CC_DBUS_IFACE)
    {
        gloop = g_main_loop_new(NULL, false);
        //# define connect_signal(interface, signal, callback)
        connect_signal(CDBusClient_proxy, Crash, _Crash_stub);
        connect_signal(CDBusClient_proxy, JobDone, _JobDone_stub);
        m_sConnName = pConnection.unique_name();
    }

public:

    /* methods exported by this interface,
     * this functions will invoke the corresponding methods on the remote objects
     */
     /*
     
     < 
      <m_sUUID;m_sUID;m_sCount;m_sExecutable;m_sPackage>
      <m_sUUID;m_sUID;m_sCount;m_sExecutable;m_sPackage>
      <m_sUUID;m_sUID;m_sCount;m_sExecutable;m_sPackage>
      ...
      >
      */
    vector_crash_infos_t GetCrashInfos()
    {
        DBus::CallMessage call;
        
        DBus::MessageIter wi = call.writer();

        call.member("GetCrashInfos");
        DBus::Message ret = invoke_method(call);
        DBus::MessageIter ri = ret.reader();

        vector_crash_infos_t argout;
        ri >> argout;
        return argout;
    }
    
    bool DeleteDebugDump(const std::string& pUUID)
    {
        DBus::CallMessage call;
        
        DBus::MessageIter wi = call.writer();

        wi << pUUID;
        call.member("DeleteDebugDump");
        DBus::Message ret = invoke_method(call);
        DBus::MessageIter ri = ret.reader();

        bool argout;
        ri >> argout;
        return argout;
    }
    
    map_crash_report_t CreateReport(const std::string& pUUID)
    {
        m_bJobDone = false;
        DBus::CallMessage call;
        
        DBus::MessageIter wi = call.writer();

        wi << pUUID;
        call.member("CreateReport");
        DBus::Message ret = invoke_method(call);
        DBus::MessageIter ri = ret.reader();
        ri >> m_iPendingJobID;
        //FIXME: what if the report is created before we start the loop? (we miss the signal and get stuck in the loop)
        g_main_loop_run(gloop);
        return GetJobResult(m_iPendingJobID);
    };
    
    void Report(map_crash_report_t pReport)
    {
        DBus::CallMessage call;
        
        DBus::MessageIter wi = call.writer();

        wi << pReport;
        call.member("Report");
        DBus::Message ret = invoke_method(call);
        DBus::MessageIter ri = ret.reader();
    }
    
    map_crash_report_t GetJobResult(uint64_t pJobID)
    {
        DBus::CallMessage call;
        
        DBus::MessageIter wi = call.writer();

        wi << pJobID;
        call.member("GetJobResult");
        DBus::Message ret = invoke_method(call);
        DBus::MessageIter ri = ret.reader();
        map_crash_report_t argout;
        ri >> argout;
        return argout;
    }
    
public:

    /* signal handlers for this interface
     */
    virtual void Crash(std::string& value){}
    
private:

    /* unmarshalers (to unpack the DBus message before calling the actual signal handler)
     */
    void _Crash_stub(const ::DBus::SignalMessage &sig)
    {
        DBus::MessageIter ri = sig.reader();

        std::string value; ri >> value;
        Crash(value);
    }
    
    void _JobDone_stub(const ::DBus::SignalMessage &sig)
    {
        DBus::MessageIter ri = sig.reader();
        std::string dest;
        ri >> dest;
        if(m_sConnName == dest)
        {
            ri >> m_iPendingJobID;
            g_main_loop_quit(gloop);
        }
    }
};