Commit f6aa7d59 authored by chenzhengzhang's avatar chenzhengzhang

[trac749] refactor C++ utility library

parent ea679d6d
......@@ -677,6 +677,8 @@ AC_CONFIG_FILES([Makefile
src/lib/datasrc/Makefile
src/lib/datasrc/tests/Makefile
src/lib/xfr/Makefile
src/lib/util/Makefile
src/lib/util/tests/Makefile
src/lib/log/Makefile
src/lib/log/compiler/Makefile
src/lib/log/tests/Makefile
......
......@@ -31,7 +31,8 @@
#include <exceptions/exceptions.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/edns.h>
#include <dns/exceptions.h>
#include <dns/messagerenderer.h>
......@@ -63,6 +64,7 @@ using namespace isc;
using namespace isc::cc;
using namespace isc::datasrc;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::auth;
using namespace isc::dns::rdata;
using namespace isc::data;
......
......@@ -109,7 +109,7 @@ public:
/// \throw isc::Unexpected Protocol type of \a message is unexpected
void processMessage(const asiolink::IOMessage& io_message,
isc::dns::MessagePtr message,
isc::dns::OutputBufferPtr buffer,
isc::util::OutputBufferPtr buffer,
asiolink::DNSServer* server);
/// \brief Set verbose flag
......
......@@ -22,7 +22,7 @@
#include <bench/benchmark.h>
#include <bench/benchmark_util.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/message.h>
#include <dns/name.h>
#include <dns/question.h>
......@@ -41,6 +41,7 @@ using namespace isc;
using namespace isc::data;
using namespace isc::auth;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::xfr;
using namespace isc::bench;
using namespace asiolink;
......
......@@ -25,7 +25,8 @@
#include <exceptions/exceptions.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>
......@@ -49,6 +50,7 @@ using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::xfr;
using namespace asiolink;
......
......@@ -41,6 +41,7 @@
using namespace std;
using namespace isc::cc;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::dns::rdata;
using namespace isc::data;
using namespace isc::xfr;
......
......@@ -24,7 +24,8 @@
#include <string>
#include <iostream>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/name.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>
......@@ -37,6 +38,7 @@
using namespace std;
using namespace isc::dns;
using namespace isc::util;
namespace {
char* dns_type = NULL; // not set, so A, AAAA, MX
......
......@@ -29,7 +29,7 @@
#include <exceptions/exceptions.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/rcode.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>
......
......@@ -29,9 +29,10 @@
#include <exceptions/exceptions.h>
#include <util/buffer.h>
#include <dns/opcode.h>
#include <dns/rcode.h>
#include <dns/buffer.h>
#include <dns/exceptions.h>
#include <dns/name.h>
#include <dns/question.h>
......@@ -50,6 +51,7 @@
using namespace std;
using namespace isc;
using namespace isc::util;
using namespace isc::dns;
using namespace isc::data;
using namespace isc::config;
......
......@@ -77,7 +77,7 @@ public:
void processMessage(const asiolink::IOMessage& io_message,
isc::dns::MessagePtr query_message,
isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer,
isc::util::OutputBufferPtr buffer,
asiolink::DNSServer* server);
/// \brief Set and get the config session
......
SUBDIRS = exceptions dns cc config python xfr bench log asiolink \
nsas cache resolve testutils datasrc server_common
SUBDIRS = exceptions util dns cc config python xfr bench log \
asiolink nsas cache resolve testutils datasrc server_common
......@@ -13,7 +13,6 @@ CLEANFILES = *.gcno *.gcda
# which would make the build fail with -Werror (our default setting).
lib_LTLIBRARIES = libasiolink.la
libasiolink_la_SOURCES = asiolink.h
libasiolink_la_SOURCES += asiolink_utilities.h
libasiolink_la_SOURCES += asiodef.cc asiodef.h
libasiolink_la_SOURCES += dns_answer.h
libasiolink_la_SOURCES += dns_lookup.h
......@@ -27,7 +26,6 @@ libasiolink_la_SOURCES += io_endpoint.cc io_endpoint.h
libasiolink_la_SOURCES += io_error.h
libasiolink_la_SOURCES += io_fetch.cc io_fetch.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_socket.h io_socket.cc
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:
virtual void operator()(const IOMessage& io_message,
isc::dns::MessagePtr query_message,
isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer) const = 0;
isc::util::OutputBufferPtr buffer) const = 0;
};
} // namespace asiolink
......
......@@ -17,7 +17,7 @@
#include <asiolink/io_message.h>
#include <asiolink/dns_server.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/message.h>
namespace asiolink {
......@@ -70,7 +70,7 @@ public:
virtual void operator()(const IOMessage& io_message,
isc::dns::MessagePtr message,
isc::dns::MessagePtr answer_message,
isc::dns::OutputBufferPtr buffer,
isc::util::OutputBufferPtr buffer,
DNSServer* server) const
{
(*self_)(io_message, message, answer_message, buffer, server);
......
......@@ -26,7 +26,7 @@
#include <exceptions/exceptions.h>
#include <coroutine.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <asiolink/io_error.h>
#include <asiolink/io_socket.h>
......@@ -270,7 +270,7 @@ public:
virtual bool processReceivedData(const void* staging, size_t length,
size_t& cumulative, size_t& offset,
size_t& expected,
isc::dns::OutputBufferPtr& outbuff) = 0;
isc::util::OutputBufferPtr& outbuff) = 0;
/// \brief Cancel I/O On AsioSocket
virtual void cancel() = 0;
......@@ -372,7 +372,7 @@ public:
virtual bool receiveComplete(const void* staging, size_t length,
size_t& cumulative, size_t& offset,
size_t& expected,
isc::dns::OutputBufferPtr& outbuff)
isc::util::OutputBufferPtr& outbuff)
{
return (true);
}
......
......@@ -22,13 +22,16 @@
#include <boost/scoped_ptr.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <util/buffer.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>
#include <dns/opcode.h>
#include <dns/rcode.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/deadline_timer.hpp>
......@@ -43,12 +46,12 @@
#include <asiolink/tcp_socket.h>
#include <asiolink/udp_endpoint.h>
#include <asiolink/udp_socket.h>
#include <asiolink/qid_gen.h>
#include <stdint.h>
using namespace asio;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::log;
using namespace std;
......@@ -76,8 +79,8 @@ struct IOFetchData {
boost::scoped_ptr<IOEndpoint> remote_snd;///< Where the fetch is sent
boost::scoped_ptr<IOEndpoint> remote_rcv;///< Where the response came from
isc::dns::Question question; ///< Question to be asked
isc::dns::OutputBufferPtr msgbuf; ///< Wire buffer for question
isc::dns::OutputBufferPtr received; ///< Received data put here
OutputBufferPtr msgbuf; ///< Wire buffer for question
OutputBufferPtr received; ///< Received data put here
IOFetch::Callback* callback; ///< Called on I/O Completion
asio::deadline_timer timer; ///< Timer to measure timeouts
IOFetch::Protocol protocol; ///< Protocol being used
......@@ -117,8 +120,7 @@ struct IOFetchData {
/// TODO: May need to alter constructor (see comment 4 in Trac ticket #554)
IOFetchData(IOFetch::Protocol proto, IOService& service,
const isc::dns::Question& query, const IOAddress& address,
uint16_t port, isc::dns::OutputBufferPtr& buff, IOFetch::Callback* cb,
int wait)
uint16_t port, OutputBufferPtr& buff, IOFetch::Callback* cb, int wait)
:
socket((proto == IOFetch::UDP) ?
static_cast<IOAsioSocket<IOFetch>*>(
......@@ -135,7 +137,7 @@ struct IOFetchData {
static_cast<IOEndpoint*>(new TCPEndpoint(address, port))
),
question(query),
msgbuf(new isc::dns::OutputBuffer(512)),
msgbuf(new OutputBuffer(512)),
received(buff),
callback(cb),
......
......@@ -25,7 +25,7 @@
#include <asio/error_code.hpp>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <dns/question.h>
namespace asiolink {
......@@ -134,7 +134,7 @@ public:
/// -1 indicates no timeout.
IOFetch(Protocol protocol, IOService& service,
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);
/// \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.
// 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?)
#ifndef __QID_GEN_H
#define __QID_GEN_H
#include <dns/message.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
namespace asiolink {
/// This class generates Qids for outgoing queries
///
/// It is implemented as a singleton; the public way to access it
/// is to call getInstance()->generateQid().
///
/// It automatically seeds it with the current time when it is first
/// used.
class QidGenerator {
public:
/// \brief Returns the singleton instance of the QidGenerator
///
/// Returns a reference to the singleton instance of the generator
static QidGenerator& getInstance();
/// \brief Default constructor
///
/// It is recommended that getInstance is used rather than creating
/// separate instances of this class.
///
/// The constructor automatically seeds the generator with the
/// current time.
QidGenerator();
/// Generate a Qid
///
/// \return A random Qid
isc::dns::qid_t generateQid();
/// \brief Seeds the QidGenerator (based on the current time)
///
/// This is automatically called by the constructor
void seed();
private:
// "Mersenne Twister: A 623-dimensionally equidistributed
// uniform pseudo-random number generator", Makoto Matsumoto and
// Takuji Nishimura, ACM Transactions on Modeling and Computer
// Simulation: Special Issue on Uniform Random Number Generation,
// Vol. 8, No. 1, January 1998, pp. 3-30.
//
// mt19937 is an implementation of one of the pseudo random
// generators described in this paper.
boost::mt19937 generator_;
// For qid's we want a uniform distribution
boost::uniform_int<> dist_;
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > vgen_;
};
} // namespace asiolink
#endif // __QID_GEN_H
......@@ -23,6 +23,8 @@
#include <log/dummylog.h>
#include <util/buffer.h>
#include <asio.hpp>
#include <asiolink/dummy_io_cb.h>
#include <asiolink/tcp_endpoint.h>
......@@ -36,6 +38,7 @@ using asio::ip::tcp;
using namespace std;
using namespace isc::dns;
using namespace isc::util;
namespace asiolink {
......
......@@ -91,7 +91,7 @@ private:
boost::shared_ptr<asio::ip::tcp::socket> socket_;
// The buffer into which the response is written
boost::shared_ptr<isc::dns::OutputBuffer> respbuf_;
boost::shared_ptr<isc::util::OutputBuffer> respbuf_;
// \c IOMessage and \c Message objects to be passed to the
// DNS lookup and answer providers
......
......@@ -33,9 +33,9 @@
#include <config.h>
#include <dns/buffer.h>
#include <util/buffer.h>
#include <util/asiolink_utilities.h>
#include <asiolink/asiolink_utilities.h>
#include <asiolink/io_asio_socket.h>
#include <asiolink/io_endpoint.h>
#include <asiolink/io_service.h>
......@@ -155,7 +155,7 @@ public:
virtual bool processReceivedData(const void* staging, size_t length,
size_t& cumulative, size_t& offset,
size_t& expected,
isc::dns::OutputBufferPtr& outbuff);
isc::util::OutputBufferPtr& outbuff);
/// \brief Cancel I/O On Socket
virtual void cancel();
......@@ -185,7 +185,7 @@ private:
// The option of sending the data in two operations, the count followed by
// the data was discounted as that would lead to two callbacks which would
// cause problems with the stackless coroutine code.
isc::dns::OutputBufferPtr send_buffer_; ///< Send buffer
isc::util::OutputBufferPtr send_buffer_; ///< Send buffer
};
// Constructor - caller manages socket
......@@ -268,7 +268,7 @@ TCPSocket<C>::asyncSend(const void* data, size_t length,
uint16_t count = boost::numeric_cast<uint16_t>(length);
// Copy data into a buffer preceded by the count field.
send_buffer_.reset(new isc::dns::OutputBuffer(length + 2));
send_buffer_.reset(new isc::util::OutputBuffer(length + 2));
send_buffer_->writeUint16(count);
send_buffer_->writeData(data, length);
......@@ -333,7 +333,7 @@ template <typename C> bool
TCPSocket<C>::processReceivedData(const void* staging, size_t length,
size_t& cumulative, size_t& offset,
size_t& expected,
isc::dns::OutputBufferPtr& outbuff)
isc::util::OutputBufferPtr& outbuff)
{
// Point to the data in the staging buffer and note how much there is.
const uint8_t* data = static_cast<const uint8_t*>(staging);
......@@ -358,7 +358,7 @@ TCPSocket<C>::processReceivedData(const void* staging, size_t length,
}
// Have enough data to interpret the packet count, so do so now.
expected = readUint16(data);
expected = isc::util::readUint16(data);
// We have two bytes less of data to process. Point to the start of the
// data and adjust the packet size. Note that at this point,
......
AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES)
AM_CPPFLAGS += -I$(top_builddir)/src/lib/dns -I$(top_srcdir)/src/bin
AM_CPPFLAGS += -I$(top_builddir)/src/lib/util -I$(top_srcdir)/src/util
AM_CPPFLAGS += -I$(top_builddir)/src/lib/cc
AM_CPPFLAGS += -DTEST_DATA_DIR=\"$(srcdir)/testdata\"