InvisiblePayload + more mysql tweaks

This commit is contained in:
HanzZ 2011-08-13 01:48:13 +02:00
parent b9fc585e26
commit c38103451c
9 changed files with 230 additions and 12 deletions

View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Elements/InvisiblePayload.h>
namespace Swift {
// This payload is NOT part of ANY XEP and it is only
// libtransport related extension.
InvisiblePayload::InvisiblePayload() {
}
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>
#include <Swiften/Elements/Payload.h>
// This payload is NOT part of ANY XEP and it is only
// libtransport related extension.
namespace Swift {
class InvisiblePayload : public Payload {
public:
typedef boost::shared_ptr<InvisiblePayload> ref;
public:
InvisiblePayload();
};
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Parser/PayloadParsers/InvisibleParser.h>
namespace Swift {
// This payload is NOT part of ANY XEP and it is only
// libtransport related extension.
InvisibleParser::InvisibleParser() /*: level_(0)*/ {
}
void InvisibleParser::handleStartElement(const std::string& /*element*/, const std::string& /*ns*/, const AttributeMap& /*attributes*/) {
}
void InvisibleParser::handleEndElement(const std::string&, const std::string&) {
}
void InvisibleParser::handleCharacterData(const std::string&) {
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <Swiften/Elements/InvisiblePayload.h>
#include <Swiften/Parser/GenericPayloadParser.h>
// This payload is NOT part of ANY XEP and it is only
// libtransport related extension.
namespace Swift {
class InvisibleParser : public GenericPayloadParser<InvisiblePayload> {
public:
InvisibleParser();
virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes);
virtual void handleEndElement(const std::string& element, const std::string&);
virtual void handleCharacterData(const std::string& data);
// private:
// int level_;
};
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h>
#include <boost/shared_ptr.hpp>
#include <Swiften/Serializer/XML/XMLTextNode.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/XML/XMLElement.h>
namespace Swift {
// This payload is NOT part of ANY XEP and it is only
// libtransport related extension.
InvisibleSerializer::InvisibleSerializer() : GenericPayloadSerializer<InvisiblePayload>() {
}
std::string InvisibleSerializer::serializePayload(boost::shared_ptr<InvisiblePayload> attention) const {
XMLElement attentionElement("invisible", "urn:xmpp:invisible:0");
return attentionElement.serialize();
}
}

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2011 Jan Kaluza
* Licensed under the Simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#pragma once
#include <Swiften/Serializer/GenericPayloadSerializer.h>
#include <Swiften/Elements/InvisiblePayload.h>
// This payload is NOT part of ANY XEP and it is only
// libtransport related extension.
namespace Swift {
class InvisibleSerializer : public GenericPayloadSerializer<InvisiblePayload> {
public:
InvisibleSerializer();
virtual std::string serializePayload(boost::shared_ptr<InvisiblePayload>) const;
};
}

View file

@ -100,6 +100,8 @@ class MySQLBackend : public StorageBackend
bool execute();
bool fetch();
// Pushes new data used as input for the statement.
template <typename T>
Statement& operator << (const T& t);
@ -109,10 +111,13 @@ class MySQLBackend : public StorageBackend
// Pulls fetched data by previous execute(); call.
template <typename T>
Statement& operator >> (T& t);
Statement& operator >> (std::string& t);
private:
MYSQL_STMT *m_stmt;
MYSQL *m_conn;
std::vector<MYSQL_BIND> m_params;
std::vector<MYSQL_BIND> m_results;
int m_resultOffset;
int m_offset;
int m_error;
@ -125,8 +130,8 @@ class MySQLBackend : public StorageBackend
// statements
// MYSQL_STMT *m_setUser;
Statement * m_setUser;
MYSQL_STMT *m_getUser;
Statement *m_setUser;
Statement *m_getUser;
MYSQL_STMT *m_getUserSetting;
MYSQL_STMT *m_setUserSetting;
MYSQL_STMT *m_updateUserSetting;

View file

@ -87,7 +87,7 @@ MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const
return;
}
for (int i = 0; i < format.length(); i++) {
for (int i = 0; i < format.length() && m_resultOffset == -1; i++) {
switch (format.at(i)) {
case 's':
m_params.resize(m_params.size() + 1);
@ -117,21 +117,57 @@ MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const
m_params.back().is_null= 0;
m_params.back().length= (unsigned long *) malloc(sizeof(unsigned long));
break;
// case 'b':
// m_params.push_back(NULL);
// break;
case '|':
m_resultOffset = i;
break;
}
}
if (m_resultOffset < 0)
m_resultOffset = format.size();
for (int i = m_resultOffset; i < format.length(); i++) {
switch (format.at(i)) {
case 's':
m_results.resize(m_results.size() + 1);
memset(&m_results.back(), 0, sizeof(MYSQL_BIND));
m_results.back().buffer_type= MYSQL_TYPE_STRING;
m_results.back().buffer= (char *) malloc(sizeof(char) * 4096);
m_results.back().buffer_length= 4096;
m_results.back().is_null= 0;
m_results.back().length= (unsigned long *) malloc(sizeof(unsigned long));
break;
case 'i':
m_results.resize(m_results.size() + 1);
memset(&m_results.back(), 0, sizeof(MYSQL_BIND));
m_results.back().buffer_type= MYSQL_TYPE_LONG;
m_results.back().buffer= (unsigned long *) malloc(sizeof(unsigned long));
m_results.back().is_null= 0;
m_results.back().length= (unsigned long *) malloc(sizeof(unsigned long));
break;
case 'b':
m_results.resize(m_results.size() + 1);
memset(&m_results.back(), 0, sizeof(MYSQL_BIND));
m_results.back().buffer_type= MYSQL_TYPE_TINY;
m_results.back().buffer= (bool *) malloc(sizeof(bool));
m_results.back().is_null= 0;
m_results.back().length= (unsigned long *) malloc(sizeof(unsigned long));
break;
}
}
if (mysql_stmt_bind_param(m_stmt, &m_params.front())) {
LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
}
if (m_resultOffset < 0)
m_resultOffset = format.size();
else {
if (mysql_stmt_bind_result(m_stmt, &m_results.front())) {
LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
}
}
m_resultOffset = 0;
}
MySQLBackend::Statement::~Statement() {
@ -156,6 +192,10 @@ bool MySQLBackend::Statement::execute() {
return true;
}
bool MySQLBackend::Statement::fetch() {
return mysql_stmt_fetch(m_stmt);
}
template <typename T>
MySQLBackend::Statement& MySQLBackend::Statement::operator << (const T& t) {
if (m_offset >= m_resultOffset)
@ -183,7 +223,25 @@ MySQLBackend::Statement& MySQLBackend::Statement::operator >> (T& t) {
if (m_offset < m_resultOffset)
return *this;
std::swap(t, *(T *) m_params[m_offset]);
if (!m_params[m_offset].is_null) {
T *data = (T *) m_params[m_offset].buffer;
t = *data;
}
if (++m_offset == m_params.size())
m_offset = 0;
return *this;
}
MySQLBackend::Statement& MySQLBackend::Statement::operator >> (std::string& t) {
std::cout << "getting " << m_offset << " " << m_resultOffset << "\n";
if (m_offset < m_resultOffset)
return *this;
if (!m_params[m_offset].is_null) {
t = (char *) m_params[m_offset].buffer;
}
if (++m_offset == m_params.size())
m_offset = 0;
return *this;
@ -198,7 +256,7 @@ MySQLBackend::MySQLBackend(Config *config) {
MySQLBackend::~MySQLBackend(){
// FINALIZE_STMT(m_setUser);
delete m_setUser;
FINALIZE_STMT(m_getUser);
delete m_getUser;
FINALIZE_STMT(m_removeUser);
FINALIZE_STMT(m_removeUserBuddies);
FINALIZE_STMT(m_removeUserSettings);
@ -232,7 +290,7 @@ bool MySQLBackend::connect() {
createDatabase();
m_setUser = new Statement(&m_conn, "sssssb", "INSERT INTO " + m_prefix + "users (jid, uin, password, language, encoding, last_login, vip) VALUES (?, ?, ?, ?, ?, NOW(), ?)");
PREP_STMT(m_getUser, "SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=?");
m_getUser = new Statement(&m_conn, "s|isssssb", "SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=?");
PREP_STMT(m_removeUser, "DELETE FROM " + m_prefix + "users WHERE id=?");
PREP_STMT(m_removeUserBuddies, "DELETE FROM " + m_prefix + "buddies WHERE user_id=?");
@ -324,7 +382,15 @@ void MySQLBackend::setUser(const UserInfo &user) {
}
bool MySQLBackend::getUser(const std::string &barejid, UserInfo &user) {
return false;
*m_getUser << barejid;
if (!m_getUser->execute())
return false;
m_getUser->fetch();
*m_getUser >> user.id >> user.jid >> user.uin >> user.password >> user.encoding >> user.language >> user.vip;
std::cout << user.id << " " << user.jid << " " << user.uin << " " << user.password << " " << user.encoding << " " << user.language << " " << user.vip << "\n";
return true;
}
void MySQLBackend::setUserOnline(long id, bool online) {

View file

@ -35,6 +35,8 @@
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
#include "Swiften/Parser/PayloadParsers/BlockParser.h"
#include "Swiften/Serializer/PayloadSerializers/BlockSerializer.h"
#include "Swiften/Parser/PayloadParsers/InvisibleParser.h"
#include "Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h"
#include "log4cxx/logger.h"
#include "log4cxx/consoleappender.h"
#include "log4cxx/patternlayout.h"
@ -93,10 +95,12 @@ Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory, T
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::BlockParser>("block", "urn:xmpp:block:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("block", "urn:xmpp:invisible:0"));
m_server->addPayloadSerializer(new Swift::AttentionSerializer());
m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer());
m_server->addPayloadSerializer(new Swift::BlockSerializer());
m_server->addPayloadSerializer(new Swift::InvisibleSerializer());
m_server->onDataRead.connect(bind(&Component::handleDataRead, this, _1));
m_server->onDataWritten.connect(bind(&Component::handleDataWritten, this, _1));