Skip to content

Commit

Permalink
Merge pull request networkupstools#1034 from jimklimov/cpp-TcpClient-…
Browse files Browse the repository at this point in the history
…mock
  • Loading branch information
jimklimov authored May 24, 2021
2 parents 9ea2aa2 + 46a921f commit ea48a46
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 5 deletions.
12 changes: 11 additions & 1 deletion clients/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ sbin_PROGRAMS = upsmon upssched
lib_LTLIBRARIES = libupsclient.la
if HAVE_CXX11
lib_LTLIBRARIES += libnutclient.la
lib_LTLIBRARIES += libnutclientstub.la
endif
if WITH_DEV
include_HEADERS = upsclient.h ../include/parseconf.h nutclient.h
include_HEADERS = upsclient.h ../include/parseconf.h nutclient.h nutclientmem.h
endif
if WITH_CGI
cgiexec_PROGRAMS = upsstats.cgi upsimage.cgi upsset.cgi
Expand Down Expand Up @@ -78,3 +79,12 @@ libnutclient_la_LIBADD = $(top_builddir)/common/libcommonclient.la
else
EXTRA_DIST += nutclient.h nutclient.cpp
endif

if HAVE_CXX11
# libnutclientstub version information and build
libnutclientstub_la_SOURCES = nutclientmem.h nutclientmem.cpp
libnutclientstub_la_LDFLAGS = -version-info 1:0:0
libnutclientstub_la_LIBADD = libnutclient.la
else
EXTRA_DIST += nutclientmem.h nutclientmem.cpp
endif
4 changes: 3 additions & 1 deletion clients/nutclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ class TcpClient : public Client
internal::Socket* _socket;
};


/**
* Device attached to a client.
* Device is a lightweight class which can be copied easily.
Expand All @@ -475,6 +474,7 @@ class Device
{
friend class Client;
friend class TcpClient;
friend class TcpClientMock;
#ifdef _NUTCLIENTTEST_BUILD
friend class NutClientTest;
#endif
Expand Down Expand Up @@ -630,6 +630,7 @@ class Variable
{
friend class Device;
friend class TcpClient;
friend class TcpClientMock;
#ifdef _NUTCLIENTTEST_BUILD
friend class NutClientTest;
#endif
Expand Down Expand Up @@ -713,6 +714,7 @@ class Command
{
friend class Device;
friend class TcpClient;
friend class TcpClientMock;
#ifdef _NUTCLIENTTEST_BUILD
friend class NutClientTest;
#endif
Expand Down
226 changes: 226 additions & 0 deletions clients/nutclientmem.cpp
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" */



112 changes: 112 additions & 0 deletions clients/nutclientmem.h
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 */
Loading

0 comments on commit ea48a46

Please sign in to comment.