// ---------------------------------------------------------------------- void LocalizationDLSModule:: initialize( void ) throw(){ shawn::Node* linearizationTool; shawn::Vec* tool; beacons_= new std::vector<shawn::Node*>(); { shawn::World::node_iterator iter = owner_w().owner_w().world_w().begin_nodes_w(); shawn::World::node_iterator end = owner_w().owner_w().world_w().end_nodes_w(); for( ; iter != end ; iter++ ){ LocalizationProcessor* temp= iter->get_processor_of_type_w<LocalizationProcessor>(); if(temp!=NULL){ if(temp->is_anchor() && !temp->is_server() ){ beacons_->push_back(&temp->owner_w()); } } } } linearizationTool=beacons_->front(); if(linearizationTool->has_est_position()){ shawn::Vec est_pos(linearizationTool->est_position()); tool = &est_pos; } else { shawn::Vec real_pos(linearizationTool->real_position()); tool = &real_pos; } beacons_->erase(beacons_->begin()); matrix_a_ = new SimpleMatrix<double>(beacons_->size(),3); vector_r_ =new SimpleMatrix<double>(beacons_->size(),1); std::vector<shawn::Node*>::iterator iter=beacons_->begin(); std::vector<shawn::Node*>::iterator end = beacons_->end(); for(int count =0; iter!= end; iter++,count++){ shawn::Vec* tmp_pos; if((*iter)->has_est_position()) { shawn::Vec est_pos((*iter)->est_position()); tmp_pos=&est_pos; } else { shawn::Vec real_pos((*iter)->real_position()); tmp_pos=&real_pos; } shawn::Vec pos( *tmp_pos - *tool ); matrix_a_->at(count,0)=pos.x(); matrix_a_->at(count,1)=pos.y(); matrix_a_->at(count,2)=pos.z(); vector_r_->at(count,0)=(pos.x()*pos.x()) + (pos.y()*pos.y()) +(pos.z()*pos.z()); //pos.euclidean_norm(); } covariance_a_ = new SimpleMatrix<double>(matrix_a_->covariance()); *matrix_a_ = matrix_a_->transposed(); *matrix_a_= *covariance_a_ * *matrix_a_; send( new LocalizationDLSInitMessage(matrix_a_,vector_r_,covariance_a_,linearizationTool,beacons_)); }
// ---------------------------------------------------------------------- void AutoCastProcessor:: set_state(const Processor::ProcessorState& state) throw() { if (state == Inactive || state == Sleeping){ if ( flood_timer_ ){ owner_w().world_w().scheduler_w().delete_event(flood_timer_); flood_timer_ = NULL; } if ( update_timer_ ){ owner_w().world_w().scheduler_w().delete_event(update_timer_); update_timer_ = NULL; } if ( answer_timer_ ){ owner_w().world_w().scheduler_w().delete_event(answer_timer_); answer_timer_ = NULL; } if ( request_timer_ ){ owner_w().world_w().scheduler_w().delete_event(request_timer_); request_timer_ = NULL; } } Processor::set_state(state); }
// ---------------------------------------------------------------------- void LocalizationProcessor:: init_proc_type( void ) throw() { if ( startup_anchor_frac_ > 0 && startup_anchor_frac_ > *random_ ) { DEBUG( logger(), "set " << owner().label() << " as anchor on startup" ); set_proc_type( anchor ); } // if( owner().is_special_node()) // set_proc_type( server); switch ( proc_type() ) { case anchor: { confidence_ = 1; double position_error= owner().world().simulation_controller().environment().optional_double_param("anchor_pos_err",-1); if(position_error>0.0){ /*shawn::UniformRandomVariable* urv = new shawn::UniformRandomVariable(); urv->set_upper_bound(2*position_error); urv->set_upper_bound_inclusive(false); urv->set_lower_bound_inclusive(false); urv->init(); double dx = (*urv)-position_error; double dy = (*urv) - position_error; double dz =(*urv) - position_error; */ shawn::UniformRandomVariable urv; urv.set_upper_bound(2*position_error); urv.set_upper_bound_inclusive(false); urv.set_lower_bound_inclusive(false); urv.init(); double dx = (urv)-position_error; double dy = (urv) - position_error; double dz =(urv) - position_error; Vec tmp=owner().real_position(); if(tmp.z()==0) dz=0; Vec pos(tmp.x()+dx, tmp.y()+dy,tmp.z()+dz); owner_w().set_est_position(pos); } else owner_w().set_est_position( owner().real_position() ); } case unknown: { confidence_ = 0.1; } case server: { } } }
// ---------------------------------------------------------------------- /// Retruns NULL if DataUnit is out of region autocast::AutoCastProcessor::LocalDataUnit* AutoCastProcessor:: handle_DataUnit(const ConstDataUnitHandle& du, bool& is_new, bool logging) throw() { double now = owner().world().scheduler().current_time(); autocast::AutoCastProcessor::LocalDataUnit* ldu = NULL; // First we assume an old DataUnit is_new = false; // DataUnit out of region returning NULL if ( !(du->distribution_area() && du->distribution_area()->is_inside(owner().real_position().x(), owner().real_position().y())) ){ return NULL; } DataUnitsMap::const_iterator old_du = complete_DataUnits_.find(du->id()); if ( old_du == complete_DataUnits_.end() ){ // Completely new DataUnit ldu = new LocalDataUnit(du); // The DataUnit is flaged as new is_new = true; ldu->last_received_time(now); complete_DataUnits_.insert(std::pair<int,LocalDataUnit*>(ldu->dataUnit()->id(),ldu)); if (logging){ // Do the logging of completely new DataUnit KeeperManagedHandle kmh = owner_w().world_w().simulation_controller_w().simulation_task_keeper_w().find_managed_w("AutoCastTask"); assert(kmh.is_not_null()); AutoCastTask* act = dynamic_cast<AutoCastTask*>( kmh.get() ); assert(act != NULL); act->process_latencies(du,&owner(),owner().world().simulation_controller()); } }else{ /// Old DataUnit /// With newer timestamp? if ( old_du->second->dataUnit()->time() < du->time() ){ if (logging){ //Do the logging of old DataUnit with newer timestamp KeeperManagedHandle kmh = owner_w().world_w().simulation_controller_w().simulation_task_keeper_w().find_managed_w("AutoCastTask"); assert(kmh.is_not_null()); autocast::AutoCastTask* act = dynamic_cast<autocast::AutoCastTask*>( kmh.get() ); assert(act != NULL); act->process_latencies(du,&owner(),owner().world().simulation_controller()); } /// If the timestap is newer replace the DataUnit in the LocalDataUnit old_du->second->dataUnit(du); ldu = old_du->second; /// The DataUnit is flaged as new is_new = true; } /// DataUnit is really old /// Set the last_received_time of the LocalDataUnit old_du->second->last_received_time(now); ldu = old_du->second; } return ldu; }
// ---------------------------------------------------------------------- void Processor:: send( const MessageHandle& mh ) throw() { owner_w().send( mh ); }
// ---------------------------------------------------------------------- void GeoRoutingProcessor:: boot( void ) throw() { routing::RoutingKeeper& rk = routing::routing_keeper_w( owner_w().world_w().simulation_controller_w() ); for( RoutingKeeper::HandleMapType::iterator it = rk.begin_handles_w(); it != rk.end_handles_w(); ++it ) { GeoRouting* routing_instance = dynamic_cast<GeoRouting*>( it->second.get() ); if( routing_instance ) { routing_instance->boot( owner_w() ); } } }
// ---------------------------------------------------------------------- void AutoCastProcessor:: update() throw() { if( answer_timer_ ){ owner_w().world_w().scheduler_w().delete_event(answer_timer_); answer_timer_ = NULL; } if( flood_timer_ ){ owner_w().world_w().scheduler_w().delete_event(flood_timer_); flood_timer_ = NULL; } if ( update_timer_ ) { owner_w().world_w().scheduler_w().delete_event(update_timer_); update_timer_ = NULL; } if ( request_timer_ ){ owner_w().world_w().scheduler_w().delete_event(request_timer_); request_timer_ = NULL; } double now = owner().world().simulation_controller().world().scheduler().current_time(); double lb = owner().world().simulation_controller().environment().optional_double_param("_lb__update",0.95); double ub = owner().world().simulation_controller().environment().optional_double_param("_ub__update",1.05); double time_interval = get_update_time() * uniform_random(lb,ub); if (state() == Processor::Active) { local_update(); unknown_DataUnit_ids_.clear(); send_update_packet(time_interval); } for (DataUnitsMap::iterator it = complete_DataUnits_.begin(); it != complete_DataUnits_.end(); it++){ it->second->unknown_count(0); } if (state() == Processor::Active) update_timer_ = owner_w().world_w().scheduler_w().new_event(*this,now + time_interval,NULL); }
// ---------------------------------------------------------------------- bool SimplifiedGeoRoutingProcessor:: process_message( const shawn::ConstMessageHandle& mh ) throw() { const SimplifiedGeoRoutingPerimeterMessage* pm = dynamic_cast<const SimplifiedGeoRoutingPerimeterMessage*>( mh.get() ); if(pm) { SimplifiedGeoRouting* routing_instance = static_cast<SimplifiedGeoRouting*>( pm->routing_instance_w() ); return routing_instance->process_perimeter_message(owner_w(),*pm); } const SimplifiedGeoRoutingGreedyMessage* gm = dynamic_cast<const SimplifiedGeoRoutingGreedyMessage*>( mh.get() ); if(gm) { SimplifiedGeoRouting* routing_instance = static_cast<SimplifiedGeoRouting*>( gm->routing_instance_w() ); return routing_instance->process_greedy_message(owner_w(),*gm); } return Processor::process_message( mh ); }
// ---------------------------------------------------------------------- void ExtIfaceProcessor:: boot() throw() { #ifdef ENABLE_TESTBEDSERVICE set_node( owner_w() ); testbedservice_proc_boot(); #endif }
void LocalizationDLSModule:: boot( void ) throw() { if(owner().owner().is_special_node()){ owner_w().set_proc_type(owner().server); initialize(); } }
// ---------------------------------------------------------------------- void ExampleTestbedServiceProcessor:: boot() throw() { // important to call boot of parent! set_node( owner_w() ); TestbedServiceProcessor::testbedservice_proc_boot(); std::cout << "ExampleTestbedServiceProcessor::boot" << std::endl; }
// ---------------------------------------------------------------------- void AutoCastProcessor:: boot( void ) throw() { neighborhood_.set_owner(owner()); fetch_parameters(); double now = owner().world().scheduler().current_time(); double lb2 = owner().world().simulation_controller().environment().optional_double_param("_lb2__boot",0.9); double ub1 = owner().world().simulation_controller().environment().optional_double_param("_ub1__boot",1.1); update_timer_ = owner_w().world_w().scheduler_w().new_event(*this,now+fabs(max_startup_time_) * uniform_random(lb2,ub1),NULL); max_iterations_ = owner().world().simulation_controller().environment().required_int_param("max_iterations"); logging_ = owner().world().simulation_controller().environment().optional_bool_param("autocast_logging", logging_); use_stale_hashes_ = owner().world().simulation_controller().environment().optional_bool_param("autocast_stale_hashes", use_stale_hashes_); std::string activity_file = owner().world().simulation_controller().environment().optional_string_param("activity_file", ""); if (activity_file != "") { std::ifstream * inputfile = new ifstream(activity_file.c_str()); if (inputfile->is_open()) { std::ostringstream oss; oss << "$g(" << owner().id() << ")"; string id(oss.str()); std::cerr << "\nSearching for " << id << " in " << activity_file << " "; char buffer[250]; std::string line; while (! inputfile->eof()) { inputfile->getline(buffer,250); line = string(buffer); if (line.find(id) != std::string::npos) { //std::cerr << line << " " << "\n"; line.assign(line,line.find("$ns_ at ")+8,std::string::npos); //std::cerr << line << "\n"; line.assign(line,0,line.find(".")); //std::cerr << line << "\n"; int time = shawn::conv_string_to_int(line); active_times_.push_back(time); std::cerr << " " << time << " "; } } inputfile->close(); // If an activity file is given, we let the processors sleep until the first given time this->set_state(Processor::Sleeping); active_times_it_ = active_times_.begin(); } } }
// ---------------------------------------------------------------------- void VisEnergyProcessor:: boot( void ) throw() { last_time_of_receive_ = simulation_round(); //Creates an event to the event_scheduler event_handle_ = owner_w().world_w().scheduler_w().new_event(*this,sending_time_ ,NULL); this->owner_w().add_tag(new shawn::DoubleTag("VisBattery", 1.0)); std::string labelvalue = "LabelTest"; this->owner_w().add_tag(new shawn::StringTag("VisLabel", labelvalue)); }
// ---------------------------------------------------------------------- bool GeoRoutingProcessor:: process_message( const shawn::ConstMessageHandle& mh ) throw() { const GeoRoutingBeaconMessage* bm = dynamic_cast<const GeoRoutingBeaconMessage*>( mh.get() ); if(bm) { return bm->routing_instance_w()->process_beacon_message(owner_w(),*bm); } const GeoRoutingPerimeterMessage* pm = dynamic_cast<const GeoRoutingPerimeterMessage*>( mh.get() ); if(pm) { GeoRouting* routing_instance = static_cast<GeoRouting*>( pm->routing_instance_w() ); return routing_instance->process_perimeter_message(owner_w(),*pm); } const GeoRoutingGreedyMessage* gm = dynamic_cast<const GeoRoutingGreedyMessage*>( mh.get() ); if(gm) { GeoRouting* routing_instance = static_cast<GeoRouting*>( gm->routing_instance_w() ); return routing_instance->process_greedy_message(owner_w(),*gm); } return Processor::process_message( mh ); }
// ---------------------------------------------------------------------- void LocalizationProcessor:: boot( void ) throw() { neighborhood_.set_source( owner_w() ); random_ = owner().world().simulation_controller(). random_variable_keeper().find( "uni[0;1]" ); init_parameters(); init_modules(); init_proc_type(); assert( dist_module_ && pos_module_ && ref_module_ ); dist_module_->set_owner( *this ); dist_module_->boot(); pos_module_->set_owner( *this ); pos_module_->boot(); ref_module_->set_owner( *this ); ref_module_->boot(); }
// ---------------------------------------------------------------------- void AutoCastProcessor:: work( void ) throw() { /*neighbors_count_+=neighborhood_.neighborhood_size(); real_neighbors_count_+=owner().get_adjacent_nodes().size(); velocity_count_+=owner().movement().velocity().euclidean_norm(); update_time_count_+=update_time_;*/ if (active_times_.size() > 0) { // If activeTimes were read from file int now = static_cast<int>(owner().world().scheduler().current_time()); if (active_times_it_ != active_times_.end() && *active_times_it_ == now) { // it's time o switch the State if (this->state() == Processor::Active) { this->set_state(Processor::Sleeping); } else { this->set_state(Processor::Active); if ( update_timer_ ) { owner_w().world_w().scheduler_w().delete_event(update_timer_); update_timer_ = NULL; } update_timer_ = owner_w().world_w().scheduler_w().new_event(*this,now + max_update_time_ * uniform_random(0.0,1.0),NULL); } ++active_times_it_; } } if (logging_ && state() == Processor::Active) activeTime_++; if (logging_ && (activeTime_ > 0) && (simulation_round() == max_iterations_ - 1)){ KeeperManagedHandle kmh = owner_w().world_w().simulation_controller_w().simulation_task_keeper_w().find_managed_w("AutoCastTask"); assert(kmh.is_not_null()); autocast::AutoCastTask* act = dynamic_cast<autocast::AutoCastTask*>( kmh.get() ); if (act){ act->process_sent_statistic( &owner(), packets_sent_total_, bytes_sent_total_, dataUnits_sent_total_, dataUnits_bytes_sent_total_, received_messages_ids_total_.size(), received_DataUnits_total_, received_DataUnit_ids_total_.size(), activeTime_, msgCountBeacon_, msgCountAnswer_, msgCountAnswer_only_, msgCountFlood_, msgCountFlood_only_, msgCountRequest_, msgCountRequest_only_ ); } // And at least clean_up(); } }
// ---------------------------------------------------------------------- void AutoCastProcessor:: send_update_packet(const double update_time) throw() { assert(stale_DataUnits_.size() == 0 || use_stale_hashes_ == false); double now = owner().world().scheduler().current_time(); uid_counter_++; AutoCastMessage * acm = new AutoCastMessage(uid_counter_,AUTOCAST_TYPE_UPDATE,update_time,owner().id(),owner().real_position().x(),owner().real_position().y()); autocast::AutoCastProcessor::LocalDataUnit * ldu = most_urgent_DataUnit(); /// Append complete DataUnits int dataUnits_bytes = 0; // NEW: The stale DataUnits int id_space = 4 * complete_DataUnits_.size() + 4 * stale_DataUnits_.size(); while( ldu && ( acm->complete_DataUnits().size() <= max_update_data_units_ ) && ( ( acm->size() + id_space + ldu->dataUnit()->size() ) <= max_update_packet_size_ ) ){ ldu->refresh(); ldu->last_send_time(now); ldu->unknown_count(0); /// Detaching DataUnit DataUnit * du = new DataUnit( *(ldu->dataUnit()) ); /// Incrementing DataUnits hop count du->hop_count( ldu->dataUnit()->hop_count() + 1 ); dataUnits_bytes += du->size(); // Assertion if (now > du->time() + du->max_life_time()){ std::cerr << "object " << ldu->dataUnit()->id() << " is too old. Stop!" << std::endl; abort(); } acm->insert_complete_DataUnit(du); id_space -= 4; ldu = most_urgent_DataUnit(); } /// Append list with also known objects for (DataUnitsMap::iterator it = complete_DataUnits_.begin(); it != complete_DataUnits_.end(); it++){ if (it->second->last_send_time() < now) acm->insert_id(it->second->dataUnit()->id()); } /// Stale DataUnits for (DataUnitsMap::const_iterator it = stale_DataUnits_.begin(); it != stale_DataUnits_.end(); ++it){ acm->insert_stale_id(it->second->dataUnit()->id()); } /// Assertion if (acm->size() > max_update_packet_size_){ assert(max_update_packet_size_ == AUTOCAST_MAX_PACKET_LENGTH); std::cerr << "Time: " << now << " Node: " << owner().id() << " wants to send " << acm->size() << " byte, but only " << max_update_packet_size_ << " are allowed!"; exit(1); } /// Statistics packets_sent_total_++; bytes_sent_total_ += acm->size(); dataUnits_sent_total_ += acm->complete_DataUnits().size(); dataUnits_bytes_sent_total_ += dataUnits_bytes; owner_w().send(acm); }
// ---------------------------------------------------------------------- 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; }