Skip to content

Commit

Permalink
add clang-format file
Browse files Browse the repository at this point in the history
reformat code
  • Loading branch information
badaix committed Sep 24, 2019
1 parent b733f64 commit b20add3
Show file tree
Hide file tree
Showing 105 changed files with 8,412 additions and 8,362 deletions.
20 changes: 20 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
AccessModifierOffset: '-4'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BreakBeforeBraces: Allman
ColumnLimit: '160'
IndentCaseLabels: 'true'
IndentWidth: '4'
Language: Cpp
MaxEmptyLinesToKeep: '3'
PenaltyBreakComment: '100000'
PointerAlignment: Left
Standard: Cpp11
UseTab: Never

...
2 changes: 1 addition & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of snapcast
# Copyright (C) 2014-2018 Johannes Pohl
# Copyright (C) 2014-2019 Johannes Pohl
#
# 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
Expand Down
291 changes: 137 additions & 154 deletions client/browseZeroConf/browseAvahi.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
***/

#include "browseAvahi.h"
#include <stdio.h>
#include "aixlog.hpp"
#include "common/snapException.h"
#include <assert.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "common/snapException.h"
#include "aixlog.hpp"


static AvahiSimplePoll *simple_poll = NULL;
static AvahiSimplePoll* simple_poll = NULL;


BrowseAvahi::BrowseAvahi() : client_(NULL), sb_(NULL)
Expand All @@ -37,188 +37,171 @@ BrowseAvahi::BrowseAvahi() : client_(NULL), sb_(NULL)

BrowseAvahi::~BrowseAvahi()
{
cleanUp();
cleanUp();
}


void BrowseAvahi::cleanUp()
{
if (sb_)
avahi_service_browser_free(sb_);
sb_ = NULL;
if (sb_)
avahi_service_browser_free(sb_);
sb_ = NULL;

if (client_)
avahi_client_free(client_);
client_ = NULL;
if (client_)
avahi_client_free(client_);
client_ = NULL;

if (simple_poll)
avahi_simple_poll_free(simple_poll);
simple_poll = NULL;
if (simple_poll)
avahi_simple_poll_free(simple_poll);
simple_poll = NULL;
}


void BrowseAvahi::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)
void BrowseAvahi::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)
{
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
assert(r);

/* Called whenever a service has been resolved successfully or timed out */

switch (event)
{
case AVAHI_RESOLVER_FAILURE:
LOG(ERROR) << "(Resolver) Failed to resolve service '" << name << "' of type '" << type << "' in domain '" << domain << "': " << avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))) << "\n";
break;

case AVAHI_RESOLVER_FOUND:
{
char a[AVAHI_ADDRESS_STR_MAX], *t;

LOG(INFO) << "Service '" << name << "' of type '" << type << "' in domain '" << domain << "':\n";

avahi_address_snprint(a, sizeof(a), address);
browseAvahi->result_.host = host_name;
browseAvahi->result_.ip = a;
browseAvahi->result_.port = port;
// protocol seems to be unreliable (0 for IPv4 and for IPv6)
browseAvahi->result_.ip_version = (browseAvahi->result_.ip.find(":") == std::string::npos)?(IPVersion::IPv4):(IPVersion::IPv6);
browseAvahi->result_.valid = true;
browseAvahi->result_.iface_idx = interface;

t = avahi_string_list_to_string(txt);
LOG(INFO) << "\t" << host_name << ":" << port << " (" << a << ")\n";
LOG(DEBUG) << "\tTXT=" << t << "\n";
LOG(DEBUG) << "\tProto=" << (int)protocol << "\n";
LOG(DEBUG) << "\tcookie is " << avahi_string_list_get_service_cookie(txt) << "\n";
LOG(DEBUG) << "\tis_local: " << !!(flags & AVAHI_LOOKUP_RESULT_LOCAL) << "\n";
LOG(DEBUG) << "\tour_own: " << !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN) << "\n";
LOG(DEBUG) << "\twide_area: " << !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA) << "\n";
LOG(DEBUG) << "\tmulticast: " << !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST) << "\n";
LOG(DEBUG) << "\tcached: " << !!(flags & AVAHI_LOOKUP_RESULT_CACHED) << "\n";
avahi_free(t);
}
}

avahi_service_resolver_free(r);
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
assert(r);

/* Called whenever a service has been resolved successfully or timed out */

switch (event)
{
case AVAHI_RESOLVER_FAILURE:
LOG(ERROR) << "(Resolver) Failed to resolve service '" << name << "' of type '" << type << "' in domain '" << domain
<< "': " << avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))) << "\n";
break;

case AVAHI_RESOLVER_FOUND:
{
char a[AVAHI_ADDRESS_STR_MAX], *t;

LOG(INFO) << "Service '" << name << "' of type '" << type << "' in domain '" << domain << "':\n";

avahi_address_snprint(a, sizeof(a), address);
browseAvahi->result_.host = host_name;
browseAvahi->result_.ip = a;
browseAvahi->result_.port = port;
// protocol seems to be unreliable (0 for IPv4 and for IPv6)
browseAvahi->result_.ip_version = (browseAvahi->result_.ip.find(":") == std::string::npos) ? (IPVersion::IPv4) : (IPVersion::IPv6);
browseAvahi->result_.valid = true;
browseAvahi->result_.iface_idx = interface;

t = avahi_string_list_to_string(txt);
LOG(INFO) << "\t" << host_name << ":" << port << " (" << a << ")\n";
LOG(DEBUG) << "\tTXT=" << t << "\n";
LOG(DEBUG) << "\tProto=" << (int)protocol << "\n";
LOG(DEBUG) << "\tcookie is " << avahi_string_list_get_service_cookie(txt) << "\n";
LOG(DEBUG) << "\tis_local: " << !!(flags & AVAHI_LOOKUP_RESULT_LOCAL) << "\n";
LOG(DEBUG) << "\tour_own: " << !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN) << "\n";
LOG(DEBUG) << "\twide_area: " << !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA) << "\n";
LOG(DEBUG) << "\tmulticast: " << !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST) << "\n";
LOG(DEBUG) << "\tcached: " << !!(flags & AVAHI_LOOKUP_RESULT_CACHED) << "\n";
avahi_free(t);
}
}

