Changeset - e585f67f9f49
[Not reviewed]
0 1 0
HanzZ - 13 years ago 2013-02-12 21:00:17
hanzz.k@gmail.com
Remove useles #defines
1 file changed with 0 insertions and 28 deletions:
0 comments (0 inline, 0 general)
backends/skype/skypeplugin.cpp
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "skypeplugin.h"
 
#include "skype.h"
 
#include "skypedb.h"
 

	
 
#include "transport/config.h"
 
#include "transport/logging.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#include "transport/memoryusage.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/userregistration.h"
 
#include "transport/user.h"
 
#include "transport/storagebackend.h"
 
#include "transport/rostermanager.h"
 
#include "transport/conversation.h"
 
#include "transport/networkplugin.h"
 
#include <boost/filesystem.hpp>
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
// #include "valgrind/memcheck.h"
 
#ifndef __FreeBSD__
 
#include "malloc.h"
 
#endif
 

	
 
// Prepare the SQL statement
 
#define PREP_STMT(sql, str) \
 
	if(sqlite3_prepare_v2(db, std::string(str).c_str(), -1, &sql, NULL)) { \
 
		LOG4CXX_ERROR(logger, str<< (sqlite3_errmsg(db) == NULL ? "" : sqlite3_errmsg(db))); \
 
		sql = NULL; \
 
	}
 

	
 
// Finalize the prepared statement
 
#define FINALIZE_STMT(prep) \
 
	if(prep != NULL) { \
 
		sqlite3_finalize(prep); \
 
	}
 
	
 
#define BEGIN(STATEMENT) 	sqlite3_reset(STATEMENT);\
 
							int STATEMENT##_id = 1;\
 
							int STATEMENT##_id_get = 0;\
 
							(void)STATEMENT##_id_get;
 

	
 
#define BIND_INT(STATEMENT, VARIABLE) sqlite3_bind_int(STATEMENT, STATEMENT##_id++, VARIABLE)
 
#define BIND_STR(STATEMENT, VARIABLE) sqlite3_bind_text(STATEMENT, STATEMENT##_id++, VARIABLE.c_str(), -1, SQLITE_STATIC)
 
#define RESET_GET_COUNTER(STATEMENT)	STATEMENT##_id_get = 0;
 
#define GET_INT(STATEMENT)	sqlite3_column_int(STATEMENT, STATEMENT##_id_get++)
 
#define GET_STR(STATEMENT)	(const char *) sqlite3_column_text(STATEMENT, STATEMENT##_id_get++)
 
#define GET_BLOB(STATEMENT)	(const void *) sqlite3_column_blob(STATEMENT, STATEMENT##_id_get++)
 
#define EXECUTE_STATEMENT(STATEMENT, NAME) 	if(sqlite3_step(STATEMENT) != SQLITE_DONE) {\
 
		LOG4CXX_ERROR(logger, NAME<< (sqlite3_errmsg(db) == NULL ? "" : sqlite3_errmsg(db)));\
 
			}
 

	
 
using namespace Transport;
 

	
 
DEFINE_LOGGER(logger, "SkypePlugin");
 

	
 
static int m_sock;
 

	
 
static gboolean transportDataReceived(GIOChannel *source, GIOCondition condition, gpointer data) {
 
	SkypePlugin *np = (SkypePlugin *) data;
 
	char buffer[65535];
 
	char *ptr = buffer;
 
	ssize_t n = read(m_sock, ptr, sizeof(buffer));
 
	if (n <= 0) {
 
		LOG4CXX_INFO(logger, "Diconnecting from spectrum2 server");
 
		Logging::shutdownLogging();
 
		exit(errno);
 
	}
 
	std::string d = std::string(buffer, n);
 
	np->handleDataRead(d);
 
	return TRUE;
 
}
 

	
 
static int create_socket(const char *host, int portno) {
 
	struct sockaddr_in serv_addr;
 
	
 
	int m_sock = socket(AF_INET, SOCK_STREAM, 0);
 
	memset((char *) &serv_addr, 0, sizeof(serv_addr));
 
	serv_addr.sin_family = AF_INET;
 
	serv_addr.sin_port = htons(portno);
 

	
 
	hostent *hos;  // Resolve name
 
	if ((hos = gethostbyname(host)) == NULL) {
 
		// strerror() will not work for gethostbyname() and hstrerror() 
 
		// is supposedly obsolete
 
		Logging::shutdownLogging();
 
		exit(1);
 
	}
 
	serv_addr.sin_addr.s_addr = *((unsigned long *) hos->h_addr_list[0]);
 

	
 
	if (connect(m_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
 
		close(m_sock);
 
		m_sock = 0;
 
	}
 

	
 
	int flags = fcntl(m_sock, F_GETFL);
 
	flags |= O_NONBLOCK;
 
	fcntl(m_sock, F_SETFL, flags);
 
	return m_sock;
 
}
 

	
 
static void io_destroy(gpointer data) {
 
	Logging::shutdownLogging();
 
	exit(1);
 
}
 

	
 
SkypePlugin::SkypePlugin(Config *config, const std::string &host, int port) : NetworkPlugin() {
 
	this->config = config;
 
	LOG4CXX_INFO(logger, "Starting the backend.");
 

	
 
	m_sock = create_socket(host.c_str(), port);
 
	GIOChannel *channel;
 
	GIOCondition cond = (GIOCondition) G_IO_IN;
 
	channel = g_io_channel_unix_new(m_sock);
 
	g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, transportDataReceived, this, io_destroy);
 
}
 

	
 
SkypePlugin::~SkypePlugin() {
 
	for (std::map<Skype *, std::string>::iterator it = m_accounts.begin(); it != m_accounts.end(); it++) {
 
		delete (*it).first;
 
	}
 
}
 

	
 
void SkypePlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
	std::string name = legacyName;
 
	if (name.find("skype.") == 0 || name.find("prpl-skype.") == 0) {
 
		name = name.substr(name.find(".") + 1);
 
	}
 
	LOG4CXX_INFO(logger,  "Creating account with name '" << name << "'");
 

	
 
	Skype *skype = new Skype(this, user, name, password);
 
	m_sessions[user] = skype;
 
	m_accounts[skype] = user;
 

	
 
	skype->login();
 
}
 

	
 
void SkypePlugin::handleMemoryUsage(double &res, double &shared) {
 
	res = 0;
 
	shared = 0;
 
	for(std::map<std::string, Skype *>::const_iterator it = m_sessions.begin(); it != m_sessions.end(); it++) {
 
		Skype *skype = it->second;
 
		if (skype) {
 
			double r;
 
			double s;
 
			process_mem_usage(s, r, skype->getPid());
 
			res += r;
 
			shared += s;
0 comments (0 inline, 0 general)