forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
88 lines (74 loc) · 2.69 KB
/
Copy pathservice.cpp
File metadata and controls
88 lines (74 loc) · 2.69 KB
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
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <service>
#include <cstdio>
#include <net/inet>
#include <mender/client.hpp>
#include <memdisk>
// Helper function to create a unique identity
std::string mac_identity(MAC::Addr mac)
{
return "{\"mac\" : \"" + mac.to_string() + "\"}";
}
// Mender server instance (IP:PORT) (local hostnames not supported yet)
net::Socket MENDER_SERVER{{10,0,0,1}, 8090};
// Current running version (artifact_name)
std::string ARTIFACT{"example"};
// The mender client
std::unique_ptr<mender::Client> client;
void Service::start(const std::string&)
{
}
// Client code is done in ::ready() because of timer offset
// when generating private key (compute heavy)
void Service::ready()
{
auto& inet = net::Inet::stack();
inet.network_config(
{ 10, 0, 0, 42 }, // IP
{ 255,255,255, 0 }, // Netmask
{ 10, 0, 0, 1 } // Gateway
);
// Mender client currently only supports sync disks (memdisk)
auto disk = fs::shared_memdisk();
disk->init_fs([](auto err, auto&) {
assert(!err && "Could not init FS."); // dont have to panic, can just generate key (but not store..)
});
using namespace mender;
// Create Keystore
auto ks = std::make_unique<Keystore>(disk, "pk.txt"); // <- load key from "pk.txt"
//auto ks = std::make_unique<Keystore>(); // <- generates key
printf("Link: %s\n", inet.link_addr().to_string().c_str());
// Create the client
client = std::make_unique<Client>(
// Auth_manager(Keystore, identity (jsonstr), seqno)
Auth_manager{std::move(ks), mac_identity(inet.link_addr())},
// Device(update_loc, current artifact)
Device{ARTIFACT},
// TCP instance for HTTP client creation and mender server endpoint
inet.tcp(), MENDER_SERVER);
// Save "state" to be restored
client->on_store([](liu::Storage& store) {
printf("Adding my state in %p.\n", &store);
});
// Restore saved "state"
client->on_resume([](liu::Restore& store) {
printf("Resuming my state in %p.\n", &store);
});
// Start the client
client->boot();
}