Commit 7bf76ff2 authored by Millian Poquet's avatar Millian Poquet
Browse files

[code] jsonreader: reject_job

parent 18ac8d00
......@@ -5,6 +5,10 @@
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include "context.hpp"
#include "jobs.hpp"
#include "network.hpp"
using namespace rapidjson;
using namespace std;
......@@ -398,13 +402,21 @@ void JsonProtocolReader::parse_and_apply_event(const Value & event_object,
void JsonProtocolReader::handle_query_request(int event_number, double timestamp, const Value &data_object)
{
/* {
"timestamp": 10.0,
"type": "QUERY_REQUEST",
"data": {
"requests": {"consumed_energy": {}}
}
} */
xbt_assert(data_object.IsObject(), "Invalid JSON message: the 'data' value of event %d (QUERY_REQUEST) should be an object", event_number);
xbt_assert(data_object.MemberCount() > 0, "Invalid JSON message: the 'data' value of event %d (QUERY_REQUEST) cannot be empty (size=%d)", event_number, (int)data_object.MemberCount());
for (auto it = data_object.MemberBegin(); it != data_object.MemberEnd(); ++it)
{
const auto & key_value = it->name;
const auto & value_object = it->value;
const Value & key_value = it->name;
const Value & value_object = it->value;
xbt_assert(key_value.IsString(), "Invalid JSON message: a key within the 'data' object of event %d (QUERY_REQUEST) is not a string", event_number);
string key = key_value.GetString();
......@@ -420,6 +432,40 @@ void JsonProtocolReader::handle_query_request(int event_number, double timestamp
}
}
void JsonProtocolReader::handle_reject_job(int event_number, double timestamp, const Value &data_object)
{
/* {
"timestamp": 10.0,
"type": "REJECT_JOB",
"data": { "job_id": "w12!45" }
} */
xbt_assert(data_object.IsObject(), "Invalid JSON message: the 'data' value of event %d (REJECT_JOB) should be an object", event_number);
xbt_assert(data_object.MemberCount() == 1, "Invalid JSON message: the 'data' value of event %d (REJECT_JOB) should be of size 1 (size=%d)", event_number, (int)data_object.MemberCount());
xbt_assert(data_object.HasMember("job_id"), "Invalid JSON message: the 'data' value of event %d (REJECT_JOB) should contain a 'job_id' key.", event_number);
const Value & job_id_value = data_object["job_id"];
xbt_assert(job_id_value.IsString(), "Invalid JSON message: the 'job_id' value in the 'data' value of event %d (REJECT_JOB) should be a string.", event_number);
string job_id = job_id_value.GetString();
JobRejectedMessage * message = new JobRejectedMessage;
if (!identify_job_from_string(context, job_id, message->job_id))
{
xbt_assert(false, "Invalid job rejection received: The job identifier '%s' is not valid. "
"Job identifiers must be of the form [WORKLOAD_NAME!]JOB_ID. "
"If WORKLOAD_NAME! is omitted, WORKLOAD_NAME='static' is used. "
"Furthermore, the corresponding job must exist.", job_id.c_str());
}
Job * job = context->workloads.job_at(message->job_id);
xbt_assert(job->state == JobState::JOB_STATE_SUBMITTED,
"Invalid rejection received: job %s cannot be rejected at the present time."
"For being rejected, a job must be submitted and not allocated yet.",
job->id.c_str());
send_message(timestamp, "server", IPMessageType::SCHED_REJECTION, (void*) message);
}
void JsonProtocolReader::send_message(double when,
const string &destination_mailbox,
IPMessageType type,
......
......@@ -10,6 +10,8 @@
#include "machine_range.hpp"
#include "ipp.hpp"
struct BatsimContext;
/**
* @brief Does the interface between protocol semantics and message representation.
*/
......@@ -463,10 +465,18 @@ public:
*/
void handle_query_request(int event_number, double timestamp, const rapidjson::Value & data_object);
/**
* @brief Handles a QUERY_REQUEST event
* @param[in] timestamp The event timestamp
* @param[in] data_object The data associated with the event (JSON object)
*/
void handle_reject_job(int event_number, double timestamp, const rapidjson::Value & data_object);
private:
void send_message(double when, const std::string & destination_mailbox, IPMessageType type, void * data = nullptr) const;
private:
std::map<std::string, std::function<void(JsonProtocolReader*, int, double, const rapidjson::Value&)>> _type_to_handler_map;
std::vector<std::string> accepted_requests = {"consumed_energy"};
BatsimContext * context = nullptr; // TODO
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment