Commit 1c503d75 authored by JINMEI Tatuya's avatar JINMEI Tatuya

constified lib/cc API as much as possible. (trac #310)


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac310@2748 e5f2f494-b856-4b98-b285-d166d9295462
parent 16b14671
......@@ -68,7 +68,7 @@ private:
public:
AuthSrvImpl(const bool use_cache, AbstractXfroutClient& xfrout_client);
~AuthSrvImpl();
isc::data::ElementPtr setDbFile(const isc::data::ElementPtr config);
isc::data::ConstElementPtr setDbFile(isc::data::ConstElementPtr config);
bool processNormalQuery(const IOMessage& io_message, Message& message,
MessageRenderer& response_renderer);
......@@ -426,7 +426,7 @@ AuthSrvImpl::processNotify(const IOMessage& io_message, Message& message,
static const string command_template_end = "\"}]}";
try {
ElementPtr notify_command = Element::fromJSON(
ConstElementPtr notify_command = Element::fromJSON(
command_template_start + question->getName().toText() +
command_template_master + remote_ip_address +
command_template_rrclass + question->getClass().toText() +
......@@ -434,7 +434,7 @@ AuthSrvImpl::processNotify(const IOMessage& io_message, Message& message,
const unsigned int seq =
xfrin_session_->group_sendmsg(notify_command, "Zonemgr",
"*", "*");
ElementPtr env, answer, parsed_answer;
ConstElementPtr env, answer, parsed_answer;
xfrin_session_->group_recvmsg(env, answer, false, seq);
int rcode;
parsed_answer = parseAnswer(rcode, answer);
......@@ -459,19 +459,17 @@ AuthSrvImpl::processNotify(const IOMessage& io_message, Message& message,
return (true);
}
ElementPtr
AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
ElementPtr answer = isc::config::createAnswer();
ElementPtr final;
ConstElementPtr
AuthSrvImpl::setDbFile(ConstElementPtr config) {
ConstElementPtr answer = isc::config::createAnswer();
if (config && config->contains("database_file")) {
db_file_ = config->get("database_file")->stringValue();
final = config;
} else if (config_session_ != NULL) {
bool is_default;
string item("database_file");
ElementPtr value = config_session_->getValue(is_default, item);
final = Element::createMap();
ConstElementPtr value = config_session_->getValue(is_default, item);
ElementPtr final = Element::createMap();
// If the value is the default, and we are running from
// a specific directory ('from build'), we need to use
......@@ -485,6 +483,7 @@ AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
"/bind10_zones.sqlite3");
}
final->set(item, value);
config = final;
db_file_ = value->stringValue();
} else {
......@@ -501,7 +500,7 @@ AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
// fail, while acquiring resources in the RAII manner. We then perform
// delete and swap operations which should not fail.
DataSrcPtr datasrc_ptr(DataSrcPtr(new Sqlite3DataSrc));
datasrc_ptr->init(final);
datasrc_ptr->init(config);
data_sources_.addDataSrc(datasrc_ptr);
// The following code should be exception free.
......@@ -513,15 +512,12 @@ AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
return (answer);
}
ElementPtr
AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
ConstElementPtr
AuthSrv::updateConfig(ConstElementPtr new_config) {
try {
// the ModuleCCSession has already checked if we have
// the correct ElementPtr type as specified in our .spec file
ElementPtr answer = isc::config::createAnswer();
answer = impl_->setDbFile(new_config);
return (answer);
return (impl_->setDbFile(new_config));
} catch (const isc::Exception& error) {
if (impl_->verbose_mode_) {
cerr << "[b10-auth] error: " << error.what() << endl;
......
......@@ -69,7 +69,7 @@ public:
isc::dns::MessageRenderer& response_renderer);
void setVerbose(bool on);
bool getVerbose() const;
isc::data::ElementPtr updateConfig(isc::data::ElementPtr config);
isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr config);
isc::config::ModuleCCSession* configSession() const;
void setConfigSession(isc::config::ModuleCCSession* config_session);
......
......@@ -66,19 +66,19 @@ AuthSrv *auth_server;
asio_link::IOService* io_service;
ElementPtr
my_config_handler(ElementPtr new_config) {
ConstElementPtr
my_config_handler(ConstElementPtr new_config) {
return (auth_server->updateConfig(new_config));
}
ElementPtr
my_command_handler(const string& command, const ElementPtr args) {
ElementPtr answer = createAnswer();
ConstElementPtr
my_command_handler(const string& command, ConstElementPtr args) {
ConstElementPtr answer = createAnswer();
if (command == "print_message") {
cout << args << endl;
/* let's add that message to our answer as well */
answer->get("result")->add(args);
answer = createAnswer(0, args);
} else if (command == "shutdown") {
io_service->stop();
}
......
......@@ -85,24 +85,25 @@ private:
{}
virtual void establish(const char* socket_file);
virtual void disconnect();
virtual int group_sendmsg(ElementPtr msg, string group,
virtual int group_sendmsg(ConstElementPtr msg, string group,
string instance, string to);
virtual bool group_recvmsg(ElementPtr& envelope, ElementPtr& msg,
virtual bool group_recvmsg(ConstElementPtr& envelope,
ConstElementPtr& msg,
bool nonblock, int seq);
virtual void subscribe(string group, string instance);
virtual void unsubscribe(string group, string instance);
virtual void startRead(boost::function<void()> read_callback);
virtual int reply(ElementPtr& envelope, ElementPtr& newmsg);
virtual bool hasQueuedMsgs();
virtual int reply(ConstElementPtr envelope, ConstElementPtr newmsg);
virtual bool hasQueuedMsgs() const;
void setMessage(ElementPtr msg) { msg_ = msg; }
void setMessage(ConstElementPtr msg) { msg_ = msg; }
void disableSend() { send_ok_ = false; }
void disableReceive() { receive_ok_ = false; }
ElementPtr sent_msg;
ConstElementPtr sent_msg;
string msg_destination;
private:
ElementPtr msg_;
ConstElementPtr msg_;
bool send_ok_;
bool receive_ok_;
};
......@@ -172,19 +173,19 @@ AuthSrvTest::MockSession::startRead(
{}
int
AuthSrvTest::MockSession::reply(ElementPtr& envelope UNUSED_PARAM,
ElementPtr& newmsg UNUSED_PARAM)
AuthSrvTest::MockSession::reply(ConstElementPtr envelope UNUSED_PARAM,
ConstElementPtr newmsg UNUSED_PARAM)
{
return (-1);
}
bool
AuthSrvTest::MockSession::hasQueuedMsgs() {
AuthSrvTest::MockSession::hasQueuedMsgs() const {
return (false);
}
int
AuthSrvTest::MockSession::group_sendmsg(ElementPtr msg, string group,
AuthSrvTest::MockSession::group_sendmsg(ConstElementPtr msg, string group,
string instance UNUSED_PARAM,
string to UNUSED_PARAM)
{
......@@ -198,8 +199,8 @@ AuthSrvTest::MockSession::group_sendmsg(ElementPtr msg, string group,
}
bool
AuthSrvTest::MockSession::group_recvmsg(ElementPtr& envelope UNUSED_PARAM,
ElementPtr& msg,
AuthSrvTest::MockSession::group_recvmsg(ConstElementPtr& envelope UNUSED_PARAM,
ConstElementPtr& msg,
bool nonblock UNUSED_PARAM,
int seq UNUSED_PARAM)
{
......@@ -537,7 +538,8 @@ TEST_F(AuthSrvTest, notify) {
EXPECT_EQ("Zonemgr", notify_session.msg_destination);
EXPECT_EQ("notify",
notify_session.sent_msg->get("command")->get(0)->stringValue());
ElementPtr notify_args = notify_session.sent_msg->get("command")->get(1);
ConstElementPtr notify_args =
notify_session.sent_msg->get("command")->get(1);
EXPECT_EQ("example.com.", notify_args->get("zone_name")->stringValue());
EXPECT_EQ(DEFAULT_REMOTE_ADDRESS,
notify_args->get("master")->stringValue());
......@@ -566,7 +568,8 @@ TEST_F(AuthSrvTest, notifyForCHClass) {
// Other conditions should be the same, so simply confirm the RR class is
// set correctly.
ElementPtr notify_args = notify_session.sent_msg->get("command")->get(1);
ConstElementPtr notify_args =
notify_session.sent_msg->get("command")->get(1);
EXPECT_EQ("CH", notify_args->get("zone_class")->stringValue());
}
......@@ -694,12 +697,12 @@ void
updateConfig(AuthSrv* server, const char* const dbfile,
const bool expect_success)
{
const ElementPtr config_answer =
ConstElementPtr config_answer =
server->updateConfig(Element::fromJSON(dbfile));
EXPECT_EQ(Element::map, config_answer->getType());
EXPECT_TRUE(config_answer->contains("result"));
const ElementPtr result = config_answer->get("result");
ConstElementPtr result = config_answer->get("result");
EXPECT_EQ(Element::list, result->getType());
EXPECT_EQ(expect_success ? 0 : 1, result->get(0)->intValue());
}
......
......@@ -20,6 +20,7 @@
#include <cassert>
#include <climits>
#include <map>
#include <cstdio>
#include <iostream>
#include <string>
......@@ -35,21 +36,21 @@ namespace isc {
namespace data {
std::string
Element::str() {
Element::str() const {
std::stringstream ss;
toJSON(ss);
return (ss.str());
}
std::string
Element::toWire() {
Element::toWire() const {
std::stringstream ss;
toJSON(ss);
return (ss.str());
}
void
Element::toWire(std::ostream& ss) {
Element::toWire(std::ostream& ss) const {
toJSON(ss);
}
......@@ -81,12 +82,12 @@ Element::getValue(std::string& t UNUSED_PARAM) {
}
bool
Element::getValue(std::vector<ElementPtr>& t UNUSED_PARAM) {
Element::getValue(std::vector<ConstElementPtr>& t UNUSED_PARAM) {
return (false);
}
bool
Element::getValue(std::map<std::string, ElementPtr>& t UNUSED_PARAM) {
Element::getValue(std::map<std::string, ConstElementPtr>& t UNUSED_PARAM) {
return (false);
}
......@@ -111,27 +112,29 @@ Element::setValue(const std::string& v UNUSED_PARAM) {
}
bool
Element::setValue(const std::vector<ElementPtr>& v UNUSED_PARAM) {
Element::setValue(const std::vector<ConstElementPtr>& v UNUSED_PARAM) {
return (false);
}
bool
Element::setValue(const std::map<std::string, ElementPtr>& v UNUSED_PARAM) {
Element::setValue(const std::map<std::string,
ConstElementPtr>& v UNUSED_PARAM)
{
return (false);
}
ElementPtr
Element::get(const int i UNUSED_PARAM) {
ConstElementPtr
Element::get(const int i UNUSED_PARAM) const {
isc_throw(TypeError, "get(int) called on a non-list Element");
}
void
Element::set(const size_t i UNUSED_PARAM, ElementPtr element UNUSED_PARAM) {
Element::set(const size_t i UNUSED_PARAM, ConstElementPtr element UNUSED_PARAM) {
isc_throw(TypeError, "set(int, element) called on a non-list Element");
}
void
Element::add(ElementPtr element UNUSED_PARAM) {
Element::add(ConstElementPtr element UNUSED_PARAM) {
isc_throw(TypeError, "add() called on a non-list Element");
}
......@@ -141,18 +144,18 @@ Element::remove(const int i UNUSED_PARAM) {
}
size_t
Element::size() {
Element::size() const {
isc_throw(TypeError, "size() called on a non-list Element");
}
ElementPtr
Element::get(const std::string& name UNUSED_PARAM) {
ConstElementPtr
Element::get(const std::string& name UNUSED_PARAM) const {
isc_throw(TypeError, "get(string) called on a non-map Element");
}
void
Element::set(const std::string& name UNUSED_PARAM,
ElementPtr element UNUSED_PARAM)
ConstElementPtr element UNUSED_PARAM)
{
isc_throw(TypeError, "set(name, element) called on a non-map Element");
}
......@@ -163,18 +166,18 @@ Element::remove(const std::string& name UNUSED_PARAM) {
}
bool
Element::contains(const std::string& name UNUSED_PARAM) {
Element::contains(const std::string& name UNUSED_PARAM) const {
isc_throw(TypeError, "contains(string) called on a non-map Element");
}
ElementPtr
Element::find(const std::string& identifier UNUSED_PARAM) {
ConstElementPtr
Element::find(const std::string& identifier UNUSED_PARAM) const {
isc_throw(TypeError, "find(string) called on a non-map Element");
}
bool
Element::find(const std::string& identifier UNUSED_PARAM,
ElementPtr& t UNUSED_PARAM)
ConstElementPtr t UNUSED_PARAM) const
{
return (false);
}
......@@ -189,12 +192,18 @@ throwJSONError(const std::string& error, const std::string& file, int line, int
}
}
std::ostream& operator <<(std::ostream &out, const isc::data::ElementPtr& e) {
return (out << e->str());
std::ostream&
operator<<(std::ostream &out, const Element& e) {
return (out << e.str());
}
bool operator==(const isc::data::ElementPtr a, const isc::data::ElementPtr b) {
return (a->equals(b));
bool
operator==(const Element& a, const Element& b) {
return (a.equals(b));
}
bool operator!=(const Element& a, const Element& b) {
return (!a.equals(b));
};
//
......@@ -428,7 +437,7 @@ from_stringstream_list(std::istream &in, const std::string& file, int& line, int
{
char c = 0;
ElementPtr list = Element::createList();
ElementPtr cur_list_element;
ConstElementPtr cur_list_element;
skip_chars(in, " \t\n", line, pos);
while (c != EOF && c != ']') {
......@@ -462,7 +471,7 @@ from_stringstream_map(std::istream &in, const std::string& file, int& line,
in.get();
pos++;
ElementPtr value = Element::fromJSON(in, file, line, pos);
ConstElementPtr value = Element::fromJSON(in, file, line, pos);
map->set(key, value);
skip_to(in, file, line, pos, ",}", " \t\n");
......@@ -614,20 +623,17 @@ Element::fromJSON(const std::string &in) {
// to JSON format
void
IntElement::toJSON(std::ostream& ss)
{
IntElement::toJSON(std::ostream& ss) const {
ss << intValue();
}
void
DoubleElement::toJSON(std::ostream& ss)
{
DoubleElement::toJSON(std::ostream& ss) const {
ss << doubleValue();
}
void
BoolElement::toJSON(std::ostream& ss)
{
BoolElement::toJSON(std::ostream& ss) const {
if (boolValue()) {
ss << "true";
} else {
......@@ -636,26 +642,23 @@ BoolElement::toJSON(std::ostream& ss)
}
void
NullElement::toJSON(std::ostream& ss)
{
NullElement::toJSON(std::ostream& ss) const {
ss << "null";
}
void
StringElement::toJSON(std::ostream& ss)
{
StringElement::toJSON(std::ostream& ss) const {
ss << "\"";
ss << stringValue();
ss << "\"";
}
void
ListElement::toJSON(std::ostream& ss)
{
ListElement::toJSON(std::ostream& ss) const {
ss << "[ ";
const std::vector<ElementPtr>& v = listValue();
for (std::vector<ElementPtr>::const_iterator it = v.begin();
const std::vector<ConstElementPtr>& v = listValue();
for (std::vector<ConstElementPtr>::const_iterator it = v.begin();
it != v.end(); ++it) {
if (it != v.begin()) {
ss << ", ";
......@@ -666,12 +669,11 @@ ListElement::toJSON(std::ostream& ss)
}
void
MapElement::toJSON(std::ostream& ss)
{
MapElement::toJSON(std::ostream& ss) const {
ss << "{ ";
const std::map<std::string, ElementPtr>& m = mapValue();
for (std::map<std::string, ElementPtr>::const_iterator it = m.begin();
const std::map<std::string, ConstElementPtr>& m = mapValue();
for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
it != m.end(); ++it) {
if (it != m.begin()) {
ss << ", ";
......@@ -690,13 +692,13 @@ MapElement::toJSON(std::ostream& ss)
// we're looking for) is not a MapElement
// returns 0 if it could simply not be found
// should that also be an exception?
ElementPtr
MapElement::find(const std::string& id) {
ConstElementPtr
MapElement::find(const std::string& id) const {
const size_t sep = id.find('/');
if (sep == std::string::npos) {
return (get(id));
} else {
ElementPtr ce = get(id.substr(0, sep));
ConstElementPtr ce = get(id.substr(0, sep));
if (ce) {
// ignore trailing slash
if (sep + 1 != id.size()) {
......@@ -735,14 +737,14 @@ Element::fromWire(std::stringstream& in, int length) {
}
void
MapElement::set(const std::string& key, ElementPtr value) {
MapElement::set(const std::string& key, ConstElementPtr value) {
m[key] = value;
}
bool
MapElement::find(const std::string& id, ElementPtr& t) {
MapElement::find(const std::string& id, ConstElementPtr t) const {
try {
ElementPtr p = find(id);
ConstElementPtr p = find(id);
if (p) {
t = p;
return (true);
......@@ -754,43 +756,43 @@ MapElement::find(const std::string& id, ElementPtr& t) {
}
bool
IntElement::equals(ElementPtr other) {
return (other->getType() == Element::integer) &&
(i == other->intValue());
IntElement::equals(const Element& other) const {
return (other.getType() == Element::integer) &&
(i == other.intValue());
}
bool
DoubleElement::equals(ElementPtr other) {
return (other->getType() == Element::real) &&
(d == other->doubleValue());
DoubleElement::equals(const Element& other) const {
return (other.getType() == Element::real) &&
(d == other.doubleValue());
}
bool
BoolElement::equals(ElementPtr other) {
return (other->getType() == Element::boolean) &&
(b == other->boolValue());
BoolElement::equals(const Element& other) const {
return (other.getType() == Element::boolean) &&
(b == other.boolValue());
}
bool
NullElement::equals(ElementPtr other) {
return (other->getType() == Element::null);
NullElement::equals(const Element& other) const {
return (other.getType() == Element::null);
}
bool
StringElement::equals(ElementPtr other) {
return (other->getType() == Element::string) &&
(s == other->stringValue());
StringElement::equals(const Element& other) const {
return (other.getType() == Element::string) &&
(s == other.stringValue());
}
bool
ListElement::equals(ElementPtr other) {
if (other->getType() == Element::list) {
ListElement::equals(const Element& other) const {
if (other.getType() == Element::list) {
const int s = size();
if (s != other->size()) {
if (s != other.size()) {
return (false);
}
for (int i = 0; i < s; ++i) {
if (!get(i)->equals(other->get(i))) {
if (!get(i)->equals(*other.get(i))) {
return (false);
}
}
......@@ -801,13 +803,14 @@ ListElement::equals(ElementPtr other) {
}
bool
MapElement::equals(ElementPtr other) {
if (other->getType() == Element::map) {
std::map<std::string, ElementPtr> m = mapValue();
for (std::map<std::string, ElementPtr>::const_iterator it = m.begin();
MapElement::equals(const Element& other) const {
if (other.getType() == Element::map) {
const std::map<std::string, ConstElementPtr>& m = mapValue();
for (std::map<std::string, ConstElementPtr>::const_iterator it =
m.begin();
it != m.end() ; ++it) {
if (other->contains((*it).first)) {
if (!get((*it).first)->equals(other->get((*it).first))) {
if (other.contains((*it).first)) {
if (!get((*it).first)->equals(*other.get((*it).first))) {
return (false);
}
} else {
......@@ -819,9 +822,10 @@ MapElement::equals(ElementPtr other) {
// compare those elements; if one of them is missing we
// differ (and if it's not missing the loop above has checked
// it)
m = other->mapValue();
for (std::map<std::string, ElementPtr>::const_iterator it = m.begin();
it != m.end() ; ++it) {
std::map<std::string, ConstElementPtr>::const_iterator it;
for (it = other.mapValue().begin();
it != other.mapValue().end();
++it) {
if (!contains((*it).first)) {
return (false);
}
......@@ -833,12 +837,12 @@ MapElement::equals(ElementPtr other) {
}
bool
isNull(ElementPtr p) {
isNull(ConstElementPtr p) {
return (!p);
}
void
removeIdentical(ElementPtr a, const ElementPtr b) {
removeIdentical(ElementPtr a, ConstElementPtr b) {
if (!b) {
return;
}
......@@ -846,26 +850,50 @@ removeIdentical(ElementPtr a, const ElementPtr b) {
isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
}
std::map<std::string, ElementPtr> m = a->mapValue();
for (std::map<std::string, ElementPtr>::const_iterator it = m.begin();
const std::map<std::string, ConstElementPtr>& m = a->mapValue();
for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
it != m.end() ; ++it) {
if (b->contains((*it).first)) {
if (a->get((*it).first)->equals(b->get((*it).first))) {
if (a->get((*it).first)->equals(*b->get((*it).first))) {
a->remove((*it).first);
}
}
}
}
ConstElementPtr
removeIdentical(ConstElementPtr a, ConstElementPtr b) {
ElementPtr result = Element::createMap();
if (!b) {
return (result);
}
if (a->