summaryrefslogtreecommitdiff
path: root/src/dbus/server/session-common.h
blob: 83fe246c370a37b042385f8160f9277e057d5e87 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2011 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef SESSION_COMMON_H
#define SESSION_COMMON_H

#include "source-status.h"
#include "source-progress.h"
#include <syncevo/util.h>
#include <syncevo/DBusTraits.h>
#include <syncevo/FilterConfigNode.h>
#include <syncevo/SynthesisEngine.h>

#include <gdbus-cxx-bridge.h>

SE_BEGIN_CXX

/**
 * This namespace holds constants and defines for Sessions and its
 * consumers.
 */
namespace SessionCommon
{
    const char * const SERVICE_NAME = "org.syncevolution";
    const char * const CONNECTION_PATH = "/org/syncevolution/Connection";
    const char * const CONNECTION_IFACE = "org.syncevolution.Connection";
    const char * const SESSION_PATH = "/org/syncevolution/Session";
    const char * const SESSION_IFACE = "org.syncevolution.Session";
    const char * const SERVER_PATH = "/org/syncevolution/Server";
    const char * const SERVER_IFACE = "org.syncevolution.Server";

    const char * const HELPER_PATH = "/dbushelper";
    const char * const HELPER_IFACE = "org.syncevolution.Helper";
    const char * const HELPER_DESTINATION = "direct.peer"; // doesn't matter, routing is off

    /**
     * The operation running inside the session.
     */
    enum RunOperation {
        OP_SYNC,            /**< running a sync */
        OP_RESTORE,         /**< restoring data */
        OP_CMDLINE,         /**< executing command line */
        OP_NULL             /**< idle, accepting commands via D-Bus */
    };

    inline std::string runOpToString(RunOperation op) {
        static const char * const strings[] = {
            "sync",
            "restore",
            "cmdline"
        };
        return op >= OP_SYNC && op <= OP_CMDLINE ?
            strings[op] :
            "";
    }

    /**
     * Used by both Connection class (inside server) and
     * DBusTransportAgent (inside helper).
     */
    enum ConnectionState {
        SETUP,          /**< ready for first message */
        PROCESSING,     /**< received message, waiting for engine's reply */
        WAITING,        /**< waiting for next follow-up message */
        FINAL,          /**< engine has sent final reply, wait for ACK by peer */
        DONE,           /**< peer has closed normally after the final reply */
        FAILED          /**< in a failed state, no further operation possible */
    };

    /** maps to names for debugging */
    inline std::string ConnectionStateToString(ConnectionState state)
    {
        static const char * const strings[] = {
            "SETUP",
            "PROCESSING",
            "WAITING",
            "FINAL",
            "DONE",
            "FAILED"
        };
        return state >= SETUP && state <= FAILED ?
            strings[state] :
            "???";
    }

    typedef StringMap SourceModes_t;
    typedef std::map<std::string, SyncEvo::FilterConfigNode::ConfigFilter> SourceFilters_t;

    /**
     * all the information that syncevo-dbus-server needs to
     * send to syncevo-dbus-helper before the latter can
     * run a sync
     */
    struct SyncParams
    {
        SyncParams() :
           m_serverMode(false),
           m_serverAlerted(false),
           m_remoteInitiated(false)
        {}

        std::string m_config;
        std::string m_mode;
        SourceModes_t m_sourceModes;
        bool m_serverMode;
        bool m_serverAlerted;
        bool m_remoteInitiated;
        std::string m_sessionID;
        SharedBuffer m_initialMessage;
        std::string m_initialMessageType;
        SyncEvo::FilterConfigNode::ConfigFilter m_syncFilter;
        SyncEvo::FilterConfigNode::ConfigFilter m_sourceFilter;
        SourceFilters_t m_sourceFilters;
    };
}

SE_END_CXX

namespace GDBusCXX {
    using namespace SyncEvo::SessionCommon;
    using namespace SyncEvo;
    template<> struct dbus_traits<SyncParams> :
        public dbus_struct_traits<SyncParams,
        dbus_member<SyncParams, std::string, &SyncParams::m_config,
        dbus_member<SyncParams, std::string, &SyncParams::m_mode,
        dbus_member<SyncParams, SourceModes_t, &SyncParams::m_sourceModes,
        dbus_member<SyncParams, bool, &SyncParams::m_serverMode,
        dbus_member<SyncParams, bool, &SyncParams::m_serverAlerted,
        dbus_member<SyncParams, bool, &SyncParams::m_remoteInitiated,
        dbus_member<SyncParams, std::string, &SyncParams::m_sessionID,
        dbus_member<SyncParams, SharedBuffer, &SyncParams::m_initialMessage,
        dbus_member<SyncParams, std::string, &SyncParams::m_initialMessageType,
        dbus_member<SyncParams, FilterConfigNode::ConfigFilter, &SyncParams::m_syncFilter,
        dbus_member<SyncParams, FilterConfigNode::ConfigFilter, &SyncParams::m_sourceFilter,
        dbus_member_single<SyncParams, SourceFilters_t, &SyncParams::m_sourceFilters
        > > > > > > > > > > > > >
        {};

