Skip to content

Commit

Permalink
Update based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Sep 26, 2024
1 parent bb0c2b1 commit 9cf00eb
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 66 deletions.
3 changes: 2 additions & 1 deletion Source/Thunder/IRemoteInstantiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace PluginHost {
struct IRemoteInstantiation : virtual public Core::IUnknown {

enum { ID = RPC::IDS::ID_REMOTE_INSTANTIATION };
using IEnvironmentIterator = RPC::IIteratorType<RPC::Object::Environment, RPC::ID_ENVIRONMENT_ITERATOR>;

~IRemoteInstantiation() override = default;

Expand Down Expand Up @@ -57,7 +58,7 @@ namespace PluginHost {
const uint8_t threads,
const int8_t priority,
const string configuration,
const std::vector<string>& environments) = 0;
IEnvironmentIterator* const& environments) = 0;
};
}
}
82 changes: 57 additions & 25 deletions Source/Thunder/PluginServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ namespace PluginHost {
DYNAMIC
};

using Services = std::unordered_map<string, Service*>;

private:
using BaseClass = PluginHost::Service;
using Jobs = Core::ThrottleQueueType<Core::ProxyType<Core::IDispatch>, ServiceMap&>;
Expand Down Expand Up @@ -1277,37 +1279,33 @@ namespace PluginHost {
string Substitute(const string& input) const override {
return (_administrator.Configuration().Substitute(input, PluginHost::Service::Configuration()));
}
std::vector<string> SubstituteList(const string& envs) const override {

std::vector<string> environmentList;;
Plugin::Config::EnvironmentList environments;
environments.FromString(envs);
if (environments.IsSet() == true) {
Core::JSON::ArrayType<Plugin::Config::Environment>::Iterator index(environments.Elements());
while (index.Next() == true) {
if ((index.Current().Key.IsSet() == true) && (index.Current().Value.IsSet() == true)) {
index.Current().Value = Substitute(index.Current().Value.Value());
string env = index.Current().Key.Value() + Plugin::Config::EnvFieldSeparator +
index.Current().Value.Value() + Plugin::Config::EnvFieldSeparator +
((index.Current().Override.Value() == true) ? "1" : "0");
environmentList.push_back(env);
} else {
SYSLOG(Logging::Startup, (_T("Failure in Substituting Value of Key:Value:[%s]:[%s]\n"), index.Current().Key.Value().c_str(), index.Current().Value.Value().c_str()));
}
}
}
return environmentList;
}
Core::hresult Metadata(string& info /* @out */) const override {
Metadata::Service result;
GetMetadata(result);
result.ToString(info);
return (Core::ERROR_NONE);
}
std::vector<RPC::Object::Environment> SubstituteList(const std::vector<RPC::Object::Environment>& environments) const {
std::vector<RPC::Object::Environment> environmentList;
for (auto& environment : environments) {
if ((environment.key.empty() != true) && (environment.value.empty() != true)) {
RPC::Object::Environment env;
env.value = Substitute(environment.value);
env.key = environment.key;
env.overriding = environment.overriding;
environmentList.push_back(environment);
} else {
SYSLOG(Logging::Startup, (_T("Failure in Substituting Value of Key:Value:[%s]:[%s]\n"), environment.key.c_str(), environment.value.c_str()));
}
}
return environmentList;
}
void* Instantiate(const RPC::Object& object, const uint32_t waitTime, uint32_t& sessionId) override
{
ASSERT(_connection == nullptr);

const_cast<RPC::Object&>(object).Environments(SubstituteList(object.Environments()));

void* result(_administrator.Instantiate(object, waitTime, sessionId, DataPath(), PersistentPath(), VolatilePath(), _administrator.Configuration().LinkerPluginPaths()));

if (result != nullptr) {
Expand Down Expand Up @@ -1536,7 +1534,7 @@ namespace PluginHost {
SystemRootPath(),
PluginHost::Service::Configuration().Root.RemoteAddress.Value(),
PluginHost::Service::Configuration().Root.Configuration.Value(),
SubstituteList(environments));
Plugin::Config::Environment::List(PluginHost::Service::Configuration().Root.Environments));

newIF = reinterpret_cast<IPlugin*>(Instantiate(definition, _administrator.Configuration().OutOfProcessWaitTime(), pid));
if (newIF == nullptr) {
Expand Down Expand Up @@ -2031,6 +2029,10 @@ namespace PluginHost {
if (instantiation == nullptr) {
result = Core::ERROR_ILLEGAL_STATE;
} else {

using Iterator = IRemoteInstantiation::IEnvironmentIterator;
Iterator* environment = Core::Service<RPC::IteratorType<Iterator>>::Create<Iterator>(_object.Environments());

result = instantiation->Instantiate(
RPC::Communicator::RemoteConnection::Id(),
_object.Locator(),
Expand All @@ -2044,7 +2046,7 @@ namespace PluginHost {
_object.Threads(),
_object.Priority(),
_object.Configuration(),
_object.Environments());
environment);

instantiation->Release();
}
Expand Down Expand Up @@ -2425,7 +2427,7 @@ namespace PluginHost {
const uint8_t threads,
const int8_t priority,
const string configuration,
const std::vector<string>& environments) override
IRemoteInstantiation::IEnvironmentIterator* const& environments) override
{
string persistentPath(_comms.PersistentPath());
string dataPath(_comms.DataPath());
Expand All @@ -2437,9 +2439,17 @@ namespace PluginHost {
volatilePath += callsign + '/';
}

std::vector<RPC::Object::Environment> _environmentList;
if (environments != nullptr) {
RPC::Object::Environment environment;
while (environments->Next(environment) == true) {
_environmentList.push_back(environment);
}
}

uint32_t id;
RPC::Config config(_connector, _comms.Application(), persistentPath, _comms.SystemPath(), dataPath, volatilePath, _comms.AppPath(), _comms.ProxyStubPath(), _comms.PostMortemPath(), _comms.LinkerPaths());
RPC::Object instance(libraryName, className, callsign, interfaceId, version, user, group, threads, priority, RPC::Object::HostType::LOCAL, systemRootPath, _T(""), configuration, environments);
RPC::Object instance(libraryName, className, callsign, interfaceId, version, user, group, threads, priority, RPC::Object::HostType::LOCAL, systemRootPath, _T(""), configuration, _environmentList);

RPC::Communicator::Process process(requestId, config, instance);

Expand All @@ -2450,6 +2460,17 @@ namespace PluginHost {
INTERFACE_ENTRY(IRemoteInstantiation)
END_INTERFACE_MAP

private:
std::vector<RPC::Object::Environment> SubstituteList(const string& callsign, const std::vector<RPC::Object::Environment>& environments) const
{
std::vector<RPC::Object::Environment> substitutedList;
Core::ProxyType<Service> service = _parent.GetService(callsign);
if (service.IsValid() == true) {
substitutedList = service->SubstituteList(environments);
}
return substitutedList;
}

private:
mutable uint32_t _refCount;
ServiceMap& _parent;
Expand Down Expand Up @@ -3090,6 +3111,17 @@ namespace PluginHost {

_adminLock.Unlock();
}
inline Core::ProxyType<Service> GetService(const string& callsign)
{
Core::ProxyType<Service> service;
for (std::pair<const string, Core::ProxyType<Service>>& entry : _services) {
if (entry.first == callsign) {
service = entry.second;
break;
}
}
return service;
}
inline Iterator Services()
{
Shells workingList;
Expand Down
41 changes: 15 additions & 26 deletions Source/ThunderPlugin/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ POP_WARNING()
class ConsoleOptions : public Core::Options {
public:
ConsoleOptions(int argumentCount, TCHAR* arguments[])
: Core::Options(argumentCount, arguments, _T("h:l:c:C:r:p:s:d:a:m:i:u:g:t:e:x:V:v:P:S:f:"))
: Core::Options(argumentCount, arguments, _T("h:l:c:C:r:p:s:d:a:m:i:u:g:t:e:E:x:V:v:P:S:f:"))
, Locator(nullptr)
, ClassName(nullptr)
, Callsign(nullptr)
Expand Down Expand Up @@ -202,7 +202,7 @@ POP_WARNING()
string ProxyStubPath;
string PostMortemPath;
string SystemRootPath;
std::vector<string> Environments;
std::vector<RPC::Object::Environment> Environments;
const TCHAR* User;
const TCHAR* Group;
uint8_t Threads;
Expand Down Expand Up @@ -252,8 +252,12 @@ POP_WARNING()
SystemRootPath = Core::Directory::Normalize(Strip(argument));
break;
case 'e':
Environments.push_back(Strip(argument));
case 'E': {
Environments.push_back(Plugin::Config::Environment::Info(Strip(argument),
((option == 'E') ? RPC::Object::Environment::Scope::GLOBAL :
RPC::Object::Environment::Scope::LOCAL)));
break;
}
case 'v':
VolatilePath = Core::Directory::Normalize(Strip(argument));
break;
Expand Down Expand Up @@ -592,7 +596,8 @@ int main(int argc, char** argv)
printf(" [-d <data path>]\n");
printf(" [-v <volatile path>]\n");
printf(" [-f <linker_path>...\n");
printf(" [-e <environment values>...]\n\n");
printf(" [-e/-E <environment values>...]\n");
printf(" e: means set as local scope, E: means set as global scope\n");
printf(" [-a <app path>]\n");
printf(" [-m <proxy stub library path>]\n");
printf(" [-P <post mortem path>]\n\n");
Expand Down Expand Up @@ -669,28 +674,12 @@ int main(int argc, char** argv)
Core::ProcessCurrent().User(string(options.User));
}

for (const string& env : options.Environments) {
size_t start = env.find_first_of(Plugin::Config::EnvFieldSeparator);
if ((start != string::npos) && (start < env.length())) {
size_t end = env.find_last_of(Plugin::Config::EnvFieldSeparator);
if ((end != string::npos) && (start != end)) {
string key = env.substr(0, start);
string value = env.substr(start + 1, end - (start + 1));
string overrideStr = env.substr(end + 1);

bool toOverride = ((overrideStr.size() == 1) && (overrideStr == "1")) ? true: false;

if ((key.empty() != true) && (value.empty() != true)) {
uint32_t status = Core::SystemInfo::SetEnvironment(key, value.c_str(), toOverride);
if (status != true) {
SYSLOG(Logging::Startup, (_T("Failure in setting Key:Value:[%s]:[%s]\n"), key.c_str(), value.c_str()));
}
} else {
SYSLOG(Logging::Startup, (_T("Environment key:value fromat is invalid :[%s]:[%s]\n"), key.c_str(), value.c_str()));
}
} else {
SYSLOG(Logging::Startup, (_T("Invalid Enviroment value :[%s]\n"), env.c_str()));
}
for (const auto& info : options.Environments) {
if ((info.key.empty() != true) && (info.value.empty() != true)) {
uint32_t status = Core::SystemInfo::SetEnvironment(info.key, info.value.c_str(), ((info.overriding == RPC::Object::Environment::Scope::GLOBAL) ? true : false));
if (status != true) {
SYSLOG(Logging::Startup, (_T("Failure in setting Key:Value:[%s]:[%s]\n"), info.key.c_str(), info.value.c_str()));
}
}
}

Expand Down
27 changes: 22 additions & 5 deletions Source/com/Communicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,23 @@ namespace RPC {

class EXTERNAL Object {
public:
static constexpr const TCHAR EnvironmentSeparator = _T(';');
enum class HostType {
LOCAL,
DISTRIBUTED,
CONTAINER
};

struct Environment {
enum class Scope {
LOCAL,
GLOBAL
};
string key;
string value;
Scope overriding;
};

Object()
: _locator()
, _className()
Expand Down Expand Up @@ -115,7 +126,7 @@ namespace RPC {
const string& systemRootPath,
const string& remoteAddress,
const string& configuration,
const std::vector<string>& environments)
const std::vector<Environment>& environments)
: _locator(locator)
, _className(className)
, _callsign(callsign)
Expand Down Expand Up @@ -235,10 +246,13 @@ namespace RPC {
{
return (_configuration);
}
inline const std::vector<string>& Environments() const
inline const std::vector<Environment>& Environments() const
{
return (_environments);
}
inline void Environments(const std::vector<Environment>& environments) {
_environments = environments;
}

private:
string _locator;
Expand All @@ -254,7 +268,7 @@ namespace RPC {
string _systemRootPath;
string _remoteAddress;
string _configuration;
std::vector<string> _environments;
std::vector<Environment> _environments;
};

class EXTERNAL Config {
Expand Down Expand Up @@ -527,8 +541,11 @@ namespace RPC {
if (instance.Threads() > 1) {
_options.Add(_T("-t")).Add(Core::NumberType<uint8_t>(instance.Threads()).Text());
}
for (auto const& env : instance.Environments()) {
_options.Add(_T("-e")).Add('"' + env + '"');
for (auto const& environment : instance.Environments()) {
string env = environment.key + RPC::Object::EnvironmentSeparator +
"\"" + environment.value + "\"";
string option = (environment.overriding == RPC::Object::Environment::Scope::GLOBAL) ? "E" : "e";
_options.Add(_T(option)).Add('"' + env + '"');
}
_priority = instance.Priority();
}
Expand Down
1 change: 1 addition & 0 deletions Source/com/Ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace RPC {
ID_SUBSYSTEM_DECRYPTION = (ID_OFFSET_INTERNAL + 0x003E),
ID_REMOTE_INSTANTIATION = (ID_OFFSET_INTERNAL + 0x003F),
ID_SYSTEM_METADATA = (ID_OFFSET_INTERNAL + 0x0040),
ID_ENVIRONMENT_ITERATOR = (ID_OFFSET_INTERNAL + 0x0041),

ID_EXTERNAL_INTERFACE_OFFSET = (ID_OFFSET_INTERNAL + 0x0080),
ID_EXTERNAL_QA_INTERFACE_OFFSET = (ID_OFFSET_INTERNAL + 0xA000)
Expand Down
46 changes: 43 additions & 3 deletions Source/plugins/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Plugin {
: Core::JSON::Container()
, Key()
, Value()
, Override(false)
, Override(RPC::Object::Environment::Scope::LOCAL)
{
Add(_T("key"), &Key);
Add(_T("value"), &Value);
Expand Down Expand Up @@ -85,14 +85,54 @@ namespace Plugin {

return (*this);
}
static std::vector<RPC::Object::Environment> List(const Core::JSON::ArrayType<Environment>& environments)
{
std::vector<RPC::Object::Environment> environmentList;

if (environments.IsSet() == true) {
Core::JSON::ArrayType<Environment>::ConstIterator index(environments.Elements());
while (index.Next() == true) {
if ((index.Current().Key.IsSet() == true) && (index.Current().Value.IsSet() == true)) {
RPC::Object::Environment env;
env.key = index.Current().Key.Value();
env.value = index.Current().Value.Value();
env.overriding = index.Current().Override.Value();
environmentList.push_back(env);
} else {
SYSLOG(Logging::Startup, (_T("Failure in Substituting Value of Key:Value:[%s]:[%s]\n"), index.Current().Key.Value().c_str(), index.Current().Value.Value().c_str()));
}
}
}
return environmentList;
}
static RPC::Object::Environment Info(const string& env, RPC::Object::Environment::Scope overriding)
{
RPC::Object::Environment info;
size_t start = env.find_first_of(RPC::Object::EnvironmentSeparator);
if ((start != string::npos) && (start < env.length())) {
string key = env.substr(0, start);
string value = env.substr(start + 1);

if ((key.empty() != true) && (value.empty() != true) &&
((value.at(0) == '\"') && (value.at(value.length()) == '\"'))) {
info.key = key;
info.value = value.substr(1, value.length() - 1);
info.overriding = overriding;
} else {
SYSLOG(Logging::Startup, (_T("Environment key:value fromat is invalid :[%s]:[%s]\n"), key.c_str(), value.c_str()));
}
} else {
SYSLOG(Logging::Startup, (_T("Invalid Enviroment value :[%s]\n"), env.c_str()));
}
return info;
}

public:
Core::JSON::String Key;
Core::JSON::String Value;
Core::JSON::Boolean Override;
Core::JSON::EnumType<RPC::Object::Environment::Scope> Override;
};
using EnvironmentList = Core::JSON::ArrayType<Environment>;
static constexpr const TCHAR EnvFieldSeparator = _T(';');

class EXTERNAL RootConfig : public Core::JSON::Container {
private:
Expand Down
Loading

0 comments on commit 9cf00eb

Please sign in to comment.