softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::Gateway Class Referenceabstract

Gateway base class for SoftwareContainer. More...

#include <gateway.h>

Inheritance diagram for softwarecontainer::Gateway:
[legend]

Public Types

enum  GatewayState : unsigned int { CREATED, CONFIGURED, ACTIVATED }
 

Public Member Functions

 Gateway (const std::string &id, std::shared_ptr< ContainerAbstractInterface > container, bool isDynamic=false)
 Constructor for inheriting classes to initilize. More...
 
virtual std::string id () const
 Returns the ID of the gateway. More...
 
virtual bool setConfig (const json_t *config)
 Configure this gateway according to the supplied JSON configuration string. More...
 
virtual bool activate ()
 Applies any configuration set by setConfig() More...
 
virtual bool teardown ()
 Restore system to the state prior to launching of gateway. More...
 
virtual bool isConfigured ()
 Is the gateway configured or not? More...
 
virtual bool isActivated ()
 Is the gateway activated or not? More...
 

Protected Member Functions

virtual bool readConfigElement (const json_t *element)=0
 Gateway specific parsing of config elements. More...
 
std::shared_ptr< ContainerAbstractInterfacegetContainer ()
 Get a handle to the associated container. More...
 
bool setEnvironmentVariable (const std::string &variable, const std::string &value)
 Set an environment variable in the associated container. More...
 
virtual bool activateGateway ()=0
 
virtual bool teardownGateway ()=0
 

Protected Attributes

bool m_activatedOnce
 

Detailed Description

Gateway base class for SoftwareContainer.

Gateways can be in one of three states:

  • Created - the gateway object exists
  • Configured - the gateway has successfully recieved and processed some configuration data, ready for activation
  • Activated - the gateway is active and running.

The complete gateway config is passed as a JSON array and all gateways then provide their specific parsing of the items in the array.

Gateways can specify if they are 'dynamic' or not when initializing this base class. A dynamic gateway supports multiple calls to setConfig and activate.

Definition at line 61 of file gateway.h.

Constructor & Destructor Documentation

softwarecontainer::Gateway::Gateway ( const std::string &  id,
std::shared_ptr< ContainerAbstractInterface container,
bool  isDynamic = false 
)

Constructor for inheriting classes to initilize.

Gateways that supports quick-launch should flag for this when initializing this base class.

Parameters
idSpecific gateway ID used to match with configurations.
containerAn interface to the Container that this gateway is part of.
isDynamicSet to 'true' to indicate the inheriting class supports re-configuration and re-activation

Definition at line 25 of file gateway.cpp.

27  :
28  m_activatedOnce(false),
29  m_id(id),
30  m_container(container),
31  m_isDynamic(isDynamic),
32  m_state(GatewayState::CREATED)
33 {
34 }

Member Function Documentation

std::string softwarecontainer::Gateway::id ( ) const
virtual

Returns the ID of the gateway.

Returns
Returns the ID of the gateway as a string

Definition at line 36 of file gateway.cpp.

Referenced by activate(), softwarecontainer::DBusGatewayInstance::DBusGatewayInstance(), setConfig(), and teardown().

37 {
38  return m_id;
39 }

Here is the caller graph for this function:

bool softwarecontainer::Gateway::setConfig ( const json_t *  config)
virtual

Configure this gateway according to the supplied JSON configuration string.

Parameters
configJSON string containing gateway-specific JSON configuration
Returns
true if config was successfully parsed, false otherwise
Exceptions
GatewayErrorIf called on an already activated gateway.

Reimplemented in softwarecontainer::DBusGateway.

Definition at line 41 of file gateway.cpp.

References id(), and readConfigElement().

42 {
43  if (GatewayState::ACTIVATED == m_state && !m_isDynamic) {
44  std::string message = "Can not configure a gateway that is already activated "
45  "if the gateway does not support dynamic behavior. "
46  "Gateway ID: " + id();
47  log_error() << message;
48  throw GatewayError(message);
49  }
50 
51  if (!json_is_array(config)) {
52  log_error() << "Root JSON element is not an array";
53  return false;
54  }
55 
56  if (json_array_size(config) == 0) {
57  log_error() << "Root JSON array is empty";
58  return false;
59  }
60 
61  for(size_t i = 0; i < json_array_size(config); i++) {
62  json_t *element = json_array_get(config, i);
63  if (!json_is_object(element)) {
64  log_error() << "json configuration is not an object";
65  return false;
66  }
67 
68  if (!readConfigElement(element)) {
69  log_warning() << "Could not read config element";
70  return false;
71  }
72  }
73 
74  m_state = GatewayState::CONFIGURED;
75  return true;
76 }
virtual std::string id() const
Returns the ID of the gateway.
Definition: gateway.cpp:36
virtual bool readConfigElement(const json_t *element)=0
Gateway specific parsing of config elements.

Here is the call graph for this function:

bool softwarecontainer::Gateway::activate ( )
virtual

Applies any configuration set by setConfig()

Returns
true upon successful application of configuration, false otherwise
Exceptions
GatewayErrorIf called on an already activated gateway, or if the gateway has not been previously configured, or if there is not container instance set.