avahi_service_resolver_free(r);
}


void BrowseAvahi::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)
void BrowseAvahi::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)
{

// AvahiClient* client = (AvahiClient*)userdata;
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
assert(b);
// AvahiClient* client = (AvahiClient*)userdata;
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
assert(b);

/* Called whenever a new services becomes available on the LAN or is removed from the LAN */
/* Called whenever a new services becomes available on the LAN or is removed from the LAN */

switch (event)
{
case AVAHI_BROWSER_FAILURE:
LOG(ERROR) << "(Browser) " << avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))) << "\n";
avahi_simple_poll_quit(simple_poll);
return;
switch (event)
{
case AVAHI_BROWSER_FAILURE:
LOG(ERROR) << "(Browser) " << avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))) << "\n";
avahi_simple_poll_quit(simple_poll);
return;

case AVAHI_BROWSER_NEW:
LOG(INFO) << "(Browser) NEW: service '" << name << "' of type '" << type << "' in domain '" << domain << "'\n";
case AVAHI_BROWSER_NEW:
LOG(INFO) << "(Browser) NEW: service '" << name << "' of type '" << type << "' in domain '" << domain << "'\n";

/* We ignore the returned resolver object. In the callback
function we free it. If the server is terminated before
the callback function is called the server will free
the resolver for us. */
/* We ignore the returned resolver object. In the callback
function we free it. If the server is terminated before
the callback function is called the server will free
the resolver for us. */

if (!(avahi_service_resolver_new(browseAvahi->client_, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, (AvahiLookupFlags)0, resolve_callback, userdata)))
LOG(ERROR) << "Failed to resolve service '" << name << "': " << avahi_strerror(avahi_client_errno(browseAvahi->client_)) << "\n";
if (!(avahi_service_resolver_new(browseAvahi->client_, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, (AvahiLookupFlags)0,
resolve_callback, userdata)))
LOG(ERROR) << "Failed to resolve service '" << name << "': " << avahi_strerror(avahi_client_errno(browseAvahi->client_)) << "\n";

break;
break;

case AVAHI_BROWSER_REMOVE:
LOG(INFO) << "(Browser) REMOVE: service '" << name << "' of type '" << type << "' in domain '" << domain << "'\n";
break;
case AVAHI_BROWSER_REMOVE:
LOG(INFO) << "(Browser) REMOVE: service '" << name << "' of type '" << type << "' in domain '" << domain << "'\n";
break;

case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
LOG(INFO) << "(Browser) " << (event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW") << "\n";
break;
}
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
LOG(INFO) << "(Browser) " << (event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW") << "\n";
break;
}
}


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);
assert(c);

/* Called whenever the client or server state changes */
// BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
/* Called whenever the client or server state changes */
// BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);

if (state == AVAHI_CLIENT_FAILURE)
{
LOG(ERROR) << "Server connection failure: " << avahi_strerror(avahi_client_errno(c)) << "\n";
avahi_simple_poll_quit(simple_poll);
}
if (state == AVAHI_CLIENT_FAILURE)
{
LOG(ERROR) << "Server connection failure: " << avahi_strerror(avahi_client_errno(c)) << "\n";
avahi_simple_poll_quit(simple_poll);
}
}


bool BrowseAvahi::browse(const std::string& serviceName, mDNSResult& result, int timeout)
{
try
{
/* Allocate main loop object */
if (!(simple_poll = avahi_simple_poll_new()))
throw SnapException("BrowseAvahi - Failed to create simple poll object");

/* Allocate a new client */
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, AVAHI_PROTO_INET, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this)))
throw SnapException("BrowseAvahi - Failed to create service browser: " + std::string(avahi_strerror(avahi_client_errno(client_))));

result_.valid = false;
while (timeout > 0)
{
avahi_simple_poll_iterate(simple_poll, 100);
timeout -= 100;
if (result_.valid)
{
result = result_;
cleanUp();
return true;
}
}

cleanUp();
return false;
}
catch (...)
{
cleanUp();
throw;
}
try
{
/* Allocate main loop object */
if (!(simple_poll = avahi_simple_poll_new()))
throw SnapException("BrowseAvahi - Failed to create simple poll object");

/* Allocate a new client */
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, AVAHI_PROTO_INET, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this)))
throw SnapException("BrowseAvahi - Failed to create service browser: " + std::string(avahi_strerror(avahi_client_errno(client_))));

result_.valid = false;
while (timeout > 0)
{
avahi_simple_poll_iterate(simple_poll, 100);
timeout -= 100;
if (result_.valid)
{
result = result_;
cleanUp();
return true;
}
}

cleanUp();
return false;
}
catch (...)
{
cleanUp();
throw;
}
}


Loading

0 comments on commit b20add3

Please sign in to comment.