2 * Copyright (C) 2011 Intel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #ifndef SESSION_COMMON_H
21 #define SESSION_COMMON_H
23 #include "source-status.h"
24 #include "source-progress.h"
25 #include <syncevo/util.h>
26 #include <syncevo/DBusTraits.h>
27 #include <syncevo/FilterConfigNode.h>
28 #include <syncevo/SynthesisEngine.h>
30 #include <gdbus-cxx-bridge.h>
35 * This namespace holds constants and defines for Sessions and its
38 namespace SessionCommon
40 const char * const SERVICE_NAME = "org.syncevolution";
41 const char * const CONNECTION_PATH = "/org/syncevolution/Connection";
42 const char * const CONNECTION_IFACE = "org.syncevolution.Connection";
43 const char * const SESSION_PATH = "/org/syncevolution/Session";
44 const char * const SESSION_IFACE = "org.syncevolution.Session";
45 const char * const SERVER_PATH = "/org/syncevolution/Server";
46 const char * const SERVER_IFACE = "org.syncevolution.Server";
48 const char * const HELPER_PATH = "/dbushelper";
49 const char * const HELPER_IFACE = "org.syncevolution.Helper";
50 const char * const HELPER_DESTINATION = "direct.peer"; // doesn't matter, routing is off
53 * The operation running inside the session.
56 OP_SYNC, /**< running a sync */
57 OP_RESTORE, /**< restoring data */
58 OP_CMDLINE, /**< executing command line */
59 OP_NULL /**< idle, accepting commands via D-Bus */
62 inline std::string runOpToString(RunOperation op) {
63 static const char * const strings[] = {
68 return op >= OP_SYNC && op <= OP_CMDLINE ?
74 * Used by both Connection class (inside server) and
75 * DBusTransportAgent (inside helper).
77 enum ConnectionState {
78 SETUP, /**< ready for first message */
79 PROCESSING, /**< received message, waiting for engine's reply */
80 WAITING, /**< waiting for next follow-up message */
81 FINAL, /**< engine has sent final reply, wait for ACK by peer */
82 DONE, /**< peer has closed normally after the final reply */
83 FAILED /**< in a failed state, no further operation possible */
86 /** maps to names for debugging */
87 inline std::string ConnectionStateToString(ConnectionState state)
89 static const char * const strings[] = {
97 return state >= SETUP && state <= FAILED ?
102 typedef StringMap SourceModes_t;
103 typedef std::map<std::string, SyncEvo::FilterConfigNode::ConfigFilter> SourceFilters_t;
106 * all the information that syncevo-dbus-server needs to
107 * send to syncevo-dbus-helper before the latter can
114 m_serverAlerted(false),
115 m_remoteInitiated(false)
118 std::string m_config;
120 SourceModes_t m_sourceModes;
122 bool m_serverAlerted;
123 bool m_remoteInitiated;
124 std::string m_sessionID;
125 SharedBuffer m_initialMessage;
126 std::string m_initialMessageType;
127 SyncEvo::FilterConfigNode::ConfigFilter m_syncFilter;
128 SyncEvo::FilterConfigNode::ConfigFilter m_sourceFilter;
129 SourceFilters_t m_sourceFilters;
136 using namespace SyncEvo::SessionCommon;
137 using namespace SyncEvo;
138 template<> struct dbus_traits<SyncParams> :
139 public dbus_struct_traits<SyncParams,
140 dbus_member<SyncParams, std::string, &SyncParams::m_config,
141 dbus_member<SyncParams, std::string, &SyncParams::m_mode,
142 dbus_member<SyncParams, SourceModes_t, &SyncParams::m_sourceModes,
143 dbus_member<SyncParams, bool, &SyncParams::m_serverMode,
144 dbus_member<SyncParams, bool, &SyncParams::m_serverAlerted,
145 dbus_member<SyncParams, bool, &SyncParams::m_remoteInitiated,
146 dbus_member<SyncParams, std::string, &SyncParams::m_sessionID,
147 dbus_member<SyncParams, SharedBuffer, &SyncParams::m_initialMessage,
148 dbus_member<SyncParams, std::string, &SyncParams::m_initialMessageType,
149 dbus_member<SyncParams, FilterConfigNode::ConfigFilter, &SyncParams::m_syncFilter,
150 dbus_member<SyncParams, FilterConfigNode::ConfigFilter, &SyncParams::m_sourceFilter,
151 dbus_member_single<SyncParams, SourceFilters_t, &SyncParams::m_sourceFilters
152 > > > > > > > > > > > > >
156 * Similar to DBusArray<uint8_t>, but with different native
157 * types. Uses encoding/decoding from the base class, copies
158 * to/from SharedBuffer as needed.
160 * DBusArray<uint8_t> is more efficient because it avoids
161 * copying the bytes from the D-Bus message when decoding,
162 * but it is harder to use natively (cannot be copied).
163 * SharedBuffer does ref counting for the memory chunk,
164 * so once initialized, copying it is cheap.
166 template <> struct dbus_traits<SharedBuffer> :
167 public dbus_traits< DBusArray<uint8_t> >
169 typedef dbus_traits< DBusArray<uint8_t> > base;
171 typedef SharedBuffer host_type;
172 typedef const SharedBuffer &arg_type;
175 static void get(GDBusCXX::ExtractArgs &context,
176 GDBusCXX::reader_type &iter, host_type &buffer)
178 base::host_type array;
179 base::get(context, iter, array);
180 buffer = SharedBuffer(reinterpret_cast<const char *>(array.second), array.first);
183 static void get(GDBusCXX::connection_type *conn, GDBusCXX::message_type *msg,
184 GDBusCXX::reader_type &iter, host_type &buffer)
186 base::host_type array;
187 base::get(conn, msg, iter, array);
188 buffer = SharedBuffer(reinterpret_cast<const char *>(array.second), array.first);
192 static void append(GDBusCXX::builder_type &builder, arg_type buffer)
194 base::host_type array(buffer.size(), reinterpret_cast<const uint8_t *>(buffer.get()));
195 base::append(builder, array);
200 #endif // SESSION_COMMON_H