/* Starts sending discovery requests */ static void start_discovery (GSSDPResourceBrowser *resource_browser) { GSSDPResourceBrowserPrivate *priv; priv = gssdp_resource_browser_get_instance_private (resource_browser); /* Send one now */ send_discovery_request (resource_browser); /* And schedule the rest for later */ priv->num_discovery = 1; priv->timeout_src = g_timeout_source_new (DISCOVERY_FREQUENCY); g_source_set_callback (priv->timeout_src, discovery_timeout, resource_browser, NULL); g_source_attach (priv->timeout_src, g_main_context_get_thread_default ()); g_source_unref (priv->timeout_src); /* Setup a set of responsive resources for cache refreshing */ priv->fresh_resources = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); }
static gboolean discovery_timeout (gpointer data) { GSSDPResourceBrowserPrivate *priv; GSSDPResourceBrowser *resource_browser; resource_browser = GSSDP_RESOURCE_BROWSER (data); priv = gssdp_resource_browser_get_instance_private (resource_browser); send_discovery_request (resource_browser); priv->num_discovery += 1; if (priv->num_discovery >= MAX_DISCOVERY_MESSAGES) { priv->timeout_src = NULL; priv->num_discovery = 0; /* Setup cache refreshing */ priv->refresh_cache_src = g_timeout_source_new_seconds (RESCAN_TIMEOUT); g_source_set_callback (priv->refresh_cache_src, refresh_cache, resource_browser, NULL); g_source_attach (priv->refresh_cache_src, g_main_context_get_thread_default ()); g_source_unref (priv->refresh_cache_src); return FALSE; } else return TRUE; }
int discovery_service_start(discovery_service *svc, discovery_strategy *strategy) { JNXCHECK(svc); // *** TODO Set up brodcast or multicast *** // It should just be a simple matter of passing a flag to the service // or changing the function signature and calling either // set_up_sockets_for_broadcast or set_up_sockets_for_multicast. // set_up_sockets_for_broadcast(svc); svc->sock_send = jnx_socket_udp_create(svc->family); svc->udp_listener = jnx_socket_udp_listener_broadcast_create( port_to_string(svc), svc->family); svc->isrunning = 1; if (0 != listen_for_discovery_packets(svc)) { JNXLOG(0, "[DISCOVERY] Couldn't start the discovery listener.\n"); return ERR_DISCOVERY_START; } initiate_discovery(svc); if (strategy == NULL) { svc->peers->is_active_peer = is_active_peer_ask_once; send_discovery_request(svc); } else { svc->peers->is_active_peer = is_active_peer_periodic_update; strategy(svc); } return 0; }
// Polling update strategy void *polling_update_loop(void *data) { // The slightly more complicated logic is here to ensure that updates // do not happen more frequently than peer_update_interval on average. // // This means that whichever node sends LIST packet last, all of the // discovery services on the network will synchronise to that time. Since // every time a LIST packet is received the service sends local peer packet, // we update the last_update time in send_peer_packet function. This way // we ensure that regardless of which service sends the LIST packets, all // services update the last_updated time, i.e. synchronise on that packet. // // The only time we may get a time shorter than peer_update_interval between // updates is when a new discovery service joins the broadcast group. int old_cancel_state; pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, &old_cancel_state); discovery_service *svc = (discovery_service *) data; time_t next_update = get_last_update_time(svc) + peer_update_interval; while (1) { if (!svc->isrunning) { return NULL; } if (next_update <= time(0)) { send_discovery_request(svc); } next_update += peer_update_interval; sleep(next_update - time(0)); } return NULL; }
void Mirobot::sendDiscovery(){ if(nextDiscovery < millis()){ if(marcel.wifi.online){ send_discovery_request((uint32_t)WiFi.localIP(), marcel.settings.ap_ssid, "Mirobot-v3"); nextDiscovery = millis() + 30000; }else{ nextDiscovery = millis() + 1000; } } }
void initiate_discovery(discovery_service *svc) { send_peer_packet(svc); int i; for (i = 0; i < INITIAL_DISCOVERY_REQS; i++) { send_discovery_request(svc); printf("."); fflush(stdout); sleep(1); } printf("\n"); }