Implemented UUID passing in externAgent and C. Enabled externAgents to print debug...
authorDaniel Axtens <[email protected]>
Tue, 14 Sep 2010 13:24:54 +0000 (21:24 +0800)
committerDaniel Axtens <[email protected]>
Tue, 14 Sep 2010 13:24:54 +0000 (21:24 +0800)
src/link/C/agents/c_wash.c
src/link/C/c_link.c
src/link/C/c_link.h
src/link/externAgent.py

index 9ca4a41..a5ab716 100644 (file)
@@ -35,7 +35,7 @@
 /* data for each instance of my agent */
 typedef struct {
        /* The name of the n-th foe who has beaten us */
-       char (*defeatingFoes)[MAXFOENAMELEN];
+       char (*defeatingFoes)[MAXAGENTNAMELEN];
 
        /* The length of the array, and how far we are along it */
        size_t foesLen;
@@ -50,7 +50,7 @@ int haveLostTo( wash_data * me, char * foeName ) {
     
     /* check every foe we know to have defeated us */
     for (foe=0; foe<me->foesCount; foe++) {
-        if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN) == 0) {
+        if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN) == 0) {
             //debugmsg( "%d\thaveLostTo( %s ) -> Yes\n", me, foeName );
             return 1;
         }
@@ -64,7 +64,7 @@ int haveLostTo( wash_data * me, char * foeName ) {
 void * Initialise( char * myName ) {
        wash_data * me = malloc( sizeof( wash_data ) );
        
-       me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( MAXFOENAMELEN*sizeof(char) ) );
+       me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( MAXAGENTNAMELEN*sizeof(char) ) );
        me->foesLen = NUMBEROFAGENTSGUESS;
     me->foesCount = 0;
        
@@ -149,7 +149,7 @@ void Results( void * this, char * foeName, int isInstigatedByYou,
     
     /* if we've already lost the foe, don't store again */
     for (foe=0; foe<me->foesCount; foe++) {
-        if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN ) == 0) {
+        if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN ) == 0) {
             /* we've found it! */
             return;
         }
@@ -159,11 +159,11 @@ void Results( void * this, char * foeName, int isInstigatedByYou,
     if (me->foesCount==me->foesLen) {
         /* double the array size. This should error check, but doesn't */
         me->defeatingFoes = realloc( me->defeatingFoes, 
-                                            me->foesLen*2*sizeof( MAXFOENAMELEN*sizeof(char) ) );
+                                            me->foesLen*2*sizeof( MAXAGENTNAMELEN*sizeof(char) ) );
         me->foesLen *= 2;
     }
     
-    strncpy( me->defeatingFoes[me->foesCount], foeName, MAXFOENAMELEN );
+    strncpy( me->defeatingFoes[me->foesCount], foeName, MAXAGENTNAMELEN );
     me->foesCount++;
     
     return;
index 6aa5709..524697d 100755 (executable)
@@ -57,21 +57,17 @@ int main( int argc, char * argv[] ) {
        srand( time( NULL ) );\r
        \r
        char command[MAXCOMMANDLEN];\r
-       char foeName[MAXFOENAMELEN];\r
+       char foeName[MAXAGENTNAMELEN];\r
        char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];\r
        char didYouInstigate[MAXBOOLLEN];\r
        char winner[MAXRESULTLEN];\r
-       char uuid[UUIDLEN];  \r
+       char uuid[MAXAGENTNAMELEN];  \r
        int pointChange;\r
        void *thisInstance = NULL;\r
 \r
        ATTACKTYPE attack;\r
        ITEMTYPE defence;\r
        \r
