void init_topology_elevation( shawn::SimulationController& sc ) { std::cout << "init_topology_elevation" << std::endl; sc.simulation_task_keeper_w().add( new ConstantElevationTask ); sc.simulation_task_keeper_w().add( new XYZFileElevationTask ); }
extern "C" void init_distest( shawn::SimulationController& sc ) { sc.distance_estimate_keeper_w().add( new shawn::NeighborhoodIntersectionDistanceEstimate ); sc.simulation_task_keeper_w().add( new distest::DistanceEstimateTask ); sc.simulation_task_keeper_w().add( new distest::MultihopDistanceEstimateTask ); sc.simulation_task_keeper_w().add( new distest::TestTask ); }
// ---------------------------------------------------------------------- void CreateDynamicEdgesTransmissionRangeTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); GroupElement* all_edges = new GroupElement("all.edges"); visualization_w().add_element(all_edges); std::string pref = sc.environment(). optional_string_param("prefix",DrawableEdgeDynamic::PREFIX); std::string node_prefix = sc.environment(). optional_string_param("node_prefix",DrawableNodeDefault::PREFIX); std::string _source_regex = sc.environment().optional_string_param("source_regex", ".*"); std::string _target_regex = sc.environment().optional_string_param("target_regex", ".*"); const shawn::Node &dummy = *(visualization().world().begin_nodes()); std::cout << "Regex: " << _source_regex << std::endl; DrawableEdgeDynamicTransmissionRange* ded = new DrawableEdgeDynamicTransmissionRange(dummy,dummy, pref, node_prefix, _source_regex, _target_regex); ded->init(); visualization_w().add_element(ded); all_edges->add_element(*ded); }
// ---------------------------------------------------------------------- void CreateEstimatedEdgesTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); std::string pref = sc.environment(). optional_string_param("prefix",DrawableEdgeEstimated::PREFIX); std::string node_prefix = sc.environment(). optional_string_param("node_prefix",DrawableNodeDefault::PREFIX); GroupElement* grp = group(sc); for( shawn::World::const_node_iterator it = visualization().world().begin_nodes(), endit = visualization().world().end_nodes(); it != endit; ++it ) { if( it->has_est_position()) { const DrawableNode* dn = drawable_node(*it, node_prefix); DrawableEdgeEstimated* dee = new DrawableEdgeEstimated(*it, *dn, DrawableEdgeEstimated::PREFIX); dee->init(); visualization_w().add_element(dee); if( grp != NULL ) grp->add_element(*dee); } } }
// ---------------------------------------------------------------------- void TreeCreationTask:: create_tree_in_file( shawn::SimulationController& sc, const std::string& filename, const shawn::Node& sink, int max_hops ) throw() { // Avoid appending bool append = sc.environment().optional_bool_param("append",false); if(!append) { remove( filename.c_str() ); } // Init stuff // double now = sink.current_time(); NodesToExamineMap nodes_to_examine; TreeCreationHopsToSinkResult& result = *( new TreeCreationHopsToSinkResult( sc.world_w() ) ); ofstream file_op(filename.c_str(),ios::app); // Treat the sink file_op << sink.id() << "\t" << sink.id() << "\t" << sink.id() << "\t" << 0 << "\t" << sink.real_position().x() << "\t" << sink.real_position().y() << endl; result[sink].hops_ = 0; nodes_to_examine.insert( NodesToExamineMapValueType(0,&sink) ); // Main loop while( ! nodes_to_examine.empty() ) { NodesToExamineMapIterator min_it = nodes_to_examine.begin(); const shawn::Node* min_node = min_it->second; nodes_to_examine.erase( min_it ); for( World::const_adjacency_iterator adj_it = min_node->begin_adjacent_nodes(); adj_it != min_node->end_adjacent_nodes(); ++adj_it ) { int hops = result[*min_node].hops_ + 1; if( hops < result[*adj_it].hops_ ) { assert( result[*adj_it].hops_ == INT_MAX ); result[*adj_it].hops_ = hops; file_op << adj_it->id() << "\t" << sink.id() << "\t" << min_node->id() << "\t" << hops << "\t" << adj_it->real_position().x() << "\t" << adj_it->real_position().y() << endl; if( hops < max_hops ) { nodes_to_examine.insert( NodesToExamineMapValueType( hops, &(*adj_it) ) ); } } } } file_op.clear(); file_op.close(); delete &result; }
AutoElements:: AutoElements( shawn::SimulationController& sc, Visualization& vis ) throw( std::runtime_error ) : current_ ( NULL ) #ifdef HAVE_BOOST_REGEX , regex_ ( NULL ) #endif { std::string parm; tag_ = sc.environment().optional_string_param("tag", ""); parm=sc.environment().optional_string_param("elem",""); if( !parm.empty() ) { mode_=Single; current_ = vis.element_w(parm); if( current_.is_null() ) throw std::runtime_error(std::string("no such element: ") + parm ); return; } parm=sc.environment().optional_string_param("elem_regex",""); std::string tagregex = sc.environment().optional_string_param("tag_regex",""); if( !parm.empty() || (!tagregex.empty() && !tag_.empty())) { #ifdef HAVE_BOOST_REGEX if(tag_.empty() || tagregex.empty()) mode_=Regex; else mode_=TagRegex; try { if(mode_==Regex) regex_= new boost::regex(parm); else regex_= new boost::regex(tagregex); } catch(...) { throw std::runtime_error(std::string("regular expression is bad")); } vis_cur_=vis.elements().begin(); vis_end_=vis.elements().end(); advance_infeasible(); return; #else throw std::runtime_error(std::string("no regexp support compiled in")); #endif } throw std::runtime_error(std::string("specify elements (either $elem or $elem_regex)")); }
// ---------------------------------------------------------------------- void ConstantElevationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { double val = sc.environment().required_double_param("height"); std::string n = sc.environment().optional_string_param("name",""); elevation_keeper_w(sc).add( new ConstantElevation(val,n) ); }
// ---------------------------------------------------------------------- void XYZFileElevationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { std::string fn = sc.environment().required_string_param("file"); std::string outn = sc.environment().required_string_param("name"); std::string xyplacen = sc.environment().optional_string_param("xy_placement",KEY_SCALE); std::string zplacen = sc.environment().optional_string_param("z_placement",KEY_SCALE); bool xyplace_box; bool zplace_box; if( xyplacen == KEY_BOX ) xyplace_box = true; else if( xyplacen==KEY_SCALE ) xyplace_box = false; else throw runtime_error(string("$xy_placement must be ")+KEY_SCALE+string(" or ")+KEY_BOX); if( zplacen == KEY_BOX ) zplace_box = true; else if( zplacen==KEY_SCALE ) zplace_box = false; else throw runtime_error(string("$z_placement must be ")+KEY_SCALE+string(" or ")+KEY_BOX); XYZFile* data = new XYZFile; Vec base_point; Vec scale_vec; XYZFileElevation* elev; try { data->read(fn); if( (data->x_dimension()<2) || (data->y_dimension()<2) ) { ostringstream oss; oss << "XYZ data must have at least 2x2 grid points, but " << fn << " just contains " << data->x_dimension() << "x" << data->y_dimension(); throw runtime_error(oss.str()); } if( xyplace_box ) xy_place_box(sc,*data,base_point,scale_vec); else xy_place_scale(sc,*data,base_point,scale_vec); if( zplace_box ) z_place_box(sc,*data,base_point,scale_vec); else z_place_scale(sc,*data,base_point,scale_vec); elev = new XYZFileElevation(data, base_point, scale_vec.x(),scale_vec.y(),scale_vec.z(), outn); } catch( runtime_error& ) { delete data; throw; } elevation_keeper_w(sc).add( elev ); }
extern "C" void init_examples( shawn::SimulationController& sc ) { std::cout << "Initialising examples" << std::endl; sc.simulation_task_keeper_w().add( new exampletask::ExampleTask ); sc.simulation_task_keeper_w().add( new examples::ConnectivityTask ); helloworld::HelloworldProcessorFactory::register_factory(sc); helloworld::HelloworldRandomProcessorFactory::register_factory(sc); sc.simulation_task_keeper_w().add( new examples::tagtest::TagTestTask ); }
// -------------------------------------------------------------------- bool TestbedServiceServer:: start_server( const shawn::SimulationController& sc ) { host_ = sc.environment().required_string_param( "testbedservice_server_host" ); port_ = sc.environment().required_int_param( "testbedservice_server_port" ); TestbedServiceServer::WorkerThread::wsdl_path = sc.environment().required_string_param( "testbedservice_wsdl" ); runner_ = new boost::thread( boost::bind( &WorkerThread::run, &worker_, host_, port_ ) ); return true; }
// ---------------------------------------------------------------------- void XYZFileElevationTask:: z_place_scale( shawn::SimulationController& sc, const XYZFile& f, shawn::Vec& bp, shawn::Vec& s ) throw( std::runtime_error ) { bp = Vec( bp.x(), bp.y(), sc.environment().optional_double_param("z_base",0.0) ); s = Vec( s.x(), s.y(), sc.environment().optional_double_param("z_scale",1.0) ); }
// ---------------------------------------------------------------------- void CreateEdgesTagTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); #ifndef HAVE_BOOST_REGEX throw std::runtime_error("no boost::regex support compiled in"); #else boost::regex sources(sc.environment().required_string_param("source_regex")); boost::regex targets(sc.environment().required_string_param("target_regex")); std::string taglabel = sc.environment().required_string_param("tag"); std::string pref = sc.environment(). optional_string_param("prefix",DrawableEdgeDefault::PREFIX); std::string node_prefix = sc.environment(). optional_string_param("node_prefix",DrawableNodeDefault::PREFIX); GroupElement* grp = group(sc); for( shawn::World::const_node_iterator it = visualization().world().begin_nodes(), endit = visualization().world().end_nodes(); it != endit; ++it ) { if( boost::regex_search(get_stringtag(&*it,taglabel)->value(),sources)) { for( shawn::Node::const_adjacency_iterator ait = it->begin_adjacent_nodes(), endait = it->end_adjacent_nodes(); ait != endait; ++ait ) if( *it != *ait ) if( boost::regex_search(get_stringtag(&*ait,taglabel)->value(),targets) ) if( (ait->label() > it->label()) || (!boost::regex_search(get_stringtag(&*it,taglabel)->value(),targets)) || (!boost::regex_search(get_stringtag(&*ait,taglabel)->value(),sources)) ) { const DrawableNode* dsrc = drawable_node(*it,node_prefix); const DrawableNode* dtgt = drawable_node(*ait,node_prefix); DrawableEdgeDefault* ded = new DrawableEdgeDefault(*it,*ait,*dsrc,*dtgt,pref); ded->init(); visualization_w().add_element(ded); if( grp != NULL ) grp->add_element(*ded); } } } #endif }
// ---------------------------------------------------------------------- void TreeCreationTask:: create_tree_from_file( shawn::SimulationController& sc, routing::tree::TreeRouting& routing_instance, const std::string& filename ) throw() { double now = sc.world().current_time(); ifstream file_in; file_in.open(filename.c_str(),ifstream::in); if(!file_in) { ERROR(this->logger(),"Unable to open file " + filename + "!"); abort(); } string buf; while( getline(file_in,buf) ) { StrTok tok(buf,"\t "); double line[6]; int i = 0; for(StrTok::iterator it = tok.begin(); it != tok.end(); ++it) { line[i++] = conv_string_to_double(*it); } shawn::Node* node = sc.world_w().find_node_by_id_w( (int)line[0] ); shawn::Node* sink = sc.world_w().find_node_by_id_w( (int)line[1] ); shawn::Node* neighbor = sc.world_w().find_node_by_id_w( (int)line[2] ); int hops_to_sink = (int)line[3]; double node_x = line[4]; double node_y = line[5]; double node_real_pos_x = conv_string_to_double(conv_double_to_string(node->real_position().x())); double node_real_pos_y = conv_string_to_double(conv_double_to_string(node->real_position().y())); if( !( node && sink && neighbor ) ) { ERROR(this->logger(),"Error while reading file!"); abort(); } if( node_x != node_real_pos_x || node_y != node_real_pos_y ) { ERROR(this->logger(),"Position mismatch! Read x: " + conv_double_to_string(node_x) + ", y: " + conv_double_to_string(node_y) + " Real position x: " + conv_double_to_string(node_real_pos_x) + ", y: " + conv_double_to_string(node_real_pos_y)); abort(); } routing_instance.tree_routing_table_update(*node,*sink,TreeRoutingTableInfo(*neighbor,hops_to_sink,now)); } INFO(this->logger(),"Reading from file: " + filename + " succeeded!"); }
extern "C" void init_reading( shawn::SimulationController& sc ) { // Keepers sc.add_keeper(new reading::ReadingKeeper()); sc.add_keeper(new reading::SensorKeeper()); // Sensors sc.keeper_by_name_w<reading::SensorKeeper>("SensorKeeper")->add( new reading::SimpleSensorDoubleFactory ); sc.keeper_by_name_w<reading::SensorKeeper>("SensorKeeper")->add( new reading::SimpleSensorIntegerFactory ); // Test reading::RandomDoubleTestProcessorFactory::register_factory(sc); sc.simulation_task_keeper_w().add( new reading::SimulationTaskReadingDoubleTestCreate ); }
// ---------------------------------------------------------------------- void DefaultNodeGenerator:: init_processor_factories( shawn::SimulationController& sc, ProcessorFactoryList& pfl ) throw( std::runtime_error ) { shawn::StrTok tok( sc.environment().optional_string_param( "processors", "" ), ", " ); for ( shawn::StrTok::iterator it = tok.begin(), end = tok.end(); it != end; ++it ) { shawn::ProcessorFactoryHandle pfh = sc.processor_keeper_w().find_w( *it ); assert( pfh != NULL ); // not found throws... pfl.push_back( pfh ); } }
// ---------------------------------------------------------------------- void XYZFileElevationTask:: xy_place_scale( shawn::SimulationController& sc, const XYZFile& f, shawn::Vec& bp, shawn::Vec& s ) throw( std::runtime_error ) { double scale = sc.environment().optional_double_param("xy_scale",1.0); bp = Vec( sc.environment().optional_double_param("x1",0.0), sc.environment().optional_double_param("y1",0.0), bp.z() ); s = Vec( sc.environment().optional_double_param("x_scale",scale), sc.environment().optional_double_param("y_scale",scale), s.z() ); }
void init_topology_node_gen( shawn::SimulationController& sc ) { std::cout << "init_topology_node_gen" << std::endl; NodeGeneratorKeeper* ng = new NodeGeneratorKeeper; sc.add_keeper( ng ); ng->add( new DefaultNodeGenerator ); }
// ---------------------------------------------------------------------- void SpyglassTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { std::string spyglass_outfile = sc.environment().required_string_param("spyglass_file"); Spyglass::set_spyglass(spyglass_outfile); }
// ---------------------------------------------------------------------- void RectangleTopologyTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { double x1 = sc.environment().required_double_param("x1"); double x2 = sc.environment().required_double_param("x2"); double y1 = sc.environment().required_double_param("y1"); double y2 = sc.environment().required_double_param("y2"); std::string n = sc.environment().optional_string_param("name",""); if( x1 > x2 ) throw std::runtime_error("rectangle has negative X size"); if( y1 > y2 ) throw std::runtime_error("rectangle has negative Y size"); topology_keeper_w(sc).add( new RectangleTopology(Box(Vec(x1,y1,0.0), Vec(x2,y2,0.0)), n) ); }
// ---------------------------------------------------------------------- void ExternalAnimationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); std::string file_name = sc.environment().optional_string_param( "filename", "animation"); double refresh_interval = sc.environment().optional_double_param( "refresh_interval", 1.0); std::string writer_type = sc.environment().optional_string_param("writer", "png"); double event_time = sc.world().scheduler().current_time(); sc.world_w().scheduler_w().new_event(*new WriteAnimationFrameEvent( refresh_interval, visualization_w(), writer_type, file_name, sc ), event_time, NULL); }
// ---------------------------------------------------------------------- void ExampleTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { require_world( sc ); double count = 0; for( shawn::World::const_node_iterator it = sc.world().begin_nodes(); it != sc.world().end_nodes(); ++it ) { count++; } INFO( logger(), "visited " << count << " nodes" ); }
// ---------------------------------------------------------------------- ConstPropertyHandle PropertyConstantVecTask:: create_property( shawn::SimulationController& sc ) throw( std::runtime_error ) { double x = sc.environment().required_double_param("x"); double y = sc.environment().required_double_param("y"); double z = sc.environment().optional_double_param("z",0.0); PropertyConstantVec* pc = new PropertyConstantVec(shawn::Vec(x,y,z)); try { fill_default_params(*pc,sc); } catch( std::runtime_error& ) { delete pc; throw; } return pc; }
// ---------------------------------------------------------------------- void CreateGroupTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); GroupElement* ge = new GroupElement( sc.environment().required_string_param("group") ); ge->init(); visualization_w().add_element( ge ); }
// ---------------------------------------------------------------------- void CuboidTopologyTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { double x1 = sc.environment().required_double_param("x1"); double x2 = sc.environment().required_double_param("x2"); double y1 = sc.environment().required_double_param("y1"); double y2 = sc.environment().required_double_param("y2"); double z1 = sc.environment().required_double_param("z1"); double z2 = sc.environment().required_double_param("z2"); std::string n = sc.environment().optional_string_param("name",""); if( x1 > x2 ) throw std::runtime_error("cuboid has negative X size"); if( y1 > y2 ) throw std::runtime_error("cuboid has negative Y size"); if( z1 > z2 ) throw std::runtime_error("cuboid has negative Z size"); topology_keeper_w(sc).add( new CuboidTopology(Box(Vec(x1,y1,z1), Vec(x2,y2,z2)), n) ); }
// ---------------------------------------------------------------------- void SingleSnapshotTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); double t = sc.environment().optional_double_param("time",0.0); int dr = sc.environment().optional_int_param("draft",0); WriterFactoryHandle wfh = sc.keeper_by_name_w<WriterKeeper>("WriterKeeper") ->find_w(sc.environment().optional_string_param("writer", "pdf")); Writer* wr = wfh->create(); wr->set_draft(dr); wr->pre_write( visualization(), sc.environment().optional_string_param("filename", "snapshot"), false ); wr->write_frame( t ); wr->post_write(); delete wr; }
// ---------------------------------------------------------------------- void XMLPolygonTopologyTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { std::string f = sc.environment().required_string_param("file"); std::string n = sc.environment().required_string_param("name"); bool create_outer = sc.environment().optional_bool_param("xml_create_outer_hull", false) || sc.environment().optional_bool_param("create_outer_hull", false); bool fix_non_simple_polygons = sc.environment().optional_bool_param("fix_non_simple_polygons", false); XMLPolygonTopology* p = new XMLPolygonTopology; p->set_name(n); p->read(sc, f, create_outer, fix_non_simple_polygons); topology_keeper_w(sc).add(p); }
// ---------------------------------------------------------------------- void CreateGraphicsTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); DrawableGraphics* dg = new DrawableGraphics( sc.environment().required_string_param("name"), sc.environment().required_string_param("file") ); dg->init(); visualization_w().add_element( dg ); }
// ---------------------------------------------------------------------- void DefaultNodeGenerator:: pre_generate( shawn::SimulationController& sc, reading::ConstBoolReadingHandle brh ) throw( std::runtime_error ) { NodeGenerator::pre_generate(sc,brh); init_processor_factories( sc, proc_factories_ ); uuid_ = shawn::UUIDGenerator::uuid(); nodes_so_far_=0; if( !sc.has_world() ) throw std::runtime_error("DefaultNodeGenerator needs a world!"); }
// ---------------------------------------------------------------------- void LatticePointGenerator:: pre_generate( shawn::SimulationController& sc, reading::ConstBoolReadingHandle brh ) throw( std::runtime_error ) { PointGenerator::pre_generate(sc,brh); spacing_ = (float) sc.environment().optional_double_param("spacing", 1.0); bbox_=extract_box(brh); check_fail_count_=true; cur_x_ = cur_y_ = cur_z_ = 0; // mycount = 0; }
extern "C" void init_routing( shawn::SimulationController& sc ) { // Create two keepers in the simulation controller // one for the factories ... sc.add_keeper( new routing::RoutingFactoryKeeper ); // ... and one for the created routing protocol instances sc.add_keeper( new routing::RoutingKeeper ); sc.simulation_task_keeper_w().add( new routing::RoutingTask ); routing::RoutingFactoryKeeper& rfk = routing::routing_factory_keeper_w(sc); // Initialize the routing algorithms // Distributed Tree { routing::tree::TreeRoutingProcessorFactory::register_factory(sc); rfk.add( new routing::tree::TreeRoutingFactory ); // Tree knowledge injection sc.simulation_task_keeper_w().add( new routing::tree::TreeCreationTask ); } // Distributed Flood { routing::flood::FloodRoutingProcessorFactory::register_factory(sc); rfk.add( new routing::flood::FloodRoutingFactory ); } // Distributed GeoRouting { routing::geo::GeoRoutingProcessorFactory::register_factory(sc); rfk.add( new routing::geo::GeoRoutingFactory ); // Pre creates the neighborhood sc.simulation_task_keeper_w().add( new routing::geo::GeoNeighborhoodCreationTask ); } // Centralized FloodRouting { rfk.add( new routing::flood::CentralizedFloodRoutingFactory ); } // Centralized TreeRouting { rfk.add( new routing::tree::CentralizedTreeRoutingFactory ); sc.simulation_task_keeper_w().add( new routing::tree::CentralizedTreeCreationTask ); } // Simplified GeoRouting { routing::geo::SimplifiedGeoRoutingProcessorFactory::register_factory(sc); rfk.add( new routing::geo::SimplifiedGeoRoutingFactory ); //// Pre creates the neighborhood //sc.simulation_task_keeper_w().add( new routing::geo::SimplifiedGeoNeighborhoodCreationTask ); } }