From dfb84db067a285917c687fcd55f0886d9aaa521e Mon Sep 17 00:00:00 2001 From: Eric Heien Date: Thu, 13 Mar 2003 21:20:33 +0000 Subject: [PATCH] unix message queues svn path=/trunk/boinc/; revision=1058 --- checkin_notes | 10 ++++-- lib/Makefile.in | 5 ++- lib/msg_queue.C | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/msg_queue.h | 6 ++++ lib/msg_test.C | 42 +++++++++++++++++++++++ todo | 2 -- 6 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 lib/msg_queue.C create mode 100644 lib/msg_queue.h create mode 100644 lib/msg_test.C diff --git a/checkin_notes b/checkin_notes index 6f32b3c811..d25045bbae 100755 --- a/checkin_notes +++ b/checkin_notes @@ -3788,11 +3788,17 @@ Eric March 11, 2003 Eric March 12, 2003 - Quit requests are now sent via the SIGQUIT signal (on UNIX) - or an inherited EventHandle (on Windows), rather than via - an XML file + or a named Event (on Windows), rather than via an XML file api/ boinc_api.C,h client/ app.C,h +Eric March 13, 2003 + - Added System V message queue functions to library + + lib/ + msg_queue.C,h + msg_test.C + diff --git a/lib/Makefile.in b/lib/Makefile.in index d5e08fc647..6a14be260e 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -13,7 +13,7 @@ CC = @CC@ $(CFLAGS) -I ../RSAEuro/source CLIBS = @LIBS@ -PROGS = md5_test shmem_test crypt_prog +PROGS = md5_test shmem_test msg_queue_test crypt_prog # PROGS = md5_test shmem_test synch_test crypt_prog OBJS = \ @@ -52,6 +52,9 @@ md5_test: md5_test.o $(MD5_OBJS) shmem_test: shmem_test.o shmem.o $(CC) shmem_test.o shmem.o $(CLIBS) -o shmem_test +msg_queue_test: msg_test.o msg_queue.o + $(CC) msg_test.o msg_queue.o $(CLIBS) -o msg_test + synch_test: synch_test.o synch.o $(CC) synch_test.o synch.o $(CLIBS) -o synch_test diff --git a/lib/msg_queue.C b/lib/msg_queue.C new file mode 100644 index 0000000000..06047b8388 --- /dev/null +++ b/lib/msg_queue.C @@ -0,0 +1,89 @@ +// The contents of this file are subject to the Mozilla Public License +// Version 1.0 (the "License"); you may not use this file except in +// compliance with the License. You may obtain a copy of the License at +// http://www.mozilla.org/MPL/ +// +// Software distributed under the License is distributed on an "AS IS" +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +// License for the specific language governing rights and limitations +// under the License. +// +// The Original Code is the Berkeley Open Infrastructure for Network Computing. +// +// The Initial Developer of the Original Code is the SETI@home project. +// Portions created by the SETI@home project are Copyright (C) 2002 +// University of California at Berkeley. All Rights Reserved. +// +// Contributor(s): +// + +// interfaces for accessing message queues + +#include +#include + +#include "msg_queue.h" + +int create_message_queue(key_t key) { + int mq_id; + + mq_id = msgget(key, IPC_CREAT | IPC_EXCL | 0666); + if (mq_id < 0) { + perror("create_message_queue: msgget"); + return -1; + } + return 0; +} + +int receive_message(key_t key, void *msg, size_t msg_size, bool wait) { + int mq_id, retval; + + mq_id = msgget(key, 0666); + if (mq_id < 0) { + perror("receive_message: msgget"); + return -1; + } + + retval = msgrcv(mq_id, msg, msg_size, 0, (wait?0:IPC_NOWAIT)); + if (retval < 0) { + perror("receive_message: msgrcv"); + return -1; + } + + return 0; +} + +int send_message(key_t key, void *msg, size_t msg_size, bool wait) { + int mq_id, retval; + + mq_id = msgget(key, 0666); + if (mq_id < 0) { + perror("send_message: msgget"); + return -1; + } + + retval = msgsnd(mq_id, msg, msg_size, (wait?0:IPC_NOWAIT)); + if (retval < 0) { + perror("send_message: msgsnd"); + return -1; + } + + return 0; +} + +int destroy_message_queue(key_t key) { + int mq_id, retval; + + mq_id = msgget(key, 0666); + if (mq_id < 0) { + perror("delete_message_queue: msgget"); + return -1; + } + retval = msgctl(mq_id, IPC_RMID, NULL); + if (retval) { + perror("delete_message_queue: msgctl"); + return -1; + } + return 0; +} + diff --git a/lib/msg_queue.h b/lib/msg_queue.h new file mode 100644 index 0000000000..bfd206ee86 --- /dev/null +++ b/lib/msg_queue.h @@ -0,0 +1,6 @@ +#include + +extern int create_message_queue(key_t); +extern int receive_message(key_t,void*,size_t,bool); +extern int send_message(key_t,void*,size_t,bool); +extern int destroy_message_queue(key_t); diff --git a/lib/msg_test.C b/lib/msg_test.C new file mode 100644 index 0000000000..97cae08402 --- /dev/null +++ b/lib/msg_test.C @@ -0,0 +1,42 @@ +// test program for message queue functions + +// -d destroy +// -c create +// -s [msg] send message [msg] +// -r receive message +// -rw wait for message + +#define KEY 0xb01fcafe + +#include +#include + +#include "msg_queue.h" + +struct my_msg { + long msg_type; + char msg_text[256]; +}; + +int main(int argc, char** argv) { + my_msg the_msg; + int retval; + + if (!strcmp(argv[1], "-d")) { + destroy_message_queue(KEY); + } else if (!strcmp(argv[1], "-c")) { + create_message_queue(KEY); + } else if (!strcmp(argv[1], "-rw")) { + retval = receive_message(KEY, &the_msg, sizeof(my_msg),true); + printf("Received message: %s\n", the_msg.msg_text); + } else if (!strcmp(argv[1], "-r")) { + retval = receive_message(KEY, &the_msg, sizeof(my_msg),false); + if (!retval) printf("Received message: %s\n", the_msg.msg_text); + } else if (!strcmp(argv[1], "-s")) { + the_msg.msg_type = 1; + strcpy(the_msg.msg_text, argv[2]); + send_message(KEY, &the_msg, sizeof(my_msg),true); + } + + return 0; +} diff --git a/todo b/todo index 0fbd9b9169..e4861f9af5 100755 --- a/todo +++ b/todo @@ -7,10 +7,8 @@ BUGS (arranged from high to low priority) should there be a user control for this? - Client treats URL "maggie/ap/" different than URL "maggie/ap", though this isn't really a bug it might be good to fix anyway -- need a way to refresh prefs from client - global battery/user active prefs are always true in the client - Client should display "Upload failed" and "Download failed" when failure occurs -- Download speed is not as fast as it should be - Result status should say "downloading files", "uploading files", etc. -----------------------