-       /* generate a random id for this bot. Hopefully it's unique\r
-          I can't use the UUID, because python doesn't pass it to me! */\r
-       me = rand();\r
-       \r
        scanf( "%s", command );\r
        \r
        while (strcasecmp("BYE",command) != 0) {\r
@@ -79,23 +75,20 @@ int main( int argc, char * argv[] ) {
                if (strcasecmp("HI", command) == 0) {\r
                        scanf( "%s", uuid );\r
                        thisInstance = Initialise( uuid );\r
-               }\r
-               else if (strcasecmp("ATTACK", command) == 0) {\r
+                       \r
+               else if (strcasecmp("ATTACK", command) == 0) {\r
                        scanf( "%s", foeName );\r
-                       if( !thisInstance )     break;\r
                        attack = Attack( thisInstance, foeName );\r
                        printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);\r
                \r
                } else if (strcasecmp("DEFEND", command) == 0) {\r
                        scanf( "%s %s", foeName, bluffItem );\r
-                       if( !thisInstance )     break;\r
                        defence = Defend(thisInstance, foeName, stringToItem(bluffItem));\r
                        printf("DEFENDING %s\n", ITEMNAMES[defence]);\r
                \r
                } else if (strcasecmp("RESULTS", command) == 0) {\r
                        /* (foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta) */\r
                        scanf( "%s %s %s %s %s %s %d", foeName, didYouInstigate, winner, attItem, defItem, bluffItem, &pointChange );\r
-                       if( !thisInstance )     break;\r
                        Results(thisInstance, foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner),\r
                                        stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange);\r
                        printf("OK\n");\r
@@ -108,7 +101,7 @@ int main( int argc, char * argv[] ) {
                scanf( "%s", command );\r
        }\r
        \r
-       if( !thisInstance )\r
+       if( thisInstance )\r
                Cleanup(thisInstance);\r
        \r
        return 0;\r
index abf8f1e..0e34644 100755 (executable)
 #include <stdio.h>\r
 \r
 #define MAXCOMMANDLEN  15\r
-#define MAXFOENAMELEN  50\r
+#define MAXAGENTNAMELEN        42 /* 40 digits, 'L', and NULL */\r
 #define MAXITEMLEN             10\r
 #define MAXRESULTLEN    10\r
 #define MAXBOOLLEN             6\r
-#define UUIDLEN        42      // 40 digits, 'L' and NULL\r
 \r
 /********** Type definitions **********/\r
 \r
@@ -37,15 +36,14 @@ typedef struct {
 \r
 /* prints a debug message. Same arguments as printf().\r
    (you can't use printf because it is used to talk between\r
-    the agent and supervisor) \r
+    the agent and supervisor)\r
+       \r
+       Hint: store the name passed to you in Initalise and use it to uniquely\r
+       identify the agent sending the message.\r
  */\r
 \r
 #define debugmsg(x...) fprintf(stderr, x)\r
 \r
-/* A (hopefully) unique identifier for this particular instance of your agent,\r
-   to help with debugging */\r
-int me;\r
-\r
 \r
 /* Returns a random item */\r
 \r
index d3e3af1..0680f33 100644 (file)
@@ -16,11 +16,13 @@ class externAgent (BaseAgent):
         BaseAgent.__init__(self)
         try:
             self.process = subprocess.Popen(externName, stdin=subprocess.PIPE, 
-                                            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                                            stdout=subprocess.PIPE,
                                             universal_newlines=True)
         except Exception, e:
             print ("Error spawning \"%s\": " % externName), e
-            
+           
+        self.process.stdin.write ( ' '.join( ["HI", repr(self.GetID()), "\r\n"] ) )
+
     def stringToItem( self, str ):
         if str == "Rock":
             return Rock
@@ -73,7 +75,7 @@ class externAgent (BaseAgent):
             return attack, bluff
         except:
             #agent is insane
-            print "Agent is insane:", self
+            print "Agent is insane:", self, self.GetID()
             pass
         
     def Defend (self, foe, bluff ):
@@ -85,7 +87,7 @@ class externAgent (BaseAgent):
             return defence
         except:
             #agent is insane
-            print "Agent is insane:", self
+            print "Agent is insane:", self, self.GetID()
             pass
 
     def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta):

UCC git Repository :: git.ucc.asn.au