From bfe3b02646e0b34562b28a3771f19b8a265f0856 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 25 Oct 2017 20:41:57 +0100 Subject: Improve exception usage around configuration lookups and simplify that code --- netfs/daemon/Jamfile.jam | 5 ++++- netfs/daemon/daemon.cpp | 7 ++----- netfs/daemon/daemonConfig.ice | 7 +++++++ netfs/daemon/daemonConfigImpl.cpp | 10 ++++++++++ netfs/daemon/daemonService.cpp | 11 ++++------- netfs/fuse/Jamfile.jam | 5 ++++- netfs/fuse/fuseApp.cpp | 8 ++------ netfs/fuse/fuseConfig.ice | 7 +++++++ netfs/fuse/fuseConfigImpl.cpp | 10 ++++++++++ netfs/ice/exceptions.ice | 5 +++++ netfs/ice/exceptionsImpl.cpp | 10 ++++++++++ 11 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 netfs/daemon/daemonConfigImpl.cpp create mode 100644 netfs/fuse/fuseConfigImpl.cpp create mode 100644 netfs/ice/exceptionsImpl.cpp diff --git a/netfs/daemon/Jamfile.jam b/netfs/daemon/Jamfile.jam index c4ae90b..9697e75 100644 --- a/netfs/daemon/Jamfile.jam +++ b/netfs/daemon/Jamfile.jam @@ -2,7 +2,10 @@ import package ; lib netfsd-configuration : daemonConfig.ice + daemonConfigImpl.cpp : + ../ice//netfs-api + ../ice//netfs-api ..//Ice ..//IceUtil ..//pthread @@ -20,7 +23,7 @@ lib netfsd-configuration : ; lib netfsd : - [ glob *.cpp ] + [ glob *.cpp : daemonConfigImpl.cpp ] : _FILE_OFFSET_BITS=64 ../ice//netfs-api diff --git a/netfs/daemon/daemon.cpp b/netfs/daemon/daemon.cpp index d5af905..8ab7bfe 100644 --- a/netfs/daemon/daemon.cpp +++ b/netfs/daemon/daemon.cpp @@ -5,6 +5,7 @@ #include #include "modeCheck.h" #include +#include NetFSDaemon::NetFSDaemon() { @@ -55,11 +56,7 @@ NetFSDaemon::LoadConfiguration(const boost::filesystem::path & path) { dc = new NetFS::Daemon::RuntimeConfiguration(); dc->CurrentConfiguration = ReadConfiguration(path); - auto selfItr = dc->CurrentConfiguration->Hosts.find(hostname()); - if (selfItr == dc->CurrentConfiguration->Hosts.end()) { - throw std::runtime_error("This host is not defined in the configuration."); - } - dc->Self = selfItr->second; + dc->Self = AdHoc::safeMapLookup(dc->CurrentConfiguration->Hosts, hostname()); } void diff --git a/netfs/daemon/daemonConfig.ice b/netfs/daemon/daemonConfig.ice index ac438fb..2ae38e2 100644 --- a/netfs/daemon/daemonConfig.ice +++ b/netfs/daemon/daemonConfig.ice @@ -1,3 +1,5 @@ +#include + module NetFS { module Daemon { local class Host { @@ -33,5 +35,10 @@ module NetFS { Configuration CurrentConfiguration; Host Self; }; + + ["cpp:ice_print"] + exception HostNotConfigured extends ConfigError { + string hostName; + }; }; }; diff --git a/netfs/daemon/daemonConfigImpl.cpp b/netfs/daemon/daemonConfigImpl.cpp new file mode 100644 index 0000000..48b1d0e --- /dev/null +++ b/netfs/daemon/daemonConfigImpl.cpp @@ -0,0 +1,10 @@ +#include +#include + +AdHocFormatter(HostNotConfiguredMsg, "This host [%?] is not defined in the configuration"); +void +NetFS::Daemon::HostNotConfigured::ice_print(std::ostream & s) const +{ + HostNotConfiguredMsg::write(s, hostName); +} + diff --git a/netfs/daemon/daemonService.cpp b/netfs/daemon/daemonService.cpp index be376a0..20082f1 100644 --- a/netfs/daemon/daemonService.cpp +++ b/netfs/daemon/daemonService.cpp @@ -1,6 +1,7 @@ #include "daemon.h" #include "daemonService.h" #include "daemonVolume.h" +#include ServiceServer::ServiceServer(NetFS::Daemon::ConfigurationPtr c) : config(c) @@ -10,15 +11,11 @@ ServiceServer::ServiceServer(NetFS::Daemon::ConfigurationPtr c) : NetFS::VolumePrx ServiceServer::connect(const std::string & share, const std::string & authtoken, const Ice::Current & ice) { - //boost::lock_guard lg(lock); - NetFS::Daemon::ExportMap::iterator e = config->Exports.find(share); - if (e == config->Exports.end()) { - throw NetFS::ConfigError(); - } - if (!e->second->AuthToken.empty() && e->second->AuthToken != authtoken) { + auto ex = AdHoc::safeMapLookup(config->Exports, share); + if (!ex->AuthToken.empty() && ex->AuthToken != authtoken) { throw NetFS::AuthError(); } return NetFS::VolumePrx::uncheckedCast(ice.adapter->addFacetWithUUID( - new VolumeServer(e->second->RootPath, userLookup, groupLookup), "v01")); + new VolumeServer(ex->RootPath, userLookup, groupLookup), "v01")); } diff --git a/netfs/fuse/Jamfile.jam b/netfs/fuse/Jamfile.jam index b17ca0b..9df3e89 100644 --- a/netfs/fuse/Jamfile.jam +++ b/netfs/fuse/Jamfile.jam @@ -5,8 +5,11 @@ lib Glacier2 : : Glacier2 ; lib netfs-client-configuration : fuseConfig.ice + fuseConfigImpl.cpp : yes + ../ice//netfs-api + ../ice//netfs-api ..//Ice ..//IceUtil ..//pthread @@ -21,7 +24,7 @@ lib netfs-client-configuration : lib netfs-client : netfs-client-configuration - [ glob *.cpp : netfs.cpp ] + [ glob *.cpp : fuseConfigImpl.cpp netfs.cpp ] : _FILE_OFFSET_BITS=64 ../ice//netfs-api diff --git a/netfs/fuse/fuseApp.cpp b/netfs/fuse/fuseApp.cpp index a998f94..b980af8 100644 --- a/netfs/fuse/fuseApp.cpp +++ b/netfs/fuse/fuseApp.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace AdHoc { template class Cache; @@ -82,12 +83,7 @@ NetFS::FuseApp::init(struct fuse_conn_info *) NetFS::Client::ResourcePtr NetFS::FuseApp::configureFromFile(const std::string & configPath, const std::string & resourceName) const { - auto fc = ReadConfiguration(configPath); - auto e = fc->Resources.find(resourceName); - if (e == fc->Resources.end()) { - throw std::runtime_error("No such resource: " + resourceName); - } - return e->second; + return AdHoc::safeMapLookup(ReadConfiguration(configPath)->Resources, resourceName); } NetFS::Client::ResourcePtr diff --git a/netfs/fuse/fuseConfig.ice b/netfs/fuse/fuseConfig.ice index 91740fc..3e17017 100644 --- a/netfs/fuse/fuseConfig.ice +++ b/netfs/fuse/fuseConfig.ice @@ -1,3 +1,5 @@ +#include + module NetFS { module Client { ["slicer:element:endpoint"] @@ -25,6 +27,11 @@ module NetFS { ["slicer:name:resources"] ResourceMap Resources; }; + + ["cpp:ice_print"] + exception ResourceNotFound extends ConfigError { + string resourceName; + }; }; }; diff --git a/netfs/fuse/fuseConfigImpl.cpp b/netfs/fuse/fuseConfigImpl.cpp new file mode 100644 index 0000000..9129bfc --- /dev/null +++ b/netfs/fuse/fuseConfigImpl.cpp @@ -0,0 +1,10 @@ +#include +#include + +AdHocFormatter(ResourceNotFoundMsg, "No such resource: %?"); +void +NetFS::Client::ResourceNotFound::ice_print(std::ostream & s) const +{ + ResourceNotFoundMsg::write(s, resourceName); +} + diff --git a/netfs/ice/exceptions.ice b/netfs/ice/exceptions.ice index 9d5b970..02a42e0 100644 --- a/netfs/ice/exceptions.ice +++ b/netfs/ice/exceptions.ice @@ -12,6 +12,11 @@ module NetFS { exception ConfigError { }; + + ["cpp:ice_print"] + exception ExportNotFound extends ConfigError { + string exportName; + }; }; #endif diff --git a/netfs/ice/exceptionsImpl.cpp b/netfs/ice/exceptionsImpl.cpp new file mode 100644 index 0000000..93f5930 --- /dev/null +++ b/netfs/ice/exceptionsImpl.cpp @@ -0,0 +1,10 @@ +#include +#include + +AdHocFormatter(ExportNotFoundMsg, "Export [%?] not found on remote host"); +void +NetFS::ExportNotFound::ice_print(std::ostream & s) const +{ + ExportNotFoundMsg::write(s, exportName); +} + -- cgit v1.2.3