Changeset - 1b8fef4d70b5
[Not reviewed]
0 1 0
HanzZ - 13 years ago 2012-02-17 15:09:35
hanzz.k@gmail.com
Skype backend is now non-blocking
1 file changed with 81 insertions and 43 deletions:
0 comments (0 inline, 0 general)
backends/skype/main.cpp
Show inline comments
 
@@ -94,6 +94,9 @@ class Skype {
 
			return m_username;
 
		}
 

	
 
		bool createDBusProxy();
 
		bool loadSkypeBuddies();
 

	
 
	private:
 
		std::string m_username;
 
		std::string m_password;
 
@@ -101,6 +104,9 @@ class Skype {
 
		DBusGConnection *m_connection;
 
		DBusGProxy *m_proxy;
 
		std::string m_user;
 
		int m_timer;
 
		int m_counter;
 
		int fd_output;
 
};
 

	
 
class SpectrumNetworkPlugin : public NetworkPlugin {
 
@@ -296,6 +302,7 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
		
 
};
 

	
 

	
 
Skype::Skype(const std::string &user, const std::string &username, const std::string &password) {
 
	m_username = username;
 
	m_user = user;
 
@@ -303,6 +310,53 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
	m_pid = 0;
 
	m_connection = 0;
 
	m_proxy = 0;
 
	m_timer = -1;
 
	m_counter = 0;
 
}
 

	
 

	
 
static gboolean load_skype_buddies(gpointer data) {
 
	Skype *skype = (Skype *) data;
 
	return skype->loadSkypeBuddies();
 
}
 

	
 
bool Skype::createDBusProxy() {
 
	if (m_proxy == NULL) {
 
		LOG4CXX_INFO(logger, "Creating DBus proxy for com.Skype.Api.");
 
		m_counter++;
 

	
 
		GError *error = NULL;
 
		m_proxy = dbus_g_proxy_new_for_name_owner (m_connection, "com.Skype.API", "/com/Skype", "com.Skype.API", &error);
 
		if (m_proxy == NULL && error != NULL) {
 
			LOG4CXX_INFO(logger,  m_username << ":" << error->message);
 

	
 
			if (m_counter == 15) {
 
				np->handleDisconnected(m_user, 0, error->message);
 
				logout();
 
				g_error_free(error);
 
				return FALSE;
 
			}
 
			g_error_free(error);
 
		}
 

	
 
		if (m_proxy) {
 
			LOG4CXX_INFO(logger, "Proxy created.");
 
			DBusObjectPathVTable vtable;
 
			vtable.message_function = &skype_notify_handler;
 
			dbus_connection_register_object_path(dbus_g_connection_get_connection(m_connection), "/com/Skype/Client", &vtable, this);
 

	
 
			m_counter = 0;
 
			m_timer = g_timeout_add_seconds(1, load_skype_buddies, this);
 
			return FALSE;
 
		}
 
		return TRUE;
 
	}
 
	return FALSE;
 
}
 

	
 
static gboolean create_dbus_proxy(gpointer data) {
 
	Skype *skype = (Skype *) data;
 
	return skype->createDBusProxy();
 
}
 

	
 
