#include "Inputs.hpp" #include #include #include Inputs::Inputs(std::string node_name){ // Here we do all the boring stuff FILE* input_file = fopen(INPUTS_FILE, "rb"); char input_file_buffer[JSON_BUFFER_SIZE]; rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); d.ParseStream(is); fclose(input_file); // Init all variables is_sender=d["nodes"][node_name.c_str()]["is_sender"].GetBool(); use_hint=d["nodes"][node_name.c_str()]["use_hint"].GetBool(); data_size=d["nodes"][node_name.c_str()]["data_size"].GetInt(); extended=d["extended"].GetBool(); seed=d["seed"].GetInt(); hint_size=d["hint_size"].GetInt(); n_nodes=d["nodes"].MemberCount(); latency=d["latency"].GetDouble(); unschedule_on_rcv=d["unschedule_on_rcv"].GetBool(); shutdown_on_rcv=d["shutdown_on_rcv"].GetBool(); farhint=d["farhint"].GetBool(); hintdist=d["hintdist"].GetDouble(); // Instantiate wake_ts for(auto& v:d["nodes"][node_name.c_str()]["wake_ts"].GetArray()){ wake_ts.push_back(v.GetDouble()); wake_ts_backup.push_back(v.GetDouble()); } // Instantiate wake_duration for(auto& v:d["nodes"][node_name.c_str()]["wake_duration"].GetArray()){ wake_duration.push_back(v.GetDouble()); wake_duration_backup.push_back(v.GetDouble()); } // Identity check if(wake_ts.size()<1){ std::cerr << "Invalid node configuration: wake_ts.size() == 0" <=next_ts){ // Create variable for convenience double start=cur_ts; double end=std::max(cur_ts+cur_duration,next_ts+next_duration); wake_duration[i]=end-start; // Now remove next event wake_ts.erase(wake_ts.begin() + i + 1); wake_duration.erase(wake_duration.begin() + i +1); // This is not optimal. Yet it is simple :D MergeEvents(); } } } double Inputs::GetNextTS(){ // Ensure that the caller is smart if(wake_duration.size()<2){ std::cerr << "You are trying to access to the next timestamp but it does not exists" <=(clock+hintdist)) return(wake_ts[i]); } return(wake_ts.back()); } return(GetNextTS()); } double Inputs::GetHintDuration(double clock){ if(farhint){ for(int i=0;i=(clock+hintdist)) return(wake_duration[i]); } return(wake_duration.back()); } return GetNextDuration(); } void Inputs::GotoNextEvent(){ wake_ts.erase(wake_ts.begin()); wake_duration.erase(wake_duration.begin()); } void Inputs::ResetEvents(double clock){ wake_ts=wake_ts_backup; wake_duration=wake_duration_backup; // Restore current event while(HasNext() && (GetNextTS()+GetNextDuration()) < clock){ GotoNextEvent(); } } void Inputs::DumpEvents(){ std::cout << "Timestamps: "; for(auto a:wake_ts){ std::cout << std::setw(5) << a << " "; } std::cout << std::endl; std::cout << "Wake Durations: "; for(auto a:wake_duration){ std::cout << std::setw(5) << a << " "; } std::cout << std::endl; } void Inputs::AddEvent(double ts, double duration){ // First handle timestamp int pos=0; for(auto it = std::begin(wake_ts); it != std::end(wake_ts); ++it) { if(*it>=ts){ wake_ts.insert(it,ts); break; } pos++; } // Ensure that ts and duration should not go to the end if(pos==wake_ts.size()){ wake_ts.push_back(ts); wake_duration.push_back(duration); } else { // Handle durations here int pos2=0; for(auto it = std::begin(wake_duration); it != std::end(wake_duration); ++it) { if(pos==pos2){ wake_duration.insert(it,duration); break; } else if (it+1==std::end(wake_duration)) { wake_duration.push_back(duration); } pos2++; } } // Don't forget MergeEvents(); } void Inputs::GeneratePlatform(std::string p){ // The boring stuff FILE* input_file = fopen(INPUTS_FILE, "rb"); char input_file_buffer[JSON_BUFFER_SIZE]; rapidjson::FileReadStream is(input_file, input_file_buffer, sizeof(input_file_buffer)); rapidjson::Document d; d.ParseStream(is); fclose(input_file); // Write platform file std::ofstream pf; pf.open (p); pf << "\n"; pf << "\n"; pf << "\n \n"; pf << " \n"; for (Value::ConstMemberIterator itr = d["nodes"].MemberBegin(); itr != d["nodes"].MemberEnd(); ++itr) { std::string name=itr->name.GetString(); double power_off=d["nodes"][itr->name.GetString()]["power_off"].GetDouble(); double power_on=d["nodes"][itr->name.GetString()]["power_on"].GetDouble(); double power_rx=power_on+d["nodes"][itr->name.GetString()]["power_rx"].GetDouble(); double power_tx=power_on+d["nodes"][itr->name.GetString()]["power_tx"].GetDouble(); // Create node pf << " \n"; pf << " \n"; pf << " \n \n"; } for (Value::ConstMemberIterator src = d["nodes"].MemberBegin(); src != d["nodes"].MemberEnd(); ++src) { for (Value::ConstMemberIterator dst = d["nodes"].MemberBegin(); dst != d["nodes"].MemberEnd(); ++dst) { if(src->name.GetString() != dst->name.GetString()) pf << " name.GetString()<<"\" dst=\""<name.GetString()<<"\" symmetrical=\"no\">\n"; } } pf << " \n\n"; pf.close(); }