~charlesk/keeper/fix-slow-tar-creator-test

« back to all changes in this revision

Viewing changes to src/service/keeper-task-restore.cpp

  • Committer: Xavi Garcia Mena
  • Date: 2016-11-11 14:50:06 UTC
  • Revision ID: xavi.garcia.mena@canonical.com-20161111145006-x2jova8adumkw8jv
Added restore classes, fake restore helper, integration test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors:
 
17
 *   Xavi Garcia <xavi.garcia.mena@canonical.com>
 
18
 *   Charles Kerr <charles.kerr@canonical.com>
 
19
 */
 
20
 
 
21
#include "util/connection-helper.h"
 
22
#include "storage-framework/storage_framework_client.h"
 
23
#include "helper/restore-helper.h"
 
24
#include "service/app-const.h" // DEKKO_APP_ID
 
25
#include "service/keeper-task-restore.h"
 
26
#include "service/keeper-task.h"
 
27
#include "service/private/keeper-task_p.h"
 
28
 
 
29
namespace sf = unity::storage::qt::client;
 
30
 
 
31
class KeeperTaskRestorePrivate : public KeeperTaskPrivate
 
32
{
 
33
    Q_DECLARE_PUBLIC(KeeperTaskRestore)
 
34
public:
 
35
    KeeperTaskRestorePrivate(KeeperTask * keeper_task,
 
36
                            KeeperTask::TaskData & task_data,
 
37
                            QSharedPointer<HelperRegistry> const & helper_registry,
 
38
                            QSharedPointer<StorageFrameworkClient> const & storage)
 
39
        : KeeperTaskPrivate(keeper_task, task_data, helper_registry, storage)
 
40
    {
 
41
    }
 
42
 
 
43
    ~KeeperTaskRestorePrivate() = default;
 
44
 
 
45
    QStringList get_helper_urls() const
 
46
    {
 
47
        return helper_registry_->get_restore_helper_urls(task_data_.metadata);
 
48
    }
 
49
 
 
50
    void init_helper()
 
51
    {
 
52
        qDebug() << "Initializing a restore helper";
 
53
        helper_.reset(new RestoreHelper(DEKKO_APP_ID), [](Helper *h){h->deleteLater();}); // TODO change this to a restore helper
 
54
        qDebug() << "Helper " <<  static_cast<void*>(helper_.data()) << " was created";
 
55
    }
 
56
 
 
57
    void ask_for_downloader()
 
58
    {
 
59
        qDebug() << "asking storage framework for a socket for reading";
 
60
 
 
61
        QString file_name;
 
62
        task_data_.metadata.get_property(Metadata::FILE_NAME_KEY, file_name);
 
63
        if (file_name.isEmpty())
 
64
        {
 
65
            qWarning() << "ERROR: the restore task does not provide a valid file name to read from.";
 
66
            return;
 
67
        }
 
68
 
 
69
        QString dir_name;
 
70
        task_data_.metadata.get_property(Metadata::DIR_NAME_KEY, dir_name);
 
71
        if (dir_name.isEmpty())
 
72
        {
 
73
            qWarning() << "ERROR: the restore task does not provide a valid directory name.";
 
74
            return;
 
75
        }
 
76
 
 
77
        // extract the dir_name.
 
78
        connections_.connect_future(
 
79
            storage_->get_new_downloader(dir_name, file_name),
 
80
            std::function<void(std::shared_ptr<Downloader> const&)>{
 
81
                [this](std::shared_ptr<Downloader> const& downloader){
 
82
                    qDebug() << "Downloader is" << static_cast<void*>(downloader.get());
 
83
                    int fd {-1};
 
84
                    if (downloader) {
 
85
                        qDebug() << "Helper is " <<  static_cast<void*>(helper_.data());
 
86
                        auto restore_helper = qSharedPointerDynamicCast<RestoreHelper>(helper_);
 
87
                        restore_helper->set_downloader(downloader);
 
88
                        fd = restore_helper->get_helper_socket();
 
89
                    }
 
90
                    qDebug("emitting task_socket_ready(socket=%d)", fd);
 
91
                    Q_EMIT(q_ptr->task_socket_ready(fd));
 
92
                }
 
93
            }
 
94
        );
 
95
    }
 
96
private:
 
97
    ConnectionHelper connections_;
 
98
};
 
99
 
 
100
KeeperTaskRestore::KeeperTaskRestore(TaskData & task_data,
 
101
           QSharedPointer<HelperRegistry> const & helper_registry,
 
102
           QSharedPointer<StorageFrameworkClient> const & storage,
 
103
           QObject *parent)
 
104
    : KeeperTask(*new KeeperTaskRestorePrivate(this, task_data, helper_registry, storage), parent)
 
105
{
 
106
}
 
107
 
 
108
KeeperTaskRestore::~KeeperTaskRestore() = default;
 
109
 
 
110
QStringList KeeperTaskRestore::get_helper_urls() const
 
111
{
 
112
    Q_D(const KeeperTaskRestore);
 
113
 
 
114
    return d->get_helper_urls();
 
115
}
 
116
 
 
117
void KeeperTaskRestore::init_helper()
 
118
{
 
119
    Q_D(KeeperTaskRestore);
 
120
 
 
121
    d->init_helper();
 
122
}
 
123
 
 
124
void KeeperTaskRestore::ask_for_downloader()
 
125
{
 
126
    Q_D(KeeperTaskRestore);
 
127
 
 
128
    d->ask_for_downloader();
 
129
}