Skip to content

Instantly share code, notes, and snippets.

@pashok3d
Created May 28, 2020 20:20
Show Gist options
  • Select an option

  • Save pashok3d/30eb385f050aa68f1de434bd47831560 to your computer and use it in GitHub Desktop.

Select an option

Save pashok3d/30eb385f050aa68f1de434bd47831560 to your computer and use it in GitHub Desktop.
TO-DO: Database queries based on ActionCode from the message
#include "Reader.h"
Reader::Reader(ConsumerProducerQueue *que, mDataBase * db) : queue(que), db(db){
}
void Reader::process() {
// Wait for a message and then take in out of the queue
queue->consume(buffer);
// Parse message
const Data data = Utility::parseCharArray(buffer);
std::cout << data.message() << std::endl;
/*
switch(data.action())
{
case ActionCode::ADD_AGENT:
// Add new agent to the database
db->AddAgent(data.id())
break;
case ActionCode::JUST_DATA:
// Write dataframe to the database
db->AddValue(data.id(), data.memory_usage())
break;
case ActionCode::REMOVE_AGENT:
// Remove agent from the database
db->RemoveAgent(data.id())
break;
case ActionCode::ADD_USER:
// Remove agent from the database
db->AddUser(???)
break;
case ActionCode::REMOVE_USER:
// Remove agent from the database
db->RemoveUser(???)
break;
case ActionCode::REQUEST_MAIL:
// Request sending mails to subscribers
//
break;
}
*/
// Write dataframe to the file
// dump(data);
}
void Reader::display(Data data) {
std::cout << "[DISPLAY] Peak memory usage for the process: " << data.memory_usage() << '\n';
std::cout << "[DISPLAY] Received message: " << data.message() << '\n';
}
void Reader::dump(Data data) {
if (!(outfile.is_open())) {
std::cout << "Reader: Opening new file..." << std::endl;
// Creating new file
// Use current data and time to name the file
// TODO(): below code should be used to create timeStamp attribute in Data object
auto open_time = std::chrono::system_clock::now();
std::time_t open_time_t = std::chrono::system_clock::to_time_t(open_time);
struct tm *tm = localtime(&open_time_t);
std::string fileName = std::to_string(tm->tm_mday) + "_" + std::to_string(tm->tm_mon + 1) + "_" +
std::to_string(tm->tm_year + 1900) + "_" + std::to_string(tm->tm_hour) + ":" +
std::to_string(tm->tm_min) + ":" + std::to_string(tm->tm_sec);
outfile.open(fileName);
if (!(outfile.is_open()))
Utility::handleError("Failed to open new file.");
}
outfile << data.message() << std::endl;
message_counter++;
check_counter();
}
void Reader::check_counter() {
if (message_counter >= kMaxMessageNum) {
outfile.close();
message_counter = 0;
}
}
@pochka15
Copy link

std::string fileName = std::to_string(tm->tm_mday) + "_" + std::to_string(tm->tm_mon + 1) + "_" + std::to_string(tm->tm_year + 1900) + "_" + std::to_string(tm->tm_hour) + ":" + std::to_string(tm->tm_min) + ":" + std::to_string(tm->tm_sec);
Nie jestem pewny, ale możliwe, że tu zbędnie tworzymy sporo obiektów typu string i można to zoptymalizować.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment