Re: How to test Gio::SocketClient::connect_to _host_async()?



Am 2015-02-06 11:51, schrieb Markus Kolb:
Hi,

I've already the next question...

I'd like to test my SmtpClient code which will be part of a Gtk App.
But for test it should be called from a Gio::Application.

Now I've the problem, that the Gio::Application exits before the
connect_to_host_async calls its SlotAsyncReady.

At least I think this, because the output is...
on_activate() started
on_activate() finished
and I'm missing the
on_connect_async_ready output from the
SmtpClient::on_connect_async_ready() in between, somehow.

What must be done, that the test app waits for its async-stuff?
I've already tried with a simple sleep(20) in on_activate() because
the connect timeout is set to 10 seconds and 20 seconds should be
enough time to get the SlotAsyncReady called.
But it is not called.
Must be that I don't understand how this works behind the scenes, but
couldn't find documentation.

Can you please give me some hints?!


#include "SmtpClient.h"
#include <giomm/application.h>
#include <iostream>

Glib::RefPtr<Gio::Application> app;

void on_activate()
{
  app->hold();
  std::cerr << "on_activate() started" << std::endl;
  SmtpClient client("smtp.example.com", 25, "test example com", false);
  client.connect();
  std::cerr << "on_activate() finished" << std::endl;
  app->release();
}

int main (int argc, char **argv)
{
  app = Gio::Application::create("eu.n4v.battleship");
  app->signal_activate().connect(&on_activate);
  return app->run(argc, argv);
}

Here some snippets of SmtpClient class...

SmtpClient::SmtpClient(const Glib::ustring& smtp_server, const guint&
smtp_port, const Glib::ustring& smtp_from, bool tls_enable);

void SmtpClient::connect() const
{
  m_p_sock_client-> (m_smtp_server, m_smtp_port, sigc::mem_fun(this,
&SmtpClient::on_connect_async_ready));
}


Ups, this is... (cutted somehow the method)

void SmtpClient::connect() const
{
m_p_sock_client->connect_to_host_async(m_smtp_server, m_smtp_port, sigc::mem_fun(this, &SmtpClient::on_connect_async_ready));
}



The m_p_sock_client = Gio::SocketClient::create();

void
SmtpClient::on_connect_async_ready(Glib::RefPtr<Gio::AsyncResult>&
result) const
{
  std::cerr << "on_connect_async_ready" << std::endl;
  Glib::RefPtr<Gio::SocketConnection> con =
m_p_sock_client->connect_to_host_finish(result);
  if (con == 0)
  {
    signal_smtp_connect_failed.emit(con);
  } else
  {
    signal_smtp_connect.emit(con);
  }
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]