summaryrefslogtreecommitdiff
path: root/daemon/appfetchmanager.cpp
blob: 9c58e2756e307b0951d86dfdd7f4ed929582d5d9 (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
#include "appfetchmanager.h"
#include "appmanager.h"
#include "uploadmanager.h"
#include "unpacker.h"
#include "packer.h"

AppFetchManager::AppFetchManager(WatchConnector *watch, UploadManager *upload, AppManager *apps, QObject *parent)
    : QObject(parent), l(metaObject()->className()),
      watch(watch), upload(upload), apps(apps)

{
    watch->setEndpointHandler(WatchConnector::watchAPP_FETCH,
                              [this](const QByteArray &data) {
        switch (data.at(0)) {
        case WatchConnector::appfetchREQUEST:
            handleAppFetchRequest(data);
            break;
        }

        return true;
    });
}

void AppFetchManager::handleAppFetchRequest(const QByteArray &msg)
{
    Unpacker u(msg);
    quint8 command = u.readLE<quint8>();
    QUuid uuid = u.readUuid();
    qint32 appId = u.readLE<qint32>();

    Q_ASSERT(command == WatchConnector::appfetchREQUEST);

    uploadApp(uuid, appId);
}

void AppFetchManager::sendAppFetchResponse(WatchConnector::AppFetchMessage command, WatchConnector::AppFetchStatus status)
{
    QByteArray msg;
    Packer p(&msg);

    p.writeLE<quint8>(command);
    p.writeLE<quint8>(status);

    watch->sendMessage(WatchConnector::watchAPP_FETCH, msg);
}

void AppFetchManager::uploadApp(const QUuid &uuid, quint32 appId)
{
    AppInfo info = apps->info(uuid);
    if (info.uuid() != uuid) {
        qCWarning(l) << "uuid" << uuid << "is not installed";
        sendAppFetchResponse(WatchConnector::appfetchREQUEST, WatchConnector::appfetchINVALID_UUID);
        return;
    }

    qCDebug(l) << "about to upload app" << info.shortName();

    QSharedPointer<QIODevice> binaryFile(info.openFile(AppInfo::APPLICATION));
    if (!binaryFile) {
        qCWarning(l) << "failed to open" << info.shortName() << "AppInfo::BINARY";
        sendAppFetchResponse(WatchConnector::appfetchREQUEST, WatchConnector::appfetchINVALID_UUID);
        return;
    }

    qCDebug(l) << "binary file size is" << binaryFile->size();

    upload->uploadAppBinaryId(appId, binaryFile.data(), info.crcFile(AppInfo::APPLICATION),
    [this, info, binaryFile, appId]() {
        qCDebug(l) << "app binary upload succesful";
        binaryFile->close();

        // Upload worker if present
        if (info.type() == "worker") {
            QSharedPointer<QIODevice> workerFile(info.openFile(AppInfo::WORKER));
            if (workerFile) {
                upload->uploadAppWorkerId(appId, workerFile.data(), info.crcFile(AppInfo::WORKER),
                [this, workerFile]() {
                    qCDebug(l) << "app worker upload succesful";
                    workerFile->close();
                }, [this, workerFile](int code) {
                    qCWarning(l) << "app worker upload failed" << code;
                    workerFile->close();
                });
            }
        }

        // Proceed to upload the resource file
        QSharedPointer<QIODevice> resourceFile(info.openFile(AppInfo::RESOURCES));
        if (resourceFile) {
            upload->uploadAppResourcesId(appId, resourceFile.data(), info.crcFile(AppInfo::RESOURCES),
            [this, resourceFile]() {
                qCDebug(l) << "app resources upload succesful";
                resourceFile->close();
                // Upload succesful
            }, [this, resourceFile](int code) {
                qCWarning(l) << "app resources upload failed" << code;
                resourceFile->close();
            });
        } else {
            // No resource file
        }
    }, [this, binaryFile](int code) {
        binaryFile->close();
        qCWarning(l) << "app binary upload failed" << code;
    });
}