- client: code cleanup: most message code into a class

svn path=/trunk/boinc/; revision=20029
This commit is contained in:
David Anderson 2009-12-23 19:52:34 +00:00
parent 8431e2be69
commit 13fe6a468c
7 changed files with 83 additions and 60 deletions

View File

@ -10688,3 +10688,12 @@ David 23 Dec 2009
client/, clientgui/, lib/
various files
David 23 Dec 2009
- client: code cleanup: most message code into a class
client/
client_msgs.cpp,h
gui_rpc_server_ops.cpp
main.cpp,h
cs_scheduler.cpp

View File

@ -29,15 +29,13 @@ using std::deque;
#include "log_flags.h"
#include "str_replace.h"
#include "client_types.h"
#include "main.h"
#include "client_msgs.h"
#define MAX_SAVED_MESSAGES 2000
// a cache of MAX_SAVED_MESSAGES most recent messages,
// stored in newest-first order
//
deque<MESSAGE_DESC*> message_descs;
MESSAGE_DESCS message_descs;
// Takes a printf style formatted string, inserts the proper values,
// and passes it to show_message
@ -59,7 +57,7 @@ void msg_printf(PROJECT *p, int priority, const char *fmt, ...) {
// add message to cache, and delete old messages if cache too big
//
void record_message(PROJECT* p, int priority, int now, char* message) {
void MESSAGE_DESCS::insert(PROJECT* p, int priority, int now, char* message) {
MESSAGE_DESC* mdp = new MESSAGE_DESC;
static int seqno = 1;
strcpy(mdp->project_name, "");
@ -72,11 +70,56 @@ void record_message(PROJECT* p, int priority, int now, char* message) {
mdp->timestamp = now;
mdp->seqno = seqno++;
mdp->message = message;
while (message_descs.size() > MAX_SAVED_MESSAGES) {
delete message_descs.back();
message_descs.pop_back();
while (msgs.size() > MAX_SAVED_MESSAGES) {
delete msgs.back();
msgs.pop_back();
}
message_descs.push_front(mdp);
msgs.push_front(mdp);
}
void MESSAGE_DESCS::write(int seqno, MIOFILE& fout) {
int i, j;
unsigned int k;
MESSAGE_DESC* mdp;
// messages are stored in descreasing seqno,
// i.e. newer ones are at the head of the vector.
// compute j = index of first message to return
//
j = (int)msgs.size()-1;
for (k=0; k<msgs.size(); k++) {
mdp = msgs[k];
if (mdp->seqno <= seqno) {
j = k-1;
break;
}
}
fout.printf("<msgs>\n");
for (i=j; i>=0; i--) {
mdp = msgs[i];
fout.printf(
"<msg>\n"
" <project>%s</project>\n"
" <pri>%d</pri>\n"
" <seqno>%d</seqno>\n"
" <body>\n%s\n</body>\n"
" <time>%d</time>\n",
mdp->project_name,
mdp->priority,
mdp->seqno,
mdp->message.c_str(),
mdp->timestamp
);
fout.printf("</msg>\n");
}
fout.printf("</msgs>\n");
}
int MESSAGE_DESCS::highest_seqno() {
if (msgs.size()) return msgs[0]->seqno;
return 0;
}
const char *BOINC_RCSID_9572274f4f = "$Id$";

View File

@ -40,9 +40,19 @@ struct MESSAGE_DESC {
std::string message;
};
extern std::deque<MESSAGE_DESC*> message_descs;
extern void record_message(struct PROJECT *p, int priority, int now, char* msg);
extern void show_message(struct PROJECT *p, char* message, int priority);
#define MAX_SAVED_MESSAGES 2000
// a cache of MAX_SAVED_MESSAGES most recent messages,
// stored in newest-first order
//
struct MESSAGE_DESCS {
std::deque<MESSAGE_DESC*> msgs;
void insert(struct PROJECT *p, int priority, int now, char* msg);
void write(int seqno, class MIOFILE&);
int highest_seqno();
};
extern MESSAGE_DESCS message_descs;
// the __attribute((format...)) tags are GCC extensions that let the compiler
// do like-checking on printf-like arguments

View File

@ -535,7 +535,7 @@ int CLIENT_STATE::handle_scheduler_reply(PROJECT* project, char* scheduler_url)
USER_MESSAGE& um = sr.messages[i];
sprintf(buf, "Message from server: %s", um.message.c_str());
int prio = (!strcmp(um.priority.c_str(), "high"))?MSG_USER_ALERT:MSG_INFO;
show_message(project, buf, prio);
msg_printf(project, prio, buf);
}
if (log_flags.sched_op_debug && sr.request_delay) {

View File

@ -392,52 +392,14 @@ static void handle_get_proxy_settings(char* , MIOFILE& fout) {
// return only msgs with seqno > n; if absent or zero, return all
//
static void handle_get_messages(char* buf, MIOFILE& fout) {
int seqno=0, i, j;
unsigned int k;
MESSAGE_DESC* mdp;
int seqno=0;
parse_int(buf, "<seqno>", seqno);
// messages are stored in descreasing seqno,
// i.e. newer ones are at the head of the vector.
// compute j = index of first message to return
//
j = (int)message_descs.size()-1;
for (k=0; k<message_descs.size(); k++) {
mdp = message_descs[k];
if (mdp->seqno <= seqno) {
j = k-1;
break;
}
}
fout.printf("<msgs>\n");
for (i=j; i>=0; i--) {
mdp = message_descs[i];
fout.printf(
"<msg>\n"
" <project>%s</project>\n"
" <pri>%d</pri>\n"
" <seqno>%d</seqno>\n"
" <body>\n%s\n</body>\n"
" <time>%d</time>\n",
mdp->project_name,
mdp->priority,
mdp->seqno,
mdp->message.c_str(),
mdp->timestamp
);
fout.printf("</msg>\n");
}
fout.printf("</msgs>\n");
message_descs.write(seqno, fout);
}
static void handle_get_message_count(char*, MIOFILE& fout) {
if (message_descs.size() == 0) {
fout.printf("<seqno>0</seqno>\n");
} else {
fout.printf("<seqno>%d</seqno>\n", message_descs[0]->seqno);
}
fout.printf("<seqno>%d</seqno>\n", message_descs.highest_seqno());
}
// <retry_file_transfer>

View File

@ -101,7 +101,7 @@ void show_message(PROJECT *p, char* msg, int priority) {
x = "---";
}
record_message(p, priority, (int)now, message);
message_descs.insert(p, priority, (int)now, message);
printf("%s [%s] %s\n", time_string, x, message);
if (gstate.executing_as_daemon) {

View File

@ -16,11 +16,10 @@
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#ifdef _WIN32
extern void log_message_startup(char* msg);
extern void log_message_error(char* msg);
extern void log_message_error(char* msg, int error_code);
extern int boinc_main_loop();
#endif
extern void show_message(struct PROJECT *p, char* message, int priority);