static int register_once() { AvahiClient *client = NULL; int error; int ret = 1; struct timeval tv; group = NULL; simple_poll = NULL; name = NULL; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } name = avahi_strdup("MegaPrinter"); /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); goto fail; } /* After 10s quit. */ avahi_simple_poll_get(simple_poll)->timeout_new( avahi_simple_poll_get(simple_poll), avahi_elapse_time(&tv, 300*10, 0), quit_callback, client); /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (client) avahi_client_free(client); if (simple_poll) avahi_simple_poll_free(simple_poll); avahi_free(name); return ret; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiClient *client = NULL; int error; int ret = 1; struct timeval tv; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } name = avahi_strdup("MegaPrinter"); /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); goto fail; } /* After 10s do some weird modification to the service */ avahi_simple_poll_get(simple_poll)->timeout_new( avahi_simple_poll_get(simple_poll), avahi_elapse_time(&tv, 1000*10, 0), modify_callback, client); /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (client) avahi_client_free(client); if (simple_poll) avahi_simple_poll_free(simple_poll); avahi_free(name); return ret; }
csm_service * csm_remove_service(csm_service_list *services, csm_service *s) { INFO("Removing service announcement: %s",s->uuid); /* Cancel expiration event */ if (s->timeout) avahi_simple_poll_get(simple_poll)->timeout_update(s->timeout,NULL); // find associated fields obj, dettach it from service_list's tree, and // reattach it to the original csm_service co_obj_t *fields = s->fields; if (fields && services && co_list_contains(services->service_fields, fields)) { co_list_delete(services->service_fields, fields); service_attach(s->fields, s); // remove service from service list co_obj_t *service_obj = (co_obj_t*)container_of(s, co_service_t, service); if (co_list_contains(services->services, service_obj)) co_list_delete(services->services, service_obj); // finalize removal by running update handlers csm_services_commit(services); return s; } return NULL; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { AvahiSimplePoll *simple_poll; const AvahiPoll *poll_api; AvahiClient *client; AvahiRecordBrowser *r; simple_poll = avahi_simple_poll_new(); assert(simple_poll); poll_api = avahi_simple_poll_get(simple_poll); assert(poll_api); client = avahi_client_new(poll_api, 0, NULL, NULL, NULL); assert(client); r = avahi_record_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "ecstasy.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, 0, callback, simple_poll); assert(r); avahi_simple_poll_loop(simple_poll); avahi_client_free(client); avahi_simple_poll_free(simple_poll); return 0; }
void PublishAvahi::publish(const std::vector<AvahiService>& services) { this->services = services; AvahiClient *client = NULL; int error; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { logE << "Failed to create simple poll object.\n"; } /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_IGNORE_USER_CONFIG, client_callback, this, &error); /* Check wether creating the client object succeeded */ if (!client) { logE << "Failed to create client: " << avahi_strerror(error) << "\n"; } active_ = true; pollThread_ = std::thread(&PublishAvahi::worker, this); }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { AvahiSimplePoll *simple_poll; const AvahiPoll *poll_api; AvahiClient *client; AvahiServiceResolver *r; simple_poll = avahi_simple_poll_new(); assert(simple_poll); poll_api = avahi_simple_poll_get(simple_poll); assert(poll_api); client = avahi_client_new(poll_api, 0, NULL, NULL, NULL); assert(client); r = avahi_service_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, "_domain._udp", "0pointer.de", AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_TXT, callback, simple_poll); assert(r); avahi_simple_poll_loop(simple_poll); avahi_client_free(client); avahi_simple_poll_free(simple_poll); return 0; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiServer *server; AvahiServerConfig config; int error; AvahiSimplePoll *simple_poll; simple_poll = avahi_simple_poll_new(); avahi_server_config_init(&config); config.publish_hinfo = 0; config.publish_addresses = 0; config.publish_workstation = 0; config.publish_domain = 0; config.use_ipv6 = 0; config.enable_reflector = 1; server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error); avahi_server_config_free(&config); for (;;) if (avahi_simple_poll_iterate(simple_poll, -1) != 0) break; avahi_server_free(server); avahi_simple_poll_free(simple_poll); return 0; }
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { Config *config = userdata; client = c; switch (state) { case AVAHI_CLIENT_FAILURE: if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) { int error; /* We have been disconnected, so let reconnect */ fprintf(stderr, "Disconnected, reconnecting ...\n"); avahi_client_free(client); client = NULL; entry_group = NULL; if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) { fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error)); avahi_simple_poll_quit(simple_poll); } } else { fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c))); avahi_simple_poll_quit(simple_poll); } break; case AVAHI_CLIENT_S_RUNNING: if (register_stuff(config) < 0) avahi_simple_poll_quit(simple_poll); break; case AVAHI_CLIENT_S_COLLISION: if (config->verbose) fprintf(stderr, "Host name conflict\n"); if (entry_group) { avahi_entry_group_free(entry_group); entry_group = NULL; } break; case AVAHI_CLIENT_CONNECTING: if (config->verbose) fprintf(stderr, "Waiting for daemon ...\n"); break; case AVAHI_CLIENT_S_REGISTERING: ; } }
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { Config *config = userdata; /* This function might be called when avahi_client_new() has not * returned yet.*/ client = c; switch (state) { case AVAHI_CLIENT_FAILURE: if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) { int error; /* We have been disconnected, so let reconnect */ fprintf(stderr, ("Disconnected, reconnecting ...\n")); avahi_client_free(client); client = NULL; avahi_string_list_free(browsed_types); browsed_types = NULL; while (services) remove_service(config, services); browsing = 0; if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) { fprintf(stderr, ("Failed to create client object: %s\n"), avahi_strerror(error)); avahi_simple_poll_quit(simple_poll); } } else { fprintf(stderr, ("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c))); avahi_simple_poll_quit(simple_poll); } break; case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_COLLISION: if (!browsing) if (start(config) < 0) avahi_simple_poll_quit(simple_poll); break; case AVAHI_CLIENT_CONNECTING: if (config->verbose && !config->parsable) fprintf(stderr, ("Waiting for daemon ...\n")); break; } }
CAMLprim value stub_avahi_simple_poll_get(value sp) { CAMLparam1(sp); const AvahiPoll *ap; ap = avahi_simple_poll_get((AvahiSimplePoll *)sp); if (ap) CAMLreturn((value)ap); caml_failwith("avahi_simple_poll_get returned NULL"); }
static void timeout_callback (AvahiTimeout *t, void *userdata) { const AvahiPoll *api = avahi_simple_poll_get (simple_poll); if (n_pending_resolvers > 0) { api->timeout_update (t, (struct timeval*) userdata); } else { avahi_free (userdata); api->timeout_free (t); avahi_simple_poll_quit (simple_poll); } }
int avahi_discover_tvh(struct htsp_t* htsp) { AvahiClient *client = NULL; AvahiServiceBrowser *sb = NULL; int error; int ret = 1; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); goto fail; } /* Create the service browser */ if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, "_htsp._tcp", NULL, 0, browse_callback, client))) { fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client))); goto fail; } /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (sb) avahi_service_browser_free(sb); if (client) avahi_client_free(client); if (simple_poll) avahi_simple_poll_free(simple_poll); if (tvh_hostname) { htsp->host = tvh_hostname; htsp->ip = tvh_ip; htsp->port = tvh_port; } return ret; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiServerConfig config; AvahiServer *server = NULL; int error; int ret = 1; /* Initialize the pseudo-RNG */ srand(time(NULL)); /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } name = avahi_strdup("MegaPrinter"); /* Let's set the host name for this server. */ avahi_server_config_init(&config); config.host_name = avahi_strdup("gurkiman"); config.publish_workstation = 0; /* Allocate a new server */ server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error); /* Free the configuration data */ avahi_server_config_free(&config); /* Check wether creating the server object succeeded */ if (!server) { fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error)); goto fail; } /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (server) avahi_server_free(server); if (simple_poll) avahi_simple_poll_free(simple_poll); avahi_free(name); return ret; }
int init(int argc, char *argv[]) { puts("Avahi zero-conf handler init."); simple_poll = avahi_simple_poll_new(); int error = 0; avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); printf("Avahi status: %s\n", avahi_strerror(error)); avahi_simple_poll_loop(simple_poll); return 255; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiClient *client = NULL; AvahiRecordBrowser *sb = NULL; int error; int ret = 1; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); goto fail; } /* Create the service browser */ if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, browse_callback, client))) { fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client))); goto fail; } /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (sb) avahi_service_browser_free(sb); if (client) avahi_client_free(client); if (simple_poll) avahi_simple_poll_free(simple_poll); return ret; }
static void * avahi_thread(void *aux) { const AvahiPoll *ap = avahi_simple_poll_get(avahi_asp); name = avahi_strdup("Tvheadend"); avahi_client_new(ap, AVAHI_CLIENT_NO_FAIL, client_callback, NULL, NULL); while(avahi_simple_poll_iterate(avahi_asp, -1) == 0); return NULL; }
static void start_server(AvahiTimeout *t, void *userdata) { int error; assert(t); if (stb) { DEBUG("Service type browser already exists"); avahi_s_service_type_browser_free(stb); stb = NULL; } if (server) { DEBUG("Server already exists"); avahi_server_free(server); server = NULL; } /* Allocate a new server */ server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error); /* Check whether creating the server object succeeded */ if (!server) { ERROR("Failed to create server: %s", avahi_strerror(error)); avahi_simple_poll_quit(simple_poll); return; } /* every UPDATE_INTERVAL seconds, shut down and re-create server. This * has the benefit of causing CSM to send queries to other nodes, prompting * them to re-multicast their services. This is done because mDNS seems to * be very unreliable on mesh, and often nodes don't get service announcements * or can't resolve them. */ struct timeval tv = {0}; avahi_elapse_time(&tv, 1000*UPDATE_INTERVAL, 0); avahi_simple_poll_get(simple_poll)->timeout_update(t, &tv); }
bool ofxAvahiCoreBrowser::lookup(string _type){ AvahiServerConfig config; int error; type = _type; if (!(poll = avahi_simple_poll_new())) { ofLogError(LOG_NAME) << "Failed to create simple poll object."; close(); return false; } /* Do not publish any local records */ avahi_server_config_init(&config); config.publish_hinfo = 0; config.publish_addresses = 0; config.publish_workstation = 0; config.publish_domain = 0; config.use_ipv6 = 0; config.disallow_other_stacks = 1; config.allow_point_to_point = 1; /* Set a unicast DNS server for wide area DNS-SD */ /*avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]); config.n_wide_area_servers = 1; config.enable_wide_area = 1;*/ server = avahi_server_new(avahi_simple_poll_get(poll), &config, (AvahiServerCallback)server_cb, this, &error); avahi_server_config_free(&config); if (!server) { ofLogError(LOG_NAME) << "Failed to create server:" << avahi_strerror(error); close(); return false; } /* Create the service browser */ if (!(sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, type.c_str(), NULL, (AvahiLookupFlags)0, (AvahiSServiceBrowserCallback)browse_cb, this))) { ofLogError(LOG_NAME) << "Failed to create service browser:" << avahi_strerror(avahi_server_errno(server)); close(); return false; } startThread(true,false); return true; }
static int discover_host(AvahiAddress *addr, uint16_t *port) { struct avahi_discovery_data ddata; int ret = 0; AvahiClient *client; AvahiServiceBrowser *browser; AvahiSimplePoll *poll = avahi_simple_poll_new(); if (!poll) return -ENOMEM; client = avahi_client_new(avahi_simple_poll_get(poll), 0, NULL, NULL, &ret); if (!client) { ERROR("Unable to start ZeroConf client :%s\n", avahi_strerror(ret)); goto err_free_poll; } memset(&ddata, 0, sizeof(ddata)); ddata.poll = poll; ddata.address = addr; ddata.port = port; browser = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_iio._tcp", NULL, 0, __avahi_browser_cb, &ddata); if (!browser) { ret = avahi_client_errno(client); ERROR("Unable to create ZeroConf browser: %s\n", avahi_strerror(ret)); goto err_free_client; } DEBUG("Trying to discover host\n"); avahi_simple_poll_loop(poll); if (!ddata.found) ret = ENXIO; avahi_service_browser_free(browser); err_free_client: avahi_client_free(client); err_free_poll: avahi_simple_poll_free(poll); return -ret; /* we want a negative error code */ }
/* Called whenever the client or server state changes */ static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) { struct runtime_data *r = userdata; ap_assert(r); r->client = c; /* ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->main_server, "client_callback(%u)", state); */ switch (state) { case AVAHI_CLIENT_S_RUNNING: create_all_services(r); break; case AVAHI_CLIENT_S_COLLISION: reset_services(r); break; case AVAHI_CLIENT_FAILURE: if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) { int error; free_services(r); avahi_client_free(r->client); if ((r->client = avahi_client_new(avahi_simple_poll_get(r->simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, r, &error))) break; ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "avahi_client_new() failed: %s", avahi_strerror(error)); } else ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "Client failure: %s", avahi_strerror(avahi_client_errno(c))); avahi_simple_poll_quit(r->simple_poll); break; case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_CONNECTING: /* do nothing */ break; } }
void Avahi::avahiPublish() { int error; /* Allocate main loop object */ if (!(simple_publish_poll = avahi_simple_poll_new())) { std::cerr << "Failed to create simple poll object." << std::endl; } /* Allocate a new client */ publishClient = avahi_client_new(avahi_simple_poll_get(simple_publish_poll), (AvahiClientFlags)0, client_publish_callback, NULL, &error); /* Check whether creating the client object succeeded */ if (!publishClient) { std::cerr << "Failed to create client: " << avahi_strerror(error) << std::endl; } /* Run the main loop */ avahi_simple_poll_loop(simple_publish_poll); }
static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) { int sig; const AvahiPoll *poll_api; assert(watch); assert(simple_poll_api); poll_api = avahi_simple_poll_get(simple_poll_api); if ((sig = daemon_signal_next()) <= 0) { avahi_log_error("daemon_signal_next() failed"); poll_api->watch_free(watch); return; } switch (sig) { case SIGINT: case SIGQUIT: case SIGTERM: avahi_log_info( "Got %s, quitting.", sig == SIGINT ? "SIGINT" : (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM")); avahi_simple_poll_quit(simple_poll_api); break; case SIGHUP: avahi_log_info("Got SIGHUP, reloading."); reload_config(); break; case SIGUSR1: avahi_log_info("Got SIGUSR1, dumping record data."); avahi_server_dump(avahi_server, dump, NULL); break; default: avahi_log_warn("Got spurious signal, ignoring."); break; } }
int Trick::Zeroconf::init() { #ifdef HAVE_ZEROCONF #if __linux int error; int ret ; char r[128] ; if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); return -1 ; } client = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, NULL, NULL, &error); if ( client != NULL ) { group = avahi_entry_group_new(client, entry_group_callback, (void *)name.c_str()) ; if (avahi_entry_group_is_empty(group)) { ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name.c_str(), type.c_str(), NULL, NULL, var_server_get_port(), NULL, r, NULL) ; ret = avahi_entry_group_commit(group) ; } } #endif #if __APPLE__ DNSServiceErrorType error ; error = DNSServiceRegister(&dns_service_ref, 0, 0, name.c_str(), type.c_str(), NULL, NULL, htons(var_server_get_port()), 0, NULL, // txt record stuff NULL, NULL); // callback stuff #endif #endif return 0 ; }
bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout) { int error; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { logE << "Failed to create simple poll object.\n"; goto fail; } /* 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; } /* 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; } result_.valid_ = false; while (timeout > 0) { avahi_simple_poll_iterate(simple_poll, 100); timeout -= 100; if (result_.valid_) { result = result_; return true; } } fail: return false; }
int sigint_install(AvahiSimplePoll *spoll) { struct sigaction sa; const AvahiPoll *p; assert(spoll); assert(!simple_poll); assert(pipe_fds[0] == -1 && pipe_fds[1] == -1); if (pipe(pipe_fds) < 0) { fprintf(stderr, "pipe() failed: %s\n", strerror(errno)); return -1; } set_nonblock(pipe_fds[0]); set_nonblock(pipe_fds[1]); memset(&sa, 0, sizeof(sa)); sa.sa_handler = handler; sa.sa_flags = SA_RESTART; if (sigaction(SIGINT, &sa, &old_sigint_sa) < 0) { fprintf(stderr, "sigaction() failed: %s\n", strerror(errno)); close_pipe_fds(); return -1; } if (sigaction(SIGTERM, &sa, &old_sigterm_sa) < 0) { sigaction(SIGINT, &old_sigint_sa, NULL); fprintf(stderr, "sigaction() failed: %s\n", strerror(errno)); close_pipe_fds(); return -1; } p = avahi_simple_poll_get(spoll); watch = p->watch_new(p, pipe_fds[0], AVAHI_WATCH_IN, watch_callback, NULL); assert(watch); simple_poll = spoll; return 0; }
void sigint_uninstall(void) { if (!simple_poll) return; sigaction(SIGTERM, &old_sigterm_sa, NULL); sigaction(SIGINT, &old_sigint_sa, NULL); close_pipe_fds(); if (watch) { const AvahiPoll *p; assert(simple_poll); p = avahi_simple_poll_get(simple_poll); p->watch_free(watch); watch = NULL; } simple_poll = NULL; }
void CommunicationManager::run() { AvahiClient *client = NULL; int error; struct timeval tv; /* Allocate main loop object */ if (!(simplePoll = avahi_simple_poll_new())) { LoggerUtil_error(logger, "Failed to create simple poll object.\n"); } else { name = avahi_strdup("Ginga Multimodal Event"); /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simplePoll), (AvahiClientFlags)0, clientCallback, NULL, &error); /* Check whether creating the client object succeeded */ if (!client) { LoggerUtil_error(logger, "Failed to create client: " << avahi_strerror(error)); } else { // Entra num loop para ficar aceitando conexões e recebendo // um xml em cada conexão estabelecida. startSocket(); /* Run the main loop */ // avahi_simple_poll_loop(simplePoll); } } /* Cleanup things */ if (client) { avahi_client_free(client); } if (simplePoll) { avahi_simple_poll_free(simplePoll); } avahi_free(name); }
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; } }
int launchRegisterService(const char* nameService) { printf("Name of Service to add = %s\n", nameService); //AvahiClient *client = NULL; int error; int ret = 1; struct timeval tv; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); return 0; } // char host_name[256]; // gethostname(host_name, sizeof(host_name)); name = avahi_strdup(nameService); /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); return 0; } //pthread_t myNewThread; if(!pthread_create(&myNewThread, NULL, registerService, simple_poll)) printf("thread was created\n"); else printf("New thread not created\n"); return 1; }
void Avahi::avahiDiscovery() { int error; /* Allocate main loop object */ if (!(simple_discovery_poll = avahi_simple_poll_new())) { std::cerr << "Failed to create simple poll object." << std::endl; } /* Allocate a new client */ discoveryClient = avahi_client_new(avahi_simple_poll_get(simple_discovery_poll), (AvahiClientFlags)0, client_discovery_callback, NULL, &error); /* Check whether creating the client object succeeded */ if (!discoveryClient) { std::cerr << "Failed to create client: " << avahi_strerror(error) << std::endl; } /* Create the service browser */ if (!(sb = avahi_service_browser_new(discoveryClient, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_heatSync._tcp", NULL, (AvahiLookupFlags)0, browse_callback, discoveryClient))) { std::cerr << "Failed to create service browser: " << avahi_strerror(avahi_client_errno(discoveryClient)) << std::endl; } /* Run the main loop */ avahi_simple_poll_loop(simple_discovery_poll); }