Reimplemented in softwarecontainer::DBusGateway.

Definition at line 78 of file gateway.cpp.

References id().

78  {
79  if (GatewayState::ACTIVATED == m_state && !m_isDynamic) {
80  std::string message = "Can not activate a gateway that is already activated "
81  "if the gateway does not support dynamic behavior. "
82  "Gateway ID: " + id();
83  log_error() << message;
84  throw GatewayError(message);
85  }
86 
87  if (GatewayState::CONFIGURED != m_state) {
88  std::string message = "Activate was called on a gateway which is not in configured state. "
89  "Gateway ID: " + id();
90  log_error() << message;
91  throw GatewayError(message);
92  }
93 
94  if (!activateGateway()) {
95  log_error() << "Couldn't activate gateway: " << id();
96  return false;
97  }
98 
99  m_state = GatewayState::ACTIVATED;
100  return true;
101 }
virtual std::string id() const
Returns the ID of the gateway.
Definition: gateway.cpp:36

Here is the call graph for this function:

bool softwarecontainer::Gateway::teardown ( )
virtual

Restore system to the state prior to launching of gateway.

Any cleanup code (removal of files, virtual interfaces, etc) should be placed here.

Returns
true upon successful clean-up, false otherwise
Exceptions
GatewayErrorIf called on a non activated gateway.

Reimplemented in softwarecontainer::DBusGateway.

Definition at line 103 of file gateway.cpp.

References id().

Referenced by softwarecontainer::DBusGatewayInstance::DBusGatewayInstance().

103  {
104  /* At this point, a gateway should either be in state ACTIVATED if it is non-dynamic, or
105  if it is dynamic it should have been activated at least once before.
106  */
107  if (GatewayState::ACTIVATED != m_state && !m_activatedOnce) {
108  std::string message = "Teardown called on non-activated gateway. Gateway ID: " + id();
109  log_error() << message;
110  throw GatewayError(message);
111  }
112 
113  if (!teardownGateway()) {
114  log_error() << "Could not tear down gateway: " << id();
115  return false;
116  }
117 
118  // Return to a state of nothingness
119  m_state = GatewayState::CREATED;
120 
121  /* Since we have been torn down, we should not be considered to have been
122  activated any more. */
123  m_activatedOnce = false;
124 
125  return true;
126 }
virtual std::string id() const
Returns the ID of the gateway.
Definition: gateway.cpp:36

Here is the call graph for this function:

Here is the caller graph for this function:

bool softwarecontainer::Gateway::isConfigured ( )
virtual

Is the gateway configured or not?

Reimplemented in softwarecontainer::DBusGateway.

Definition at line 134 of file gateway.cpp.

135 {
136  return m_state >= GatewayState::CONFIGURED;
137 }
bool softwarecontainer::Gateway::isActivated ( )
virtual

Is the gateway activated or not?

Dynamic gateways will return true if they have been activated at least once. Non-dynamic gateways will return true if they are in state ACTIVATED

Reimplemented in softwarecontainer::DBusGateway.

Definition at line 139 of file gateway.cpp.

Referenced by softwarecontainer::DBusGatewayInstance::DBusGatewayInstance().

140 {
141  // For dynamic gateways it's only relevant to know if it has been activated
142  // at least once, the current state is not important
143  if (m_isDynamic) {
144  return m_activatedOnce;
145  }
146 
147  // For non-dynamic gateways, the current state is the only relevant info
148  return m_state >= GatewayState::ACTIVATED;
149 }

Here is the caller graph for this function:

virtual bool softwarecontainer::Gateway::readConfigElement ( const json_t *  element)
protectedpure virtual

Gateway specific parsing of config elements.

All gateways implement this method in order to provide gateway specific parsing of the configuration content.

Parameters
elementA JSON configuration item.
Returns
false if an error was encountered while parsing, true otherwise.

Implemented in softwarecontainer::NetworkGateway, softwarecontainer::DeviceNodeGateway, softwarecontainer::DBusGatewayInstance, softwarecontainer::DBusGateway, softwarecontainer::FileGateway, softwarecontainer::CgroupsGateway, softwarecontainer::PulseGateway, softwarecontainer::WaylandGateway, and softwarecontainer::EnvironmentGateway.

Referenced by setConfig().

Here is the caller graph for this function:

std::shared_ptr< ContainerAbstractInterface > softwarecontainer::Gateway::getContainer ( )
protected

Get a handle to the associated container.

Exceptions
GatewayErrorIf called before setContainer() has been called.

Definition at line 128 of file gateway.cpp.

Referenced by softwarecontainer::NetworkGateway::activateGateway(), softwarecontainer::DBusGatewayInstance::activateGateway(), softwarecontainer::WaylandGateway::readConfigElement(), and softwarecontainer::NetworkGateway::teardownGateway().

129 {
130  std::shared_ptr<ContainerAbstractInterface> ptrCopy = m_container;
131  return ptrCopy;
132 }

Here is the caller graph for this function:

bool softwarecontainer::Gateway::setEnvironmentVariable ( const std::string &  variable,
const std::string &  value 
)
protected

Set an environment variable in the associated container.


The documentation for this class was generated from the following files: