forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request networkupstools#1034 from jimklimov/cpp-TcpClient-…
…mock
- Loading branch information
Showing
11 changed files
with
527 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
/* nutclientmem.cpp - nutclientmem C++ library implementation | ||
Copyright (C) 2021 Eric Clappier <[email protected]> | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
#include "nutclientmem.h" | ||
#include <common.h> | ||
|
||
namespace nut | ||
{ | ||
|
||
/* | ||
* | ||
* Memory Client stub implementation | ||
* | ||
*/ | ||
|
||
Device MemClientStub::getDevice(const std::string& name) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
std::set<std::string> MemClientStub::getDeviceNames() | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
std::string MemClientStub::getDeviceDescription(const std::string& name) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
std::set<std::string> MemClientStub::getDeviceVariableNames(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
std::set<std::string> MemClientStub::getDeviceRWVariableNames(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
std::string MemClientStub::getDeviceVariableDescription(const std::string& dev, const std::string& name) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
ListValue MemClientStub::getDeviceVariableValue(const std::string& dev, const std::string& name) | ||
{ | ||
ListValue res; | ||
auto it_dev = _values.find(dev); | ||
if (it_dev != _values.end()) | ||
{ | ||
auto map = it_dev->second; | ||
auto it_map = map.find(name); | ||
if (it_map != map.end()) | ||
{ | ||
res = it_map->second; | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
ListObject MemClientStub::getDeviceVariableValues(const std::string& dev) | ||
{ | ||
ListObject res; | ||
auto it_dev = _values.find(dev); | ||
if (it_dev != _values.end()) | ||
{ | ||
res = it_dev->second; | ||
} | ||
return res; | ||
} | ||
|
||
ListDevice MemClientStub::getDevicesVariableValues(const std::set<std::string>& devs) | ||
{ | ||
ListDevice res; | ||
for (auto itr = devs.begin(); itr != devs.end(); itr++) | ||
{ | ||
std::string dev = *itr; | ||
auto it_dev = _values.find(dev); | ||
if (it_dev != _values.end()) | ||
{ | ||
res.insert(std::pair<std::string, ListObject>(dev, it_dev->second)); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
TrackingID MemClientStub::setDeviceVariable(const std::string& dev, const std::string& name, const std::string& value) | ||
{ | ||
auto it_dev = _values.find(dev); | ||
if (it_dev == _values.end()) | ||
{ | ||
ListObject list; | ||
_values.emplace(dev, list); | ||
it_dev = _values.find(dev); | ||
} | ||
if (it_dev != _values.end()) | ||
{ | ||
auto map = &(it_dev->second); | ||
auto it_map = map->find(name); | ||
if (it_map != map->end()) | ||
{ | ||
it_map->second[0] = value; | ||
} | ||
else | ||
{ | ||
ListValue list_value; | ||
list_value.push_back(value); | ||
map->emplace(name, list_value); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
TrackingID MemClientStub::setDeviceVariable(const std::string& dev, const std::string& name, const ListValue& values) | ||
{ | ||
auto it_dev = _values.find(dev); | ||
if (it_dev != _values.end()) | ||
{ | ||
auto map = &(it_dev->second); | ||
auto it_map = map->find(name); | ||
if (it_map != map->end()) | ||
{ | ||
it_map->second = values; | ||
} | ||
else | ||
{ | ||
map->emplace(name, values); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
std::set<std::string> MemClientStub::getDeviceCommandNames(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
std::string MemClientStub::getDeviceCommandDescription(const std::string& dev, const std::string& name) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
TrackingID MemClientStub::executeDeviceCommand(const std::string& dev, const std::string& name, const std::string& param) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
void MemClientStub::deviceLogin(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
void MemClientStub::deviceMaster(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
void MemClientStub::deviceForcedShutdown(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
int MemClientStub::deviceGetNumLogins(const std::string& dev) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
TrackingResult MemClientStub::getTrackingResult(const TrackingID& id) | ||
{ | ||
throw NutException("Not implemented"); | ||
//return TrackingResult::SUCCESS; | ||
} | ||
|
||
bool MemClientStub::isFeatureEnabled(const Feature& feature) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
void MemClientStub::setFeature(const Feature& feature, bool status) | ||
{ | ||
throw NutException("Not implemented"); | ||
} | ||
|
||
} /* namespace nut */ | ||
|
||
/** | ||
* C nutclient API. | ||
*/ | ||
extern "C" { | ||
|
||
NUTCLIENT_MEM_t nutclient_mem_create_client() | ||
{ | ||
nut::MemClientStub* client = new nut::MemClientStub; | ||
try | ||
{ | ||
return static_cast<NUTCLIENT_MEM_t>(client); | ||
} | ||
catch(nut::NutException& ex) | ||
{ | ||
// TODO really catch it | ||
NUT_UNUSED_VARIABLE(ex); | ||
delete client; | ||
return nullptr; | ||
} | ||
} | ||
|
||
} /* extern "C" */ | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* nutclientmem.h - definitions for nutclientmem C/C++ library | ||
Copyright (C) 2021 Eric Clappier <[email protected]> | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
#ifndef NUTCLIENTMEM_HPP_SEEN | ||
#define NUTCLIENTMEM_HPP_SEEN | ||
|
||
/* Begin of C++ nutclient library declaration */ | ||
#ifdef __cplusplus | ||
|
||
#include "nutclient.h" | ||
|
||
namespace nut | ||
{ | ||
|
||
typedef std::vector<std::string> ListValue; | ||
typedef std::map<std::string, ListValue> ListObject; | ||
typedef std::map<std::string, ListObject> ListDevice; | ||
|
||
/** | ||
* Memory client stub. | ||
* Class to stub TCPClient for test (data store in local memory). | ||
*/ | ||
class MemClientStub : public Client | ||
{ | ||
public: | ||
/** | ||
* Construct a nut MemClientStub object. | ||
*/ | ||
MemClientStub() {}; | ||
~MemClientStub() {}; | ||
|
||
virtual void authenticate(const std::string& user, const std::string& passwd) {}; | ||
virtual void logout() {}; | ||
|
||
virtual Device getDevice(const std::string& name); | ||
virtual std::set<std::string> getDeviceNames(); | ||
virtual std::string getDeviceDescription(const std::string& name); | ||
|
||
virtual std::set<std::string> getDeviceVariableNames(const std::string& dev); | ||
virtual std::set<std::string> getDeviceRWVariableNames(const std::string& dev); | ||
virtual std::string getDeviceVariableDescription(const std::string& dev, const std::string& name); | ||
virtual ListValue getDeviceVariableValue(const std::string& dev, const std::string& name); | ||
virtual ListObject getDeviceVariableValues(const std::string& dev); | ||
virtual ListDevice getDevicesVariableValues(const std::set<std::string>& devs); | ||
virtual TrackingID setDeviceVariable(const std::string& dev, const std::string& name, const std::string& value); | ||
virtual TrackingID setDeviceVariable(const std::string& dev, const std::string& name, const ListValue& values); | ||
|
||
virtual std::set<std::string> getDeviceCommandNames(const std::string& dev); | ||
virtual std::string getDeviceCommandDescription(const std::string& dev, const std::string& name); | ||
virtual TrackingID executeDeviceCommand(const std::string& dev, const std::string& name, const std::string& param=""); | ||
|
||
virtual void deviceLogin(const std::string& dev); | ||
virtual void deviceMaster(const std::string& dev); | ||
virtual void deviceForcedShutdown(const std::string& dev); | ||
virtual int deviceGetNumLogins(const std::string& dev); | ||
|
||
virtual TrackingResult getTrackingResult(const TrackingID& id); | ||
|
||
virtual bool isFeatureEnabled(const Feature& feature); | ||
virtual void setFeature(const Feature& feature, bool status); | ||
|
||
private: | ||
ListDevice _values; | ||
}; | ||
|
||
} /* namespace nut */ | ||
|
||
#endif /* __cplusplus */ | ||
/* End of C++ nutclient library declaration */ | ||
|
||
/* Begin of C nutclient library declaration */ | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif /* __cplusplus */ | ||
|
||
/** | ||
* Nut MEM client dedicated types and functions | ||
*/ | ||
/** | ||
* Hidden structure representing a MEM connection. | ||
* NUTCLIENT_MEM_t is back compatible to NUTCLIENT_t. | ||
*/ | ||
typedef NUTCLIENT_t NUTCLIENT_MEM_t; | ||
|
||
/** | ||
* Create a client to NUTD using memory. | ||
* \return New client or nullptr if failed. | ||
*/ | ||
NUTCLIENT_MEM_t nutclient_mem_create_client(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif /* __cplusplus */ | ||
/* End of C nutclient library declaration */ | ||
|
||
#endif /* NUTCLIENTMOCK_HPP_SEEN */ |
Oops, something went wrong.