void Skype::login() {
 
@@ -339,6 +393,7 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
								"</UI>\n"
 
							"</config>\n";
 
	g_file_set_contents(std::string(std::string("/tmp/skype/") + m_username + "/" + m_username +"/config.xml").c_str(), config_xml.c_str(), -1, NULL);
 

	
 
	std::string db_path = std::string("/tmp/skype/") + m_username;
 
	char *db = (char *) malloc(db_path.size() + 1);
 
	strcpy(db, db_path.c_str());
 
@@ -346,7 +401,6 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
	gchar* argv[6] = {"skype", "--disable-cleanlooks", "--pipelogin", "--dbpath", db, 0};
 

	
 
	int fd;
 
			int fd_output;
 
	g_spawn_async_with_pipes(NULL,
 
		argv,
 
		NULL /*envp*/,
 
@@ -367,15 +421,13 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 

	
 
	free(db);
 

	
 
			GError *error = NULL;
 
			DBusObjectPathVTable vtable;
 

	
 
	//Initialise threading
 
	dbus_threads_init_default();
 

	
 
	if (m_connection == NULL)
 
	{
 
		LOG4CXX_INFO(logger, "Creating DBus connection.");
 
		GError *error = NULL;
 
		m_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
		if (m_connection == NULL && error != NULL)
 
		{
 
@@ -385,54 +437,34 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
		}
 
	}
 

	
 
			if (m_proxy == NULL)
 
			{
 
				int counter = 0;
 
				while (m_proxy == NULL) {
 
					counter++;
 
					sleep(1);
 
					LOG4CXX_INFO(logger, "Creating DBus proxy for com.Skype.Api.");
 
					m_proxy = dbus_g_proxy_new_for_name_owner (m_connection,
 
													"com.Skype.API",
 
													"/com/Skype",
 
													"com.Skype.API",
 
													&error);
 
					if (m_proxy == NULL && error != NULL)
 
					{
 
						LOG4CXX_INFO(logger,  m_username << ":" << error->message);
 

	
 
						if (counter == 15) {
 
							np->handleDisconnected(m_user, 0, error->message);
 
							close(fd_output);
 
							logout();
 
							g_error_free(error);
 
							break;
 
						}
 
						g_error_free(error);
 
					}
 
	m_timer = g_timeout_add_seconds(1, create_dbus_proxy, this);
 
}
 

	
 
				vtable.message_function = &skype_notify_handler;
 
				dbus_connection_register_object_path(dbus_g_connection_get_connection(m_connection), "/com/Skype/Client", &vtable, this);
 
			}
 
bool Skype::loadSkypeBuddies() {
 
//	std::string re = "CONNSTATUS OFFLINE";
 
//	while (re == "CONNSTATUS OFFLINE" || re.empty()) {
 
//		sleep(1);
 

	
 
			int counter = 0;
 
			std::string re = "CONNSTATUS OFFLINE";
 
			while (re == "CONNSTATUS OFFLINE" || re.empty()) {
 
				sleep(1);
 
	gchar buffer[1024];
 
				int bytes_read;
 
				bytes_read = read (fd_output, buffer, 1023);
 
	int bytes_read = read(fd_output, buffer, 1023);
 
	if (bytes_read > 0) {
 
		buffer[bytes_read] = 0;
 
		np->handleDisconnected(m_user, 0, buffer);
 
		close(fd_output);
 
		logout();
 
					return;
 
		return FALSE;
 
	}
 
				re = send_command("NAME Spectrum");
 
				if (counter++ > 15)
 
					break;
 

	
 
	std::string re = send_command("NAME Spectrum");
 
	if (m_counter++ > 15) {
 
		np->handleDisconnected(m_user, 0, "");
 
		close(fd_output);
 
		logout();
 
		return FALSE;
 
	}
 

	
 
	if (re.empty() || re == "CONNSTATUS OFFLINE") {
 
		return TRUE;
 
	}
 

	
 
	close(fd_output);
 
@@ -440,7 +472,7 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
	if (send_command("PROTOCOL 7") != "PROTOCOL 7") {
 
		np->handleDisconnected(m_user, 0, "Skype is not ready");
 
		logout();
 
				return;
 
		return FALSE;
 
	}
 

	
 
	np->handleConnected(m_user);
 
@@ -501,6 +533,7 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
	g_strfreev(full_friends_list);
 

	
 
	send_command("SET AUTOAWAY OFF");
 
	return FALSE;
 
}
 

	
 
void Skype::logout() {
 
@@ -696,6 +729,10 @@ static void io_destroy(gpointer data) {
 
	exit(1);
 
}
 

	
 
static void log_glib_error(const gchar *string) {
 
	LOG4CXX_ERROR(logger, "GLIB ERROR:" << string);
 
}
 

	
 
int main(int argc, char **argv) {
 
	GError *error = NULL;
 
	GOptionContext *context;
 
@@ -783,6 +820,7 @@ int main(int argc, char **argv) {
 

	
 
		m_sock = create_socket(host, port);
 

	
 
		g_set_printerr_handler(log_glib_error);
 

	
 
	GIOChannel *channel;
 
	GIOCondition cond = (GIOCondition) G_IO_IN;
0 comments (0 inline, 0 general)