    /**
     * Similar to DBusArray<uint8_t>, but with different native
     * types. Uses encoding/decoding from the base class, copies
     * to/from SharedBuffer as needed.
     *
     * DBusArray<uint8_t> is more efficient because it avoids
     * copying the bytes from the D-Bus message when decoding,
     * but it is harder to use natively (cannot be copied).
     * SharedBuffer does ref counting for the memory chunk,
     * so once initialized, copying it is cheap.
     */
    template <> struct dbus_traits<SharedBuffer> :
        public dbus_traits< DBusArray<uint8_t> >
    {
        typedef dbus_traits< DBusArray<uint8_t> > base;

        typedef SharedBuffer host_type;
        typedef const SharedBuffer &arg_type;

#ifdef GDBUS_CXX_GIO
        static void get(GDBusCXX::ExtractArgs &context,
                        GDBusCXX::reader_type &iter, host_type &buffer)
        {
            base::host_type array;
            base::get(context, iter, array);
            buffer = SharedBuffer(reinterpret_cast<const char *>(array.second), array.first);
        }
#else
        static void get(GDBusCXX::connection_type *conn, GDBusCXX::message_type *msg,
                        GDBusCXX::reader_type &iter, host_type &buffer)
        {
            base::host_type array;
            base::get(conn, msg, iter, array);
            buffer = SharedBuffer(reinterpret_cast<const char *>(array.second), array.first);
        }
#endif

        static void append(GDBusCXX::builder_type &builder, arg_type buffer)
        {
            base::host_type array(buffer.size(), reinterpret_cast<const uint8_t *>(buffer.get()));
            base::append(builder, array);
        }
    };

    template <> struct dbus_traits<SyncReport> :
        public dbus_traits< std::string >
    {
        typedef dbus_traits< std::string > base;

        typedef SyncReport host_type;
        typedef const SyncReport &arg_type;

#ifdef GDBUS_CXX_GIO
        static void get(GDBusCXX::ExtractArgs &context,
                        GDBusCXX::reader_type &iter, host_type &report)
        {
            std::string dump;
            base::get(context, iter, dump);
            report = SyncReport(dump);
        }
#else
        static void get(GDBusCXX::connection_type *conn, GDBusCXX::message_type *msg,
                        GDBusCXX::reader_type &iter, host_type &report)
        {
            std::string dump;
            base::get(conn, msg, iter, dump);
            report = SyncReport(dump);
        }
#endif

        static void append(GDBusCXX::builder_type &builder, arg_type report)
        {
            base::append(builder, report.toString());
        }
    };

    template <> struct dbus_traits<SyncSourceReport> :
        public dbus_traits< std::string >
    {
        typedef dbus_traits< std::string > base;

        typedef SyncSourceReport host_type;
        typedef const SyncSourceReport &arg_type;

#ifdef GDBUS_CXX_GIO
        static void get(GDBusCXX::ExtractArgs &context,
                        GDBusCXX::reader_type &iter, host_type &source)
        {
            std::string dump;
            base::get(context, iter, dump);
            SyncReport report = SyncReport(dump);
            const SyncSourceReport *foo = report.findSyncSourceReport("foo");
            if (!foo) {
                SE_THROW("incomplete SyncReport");
            }
            source = *foo;
        }
#else
        static void get(GDBusCXX::connection_type *conn, GDBusCXX::message_type *msg,
                        GDBusCXX::reader_type &iter, host_type &source)
        {
            std::string dump;
            base::get(conn, msg, iter, dump);
            SyncReport report = SyncReport(dump);
            const SyncSourceReport *foo = report.findSyncSourceReport("foo");
            if (!foo) {
                SE_THROW("incomplete SyncReport");
            }
            source = *foo;
        }
#endif

        static void append(GDBusCXX::builder_type &builder, arg_type source)
        {
            SyncReport report;
            report.addSyncSourceReport("foo", source);
            base::append(builder, report.toString());
        }
    };

}

#endif // SESSION_COMMON_H