improved error handling

This commit is contained in:
badaix 2015-08-26 19:01:29 +02:00
parent 5cc5aa7f04
commit 635daabd8c
3 changed files with 58 additions and 43 deletions

View file

@ -24,6 +24,7 @@
#include <time.h>
#include <unistd.h>
#include <iostream>
#include "common/snapException.h"
#include "common/log.h"
@ -36,15 +37,24 @@ BrowseAvahi::BrowseAvahi() : client_(NULL), sb_(NULL)
BrowseAvahi::~BrowseAvahi()
{
cleanUp();
}
void BrowseAvahi::cleanUp()
{
if (sb_)
avahi_service_browser_free(sb_);
sb_ = NULL;
if (client_)
avahi_client_free(client_);
client_ = NULL;
if (simple_poll)
avahi_simple_poll_free(simple_poll);
simple_poll = NULL;
}
@ -88,7 +98,7 @@ void BrowseAvahi::resolve_callback(
browseAvahi->result_.valid_ = true;
t = avahi_string_list_to_string(txt);
logO << "\t" << host_name << ":" << port << "(" << a << ")\n";
logO << "\t" << host_name << ":" << port << " (" << a << ")\n";
logD << "\tTXT=" << t << "\n";
logD << "\tProto=" << (int)protocol << "\n";
logD << "\tcookie is " << avahi_string_list_get_service_cookie(txt) << "\n";
@ -114,7 +124,8 @@ void BrowseAvahi::browse_callback(
const char *type,
const char *domain,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
void* userdata) {
void* userdata)
{
// AvahiClient* client = (AvahiClient*)userdata;
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
@ -154,7 +165,8 @@ void BrowseAvahi::browse_callback(
}
void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata)
{
assert(c);
/* Called whenever the client or server state changes */
@ -170,31 +182,20 @@ void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_
bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout)
{
int error;
try
{
/* Allocate main loop object */
if (!(simple_poll = avahi_simple_poll_new()))
{
logE << "Failed to create simple poll object.\n";
goto fail;
}
throw SnapException("BrowseAvahi - Failed to create simple poll object");
/* Allocate a new client */
client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, this, &error);
/* Check wether creating the client object succeeded */
if (!client_)
{
logE << "Failed to create client: " << avahi_strerror(error) << "\n";
goto fail;
}
int error;
if (!(client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, this, &error)))
throw SnapException("BrowseAvahi - Failed to create client: " + std::string(avahi_strerror(error)));
/* Create the service browser */
if (!(sb_ = avahi_service_browser_new(client_, AVAHI_IF_UNSPEC, proto, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this)))
{
logE << "Failed to create service browser: " << avahi_strerror(avahi_client_errno(client_)) << "\n";
goto fail;
}
throw SnapException("BrowseAvahi - Failed to create service browser: " + std::string(avahi_strerror(avahi_client_errno(client_))));
result_.valid_ = false;
while (timeout > 0)
@ -204,14 +205,19 @@ bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult&
if (result_.valid_)
{
result = result_;
cleanUp();
return true;
}
}
fail:
cleanUp();
return false;
}
catch (...)
{
cleanUp();
throw;
}
}

View file

@ -44,6 +44,7 @@ public:
bool browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout);
private:
void cleanUp();
static void resolve_callback(AvahiServiceResolver *r, AVAHI_GCC_UNUSED AvahiIfIndex interface, AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, AVAHI_GCC_UNUSED void* userdata);
static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void* userdata);
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata);

View file

@ -144,15 +144,23 @@ int main (int argc, char *argv[])
BrowseAvahi browseAvahi;
AvahiResult avahiResult;
while (!g_terminated)
{
try
{
if (browseAvahi.browse("_snapcast._tcp", AVAHI_PROTO_INET, avahiResult, 5000))
{
ip = avahiResult.ip_;
port = avahiResult.port_;
std::cout << ip << ":" << port << "\n";
logO << "Found server " << ip << ":" << port << "\n";
break;
}
}
catch (const std::exception& e)
{
logS(kLogErr) << "Exception: " << e.what() << std::endl;
}
usleep(500*1000);
}
}
std::unique_ptr<Controller> controller(new Controller());