// ---------------------------------------------------------------------- bool LocalizationGPSfreeLCSModule:: process_message( const ConstMessageHandle& mh ) throw() { /// if ( dynamic_cast<const LocalizationGPSfreeLCSInitMessage*>( mh.get() ) != NULL ) return process_gpsfree_lcs_init_message( *dynamic_cast<const LocalizationGPSfreeLCSInitMessage*>( mh.get() ) ); /// else if ( dynamic_cast<const LocalizationGPSfreeLCSNeighborMessage*>( mh.get() ) != NULL ) return process_gpsfree_lcs_neighbor_message( *dynamic_cast<const LocalizationGPSfreeLCSNeighborMessage*>( mh.get() ) ); return LocalizationModule::process_message( mh ); }
// ---------------------------------------------------------------------- bool LocalizationIterLaterationModule:: process_message( const ConstMessageHandle& mh ) throw() { if ( dynamic_cast<const LocalizationIterLaterationMessage*>( mh.get() ) != NULL ) { return process_iter_lateration_message( *dynamic_cast<const LocalizationIterLaterationMessage*>( mh.get() )); } else if ( dynamic_cast<const LocalizationIterLaterationSoundMessage*>( mh.get() ) != NULL ) { return process_iter_lateration_sound_message( *dynamic_cast<const LocalizationIterLaterationSoundMessage*>( mh.get() )); } return LocalizationModule::process_message( mh ); }
// ---------------------------------------------------------------------- bool HelloworldProcessor:: process_message( const ConstMessageHandle& mh ) throw() { const HelloworldMessage* hmsg = dynamic_cast<const HelloworldMessage*> ( mh.get() ); if( hmsg != NULL ) { last_time_of_receive_=simulation_round(); neighbours_.insert( &hmsg->source() ); if( owner() != hmsg->source() ) cout << "ID '" << owner().label() << "' GOT HELLO FROM '" << hmsg->source().label() << "'" << endl; return true; } else { cout << "ID '" << owner().label() << "' GOT MESSAGE FROM '" << mh.get()->source().label() << "'" << endl; } return Processor::process_message( mh ); }
// ---------------------------------------------------------------------- bool VisEnergyProcessor:: process_message( const ConstMessageHandle& mh ) throw() { const helloworld::HelloworldMessage* hmsg = dynamic_cast<const helloworld::HelloworldMessage*> ( mh.get() ); if( hmsg != NULL ) { last_time_of_receive_=simulation_round(); neighbours_.insert( &hmsg->source() ); cout << "ID '" << owner().label() << "' GOT HELLO FROM '" << hmsg->source().label() << "'" << endl; this->owner_w().remove_tag_by_name("VisBattery"); this->owner_w().add_tag(new shawn::DoubleTag("VisBattery", 0.0)); return true; } this->owner_w().remove_tag_by_name("VisBattery"); this->owner_w().add_tag(new shawn::DoubleTag("VisBattery", 0.0)); return Processor::process_message( mh ); }
// ---------------------------------------------------------------------- bool AutoCastProcessor:: process_message( const ConstMessageHandle& mh ) throw() { if (state() != Processor::Active) return false; const autocast::AutoCastMessage * acm = dynamic_cast<const autocast::AutoCastMessage*>(mh.get()); if (!acm){ std::cerr << "Cast to AutoCastMessage failed!" << std::endl; return false; } if (acm->last_hop_addr() == owner().id()){ /// Prevent processor from receiving his sent messages return false; } if (logging_) { if(received_messages_ids_total_.find(acm->uid()) != received_messages_ids_total_.end()){ /// For debug purposes only std::cerr << "Message with id " << acm->uid() << " already received!" << std::endl; return false; } /// Now receive the packet! received_messages_ids_total_.insert(acm->uid()); } double now = owner().world().scheduler().current_time(); /// Update the local DataUnits local_update(); neighborhood_.append_to_neighborhood( acm->last_hop_addr(),acm->next_update_interval() ); if (acm->packet_type() != AUTOCAST_TYPE_UPDATE){ std::cerr << "_" << acm->source().id() << "_received UNKNOWN packet @ " << owner().world().simulation_controller().world().scheduler().current_time() << std::endl; std::cerr << "Type is " << acm->packet_type() << " and not " << AUTOCAST_TYPE_UPDATE << std::endl; abort(); } /// Two of the neighborhood should flood the information or answer with unknown data int neighborhood_size = neighborhood_.neighborhood_size(); int barker = owner().world().simulation_controller().environment().optional_int_param("_barker__process_message",5); bool answer_decision = (neighborhood_size <= barker || uniform_random(0.0,1.0) <= (1.0 * barker / neighborhood_size)); /// Get the known ID's from the sender /// received_ids saves the ID's received in this "receiving round" std::set<unsigned int> received_ids(acm->known_DataUnits()); /// Get the stale ID's std::set<unsigned int> stale_ids(acm->stale_ids()); /// Fetching the complete DataUnits and give it to the applications or /// hold them in the transport layer bool need_to_flood = false; for (std::set<ConstDataUnitHandle>::const_iterator du_it = acm->complete_DataUnits().begin(); du_it != acm->complete_DataUnits().end(); du_it++){ // Add also the ID's of the complete DataUnits received_ids.insert((*du_it)->id()); if (logging_) received_DataUnit_ids_total_.insert((*du_it)->id()); received_DataUnits_total_++; /// Give to applications bool application_responsible = false; for (Node::ProcessorList::iterator app_it = owner_w().begin_processors_w(); app_it != owner_w().end_processors_w(); app_it++){ autocast::AutoCastApplication* ac_app = dynamic_cast<autocast::AutoCastApplication*>((*app_it).get()); if (ac_app){ application_responsible = ac_app->receive_DataUnit(*du_it); } } if ( !application_responsible && (*du_it)->distribution_area() && ( now < (*du_it)->time() + (*du_it)->max_life_time() ) ){ if (stale_DataUnits_.find((*du_it)->id()) == stale_DataUnits_.end()){ /// Set the flag if the DataUnit is new bool is_new_DataUnit = false; /// Interested in if is a new DataUnit and enable logging (if logging_ is true) autocast::AutoCastProcessor::LocalDataUnit * ldu = handle_DataUnit(*du_it,is_new_DataUnit,logging_); if (ldu){ /// DataUnit is now known locally unknown_DataUnit_ids_.erase(ldu->dataUnit()->id()); if ( unknown_DataUnit_ids_.size() == 0 && request_timer_ ) { owner_w().world_w().scheduler_w().delete_event(request_timer_); request_timer_ = NULL; } // New data unit? if (answer_decision && is_new_DataUnit){ ldu->unknown_count(ldu->unknown_count() + 1); need_to_flood = true; } // }else if(ldu && !is_new_DataUnit){ // /// else old object or no answer_decision // if(ldu->unknown_count() > 0) ldu->unknown_count(ldu->unknown_count() - 1); }/// else out of region }/// else no one feels responsible } } bool need_to_answer = false; if(answer_decision) { for (std::map<int,autocast::AutoCastProcessor::LocalDataUnit*>::iterator it = complete_DataUnits_.begin(); it != complete_DataUnits_.end(); it++){ if ( received_ids.find( it->second->dataUnit()->id() ) == received_ids.end() && stale_ids.find(it->second->dataUnit()->id()) == stale_ids.end()) { it->second->unknown_count(it->second->unknown_count() + 1); need_to_answer = true; } } } bool need_to_request = false; if (answer_decision) { /// Mark data units for request for(std::set<unsigned int>::iterator it = received_ids.begin(); it != received_ids.end(); ++it){ if( complete_DataUnits_.find(*it) == complete_DataUnits_.end() && stale_DataUnits_.find(*it) == stale_DataUnits_.end()) { unknown_DataUnit_ids_.insert(*it); need_to_request = true; } } } // Schedule events double delta = owner().world().simulation_controller().environment().optional_double_param("delay_delta",0.03); double delta_variance = owner().world().simulation_controller().environment().optional_double_param("delay_delta_variance",0.0025); shawn::EventScheduler& es = owner_w().world_w().scheduler_w(); if ( (answer_decision && need_to_answer) || answer_timer_ ){ if ( answer_timer_ ) es.delete_event(answer_timer_); answer_timer_ = es.new_event(*this, now + delta + uniform_random(-delta_variance,delta_variance), NULL); } if( (answer_decision && need_to_flood) || flood_timer_ ){ if ( flood_timer_ ) es.delete_event(flood_timer_); flood_timer_ = es.new_event(*this, now + 2 * delta + uniform_random(-delta_variance,delta_variance), NULL); } if ( (answer_decision && need_to_request) || request_timer_ ) { if ( request_timer_ ) es.delete_event(request_timer_); request_timer_ = es.new_event(*this, now + 3 * delta + uniform_random(-delta_variance,delta_variance), NULL); } return true; }