cyphesis/server/CommPSQLSocket.cpp
Al Riddoch b035ecfaab 2004-08-27 Al Riddoch <alriddoch@zepler.org>
* common/BaseWorld.h, server/CommSocket.h, server/CommIdleSocket.h:
	  Make constructors of some base classes protected.

	* server/Account.cpp: Note a possible bug in the character
	  creation code.

	* server/CommIdleSocket.cpp, server/CommIdleSocket.h,
	  server/CommListener.cpp, server/CommListener.h,
	  server/CommLocalClient.cpp, server/CommMetaClient.cpp,
	  server/CommMetaClient.h, server/CommPSQLSocket.cpp,
	  server/CommPSQLSocket.h, server/CommRemoteClient.cpp,
	  server/CommSocket.cpp, server/CommSocket.h,
	  server/CommUnixListener.cpp, server/CommUnixListener.h:
	  Clean up the Comm classes for consistancy and readability.
2004-08-28 00:58:01 +00:00

116 lines
3 KiB
C++

// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2003 Alistair Riddoch
#include "CommPSQLSocket.h"
#include "common/Database.h"
#include "common/log.h"
#include "common/debug.h"
#include <iostream>
#include <cassert>
static const bool debug_flag = false;
/// \brief Constructor for PostgreSQL socket polling object.
///
/// @param svr Reference to the object that manages all socket communication.
/// @param db Reference to the low level database management object.
CommPSQLSocket::CommPSQLSocket(CommServer & svr, Database & db) :
CommIdleSocket(svr), m_db(db),
m_vacuumTime(0), m_reindexTime(0),
m_vacuumFull(false)
{
// This assumes the database connection is already sorted, which I think
// is okay
PGconn * con = m_db.getConnection();
assert(con != 0);
if (PQsetnonblocking(con, 1) == -1) {
log(ERROR, "Unable to put database connection in non-blocking mode.");
}
}
CommPSQLSocket::~CommPSQLSocket()
{
}
int CommPSQLSocket::getFd() const
{
debug(std::cout << "CommPSQLSocket::getFd()" << std::endl << std::flush;);
PGconn * con = m_db.getConnection();
assert(con != 0);
return PQsocket(con);
}
bool CommPSQLSocket::eof()
{
debug(std::cout << "CommPSQLSocket::eof()" << std::endl << std::flush;);
return false;
}
bool CommPSQLSocket::isOpen() const
{
debug(std::cout << "CommPSQLSocket::isOpen()" << std::endl << std::flush;);
return true;
}
bool CommPSQLSocket::read()
{
debug(std::cout << "CommPSQLSocket::read()" << std::endl << std::flush;);
PGconn * con = m_db.getConnection();
assert(con != 0);
if (PQconsumeInput(con) == 0) {
log(ERROR, "Error reading from database connection.");
m_db.reportError();
}
PGresult * res;
while (PQisBusy(con) == 0) {
if ((res = PQgetResult(con)) != 0) {
m_db.queryResult(PQresultStatus(res));
PQclear(res);
} else {
m_db.queryComplete();
return false;
}
};
return false;
}
void CommPSQLSocket::dispatch()
{
debug(std::cout << "CommPSQLSocket::dispatch()"
<< std::endl << std::flush;);
if (m_db.queryInProgress()) {
return;
}
m_db.launchNewQuery();
}
void CommPSQLSocket::idle(time_t t)
{
debug(std::cout << "CommPSQLSocket::idle()" << std::endl << std::flush;);
if (t > m_vacuumTime) {
if (m_vacuumFull) {
m_db.runMaintainance(Database::MAINTAIN_VACUUM |
Database::MAINTAIN_VACUUM_FULL);
} else {
m_db.runMaintainance(Database::MAINTAIN_VACUUM |
Database::MAINTAIN_VACUUM_ANALYZE);
}
m_vacuumFull = !m_vacuumFull;
m_vacuumTime = t + vacFreq;
}
if (t > m_reindexTime) {
m_db.runMaintainance(Database::MAINTAIN_REINDEX);
m_reindexTime = t + reindexFreq;
}
}