static v_message findUnregisterMessage( c_iter msgs, v_message tmpl) { c_long i; v_message result = NULL; if(msgs && tmpl){ for(i=0; i<c_iterLength(msgs) && !result; i++){ result = (v_message)(c_iterObject(msgs, i)); if(v_gidCompare(result->writerGID, tmpl->writerGID) != C_EQ){ result = NULL; } } } 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; }
static d_storeResult d_instanceRemove ( d_instance instance, v_message message) { d_sample current, sample; v_state state; v_message m; d_storeResult result; if ((instance != NULL) && (message != NULL)) { sample = NULL; current = d_instanceGetHead(instance); while(!sample && current){ m = d_sampleGetMessage(current); if(c_timeCompare(m->writeTime, message->writeTime) == C_EQ){ if(v_gidCompare(m->writerGID, message->writerGID) == C_EQ){ if(m->sequenceNumber == message->sequenceNumber){ sample = current; } } } if(!sample){ current = current->older; } } if(sample){ if (sample->newer != NULL) { d_sample(sample->newer)->older = c_keep(sample->older); } else { assert(d_instanceGetHead(instance) == sample); d_instanceSetHead(instance,sample->older); } if (sample->older != NULL) { d_sample(sample->older)->newer = sample->newer; } else { assert(d_instanceGetTail(instance) == sample); d_instanceSetTail(instance,sample->newer); } state = v_nodeState(d_sampleGetMessage(sample)); if (v_stateTest(state, L_WRITE)) { instance->count--; instance->messageCount--; } if (v_stateTest(state, L_DISPOSED)) { instance->count--; } c_free(sample); if (instance->oldest == NULL) { v_stateSet(instance->state, L_EMPTY); } } assert((instance->count == 0) == (d_instanceGetTail(instance) == NULL)); assert((instance->count == 0) == (d_instanceGetHead(instance) == NULL)); result = D_STORE_RESULT_OK; } else { result = D_STORE_RESULT_ILL_PARAM; } return result; }
v_ownershipResult v_determineOwnershipByStrength ( struct v_owner *owner, const struct v_owner *candidate, v_state messageState) { c_equality equality; v_ownershipResult result; assert (owner != NULL); assert (candidate != NULL); if (owner->exclusive == TRUE) { if (v_gidIsValid (candidate->gid)) { if (owner->exclusive == candidate->exclusive) { if (v_gidIsValid (owner->gid)) { equality = v_gidCompare (owner->gid, candidate->gid); if (candidate->strength > owner->strength) { if (equality == C_EQ) { result = V_OWNERSHIP_ALREADY_OWNER; owner->strength = candidate->strength; } else { result = claimOwnership(owner, candidate, messageState); } } else if (candidate->strength < owner->strength) { if (equality == C_EQ) { /* The current message comes from the a writer, * which is the owner AND which lowered it's * strength. The strength associated with the * ownership must be updated. */ owner->strength = candidate->strength; result = V_OWNERSHIP_ALREADY_OWNER; } else { result = V_OWNERSHIP_NOT_OWNER; } } else { if (equality == C_LT) { result = V_OWNERSHIP_NOT_OWNER; } else { result = claimOwnership(owner, candidate, messageState); } } } else { /* When the owner is not valid, nobody can claim ownership naturally because * the smallest GID always wins. So enforce ownership transfer explicitly. */ result = claimOwnership(owner, candidate, messageState); } } else { result = V_OWNERSHIP_INCOMPATIBLE_QOS; } } else { v_gidSetNil (owner->gid); owner->strength = 0; result = V_OWNERSHIP_OWNER_RESET; } } else { result = V_OWNERSHIP_SHARED_QOS; } return result; }