This now compiles, i gaurantee it doesnt work :P

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4478 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
brain 2006-07-21 18:39:05 +00:00
parent 221934729e
commit b525225088

View File

@ -29,17 +29,132 @@ using namespace std;
/* VERSION 2 API: With nonblocking (threaded) requests */ /* VERSION 2 API: With nonblocking (threaded) requests */
/* $ModDesc: SQL Service Provider module for all other m_sql* modules */ /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
/* $CompileFlags: -pthread `mysql_config --include` */ /* $CompileFlags: `mysql_config --include` */
/* $LinkerFlags: -pthread `mysql_config --libs_r` `perl ../mysql_rpath.pl` */ /* $LinkerFlags: `mysql_config --libs_r` `perl ../mysql_rpath.pl` */
class SQLConnection;
extern InspIRCd* ServerInstance;
typedef std::map<std::string, SQLConnection*> ConnMap;
/** SQLConnection represents one mysql session.
* Each session has its own persistent connection to the database.
*/
#if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
#define mysql_field_count mysql_num_fields #define mysql_field_count mysql_num_fields
#endif #endif
class QueryQueue : public classbase
{
private:
typedef std::deque<SQLrequest> ReqDeque;
ReqDeque priority; /* The priority queue */
ReqDeque normal; /* The 'normal' queue */
enum { PRI, NOR, NON } which; /* Which queue the currently active element is at the front of */
public:
QueryQueue()
: which(NON)
{
}
void push(const SQLrequest &q)
{
log(DEBUG, "QueryQueue::push(): Adding %s query to queue: %s", ((q.pri) ? "priority" : "non-priority"), q.query.q.c_str());
if(q.pri)
priority.push_back(q);
else
normal.push_back(q);
}
void pop()
{
if((which == PRI) && priority.size())
{
priority.pop_front();
}
else if((which == NOR) && normal.size())
{
normal.pop_front();
}
/* Reset this */
which = NON;
/* Silently do nothing if there was no element to pop() */
}
SQLrequest& front()
{
switch(which)
{
case PRI:
return priority.front();
case NOR:
return normal.front();
default:
if(priority.size())
{
which = PRI;
return priority.front();
}
if(normal.size())
{
which = NOR;
return normal.front();
}
/* This will probably result in a segfault,
* but the caller should have checked totalsize()
* first so..meh - moron :p
*/
return priority.front();
}
}
std::pair<int, int> size()
{
return std::make_pair(priority.size(), normal.size());
}
int totalsize()
{
return priority.size() + normal.size();
}
void PurgeModule(Module* mod)
{
DoPurgeModule(mod, priority);
DoPurgeModule(mod, normal);
}
private:
void DoPurgeModule(Module* mod, ReqDeque& q)
{
for(ReqDeque::iterator iter = q.begin(); iter != q.end(); iter++)
{
if(iter->GetSource() == mod)
{
if(iter->id == front().id)
{
/* It's the currently active query.. :x */
iter->SetSource(NULL);
}
else
{
/* It hasn't been executed yet..just remove it */
iter = q.erase(iter);
}
}
}
}
};
class SQLConnection : public classbase class SQLConnection : public classbase
{ {
protected: protected:
@ -201,34 +316,28 @@ class SQLConnection : public classbase
}; };
typedef std::vector<SQLConnection> ConnectionList; ConnMap Connections;
class ModuleSQL : public Module void ConnectDatabases(Server* Srv)
{ {
Server *Srv; for (ConnMap::iterator i = Connections.begin(); i != Connections.end(); i++)
ConfigReader *Conf;
ConnectionList Connections;
public:
void ConnectDatabases()
{ {
for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++) i->second->Enable();
if (i->second->Connect())
{ {
i->Enable(); Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->second->GetHost());
if (i->Connect())
{
Srv->Log(DEFAULT,"SQL: Successfully connected database "+i->GetHost());
} }
else else
{ {
Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->GetHost()+": Error: "+i->GetError()); Srv->Log(DEFAULT,"SQL: Failed to connect database "+i->second->GetHost()+": Error: "+i->second->GetError());
i->Disable(); i->second->Disable();
}
} }
} }
}
void LoadDatabases(ConfigReader* ThisConf)
{ void LoadDatabases(ConfigReader* ThisConf, Server* Srv)
{
Srv->Log(DEFAULT,"SQL: Loading database settings"); Srv->Log(DEFAULT,"SQL: Loading database settings");
Connections.clear(); Connections.clear();
Srv->Log(DEBUG,"Cleared connections"); Srv->Log(DEBUG,"Cleared connections");
@ -242,79 +351,23 @@ class ModuleSQL : public Module
Srv->Log(DEBUG,"Read database settings"); Srv->Log(DEBUG,"Read database settings");
if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != "")) if ((db != "") && (host != "") && (user != "") && (id != "") && (pass != ""))
{ {
SQLConnection ThisSQL(host,user,pass,db,atoi(id.c_str())); SQLConnection* ThisSQL = new SQLConnection(host,user,pass,db,atoi(id.c_str()));
Srv->Log(DEFAULT,"Loaded database: "+ThisSQL.GetHost()); Srv->Log(DEFAULT,"Loaded database: "+ThisSQL->GetHost());
Connections.push_back(ThisSQL); Connections[id] = ThisSQL;
Srv->Log(DEBUG,"Pushed back connection"); Srv->Log(DEBUG,"Pushed back connection");
} }
} }
ConnectDatabases(); ConnectDatabases(Srv);
} }
void ResultType(SQLRequest *r, SQLResult *res) void* DispatcherThread(void* arg);
{
for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
{
if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
{
bool xr = i->QueryResult(r->GetQuery());
if (!xr)
{
res->SetType(SQL_ERROR);
res->SetError(i->GetError());
return;
}
res->SetType(SQL_OK);
return;
}
}
}
void CountType(SQLRequest *r, SQLResult* res) class ModuleSQL : public Module
{ {
for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++) public:
{ Server *Srv;
if ((i->GetID() == r->GetConnID()) && (i->IsEnabled())) ConfigReader *Conf;
{ pthread_t Dispatcher;
res->SetType(SQL_COUNT);
res->SetCount(i->QueryCount(r->GetQuery()));
return;
}
}
}
void DoneType(SQLRequest *r, SQLResult* res)
{
for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
{
if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
{
res->SetType(SQL_DONE);
if (!i->QueryDone())
res->SetType(SQL_ERROR);
}
}
}
void RowType(SQLRequest *r, SQLResult* res)
{
for (ConnectionList::iterator i = Connections.begin(); i != Connections.end(); i++)
{
if ((i->GetID() == r->GetConnID()) && (i->IsEnabled()))
{
log(DEBUG,"*** FOUND MATCHING ROW");
std::map<std::string,std::string> row = i->GetRow();
res->SetRow(row);
res->SetType(SQL_ROW);
if (!row.size())
{
log(DEBUG,"ROW SIZE IS 0");
res->SetType(SQL_END);
}
return;
}
}
}
void Implements(char* List) void Implements(char* List)
{ {
@ -323,27 +376,6 @@ class ModuleSQL : public Module
char* OnRequest(Request* request) char* OnRequest(Request* request)
{ {
if (request)
{
SQLResult* Result = new SQLResult();
SQLRequest *r = (SQLRequest*)request->GetData();
switch (r->GetQueryType())
{
case SQL_RESULT:
ResultType(r,Result);
break;
case SQL_COUNT:
CountType(r,Result);
break;
case SQL_ROW:
RowType(r,Result);
break;
case SQL_DONE:
DoneType(r,Result);
break;
}
return (char*)Result;
}
return NULL; return NULL;
} }
@ -352,29 +384,41 @@ class ModuleSQL : public Module
{ {
Srv = Me; Srv = Me;
Conf = new ConfigReader(); Conf = new ConfigReader();
LoadDatabases(Conf); pthread_attr_t attribs;
pthread_attr_init(&attribs);
pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED);
if (pthread_create(&this->Dispatcher, &attribs, DispatcherThread, (void *)this) != 0)
{
log(DEBUG,"m_mysql: Failed to create dispatcher thread: %s", strerror(errno));
}
} }
virtual ~ModuleSQL() virtual ~ModuleSQL()
{ {
Connections.clear();
DELETE(Conf); DELETE(Conf);
} }
virtual void OnRehash(const std::string &parameter) virtual void OnRehash(const std::string &parameter)
{ {
DELETE(Conf); /* TODO: set rehash bool here, which makes the dispatcher thread rehash at next opportunity */
Conf = new ConfigReader();
LoadDatabases(Conf);
} }
virtual Version GetVersion() virtual Version GetVersion()
{ {
return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER); return Version(1,1,0,0,VF_VENDOR|VF_SERVICEPROVIDER);
} }
}; };
void* DispatcherThread(void* arg)
{
ModuleSQL* thismodule = (ModuleSQL*)arg;
LoadDatabases(thismodule->Conf, thismodule->Srv);
return NULL;
}
// stuff down here is the module-factory stuff. For basic modules you can ignore this. // stuff down here is the module-factory stuff. For basic modules you can ignore this.
class ModuleSQLFactory : public ModuleFactory class ModuleSQLFactory : public ModuleFactory