Leosac  0.7.0
OpenSourceAccessControl
UpdateService.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "UpdateService.hpp"
22 #include "core/update/Update_odb.h"
23 #include "tools/GenGuid.h"
25 
26 namespace Leosac
27 {
28 namespace update
29 {
31 {
32  check_update_sig_.connect(
33  CheckUpdateT::slot_type(&UpdateBackend::check_update, backend.get())
34  .track_foreign(backend));
35 
36  create_update_sig_.connect(
37  CreateUpdateT::slot_type(&UpdateBackend::create_update, backend.get(),
38  boost::placeholders::_1, boost::placeholders::_2)
39  .track_foreign(backend));
40 
41  ack_update_sig_.connect(
42  AckUpdateT::slot_type(&UpdateBackend::ack_update, backend.get(),
43  boost::placeholders::_1, boost::placeholders::_2)
44  .track_foreign(backend));
45 
46  cancel_update_sig_.connect(
47  CancelUpdateT::slot_type(&UpdateBackend::cancel_update, backend.get(),
48  boost::placeholders::_1, boost::placeholders::_2)
49  .track_foreign(backend));
50 }
51 
52 std::vector<UpdateDescriptorPtr> UpdateService::check_update()
53 {
54  auto descriptors = check_update_sig_();
55 
56  // When a new check_update happens, we clear all
57  // previous update descriptor as they would possibly
58  // become outdated.
59  published_descriptors_.clear();
60  for (const auto &descriptor : descriptors)
61  {
62  // This is so a client can reference a descriptor later to
63  // trigger creation of an update. (This is needed since we don't want
64  // update descriptor to be database-persisted).
65  published_descriptors_[descriptor->uuid] = descriptor;
66  }
67 
68  return descriptors;
69 }
70 
71 std::vector<IUpdatePtr> UpdateService::pending_updates()
72 {
73  const auto &db = get_service_registry().get_service<DBService>()->db();
74  db::OptionalTransaction t(db->begin());
75 
76  std::vector<IUpdatePtr> updates;
77  using Query = odb::query<update::Update>;
78  auto updates_odb =
79  db->query<update::Update>(Query::status == update::Status::PENDING);
80 
81  for (auto i(updates_odb.begin()); i != updates_odb.end(); ++i)
82  {
83  IUpdatePtr ptr(i.load());
84  ASSERT_LOG(ptr, "Loading failed, but object should already be loaded.");
85  updates.push_back(ptr);
86  }
87  t.commit();
88  return updates;
89 }
90 
91 IUpdatePtr UpdateService::create_update(const std::string &update_descriptor_uuid,
92  const ExecutionContext &ec)
93 {
94  auto itr = published_descriptors_.find(update_descriptor_uuid);
95  if (itr == published_descriptors_.end())
96  {
97  throw LEOSACException("UpdateDescriptor doesn't exist anymore.");
98  }
99 
100  return create_update_sig_(*(itr->second), ec);
101 }
102 
104 {
105  ack_update_sig_(update, ec);
106 }
107 
109 {
110  cancel_update_sig_(update, ec);
111 }
112 
114  : uuid(gen_uuid())
115  , severity(Severity::NORMAL)
116 {
117 }
118 }
119 }
void ack_update(IUpdatePtr update, const ExecutionContext &ec)
virtual IUpdatePtr create_update(const UpdateDescriptor &ud, const ExecutionContext &)=0
Create an update based on the UpdateDescriptor ud.
This is the header file for a generated source file, GitSHA1.cpp.
std::vector< IUpdatePtr > pending_updates()
Retrieve the list of pending updates.
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
std::shared_ptr< IUpdate > IUpdatePtr
Definition: UpdateFwd.hpp:42
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Provides various database-related services to consumer.
Definition: DBService.hpp:34
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
std::string gen_uuid()
Generate a new UUID.
Definition: GenGuid.cpp:26
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
std::vector< UpdateDescriptorPtr > check_update()
virtual void cancel_update(IUpdatePtr u, const ExecutionContext &)=0
Cancel (not rollback) the pending update u.
A base class for Leosac specific exception.
virtual void ack_update(IUpdatePtr u, const ExecutionContext &)=0
Acknowledge the pending update u.
std::shared_ptr< UpdateBackend > UpdateBackendPtr
Definition: UpdateFwd.hpp:49
virtual std::vector< UpdateDescriptorPtr > check_update()=0
Check for updates against arbitrary, module-owned object.
void cancel_update(IUpdatePtr update, const ExecutionContext &ec)
void register_backend(UpdateBackendPtr backend)
Register a backend object.
IUpdatePtr create_update(const std::string &update_descriptor_uuid, const ExecutionContext &ec)
Create an Update object corresponding to the update descriptor whose uuid is update_descriptor_uuid.
Represent an update.
Definition: Update.hpp:39
std::map< std::string, UpdateDescriptorPtr > published_descriptors_
An ExecutionContext is passed around to service so they have context about who is making the call and...