Skip to content

Commit

Permalink
[ProcessContainers] Update AWC implementation (#1773)
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Daniluk <[email protected]>
Co-authored-by: Pierre Wielders <[email protected]>
Co-authored-by: MFransen69 <[email protected]>
  • Loading branch information
4 people authored Nov 29, 2024
1 parent 2977252 commit ef4ca1a
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

#include <algorithm>

#include "Tracing.h"


using namespace Thunder::ProcessContainers;

namespace Thunder {
namespace ProcessContainers {

AWCStateChangeNotifier::AWCStateChangeNotifier() : _listeners(), _mutex() {};

Expand Down Expand Up @@ -59,3 +56,6 @@ void AWCListener::notifyServiceReady()
TRACE_L3("%s", _TRACE_FUNCTION_);
// no action
}

} // namespace ProcessContainers
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace ProcessContainers {

class AWCStateChangeListener {
public:
virtual ~AWCStateChangeListener() {}
virtual void notifyStateChange(int req_id, awc::awc_app_state_t app_state, int status, unsigned int pid) = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "AWCContainerAdministrator.h"
#include "AWCImplementation.h"
#include "AWCProxyContainer.h"
#include "processcontainers/Tracing.h"

using namespace Thunder::ProcessContainers;
using namespace Thunder::Core;
#include "processcontainers/ContainerProducer.h"

namespace Thunder {
namespace ProcessContainers {

using namespace Core;

PluginConfig::PluginConfig(const std::string &path):
JSON::Container()
Expand Down Expand Up @@ -44,27 +46,40 @@ PluginConfig::PluginConfig(const std::string &path):
}

AWCContainerAdministrator::AWCContainerAdministrator()
: BaseContainerAdministrator()
{
TRACE_L1("%p", this);
awcClient_ = awc::AWCClient::getInstance();
}

AWCContainerAdministrator::~AWCContainerAdministrator()
{
TRACE_L1("%p", this);
}

uint32_t AWCContainerAdministrator::Initialize(const string& configuration VARIABLE_IS_NOT_USED)
{
uint32_t result = Core::ERROR_GENERAL;

if (awcClient_) {
awcClientListener_ = std::make_shared<AWCListener>(this);
awcClient_->setListener(awcClientListener_);
result = Core::ERROR_NONE;
}

return (result);
}

AWCContainerAdministrator::~AWCContainerAdministrator()
void AWCContainerAdministrator::Deinitialize()
{
TRACE_L1("%p", this);
if (awcClient_ && awcClientListener_) {
awcClient_->removeListener(awcClientListener_);
awcClientListener_.reset();
}
}

IContainer* AWCContainerAdministrator::Container(
Core::ProxyType<IContainer> AWCContainerAdministrator::Container(
const string& name,
IStringIterator& searchpaths,
IStringIterator& searchpaths VARIABLE_IS_NOT_USED,
const string& containerLogDir,
const string& configuration)
{
Expand All @@ -81,30 +96,24 @@ IContainer* AWCContainerAdministrator::Container(

const PluginConfig cfg{configuration};

IContainer* container = Get(name);
const auto append = !container;
Core::ProxyType<IContainer> container;

if(!container && cfg.useProxy)
{
container = new AWCProxyContainer(name, cfg, &dbusClient_);
if(cfg.useProxy) {
container = ContainerAdministrator::Instance().Create<AWCProxyContainer>(name, cfg, &dbusClient_);
}
else if(!container)
{
container = new AWCContainer(name, awcClient_, this);
else {
container = ContainerAdministrator::Instance().Create<AWCContainer>(name, awcClient_, this);
}
if(append)
{
this->InternalLock();
InsertContainer(container);
this->InternalUnlock();

if(container.IsValid() == false) {
TRACE_L1("not supported container type: %s", name.c_str());
}

if(!container) TRACE_L1("not supported container type: %s", name.c_str());
return container;
}

IContainerAdministrator& IContainerAdministrator::Instance()
{
static AWCContainerAdministrator& myAWCContainerAdministrator = Core::SingletonType<AWCContainerAdministrator>::Instance();
return myAWCContainerAdministrator;
// FACTORY REGISTRATION
static ContainerProducerRegistrationType<AWCContainerAdministrator, IContainer::containertype::AWC> registration;

} // namespace ProcessContainers
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include <mutex>
#include <vector>


#include "AWC.h"
#include "dbus/Client.h"
#include "processcontainers/common/BaseAdministrator.h"
#include "core/JSON.h"

#include "processcontainers/Messaging.h"
#include "processcontainers/ContainerAdministrator.h"

namespace Thunder {
namespace ProcessContainers {

Expand All @@ -25,22 +26,24 @@ struct PluginConfig: public Core::JSON::Container


class AWCContainerAdministrator:
public BaseContainerAdministrator<IContainer>,
public IContainerProducer,
public AWCStateChangeNotifier
{
friend class AWCContainer;
friend class Core::SingletonType<AWCContainerAdministrator>;
private:
awc::AWCClient * awcClient_;
std::shared_ptr<AWCListener> awcClientListener_;
dbus::Client dbusClient_;
AWCContainerAdministrator();

public:
AWCContainerAdministrator();
AWCContainerAdministrator(const AWCContainerAdministrator&) = delete;
AWCContainerAdministrator& operator=(const AWCContainerAdministrator&) = delete;
~AWCContainerAdministrator() override;
IContainer* Container(const string& name, IStringIterator& searchpaths, const string& containerLogDir, const string& configuration) override;
void Logging(const string& globalLogDir, const string& loggingOptions) override {};

uint32_t Initialize(const string& config) override;
void Deinitialize() override;
Core::ProxyType<IContainer> Container(const string& name, IStringIterator& searchpaths,
const string& containerLogDir, const string& configuration) override;
};

} /* ProcessContainers */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

#include "AWCContainerBase.h"

using namespace Thunder::ProcessContainers;
#include "processcontainers/common/BaseRefCount.h"
#include "processcontainers/IProcessContainers.h"


namespace Thunder {
namespace ProcessContainers {

struct AWCMemoryInfo : public BaseRefCount<IMemoryInfo> {
AWCMemoryInfo()
Expand Down Expand Up @@ -61,3 +67,6 @@ INetworkInterfaceIterator* AWCContainerBase::NetworkInterfaces() const
TRACE_L3("%s", _TRACE_FUNCTION_);
return nullptr;
}

} // namespace ProcessContainers
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#pragma once

#include "common/BaseRefCount.h"
#include "processcontainers/ProcessContainer.h"
#include "processcontainers/ContainerAdministrator.h"

namespace Thunder {
namespace ProcessContainers {

class AWCContainerBase:
public BaseRefCount<IContainer>
public IContainer
{
private:
string id_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 Metrological
*
* 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 "AWCContainerAdministrator.h"
#include "AWCImplementation.h"
#include "processcontainers/Tracing.h"


using namespace Thunder::ProcessContainers;
namespace Thunder {
namespace ProcessContainers {

AWCContainer::AWCContainer(const string& id, awc::AWCClient * client, AWCStateChangeNotifier * notifier):
AWCContainerBase(id)
Expand All @@ -41,8 +23,6 @@ AWCContainer::~AWCContainer()
}
_notifier->removeListener(this);
TRACE(ProcessContainerization, (_T("Container [%s] released"), Id().c_str()));

static_cast<AWCContainerAdministrator &>(AWCContainerAdministrator::Instance()).RemoveContainer(this);
}

uint32_t AWCContainer::Pid() const
Expand Down Expand Up @@ -200,3 +180,6 @@ void AWCContainer::notifyStateChange(int req_id, awc::awc_app_state_t app_state,
}
}
}

} // namespace ProcessContainers
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 Metrological
*
* 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.
*/

#pragma once

#include <condition_variable>
Expand All @@ -25,27 +6,28 @@
#include "AWC.h"
#include "AWCContainerBase.h"


namespace Thunder {
namespace ProcessContainers {

class AWCContainer:
public AWCContainerBase,
public AWCStateChangeListener
{
private:
friend class AWCContainerAdministrator;

AWCContainer(const string& id, awc::AWCClient * client, AWCStateChangeNotifier * notifier);
public:
AWCContainer(const string& id, awc::AWCClient * client, AWCStateChangeNotifier * notifier);
AWCContainer(const AWCContainer&) = delete;
~AWCContainer() override;
AWCContainer& operator=(const AWCContainer&) = delete;

public:
containertype Type() const override { return IContainer::AWC; }
uint32_t Pid() const override;
bool IsRunning() const override;
bool Start(const string& command, ProcessContainers::IStringIterator& parameters) override;
bool Stop(const uint32_t timeout /*ms*/) override;
void notifyStateChange(int req_id, awc::awc_app_state_t app_state, int status, unsigned int pid) override;

private:
uint32_t _pid{0};
int _runId{-1};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#define CHECK(cond) ASSERT((cond))

using namespace Thunder::ProcessContainers;
namespace Thunder {
namespace ProcessContainers {

AWCProxyContainer::DBusInterface::DBusInterface(
AWCProxyContainer *container):
Expand Down Expand Up @@ -80,8 +81,6 @@ AWCProxyContainer::~AWCProxyContainer()
if(IsRunning()) Stop(2000);
CHECK(client_);
client_->unbind(dbusInterface_);
static_cast<AWCContainerAdministrator&>(
AWCContainerAdministrator::Instance()).RemoveContainer(this);
}

uint32_t AWCProxyContainer::Pid() const
Expand Down Expand Up @@ -183,3 +182,6 @@ bool AWCProxyContainer::Stop(const uint32_t timeout /*ms*/ VARIABLE_IS_NOT_USED)
std::unique_lock<std::mutex> lock(mutex_);
return waitForStateChange(lock, AppState::STOPPED, stopTimeout_ - diff);
}

} // namespace ProcessContainers
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ class AWCProxyContainer: public AWCContainerBase
void onStateChange(int, AppState) override final;
public:
DBusInterface(AWCProxyContainer *);
~DBusInterface() override final;
~DBusInterface() override;
};

using SharedDBusInterface = std::shared_ptr<DBusInterface>;

friend class AWCContainerAdministrator;

AWCProxyContainer(const string &name, const PluginConfig &, dbus::Client *);

bool waitForStateChange(Lock &, AppState, milliseconds);

public:
AWCProxyContainer(const string &name, const PluginConfig &, dbus::Client *);
AWCProxyContainer(const AWCProxyContainer&) = delete;
~AWCProxyContainer() override final;
~AWCProxyContainer() override;
AWCProxyContainer& operator=(const AWCProxyContainer&) = delete;

containertype Type() const override { return IContainer::AWC; }
uint32_t Pid() const override final;
bool IsRunning() const override final;
bool Start(const string &, ProcessContainers::IStringIterator &) override final;
Expand Down
Loading

0 comments on commit ef4ca1a

Please sign in to comment.