/*

Copyright (c) 2002-2005, Yauheni Akhotnikau
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

- The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

*/

/*
	ObjESSty-1
*/

#include <sstream>

#include <auto_ptr_3/h/obj_ptr.hpp>

#include <oess_1/defs/h/ex.hpp>

#include <oess_1/db/site/h/errno.hpp>

#include <oess_1/db/site/impl/h/localhost_connector.hpp>
#include <oess_1/db/site/impl/h/request.hpp>
#include <oess_1/db/site/impl/h/response.hpp>

namespace oess_1 {

namespace db {

namespace site {

namespace impl {

//
// localhost_connector_t
//

localhost_connector_t::localhost_connector_t(
	localhost_t & localhost ) :
	m_localhost( localhost )
{
	// В качестве идентификатора клиента используем
	// текстовое представление адреса нашего объекта.
	// Поскольку в ОП не может быть одновременно несколько
	// разных объектов с одинаковым адресом, это будет
	// уникальный идентификатор.
	std::ostringstream idstream;
	idstream << "{localhost " << (void *)this << "}";
	m_client_id = idstream.str();
}

localhost_connector_t::~localhost_connector_t()
{
}

const std::string &
localhost_connector_t::query_client_id() const
{
	return m_client_id;
}

void
localhost_connector_t::transmit(
	const impl::req_wrapper_t & req_wrapper,
	impl::resp_wrapper_t & resp_wrapper,
	oess_1::uint_t awaiting_resp_id )
{
	resp_wrapper_t resp_ptr(
		m_localhost.process( *req_wrapper ) );

	if( !resp_ptr.get() )
		OESS_THROW_PHYSIC(
			oess_1::db::site::err::c_no_response,
			"no response for req_id: "
			<< req_wrapper->query_req_id() )

	if( awaiting_resp_id != resp_ptr->query_resp_id() )
		OESS_THROW_LOGIC(
			oess_1::db::site::err::c_resp_id_mismatch,
			"awaiting_resp_id: " << awaiting_resp_id
			<< ", received_resp_id: "
			<< resp_ptr->query_resp_id() )

	resp_wrapper = resp_ptr;
}

} /* namespace impl */

} /* namespace site */

} /* namespace db */

} /* namespace oess_1 */
