Skip to content

Commit

Permalink
Refactor Msg class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Egesdahl authored and deriamis committed Oct 9, 2022
1 parent 8fcdd73 commit c0f9d53
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 234 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ tests/testargs.log
tests/testargs.trs
tests/test-setup.sh
tests/listing.txt
unittests/testargs
unittests/testargs.trs
# others
compile_commands.json
.vs*
4 changes: 2 additions & 2 deletions client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ int main(int argc, char **argv)

string native;

if (umsg && umsg->type == M_NATIVE_ENV) {
if (umsg && *umsg == Msg::NATIVE_ENV) {
native = static_cast<UseNativeEnvMsg*>(umsg)->nativeVersion;
}

Expand Down Expand Up @@ -600,7 +600,7 @@ int main(int argc, char **argv)

/* If we can't talk to the daemon anymore we need to fall back
to lock file locking. */
if (!startme || startme->type != M_JOB_LOCAL_BEGIN) {
if (!startme || *startme != Msg::JOB_LOCAL_BEGIN) {
delete startme;
delete local_daemon;
return build_local(job, nullptr);
Expand Down
20 changes: 10 additions & 10 deletions client/remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ static UseCSMsg *get_server(MsgChannel *local_daemon)
timeout = 60 * 60;
Msg *umsg = local_daemon->get_msg( timeout );

if (!umsg || umsg->type != M_USE_CS) {
log_warning() << "reply was not expected use_cs " << (umsg ? (char)umsg->type : '0') << endl;
if (!umsg || *umsg != Msg::USE_CS) {
log_warning() << "reply was not expected use_cs " << (umsg ? umsg->to_string() : Msg(Msg::UNKNOWN).to_string()) << endl;
ostringstream unexpected_msg;
unexpected_msg << "Error 1 - expected use_cs reply, but got " << (umsg ? (char)umsg->type : '0') << " instead";
unexpected_msg << "Error 1 - expected use_cs reply, but got " << (umsg ? umsg->to_string() : Msg(Msg::UNKNOWN).to_string()) << " instead";
delete umsg;
throw client_error(1, unexpected_msg.str());
}
Expand All @@ -260,7 +260,7 @@ static UseCSMsg *get_server(MsgChannel *local_daemon)

static void check_for_failure(Msg *msg, MsgChannel *cserver)
{
if (msg && msg->type == M_STATUS_TEXT) {
if (msg && *msg == Msg::STATUS_TEXT) {
log_error() << "Remote status (compiled on " << cserver->name << "): "
<< static_cast<StatusTextMsg*>(msg)->text << endl;
throw client_error(23, "Error 23 - Remote status (compiled on " + cserver->name + ")\n" +
Expand Down Expand Up @@ -361,11 +361,11 @@ static void receive_file(const string& output_file, MsgChannel* cserver)

check_for_failure(msg, cserver);

if (msg->type == M_END) {
if (*msg == Msg::END) {
break;
}

if (msg->type != M_FILE_CHUNK) {
if (*msg != Msg::FILE_CHUNK) {
unlink(tmp_file.c_str());
delete msg;
throw client_error(20, "Error 20 - unexpected message");
Expand Down Expand Up @@ -482,7 +482,7 @@ static int build_remote_int(CompileJob &job, UseCSMsg *usecs, MsgChannel *local_

Msg *verify_msg = cserver->get_msg(60);

if (verify_msg && verify_msg->type == M_VERIFY_ENV_RESULT) {
if (verify_msg && *verify_msg == Msg::VERIFY_ENV_RESULT) {
if (!static_cast<VerifyEnvResultMsg*>(verify_msg)->ok) {
// The remote can't handle the environment at all (e.g. kernel too old),
// mark it as never to be used again for this environment.
Expand Down Expand Up @@ -609,8 +609,8 @@ static int build_remote_int(CompileJob &job, UseCSMsg *usecs, MsgChannel *local_

check_for_failure(msg, cserver);

if (msg->type != M_COMPILE_RESULT) {
log_warning() << "waited for compile result, but got " << (char)msg->type << endl;
if (*msg != Msg::COMPILE_RESULT) {
log_warning() << "waited for compile result, but got " << msg->to_string() << endl;
delete msg;
throw client_error(13, "Error 13 - did not get compile response message");
}
Expand Down Expand Up @@ -670,7 +670,7 @@ static int build_remote_int(CompileJob &job, UseCSMsg *usecs, MsgChannel *local_
// Handle pending status messages, if any.
if(cserver) {
while(Msg* msg = cserver->get_msg(0, true)) {
if(msg->type == M_STATUS_TEXT)
if(*msg == Msg::STATUS_TEXT)
log_error() << "Remote status (compiled on " << cserver->name << "): "
<< static_cast<StatusTextMsg*>(msg)->text << endl;
delete msg;
Expand Down
2 changes: 1 addition & 1 deletion daemon/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pid_t start_install_environment(const std::string &basename, const std::string &
string dirname = basename + "/target=" + target;
Msg *msg = c->get_msg(30);

if (!msg || msg->type != M_FILE_CHUNK) {
if (!msg || *msg != Msg::FILE_CHUNK) {
trace() << "Expected first file chunk\n";
return 0;
}
Expand Down
46 changes: 23 additions & 23 deletions daemon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,13 +1091,13 @@ bool Daemon::handle_file_chunk_env(Client *client, Msg *msg)
the file chunk to the child, but we can't let the child
handle MsgChannel itself due to MsgChannel's stupid
caching layer inbetween, which causes us to lose partial
data after the M_END msg of the env transfer. */
data after the END msg of the env transfer. */

assert(client);
assert(client->status == Client::TOINSTALL || client->status == Client::WAITINSTALL);
assert(client->pipe_to_child >= 0);

if (msg->type == M_FILE_CHUNK) {
if (*msg == Msg::FILE_CHUNK) {
FileChunkMsg *fcmsg = static_cast<FileChunkMsg *>(msg);
ssize_t len = fcmsg->len;
off_t off = 0;
Expand Down Expand Up @@ -1129,7 +1129,7 @@ bool Daemon::handle_file_chunk_env(Client *client, Msg *msg)
return true;
}

if (msg->type == M_END) {
if (*msg == Msg::END) {
trace() << "received end of environment, waiting for child" << endl;
close(client->pipe_to_child);
client->pipe_to_child = -1;
Expand All @@ -1143,7 +1143,7 @@ bool Daemon::handle_file_chunk_env(Client *client, Msg *msg)
}

// unexpected message type
log_error() << "protocol error while receiving environment (" << msg->type << ")" << endl;
log_error() << "protocol error while receiving environment (" << msg->to_string() << ")" << endl;
handle_end(client, 138);
return false;
}
Expand Down Expand Up @@ -1176,7 +1176,7 @@ bool Daemon::handle_env_install_child_done(Client *client)
if( !success )
return finish_transfer_env( client, true ); // cancel
if( client->pipe_to_child >= 0 ) {
// we still haven't received M_END message, wait for that
// we still haven't received END message, wait for that
assert( client->status == Client::TOINSTALL );
return true;
}
Expand Down Expand Up @@ -1896,37 +1896,37 @@ bool Daemon::handle_activity(Client *client)
return ret;
}

switch (msg->type) {
case M_GET_NATIVE_ENV:
switch (*msg) {
case Msg::GET_NATIVE_ENV:
ret = handle_get_native_env(client, dynamic_cast<GetNativeEnvMsg *>(msg));
break;
case M_COMPILE_FILE:
case Msg::COMPILE_FILE:
ret = handle_compile_file(client, msg);
break;
case M_TRANFER_ENV:
case Msg::TRANFER_ENV:
ret = handle_transfer_env(client, dynamic_cast<EnvTransferMsg*>(msg));
break;
case M_GET_CS:
case Msg::GET_CS:
ret = handle_get_cs(client, msg);
break;
case M_END:
case Msg::END:
handle_end(client, 119);
ret = false;
break;
case M_JOB_LOCAL_BEGIN:
case Msg::JOB_LOCAL_BEGIN:
ret = handle_local_job(client, msg);
break;
case M_JOB_DONE:
case Msg::JOB_DONE:
ret = handle_job_done(client, dynamic_cast<JobDoneMsg *>(msg));
break;
case M_VERIFY_ENV:
case Msg::VERIFY_ENV:
ret = handle_verify_env(client, dynamic_cast<VerifyEnvMsg *>(msg));
break;
case M_BLACKLIST_HOST_ENV:
case Msg::BLACKLIST_HOST_ENV:
ret = handle_blacklist_host_env(client, msg);
break;
default:
log_error() << "protocol error " << msg->type << " on client "
log_error() << "protocol error " << msg->to_string() << " on client "
<< client->dump() << endl;
client->channel->send_msg(EndMsg());
handle_end(client, 120);
Expand Down Expand Up @@ -2065,28 +2065,28 @@ void Daemon::answer_client_requests()

ret = 0;

switch (msg->type) {
case M_PING:
switch (*msg) {
case Msg::PING:

if (!IS_PROTOCOL_27(scheduler)) {
ret = !send_scheduler(PingMsg());
}

break;
case M_USE_CS:
case Msg::USE_CS:
ret = scheduler_use_cs(static_cast<UseCSMsg *>(msg));
break;
case M_NO_CS:
case Msg::NO_CS:
ret = scheduler_no_cs(static_cast<NoCSMsg *>(msg));
break;
case M_GET_INTERNALS:
case Msg::GET_INTERNALS:
ret = scheduler_get_internals();
break;
case M_CS_CONF:
case Msg::CS_CONF:
ret = handle_cs_conf(static_cast<ConfCSMsg *>(msg));
break;
default:
log_error() << "unknown scheduler type " << (char)msg->type << endl;
log_error() << "unknown scheduler type " << msg->to_string() << endl;
ret = 1;
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/workit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ int work_it(CompileJob &j, unsigned int job_stat[], MsgChannel *client, CompileR
fcmsg = nullptr;
delete msg;
} else {
if (msg->type == M_END) {
if (*msg == Msg::END) {
input_complete = true;

if (!fcmsg && sock_in[1] != -1) {
Expand All @@ -460,7 +460,7 @@ int work_it(CompileJob &j, unsigned int job_stat[], MsgChannel *client, CompileR
}

delete msg;
} else if (msg->type == M_FILE_CHUNK) {
} else if (*msg == Msg::FILE_CHUNK) {
fcmsg = static_cast<FileChunkMsg*>(msg);
off = 0;

Expand Down
36 changes: 18 additions & 18 deletions scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ static bool handle_line(CompileServer *cs, Msg *_m)
msg = it->get_msg();
}

if (msg && msg->type == M_STATUS_TEXT) {
if (msg && *msg == Msg::STATUS_TEXT) {
if (!cs->send_msg(TextMsg(dynamic_cast<StatusTextMsg *>(msg)->text))) {
return false;
}
Expand Down Expand Up @@ -1757,17 +1757,17 @@ static bool try_login(CompileServer *cs, Msg *m)
{
bool ret = true;

switch (m->type) {
case M_LOGIN:
switch (*m) {
case Msg::LOGIN:
cs->setType(CompileServer::DAEMON);
ret = handle_login(cs, m);
break;
case M_MON_LOGIN:
case Msg::MON_LOGIN:
cs->setType(CompileServer::MONITOR);
ret = handle_mon_login(cs, m);
break;
default:
log_info() << "Invalid first message " << (char)m->type << endl;
log_info() << "Invalid first message " << m->to_string() << endl;
ret = false;
break;
}
Expand Down Expand Up @@ -1898,43 +1898,43 @@ static bool handle_activity(CompileServer *cs)
return try_login(cs, m);
}

switch (m->type) {
case M_JOB_BEGIN:
switch (*m) {
case Msg::JOB_BEGIN:
ret = handle_job_begin(cs, m);
break;
case M_JOB_DONE:
case Msg::JOB_DONE:
ret = handle_job_done(cs, m);
break;
case M_PING:
case Msg::PING:
ret = handle_ping(cs, m);
break;
case M_STATS:
case Msg::STATS:
ret = handle_stats(cs, m);
break;
case M_END:
case Msg::END:
handle_end(cs, m);
ret = false;
break;
case M_JOB_LOCAL_BEGIN:
case Msg::JOB_LOCAL_BEGIN:
ret = handle_local_job(cs, m);
break;
case M_JOB_LOCAL_DONE:
case Msg::JOB_LOCAL_DONE:
ret = handle_local_job_done(cs, m);
break;
case M_LOGIN:
case Msg::LOGIN:
ret = handle_relogin(cs, m);
break;
case M_TEXT:
case Msg::TEXT:
ret = handle_line(cs, m);
break;
case M_GET_CS:
case Msg::GET_CS:
ret = handle_cs_request(cs, m);
break;
case M_BLACKLIST_HOST_ENV:
case Msg::BLACKLIST_HOST_ENV:
ret = handle_blacklist_host_env(cs, m);
break;
default:
log_info() << "Invalid message type arrived " << (char)m->type << endl;
log_info() << "Invalid message type arrived " << m->to_string() << endl;
handle_end(cs, m);
ret = false;
break;
Expand Down
Loading

1 comment on commit c0f9d53

@wonson
Copy link

@wonson wonson commented on c0f9d53 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

icecc/icemon#68

This commit break icemon compiling

Please sign in to comment.