Browse Source

little script to clear message queues while developing

max 8 years ago
parent
commit
aaaf955624
3 changed files with 26 additions and 22 deletions
  1. 13 0
      helper_scripts/clear_message_queues.sh
  2. 10 17
      src/myco-agent.c
  3. 3 5
      test/mycoagent.c

+ 13 - 0
helper_scripts/clear_message_queues.sh

@@ -0,0 +1,13 @@
+#!/bin/bash
+
+ipcs -q
+MESSAGE_QUEUES="$(ipcs -q | grep $USER)"
+COUNT=-2
+for line in $MESSAGE_QUEUES; do 
+    let "COUNT += 1"
+    let "MODULO = COUNT % 6"
+    if [ $MODULO = 0 ]; then
+        ipcrm -q $line; 
+    fi
+done;
+ipcs -q

+ 10 - 17
src/myco-agent.c

@@ -15,6 +15,8 @@
 
 #include "myco-agent.h"
 
+#define DEBUG 1
+
 int myco_agent_register(const char *agent_name) {
     int agent_message_queue_id;
     int global_message_queue_id;
@@ -28,9 +30,8 @@ int myco_agent_register(const char *agent_name) {
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 
-    if (strncmp(msg.message, "SUCCESS:", 8) != 0) {
+    if (DEBUG) {
         printf("%s", msg.message);
-        return -1;
     }
 
     return agent_message_queue_id;
@@ -44,15 +45,11 @@ int myco_agent_unregister (const char *agent_name, int agent_message_queue_id) {
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 
-    if (strncmp(msg.message, "SUCCESS:", 8) != 0) {
+    if (DEBUG) {
         printf("%s\n", msg.message);
-        return -1;
     }
 
-    if (myco_remove_message_queue(agent_message_queue_id) == -1) {
-        printf("FATAL ERROR: could not remove message queue %d\n", agent_message_queue_id);
-        return -1;
-    }
+    myco_remove_message_queue(agent_message_queue_id);
 
     return 0;
 }
@@ -67,9 +64,8 @@ int myco_agent_register_resource (int agent_message_queue_id, const char *agent_
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 
-    if (strncmp(msg.message, "SUCCESS:", 8) != 0) {
+    if(DEBUG) {
         printf("%s", msg.message);
-        return -1;
     }
 
     return 0;
@@ -83,9 +79,8 @@ int myco_agent_unregister_resource (int agent_message_queue_id, const char *reso
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 
-    if (strncmp(msg.message, "SUCCESS:", 8) != 0) {
+    if(DEBUG) {
         printf("%s", msg.message);
-        return -1;
     }
 
     return 0;
@@ -100,15 +95,14 @@ int myco_agent_request_resource(int agent_message_queue_id, char *resource_name)
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 
-    if (strncmp(msg.message, "SUCCESS:", 8) != 0) {
+    if(DEBUG) {
         printf("%s", msg.message);
-        return -1;
     }
 
     resource_pointer = malloc(msg.resource_size);
 
     if (myco_read_transactional(msg.sender_pid, msg.resource_pointer, msg.resource_size, (void *)resource_pointer, msg.resource_size) == -1) {
-        printf("ERROR: %s\n", strerror(errno));
+        fprintf(stderr, "%s\n", strerror(errno));
         return -1;
     }
 
@@ -126,9 +120,8 @@ int myco_agent_request_resource_list(int agent_message_queue_id) {
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 
-    if (strncmp(msg.message, "SUCCESS:", 8) != 0) {
+    if(DEBUG) {
         printf("%s", msg.message);
-        return -1;
     }
 
     resource_pointer = malloc(msg.resource_size);

+ 3 - 5
test/mycoagent.c

@@ -7,16 +7,14 @@
 int main() {
     int message_queue_id;
 
-    if ((message_queue_id = myco_agent_register("MYAGENT")) == -1) {
-        return EXIT_FAILURE;
-    }
-
-    myco_agent_register_resource(message_queue_id, "MYAGENT", "MYTRANSACTIONALRESOURCE", RESOURCE_TRANSACTIONAL);
+    message_queue_id = myco_agent_register("MYAGENT");
     myco_agent_register_resource(message_queue_id, "MYAGENT", "MYTRANSACTIONALRESOURCE", RESOURCE_TRANSACTIONAL);
+    myco_agent_register_resource(message_queue_id, "MYAGENT", "MYTRANSACTIONALRESOURCE2", RESOURCE_TRANSACTIONAL);
     myco_agent_register_resource(message_queue_id, "MYAGENT2", "MYTRANSACTIONALRESOURCE2", RESOURCE_TRANSACTIONAL);
     myco_agent_request_resource_list(message_queue_id);
     myco_agent_unregister("MYAGENT", message_queue_id);
     myco_agent_unregister_resource(message_queue_id, "MYTRANSACTIONALRESOURCE");
+    myco_agent_unregister_resource(message_queue_id, "MYTRANSACTIONALRESOURCE2");
     myco_agent_unregister("MYAGENT", message_queue_id);