Commit f6aa7d59 authored by chenzhengzhang's avatar chenzhengzhang

[trac749] refactor C++ utility library

parent ea679d6d
...@@ -677,6 +677,8 @@ AC_CONFIG_FILES([Makefile ...@@ -677,6 +677,8 @@ AC_CONFIG_FILES([Makefile
src/lib/datasrc/Makefile src/lib/datasrc/Makefile
src/lib/datasrc/tests/Makefile src/lib/datasrc/tests/Makefile
src/lib/xfr/Makefile src/lib/xfr/Makefile
src/lib/util/Makefile
src/lib/util/tests/Makefile
src/lib/log/Makefile src/lib/log/Makefile
src/lib/log/compiler/Makefile src/lib/log/compiler/Makefile
src/lib/log/tests/Makefile src/lib/log/tests/Makefile
......
...@@ -31,7 +31,8 @@ ...@@ -31,7 +31,8 @@
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/edns.h> #include <dns/edns.h>
#include <dns/exceptions.h> #include <dns/exceptions.h>
#include <dns/messagerenderer.h> #include <dns/messagerenderer.h>
...@@ -63,6 +64,7 @@ using namespace isc; ...@@ -63,6 +64,7 @@ using namespace isc;
using namespace isc::cc; using namespace isc::cc;
using namespace isc::datasrc; using namespace isc::datasrc;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::util;
using namespace isc::auth; using namespace isc::auth;
using namespace isc::dns::rdata; using namespace isc::dns::rdata;
using namespace isc::data; using namespace isc::data;
......
...@@ -109,7 +109,7 @@ public: ...@@ -109,7 +109,7 @@ public:
/// \throw isc::Unexpected Protocol type of \a message is unexpected /// \throw isc::Unexpected Protocol type of \a message is unexpected
void processMessage(const asiolink::IOMessage& io_message, void processMessage(const asiolink::IOMessage& io_message,
isc::dns::MessagePtr message, isc::dns::MessagePtr message,
isc::dns::OutputBufferPtr buffer, isc::util::OutputBufferPtr buffer,
asiolink::DNSServer* server); asiolink::DNSServer* server);
/// \brief Set verbose flag /// \brief Set verbose flag
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include <bench/benchmark.h> #include <bench/benchmark.h>
#include <bench/benchmark_util.h> #include <bench/benchmark_util.h>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/message.h> #include <dns/message.h>
#include <dns/name.h> #include <dns/name.h>
#include <dns/question.h> #include <dns/question.h>
...@@ -41,6 +41,7 @@ using namespace isc; ...@@ -41,6 +41,7 @@ using namespace isc;
using namespace isc::data; using namespace isc::data;
using namespace isc::auth; using namespace isc::auth;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::util;
using namespace isc::xfr; using namespace isc::xfr;
using namespace isc::bench; using namespace isc::bench;
using namespace asiolink; using namespace asiolink;
......
...@@ -25,7 +25,8 @@ ...@@ -25,7 +25,8 @@
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/message.h> #include <dns/message.h>
#include <dns/messagerenderer.h> #include <dns/messagerenderer.h>
...@@ -49,6 +50,7 @@ using namespace isc::data; ...@@ -49,6 +50,7 @@ using namespace isc::data;
using namespace isc::cc; using namespace isc::cc;
using namespace isc::config; using namespace isc::config;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::util;
using namespace isc::xfr; using namespace isc::xfr;
using namespace asiolink; using namespace asiolink;
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
using namespace std; using namespace std;
using namespace isc::cc; using namespace isc::cc;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::util;
using namespace isc::dns::rdata; using namespace isc::dns::rdata;
using namespace isc::data; using namespace isc::data;
using namespace isc::xfr; using namespace isc::xfr;
......
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/name.h> #include <dns/name.h>
#include <dns/message.h> #include <dns/message.h>
#include <dns/messagerenderer.h> #include <dns/messagerenderer.h>
...@@ -37,6 +38,7 @@ ...@@ -37,6 +38,7 @@
using namespace std; using namespace std;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::util;
namespace { namespace {
char* dns_type = NULL; // not set, so A, AAAA, MX char* dns_type = NULL; // not set, so A, AAAA, MX
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/rcode.h> #include <dns/rcode.h>
#include <dns/message.h> #include <dns/message.h>
#include <dns/messagerenderer.h> #include <dns/messagerenderer.h>
......
...@@ -29,9 +29,10 @@ ...@@ -29,9 +29,10 @@
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <util/buffer.h>
#include <dns/opcode.h> #include <dns/opcode.h>
#include <dns/rcode.h> #include <dns/rcode.h>
#include <dns/buffer.h>
#include <dns/exceptions.h> #include <dns/exceptions.h>
#include <dns/name.h> #include <dns/name.h>
#include <dns/question.h> #include <dns/question.h>
...@@ -50,6 +51,7 @@ ...@@ -50,6 +51,7 @@
using namespace std; using namespace std;
using namespace isc; using namespace isc;
using namespace isc::util;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::data; using namespace isc::data;
using namespace isc::config; using namespace isc::config;
......
...@@ -77,7 +77,7 @@ public: ...@@ -77,7 +77,7 @@ public:
void processMessage(const asiolink::IOMessage& io_message, void processMessage(const asiolink::IOMessage& io_message,
isc::dns::MessagePtr query_message, isc::dns::MessagePtr query_message,
isc::dns::MessagePtr answer_message, isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer, isc::util::OutputBufferPtr buffer,
asiolink::DNSServer* server); asiolink::DNSServer* server);
/// \brief Set and get the config session /// \brief Set and get the config session
......
SUBDIRS = exceptions dns cc config python xfr bench log asiolink \ SUBDIRS = exceptions util dns cc config python xfr bench log \
nsas cache resolve testutils datasrc server_common asiolink nsas cache resolve testutils datasrc server_common
...@@ -13,7 +13,6 @@ CLEANFILES = *.gcno *.gcda ...@@ -13,7 +13,6 @@ CLEANFILES = *.gcno *.gcda
# which would make the build fail with -Werror (our default setting). # which would make the build fail with -Werror (our default setting).
lib_LTLIBRARIES = libasiolink.la lib_LTLIBRARIES = libasiolink.la
libasiolink_la_SOURCES = asiolink.h libasiolink_la_SOURCES = asiolink.h
libasiolink_la_SOURCES += asiolink_utilities.h
libasiolink_la_SOURCES += asiodef.cc asiodef.h libasiolink_la_SOURCES += asiodef.cc asiodef.h
libasiolink_la_SOURCES += dns_answer.h libasiolink_la_SOURCES += dns_answer.h
libasiolink_la_SOURCES += dns_lookup.h libasiolink_la_SOURCES += dns_lookup.h
...@@ -27,7 +26,6 @@ libasiolink_la_SOURCES += io_endpoint.cc io_endpoint.h ...@@ -27,7 +26,6 @@ libasiolink_la_SOURCES += io_endpoint.cc io_endpoint.h
libasiolink_la_SOURCES += io_error.h libasiolink_la_SOURCES += io_error.h
libasiolink_la_SOURCES += io_fetch.cc io_fetch.h libasiolink_la_SOURCES += io_fetch.cc io_fetch.h
libasiolink_la_SOURCES += io_message.h libasiolink_la_SOURCES += io_message.h
libasiolink_la_SOURCES += qid_gen.cc qid_gen.h
libasiolink_la_SOURCES += io_service.h io_service.cc libasiolink_la_SOURCES += io_service.h io_service.cc
libasiolink_la_SOURCES += io_socket.h io_socket.cc libasiolink_la_SOURCES += io_socket.h io_socket.cc
libasiolink_la_SOURCES += simple_callback.h libasiolink_la_SOURCES += simple_callback.h
......
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#ifndef __ASIOLINK_UTILITIES_H
#define __ASIOLINK_UTILITIES_H
#include <cstddef>
namespace asiolink {
/// \brief Read Unsigned 16-Bit Integer from Buffer
///
/// This is essentially a copy of the isc::dns::InputBuffer::readUint16. It
/// should really be moved into a separate library.
///
/// \param buffer Data buffer at least two bytes long of which the first two
/// bytes are assumed to represent a 16-bit integer in network-byte
/// order.
///
/// \return Value of 16-bit integer
inline uint16_t
readUint16(const void* buffer) {
const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
uint16_t result = (static_cast<uint16_t>(byte_buffer[0])) << 8;
result |= static_cast<uint16_t>(byte_buffer[1]);
return (result);
}
/// \brief Write Unisgned 16-Bit Integer to Buffer
///
/// This is essentially a copy of isc::dns::OutputBuffer::writeUint16. It
/// should really be moved into a separate library.
///
/// \param value 16-bit value to convert
/// \param buffer Data buffer at least two bytes long into which the 16-bit
/// value is written in network-byte order.
inline void
writeUint16(uint16_t value, void* buffer) {
uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
byte_buffer[0] = static_cast<uint8_t>((value & 0xff00U) >> 8);
byte_buffer[1] = static_cast<uint8_t>(value & 0x00ffU);
}
} // namespace asiolink
#endif // __ASIOLINK_UTILITIES_H
...@@ -66,7 +66,7 @@ public: ...@@ -66,7 +66,7 @@ public:
virtual void operator()(const IOMessage& io_message, virtual void operator()(const IOMessage& io_message,
isc::dns::MessagePtr query_message, isc::dns::MessagePtr query_message,
isc::dns::MessagePtr answer_message, isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer) const = 0; isc::util::OutputBufferPtr buffer) const = 0;
}; };
} // namespace asiolink } // namespace asiolink
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include <asiolink/io_message.h> #include <asiolink/io_message.h>
#include <asiolink/dns_server.h> #include <asiolink/dns_server.h>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/message.h> #include <dns/message.h>
namespace asiolink { namespace asiolink {
...@@ -70,7 +70,7 @@ public: ...@@ -70,7 +70,7 @@ public:
virtual void operator()(const IOMessage& io_message, virtual void operator()(const IOMessage& io_message,
isc::dns::MessagePtr message, isc::dns::MessagePtr message,
isc::dns::MessagePtr answer_message, isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer, isc::util::OutputBufferPtr buffer,
DNSServer* server) const DNSServer* server) const
{ {
(*self_)(io_message, message, answer_message, buffer, server); (*self_)(io_message, message, answer_message, buffer, server);
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <coroutine.h> #include <coroutine.h>
#include <dns/buffer.h> #include <util/buffer.h>
#include <asiolink/io_error.h> #include <asiolink/io_error.h>
#include <asiolink/io_socket.h> #include <asiolink/io_socket.h>
...@@ -270,7 +270,7 @@ public: ...@@ -270,7 +270,7 @@ public:
virtual bool processReceivedData(const void* staging, size_t length, virtual bool processReceivedData(const void* staging, size_t length,
size_t& cumulative, size_t& offset, size_t& cumulative, size_t& offset,
size_t& expected, size_t& expected,
isc::dns::OutputBufferPtr& outbuff) = 0; isc::util::OutputBufferPtr& outbuff) = 0;
/// \brief Cancel I/O On AsioSocket /// \brief Cancel I/O On AsioSocket
virtual void cancel() = 0; virtual void cancel() = 0;
...@@ -372,7 +372,7 @@ public: ...@@ -372,7 +372,7 @@ public:
virtual bool receiveComplete(const void* staging, size_t length, virtual bool receiveComplete(const void* staging, size_t length,
size_t& cumulative, size_t& offset, size_t& cumulative, size_t& offset,
size_t& expected, size_t& expected,
isc::dns::OutputBufferPtr& outbuff) isc::util::OutputBufferPtr& outbuff)
{ {
return (true); return (true);
} }
......
...@@ -22,13 +22,16 @@ ...@@ -22,13 +22,16 @@
#include <boost/scoped_ptr.hpp> #include <boost/scoped_ptr.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp>
#include <util/buffer.h>
#include <dns/message.h> #include <dns/message.h>
#include <dns/messagerenderer.h> #include <dns/messagerenderer.h>
#include <dns/opcode.h> #include <dns/opcode.h>
#include <dns/rcode.h> #include <dns/rcode.h>
#include <log/logger.h> #include <log/logger.h>
#include <asiolink/qid_gen.h> #include <util/qid_gen.h>
#include <util/qid_gen.h>
#include <asio.hpp> #include <asio.hpp>
#include <asio/deadline_timer.hpp> #include <asio/deadline_timer.hpp>
...@@ -43,12 +46,12 @@ ...@@ -43,12 +46,12 @@
#include <asiolink/tcp_socket.h> #include <asiolink/tcp_socket.h>
#include <asiolink/udp_endpoint.h> #include <asiolink/udp_endpoint.h>
#include <asiolink/udp_socket.h> #include <asiolink/udp_socket.h>
#include <asiolink/qid_gen.h>
#include <stdint.h> #include <stdint.h>
using namespace asio; using namespace asio;
using namespace isc::dns; using namespace isc::dns;
using namespace isc::util;
using namespace isc::log; using namespace isc::log;
using namespace std; using namespace std;
...@@ -76,8 +79,8 @@ struct IOFetchData { ...@@ -76,8 +79,8 @@ struct IOFetchData {
boost::scoped_ptr<IOEndpoint> remote_snd;///< Where the fetch is sent boost::scoped_ptr<IOEndpoint> remote_snd;///< Where the fetch is sent
boost::scoped_ptr<IOEndpoint> remote_rcv;///< Where the response came from boost::scoped_ptr<IOEndpoint> remote_rcv;///< Where the response came from
isc::dns::Question question; ///< Question to be asked isc::dns::Question question; ///< Question to be asked
isc::dns::OutputBufferPtr msgbuf; ///< Wire buffer for question OutputBufferPtr msgbuf; ///< Wire buffer for question
isc::dns::OutputBufferPtr received; ///< Received data put here OutputBufferPtr received; ///< Received data put here
IOFetch::Callback* callback; ///< Called on I/O Completion IOFetch::Callback* callback; ///< Called on I/O Completion
asio::deadline_timer timer; ///< Timer to measure timeouts asio::deadline_timer timer; ///< Timer to measure timeouts
IOFetch::Protocol protocol; ///< Protocol being used IOFetch::Protocol protocol; ///< Protocol being used
...@@ -117,8 +120,7 @@ struct IOFetchData { ...@@ -117,8 +120,7 @@ struct IOFetchData {
/// TODO: May need to alter constructor (see comment 4 in Trac ticket #554) /// TODO: May need to alter constructor (see comment 4 in Trac ticket #554)
IOFetchData(IOFetch::Protocol proto, IOService& service, IOFetchData(IOFetch::Protocol proto, IOService& service,
const isc::dns::Question& query, const IOAddress& address, const isc::dns::Question& query, const IOAddress& address,
uint16_t port, isc::dns::OutputBufferPtr& buff, IOFetch::Callback* cb, uint16_t port, OutputBufferPtr& buff, IOFetch::Callback* cb, int wait)
int wait)
: :
socket((proto == IOFetch::UDP) ? socket((proto == IOFetch::UDP) ?
static_cast<IOAsioSocket<IOFetch>*>( static_cast<IOAsioSocket<IOFetch>*>(
...@@ -135,7 +137,7 @@ struct IOFetchData { ...@@ -135,7 +137,7 @@ struct IOFetchData {
static_cast<IOEndpoint*>(new TCPEndpoint(address, port)) static_cast<IOEndpoint*>(new TCPEndpoint(address, port))
), ),
question(query), question(query),
msgbuf(new isc::dns::OutputBuffer(512)), msgbuf(new OutputBuffer(512)),
received(buff), received(buff),
callback(cb), callback(cb),
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include <asio/error_code.hpp> #include <asio/error_code.hpp>
#include <dns/buffer.h> #include <util/buffer.h>
#include <dns/question.h> #include <dns/question.h>
namespace asiolink { namespace asiolink {
...@@ -134,7 +134,7 @@ public: ...@@ -134,7 +134,7 @@ public:
/// -1 indicates no timeout. /// -1 indicates no timeout.
IOFetch(Protocol protocol, IOService& service, IOFetch(Protocol protocol, IOService& service,
const isc::dns::Question& question, const IOAddress& address, const isc::dns::Question& question, const IOAddress& address,
uint16_t port, isc::dns::OutputBufferPtr& buff, Callback* cb, uint16_t port, isc::util::OutputBufferPtr& buff, Callback* cb,
int wait = -1); int wait = -1);
/// \brief Return Current Protocol /// \brief Return Current Protocol
......
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// qid_gen defines a generator for query id's
//
// We probably want to merge this with the weighted random in the nsas
// (and other parts where we need randomness, perhaps another thing
// for a general libutil?)
#include <asiolink/qid_gen.h>
#include <sys/time.h>
namespace {
asiolink::QidGenerator qid_generator_instance;
}
namespace asiolink {
QidGenerator&
QidGenerator::getInstance() {
return (qid_generator_instance);
}
QidGenerator::QidGenerator() : dist_(0, 65535),
vgen_(generator_, dist_)
{
seed();
}
void
QidGenerator::seed() {
struct timeval tv;
gettimeofday(&tv, 0);
generator_.seed((tv.tv_sec * 1000000) + tv.tv_usec);
}
isc::dns::qid_t
QidGenerator::generateQid() {
return (vgen_());
}
} // namespace asiolink
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.