static u_result handlePublication( u_dataReader dataReader, c_long dataOffset, u_dataReader pDataReader, c_long pDataOffset) { v_dataReaderSample sample; u_result result; v_state state; v_message msg; struct v_publicationInfo *data; in_writer writer; in_participant participant; sample = NULL; result = u_dataReaderTake(dataReader, takeOne, &sample); while(sample && (result == U_RESULT_OK)){ state = v_readerSample(sample)->sampleState; msg = v_dataReaderSampleMessage(sample); data = (struct v_publicationInfo *)(C_DISPLACE(msg, dataOffset)); os_mutexLock (&gluelock); if(v_stateTest(state, L_DISPOSED)){ writer = in_writerLookup(&data->key); if(writer){ in_writerFree(writer, NULL); } else { nn_log (LC_WARNING, "handlePublication: disposed writer not found\n"); /*abort();*/ } } else { participant = in_participantLookup(&(data->participant_key)); if(!participant){ result = handleParticipant(pDataReader, pDataOffset, 1); if(result == U_RESULT_OK){ participant = in_participantLookup(&(data->participant_key)); } } if(participant){ in_writerNew(participant, data); } else { nn_log (LC_ERROR, "handlePublication: participant not found\n"); result = U_RESULT_INTERNAL_ERROR; /*abort();*/ } } os_mutexUnlock (&gluelock); c_free(sample); sample = NULL; result = u_dataReaderTake(dataReader, takeOne, &sample); } return result; }
static u_result startMonitoring( const u_participant participant, const u_waitset waitset, const struct builtin_datareader_set *drset) { c_iter events, topics; u_waitsetEvent event; c_time timeout; os_uint32 reportInterval; v_gid participantGid, publicationGid, subscriptionGid, gid; u_result result; u_dataReader dataReader; u_topic topic; c_iter vgroups; v_group vgroup; v_duration duration; c_long participantOffset, publicationOffset, subscriptionOffset; os_threadAttr attr; os_result osr; /*Resolve unique identifications of readers*/ participantGid = u_entityGid((u_entity)drset->participant_dr); publicationGid = u_entityGid((u_entity)drset->publication_dr); subscriptionGid = u_entityGid((u_entity)drset->subscription_dr); /*Resolve topics to find offsets in the data. The offsets are used later on*/ duration.seconds = 0; duration.nanoseconds = 0; topics = u_participantFindTopic(participant, V_PARTICIPANTINFO_NAME, duration); topic = c_iterTakeFirst(topics); if(topic){ result = u_entityAction(u_entity(topic), resolveOffset, &participantOffset); } else { result = U_RESULT_INTERNAL_ERROR; in_printf(IN_LEVEL_SEVERE, "Could not resolve participant info offset.\n"); } c_iterFree(topics); if(result == U_RESULT_OK){ topics = u_participantFindTopic(participant, V_PUBLICATIONINFO_NAME, duration); topic = c_iterTakeFirst(topics); if(topic){ result = u_entityAction(u_entity(topic), resolveOffset, &publicationOffset); } else { result = U_RESULT_INTERNAL_ERROR; in_printf(IN_LEVEL_SEVERE, "Could not resolve publication info offset.\n"); } c_iterFree(topics); } if(result == U_RESULT_OK){ topics = u_participantFindTopic(participant, V_SUBSCRIPTIONINFO_NAME, duration); topic = c_iterTakeFirst(topics); if(topic){ result = u_entityAction(u_entity(topic), resolveOffset, &subscriptionOffset); } else { result = U_RESULT_INTERNAL_ERROR; in_printf(IN_LEVEL_SEVERE, "Could not resolve subscription info offset.\n"); } c_iterFree(topics); } if(result == U_RESULT_OK){ timeout.seconds = 0; timeout.nanoseconds = 100 * 1000 * 1000; /*100 ms*/ in_printf(IN_LEVEL_FINE, "Collecting initial entities...\n"); result = handleParticipant(drset->participant_dr, participantOffset); if(result == U_RESULT_OK){ result = handlePublication(drset->publication_dr, publicationOffset, drset->participant_dr, participantOffset); if(result == U_RESULT_OK){ result = handleSubscription(drset->subscription_dr, subscriptionOffset, drset->participant_dr, participantOffset); if(result == U_RESULT_OK){ vgroups = v_serviceTakeNewGroups(service); vgroup = (v_group)c_iterTakeFirst(vgroups); while(vgroup && result == U_RESULT_OK){ result = handleGroup(service, vgroup); c_free(vgroup); vgroup = (v_group)c_iterTakeFirst(vgroups); } c_iterFree(vgroups); if(result == U_RESULT_OK){ in_printf(IN_LEVEL_FINE, "Waiting for entities to be created/deleted...\n"); } else { in_printf(IN_LEVEL_SEVERE, "Could not collect initial groups...\n"); } } else { in_printf(IN_LEVEL_SEVERE, "Could not collect initial subscriptions...\n"); } } else { in_printf(IN_LEVEL_SEVERE, "Could not collect initial publications...\n"); } } else { in_printf(IN_LEVEL_SEVERE, "Could not collect initial participants...\n"); } } osr = os_threadAttrInit(&attr); if(osr == os_resultSuccess){ osr = os_threadCreate(&clientWriterThread, "clientWriterMonitor", &attr, in_discoveryClientWriterMonitor, NULL); if(osr != os_resultSuccess){ result = U_RESULT_INTERNAL_ERROR; } } else { result = U_RESULT_INTERNAL_ERROR; } reportInterval = 0; while(result == U_RESULT_OK && !terminate){ events = NULL; /*Wait for events to occur*/ result = u_waitsetTimedWaitEvents(waitset, timeout, &events); if(result == U_RESULT_OK){ event = (u_waitsetEvent)(c_iterTakeFirst(events)); while(event){ if(((event->events) & V_EVENT_DATA_AVAILABLE) == V_EVENT_DATA_AVAILABLE) { if(event->entity){ dataReader = (u_dataReader)event->entity; gid = u_entityGid(event->entity); if(v_gidCompare(gid, participantGid) == C_EQ){ result = handleParticipant( drset->participant_dr, participantOffset); } else if(v_gidCompare(gid, subscriptionGid) == C_EQ){ result = handleSubscription( drset->subscription_dr, subscriptionOffset, drset->participant_dr, participantOffset); } else if(v_gidCompare(gid, publicationGid) == C_EQ){ result = handlePublication( drset->publication_dr, publicationOffset, drset->participant_dr, participantOffset); } else { in_printf(IN_LEVEL_SEVERE, "This is impossible...at least in my understanding of the world.\n"); result = U_RESULT_INTERNAL_ERROR; } } else { in_printf(IN_LEVEL_WARNING, "DATA_AVAILABLE (%d) but no entity.\n", event->events); } } else if(((event->events) & V_EVENT_NEW_GROUP) == V_EVENT_NEW_GROUP) { vgroups = v_serviceTakeNewGroups(service); vgroup = (v_group)c_iterTakeFirst(vgroups); while(vgroup && result == U_RESULT_OK){ result = handleGroup(service, vgroup); c_free(vgroup); vgroup = (v_group)c_iterTakeFirst(vgroups); } c_iterFree(vgroups); } else { in_printf(IN_LEVEL_SEVERE, "Received unexpected event %d.\n", event->events); result = U_RESULT_INTERNAL_ERROR; } u_waitsetEventFree(event); event = (u_waitsetEvent)(c_iterTakeFirst(events)); } } else if(result == U_RESULT_DETACHING){ in_printf(IN_LEVEL_INFO, "Starting termination now...\n"); } else if(result == U_RESULT_TIMEOUT){ result = U_RESULT_OK; } else { in_printf(IN_LEVEL_SEVERE, "Waitset wait failed.\n"); } if(events){/* events may be null if waitset was deleted */ c_iterFree(events); } reportInterval++; if(reportInterval >= 5){ /*reportEntities();*/ reportInterval = 0; } } return result; }