Imported Upstream version 0.1.2
[platform/upstream/snapper.git] / server / Client.h
1 /*
2  * Copyright (c) 2012 Novell, Inc.
3  *
4  * All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as published
8  * by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, contact Novell, Inc.
17  *
18  * To contact Novell about this file by physical or electronic mail, you may
19  * find current contact information at www.novell.com.
20  */
21
22
23 #ifndef SNAPPER_CLIENT_H
24 #define SNAPPER_CLIENT_H
25
26
27 #include <string>
28 #include <list>
29 #include <queue>
30 #include <set>
31 #include <boost/thread.hpp>
32
33 #include <snapper/Snapper.h>
34 #include <snapper/Snapshot.h>
35 #include <snapper/Comparison.h>
36 #include <dbus/DBusConnection.h>
37 #include <dbus/DBusMessage.h>
38
39 #include "MetaSnapper.h"
40
41
42 using namespace std;
43 using namespace snapper;
44
45
46 #define SERVICE "org.opensuse.Snapper"
47 #define PATH "/org/opensuse/Snapper"
48 #define INTERFACE "org.opensuse.Snapper"
49
50
51 extern boost::shared_mutex big_mutex;
52
53
54 struct NoComparison : public std::exception
55 {
56     explicit NoComparison() throw() {}
57     virtual const char* what() const throw() { return "no comparison"; }
58 };
59
60
61 class Client : private boost::noncopyable
62 {
63 public:
64
65     static void introspect(DBus::Connection& conn, DBus::Message& msg);
66
67     void check_permission(DBus::Connection& conn, DBus::Message& msg) const;
68     void check_permission(DBus::Connection& conn, DBus::Message& msg,
69                           const MetaSnapper& meta_snapper) const;
70     void check_lock(DBus::Connection& conn, DBus::Message& msg, const string& config_name) const;
71     void check_config_in_use(const MetaSnapper& meta_snapper) const;
72     void check_snapshot_in_use(const MetaSnapper& meta_snapper, unsigned int number) const;
73
74     void signal_config_created(DBus::Connection& conn, const string& config_name);
75     void signal_config_deleted(DBus::Connection& conn, const string& config_name);
76     void signal_snapshot_created(DBus::Connection& conn, const string& config_name,
77                                  unsigned int num);
78     void signal_snapshot_modified(DBus::Connection& conn, const string& config_name,
79                                   unsigned int num);
80     void signal_snapshots_deleted(DBus::Connection& conn, const string& config_name,
81                                   const list<dbus_uint32_t>& nums);
82
83     void list_configs(DBus::Connection& conn, DBus::Message& msg);
84     void get_config(DBus::Connection& conn, DBus::Message& msg);
85     void create_config(DBus::Connection& conn, DBus::Message& msg);
86     void delete_config(DBus::Connection& conn, DBus::Message& msg);
87     void lock_config(DBus::Connection& conn, DBus::Message& msg);
88     void unlock_config(DBus::Connection& conn, DBus::Message& msg);
89     void list_snapshots(DBus::Connection& conn, DBus::Message& msg);
90     void list_snapshots_at_time(DBus::Connection& conn, DBus::Message& msg);
91     void get_snapshot(DBus::Connection& conn, DBus::Message& msg);
92     void set_snapshot(DBus::Connection& conn, DBus::Message& msg);
93     void create_single_snapshot(DBus::Connection& conn, DBus::Message& msg);
94     void create_pre_snapshot(DBus::Connection& conn, DBus::Message& msg);
95     void create_post_snapshot(DBus::Connection& conn, DBus::Message& msg);
96     void delete_snapshots(DBus::Connection& conn, DBus::Message& msg);
97     void mount_snapshot(DBus::Connection& conn, DBus::Message& msg);
98     void umount_snapshot(DBus::Connection& conn, DBus::Message& msg);
99     void get_mount_point(DBus::Connection& conn, DBus::Message& msg);
100     void create_comparison(DBus::Connection& conn, DBus::Message& msg);
101     void delete_comparison(DBus::Connection& conn, DBus::Message& msg);
102     void get_files(DBus::Connection& conn, DBus::Message& msg);
103     void debug(DBus::Connection& conn, DBus::Message& msg) const;
104
105     void dispatch(DBus::Connection& conn, DBus::Message& msg);
106
107     Client(const string& name);
108     ~Client();
109
110     list<Comparison*>::iterator find_comparison(Snapper* snapper, unsigned int number1,
111                                                 unsigned int number2);
112
113     list<Comparison*>::iterator find_comparison(Snapper* snapper,
114                                                 Snapshots::const_iterator snapshot1,
115                                                 Snapshots::const_iterator snapshot2);
116
117     void delete_comparison(list<Comparison*>::iterator);
118
119     void add_lock(const string& config_name);
120     void remove_lock(const string& config_name);
121     bool has_lock(const string& config_name) const;
122
123     void add_mount(const string& config_name, unsigned int number);
124     void remove_mount(const string& config_name, unsigned int number);
125
126     const string name;
127
128     list<Comparison*> comparisons;
129
130     set<string> locks;
131
132     map<pair<string, unsigned int>, unsigned int> mounts;
133
134     struct Task
135     {
136         Task(DBus::Connection& conn, DBus::Message& msg) : conn(conn), msg(msg) {}
137
138         DBus::Connection& conn;
139         DBus::Message msg;
140     };
141
142     boost::condition_variable condition;
143     boost::mutex mutex;
144     boost::thread thread;
145     queue<Task> tasks;
146
147     bool zombie;
148
149     void add_task(DBus::Connection& conn, DBus::Message& msg);
150
151 private:
152
153     void worker();
154
155 };
156
157
158 class Clients
159 {
160 public:
161
162     typedef list<Client>::iterator iterator;
163     typedef list<Client>::const_iterator const_iterator;
164
165     iterator begin() { return entries.begin(); }
166     const_iterator begin() const { return entries.begin(); }
167
168     iterator end() { return entries.end(); }
169     const_iterator end() const { return entries.end(); }
170
171     bool empty() const { return entries.empty(); }
172
173     iterator find(const string& name);
174
175     iterator add(const string& name);
176     void remove_zombies();
177
178     bool has_zombies() const;
179
180 private:
181
182     list<Client> entries;
183
184 };
185
186
187 extern Clients clients;
188
189
190 #endif