]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_pgsql.cpp
Update Event and Request APIs
[user/henk/code/inspircd.git] / src / modules / extra / m_pgsql.cpp
index 3bda30fb38b396c67e0a7ff40fcb5d11b01fb089..df0ca7f4266bb88430a3aa9b9d894822bef4b9d9 100644 (file)
@@ -47,7 +47,7 @@ enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE, RREAD, RWRITE };
 unsigned long count(const char * const str, char a)
 {
        unsigned long n = 0;
-       for (const char *p = reinterpret_cast<const char *>(str); *p; ++p)
+       for (const char *p = str; *p; ++p)
        {
                if (*p == '?')
                        ++n;
@@ -59,10 +59,10 @@ unsigned long count(const char * const str, char a)
  */
 std::string SQLhost::GetDSN()
 {
-       std::ostringstream conninfo("connect_timeout = '2'");
+       std::ostringstream conninfo("connect_timeout = '5'");
 
-       if (ip.length())
-               conninfo << " hostaddr = '" << ip << "'";
+       if (host.length())
+               conninfo << " host = '" << host << "'";
 
        if (port)
                conninfo << " port = '" << port << "'";
@@ -91,37 +91,15 @@ std::string SQLhost::GetDSN()
 class ReconnectTimer : public Timer
 {
  private:
-       Module* mod;
+       Module* const mod;
  public:
-       ReconnectTimer(InspIRCd* SI, Module* m)
-       : Timer(5, SI->Time(), false), mod(m)
+       ReconnectTimer(Module* m) : Timer(5, ServerInstance->Time(), false), mod(m)
        {
        }
        virtual void Tick(time_t TIME);
 };
 
 
-/** Used to resolve sql server hostnames
- */
-class SQLresolver : public Resolver
-{
- private:
-       SQLhost host;
-       Module* mod;
- public:
-       SQLresolver(Module* m, InspIRCd* Instance, const SQLhost& hi, bool &cached)
-       : Resolver(Instance, hi.host, DNS_QUERY_FORWARD, cached, (Module*)m), host(hi), mod(m)
-       {
-       }
-
-       virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0);
-
-       virtual void OnError(ResolverError e, const std::string &errormessage)
-       {
-               ServerInstance->Logs->Log("m_pgsql",DEBUG, "PgSQL: DNS lookup failed (%s), dying horribly", errormessage.c_str());
-       }
-};
-
 /** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult.
  * All SQL providers must create their own subclass and define it's methods using that
  * database library's data retriveal functions. The aim is to avoid a slow and inefficient process
@@ -322,7 +300,6 @@ public:
 class SQLConn : public EventHandler
 {
  private:
-       InspIRCd*               ServerInstance;
        SQLhost                 confhost;       /* The <database> entry */
        Module*                 us;                     /* Pointer to the SQL provider itself */
        PGconn*                 sql;            /* PgSQL database connection handle */
@@ -332,10 +309,10 @@ class SQLConn : public EventHandler
        time_t                  idle;           /* Time we last heard from the database */
 
  public:
-       SQLConn(InspIRCd* SI, Module* self, const SQLhost& hi)
-       : EventHandler(), ServerInstance(SI), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
+       SQLConn(Module* self, const SQLhost& hi)
+       : EventHandler(), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
        {
-               idle = this->ServerInstance->Time();
+               idle = ServerInstance->Time();
                if(!DoConnect())
                {
                        ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: Could not connect to database with id: " + ConvToStr(hi.id));
@@ -388,7 +365,7 @@ class SQLConn : public EventHandler
                if(this->fd <= -1)
                        return false;
 
-               if (!this->ServerInstance->SE->AddFd(this))
+               if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_WRITE | FD_WANT_NO_READ))
                {
                        ServerInstance->Logs->Log("m_pgsql",DEBUG, "BUG: Couldn't add pgsql socket to socket engine");
                        return false;
@@ -403,15 +380,17 @@ class SQLConn : public EventHandler
                switch(PQconnectPoll(sql))
                {
                        case PGRES_POLLING_WRITING:
-                               ServerInstance->SE->WantWrite(this);
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ);
                                status = CWRITE;
                                return true;
                        case PGRES_POLLING_READING:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = CREAD;
                                return true;
                        case PGRES_POLLING_FAILED:
                                return false;
                        case PGRES_POLLING_OK:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = WWRITE;
                                return DoConnectedPoll();
                        default:
@@ -424,8 +403,8 @@ class SQLConn : public EventHandler
                if(!qinprog && queue.totalsize())
                {
                        /* There's no query currently in progress, and there's queries in the queue. */
-                       SQLrequest& query = queue.front();
-                       DoQuery(query);
+                       SQLrequest* query = queue.front();
+                       DoQuery(*query);
                }
 
                if(PQconsumeInput(sql))
@@ -433,7 +412,7 @@ class SQLConn : public EventHandler
                        /* We just read stuff from the server, that counts as it being alive
                         * so update the idle-since time :p
                         */
-                       idle = this->ServerInstance->Time();
+                       idle = ServerInstance->Time();
 
                        if (PQisBusy(sql))
                        {
@@ -442,10 +421,10 @@ class SQLConn : public EventHandler
                        else if (qinprog)
                        {
                                /* Grab the request we're processing */
-                               SQLrequest& query = queue.front();
+                               SQLrequest* query = queue.front();
 
                                /* Get a pointer to the module we're about to return the result to */
-                               Module* to = query.GetSource();
+                               Module* to = query->source;
 
                                /* Fetch the result.. */
                                PGresult* result = PQgetResult(sql);
@@ -465,10 +444,10 @@ class SQLConn : public EventHandler
                                if(to)
                                {
                                        /* ..and the result */
-                                       PgSQLresult reply(us, to, query.id, result);
+                                       PgSQLresult reply(us, to, query->id, result);
 
                                        /* Fix by brain, make sure the original query gets sent back in the reply */
-                                       reply.query = query.query.q;
+                                       reply.query = query->query.q;
 
                                        switch(PQresultStatus(result))
                                        {
@@ -517,15 +496,17 @@ class SQLConn : public EventHandler
                switch(PQresetPoll(sql))
                {
                        case PGRES_POLLING_WRITING:
-                               ServerInstance->SE->WantWrite(this);
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ);
                                status = CWRITE;
                                return DoPoll();
                        case PGRES_POLLING_READING:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = CREAD;
                                return true;
                        case PGRES_POLLING_FAILED:
                                return false;
                        case PGRES_POLLING_OK:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = WWRITE;
                                return DoConnectedPoll();
                        default:
@@ -728,13 +709,13 @@ class SQLConn : public EventHandler
 
        SQLerror Query(const SQLrequest &req)
        {
-               queue.push(req);
+               queue.push(new SQLrequest(req));
 
                if(!qinprog && queue.totalsize())
                {
                        /* There's no query currently in progress, and there's queries in the queue. */
-                       SQLrequest& query = queue.front();
-                       return DoQuery(query);
+                       SQLrequest* query = queue.front();
+                       return DoQuery(*query);
                }
                else
                {
@@ -754,11 +735,11 @@ class SQLConn : public EventHandler
 
        void Close()
        {
-               if (!this->ServerInstance->SE->DelFd(this))
+               if (!ServerInstance->SE->DelFd(this))
                {
                        if (sql && PQstatus(sql) == CONNECTION_BAD)
                        {
-                               this->ServerInstance->SE->DelFd(this, true);
+                               ServerInstance->SE->DelFd(this, true);
                        }
                        else
                        {
@@ -784,8 +765,8 @@ class ModulePgSQL : public Module
        ReconnectTimer* retimer;
 
  public:
-       ModulePgSQL(InspIRCd* Me)
-       : Module(Me), currid(0)
+       ModulePgSQL()
+       : currid(0)
        {
                ServerInstance->Modules->UseInterface("SQLutils");
 
@@ -801,8 +782,8 @@ class ModulePgSQL : public Module
                ReadConf();
 
                ServerInstance->Modules->PublishInterface("SQL", this);
-               Implementation eventlist[] = { I_OnUnloadModule, I_OnRequest, I_OnRehash, I_OnUserRegister, I_OnCheckReady, I_OnUserDisconnect };
-               ServerInstance->Modules->Attach(eventlist, this, 6);
+               Implementation eventlist[] = { I_OnUnloadModule, I_OnRehash };
+               ServerInstance->Modules->Attach(eventlist, this, 2);
        }
 
        virtual ~ModulePgSQL()
@@ -817,7 +798,7 @@ class ModulePgSQL : public Module
        }
 
 
-       virtual void OnRehash(User* user, const std::string &parameter)
+       virtual void OnRehash(User* user)
        {
                ReadConf();
        }
@@ -834,7 +815,7 @@ class ModulePgSQL : public Module
 
        bool HostInConf(const SQLhost &h)
        {
-               ConfigReader conf(ServerInstance);
+               ConfigReader conf;
                for(int i = 0; i < conf.Enumerate("database"); i++)
                {
                        SQLhost host;
@@ -855,11 +836,10 @@ class ModulePgSQL : public Module
        {
                ClearOldConnections();
 
-               ConfigReader conf(ServerInstance);
+               ConfigReader conf;
                for(int i = 0; i < conf.Enumerate("database"); i++)
                {
                        SQLhost host;
-                       int ipvalid;
 
                        host.id         = conf.ReadValue("database", "id", i);
                        host.host       = conf.ReadValue("database", "hostname", i);
@@ -872,46 +852,7 @@ class ModulePgSQL : public Module
                        if (HasHost(host))
                                continue;
 
-#ifdef IPV6
-                       if (strchr(host.host.c_str(),':'))
-                       {
-                               in6_addr blargle;
-                               ipvalid = inet_pton(AF_INET6, host.host.c_str(), &blargle);
-                       }
-                       else
-#endif
-                       {
-                               in_addr blargle;
-                               ipvalid = inet_aton(host.host.c_str(), &blargle);
-                       }
-
-                       if(ipvalid > 0)
-                       {
-                               /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
-                               host.ip = host.host;
-                               this->AddConn(host);
-                       }
-                       else if(ipvalid == 0)
-                       {
-                               /* Conversion failed, assume it's a host */
-                               SQLresolver* resolver;
-
-                               try
-                               {
-                                       bool cached;
-                                       resolver = new SQLresolver(this, ServerInstance, host, cached);
-                                       ServerInstance->AddResolver(resolver, cached);
-                               }
-                               catch(...)
-                               {
-                                       /* THE WORLD IS COMING TO AN END! */
-                               }
-                       }
-                       else
-                       {
-                               /* Invalid address family, die horribly. */
-                               ServerInstance->Logs->Log("m_pgsql",DEBUG, "BUG: insp_aton failed returning -1, oh noes.");
-                       }
+                       this->AddConn(host);
                }
        }
 
@@ -944,14 +885,13 @@ class ModulePgSQL : public Module
        {
                if (HasHost(hi))
                {
-                       ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: A pgsql connection with id: %s already exists, possibly due to DNS delay. Aborting connection attempt.", hi.id.c_str());
+                       ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: A pgsql connection with id: %s already exists. Aborting connection attempt.", hi.id.c_str());
                        return;
                }
 
                SQLConn* newconn;
 
-               /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
-               newconn = new SQLConn(ServerInstance, this, hi);
+               newconn = new SQLConn(this, hi);
 
                connections.insert(std::make_pair(hi.id, newconn));
        }
@@ -967,31 +907,27 @@ class ModulePgSQL : public Module
                                break;
                        }
                }
-               retimer = new ReconnectTimer(ServerInstance, this);
+               retimer = new ReconnectTimer(this);
                ServerInstance->Timers->AddTimer(retimer);
        }
 
-       virtual const char* OnRequest(Request* request)
+       void OnRequest(Request& request)
        {
-               if(strcmp(SQLREQID, request->GetId()) == 0)
+               if(strcmp(SQLREQID, request.id) == 0)
                {
-                       SQLrequest* req = (SQLrequest*)request;
+                       SQLrequest* req = (SQLrequest*)&request;
                        ConnMap::iterator iter;
                        if((iter = connections.find(req->dbid)) != connections.end())
                        {
                                /* Execute query */
                                req->id = NewID();
                                req->error = iter->second->Query(*req);
-
-                               return (req->error.Id() == SQL_NO_ERROR) ? sqlsuccess : NULL;
                        }
                        else
                        {
                                req->error.Id(SQL_BAD_DBID);
-                               return NULL;
                        }
                }
-               return NULL;
        }
 
        virtual void OnUnloadModule(Module* mod, const std::string&     name)
@@ -1018,23 +954,10 @@ class ModulePgSQL : public Module
 
        virtual Version GetVersion()
        {
-               return Version("$Id$", VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
+               return Version("PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API", VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
        }
 };
 
-/* move this here to use AddConn, rather that than having the whole
- * module above SQLConn, since this is buggin me right now :/
- */
-void SQLresolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum)
-{
-       if (!resultnum)
-       {
-               host.ip = result;
-               ((ModulePgSQL*)mod)->AddConn(host);
-               ((ModulePgSQL*)mod)->ClearOldConnections();
-       }
-}
-
 void ReconnectTimer::Tick(time_t time)
 {
        ((ModulePgSQL*)mod)->ReadConf();