From: John Hodge Date: Thu, 6 Jan 2011 05:47:14 +0000 (+0800) Subject: Changed ENUM_ITEMS to the ENUM_USERS format X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=1e3c14b4fe54108b8bebbc000a93f7eb30284c67;p=tpg%2Fopendispense2.git Changed ENUM_ITEMS to the ENUM_USERS format - Much cleaner client and server code now :) --- diff --git a/proto.txt b/proto.txt index ac2d139..6e7c5cf 100644 --- a/proto.txt +++ b/proto.txt @@ -52,7 +52,10 @@ s 200 Set OK\n or 403 Not Coke\n or 404 Bad User\n --- Get Item list --- c ENUM_ITEMS\n -s 201 Items ...\n +s 201 Items \n +s 202 Item \n + ... +s 200 List End\n --- Get Item Information --- c ITEM_INFO \n s 202 Item \n diff --git a/src/client/main.c b/src/client/main.c index e1de523..acef5b5 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -686,7 +686,9 @@ void PopulateItemList(int Socket) // - Get item list - - // Expected format: 201 Items ... + // Expected format: + // 201 Items + // 202 Item RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response"); itemType = &buf[ matches[2].rm_so ]; buf[ matches[2].rm_eo ] = '\0'; @@ -701,29 +703,21 @@ void PopulateItemList(int Socket) } itemStart = &buf[ matches[3].rm_eo ]; - - gaItems = malloc( count * sizeof(tItem) ); - - for( giNumItems = 0; giNumItems < count && itemStart; giNumItems ++ ) - { - char *next = strchr( ++itemStart, ' ' ); - if( next ) *next = '\0'; - gaItems[giNumItems].Ident = strdup(itemStart); - itemStart = next; - } free(buf); + giNumItems = count; + gaItems = malloc( giNumItems * sizeof(tItem) ); + // Fetch item information for( i = 0; i < giNumItems; i ++ ) { regmatch_t matches[6]; // Get item info - sendf(Socket, "ITEM_INFO %s\n", gaItems[i].Ident); buf = ReadLine(Socket); - responseCode = atoi(buf); + if( responseCode != 202 ) { fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode); exit(-1); @@ -733,11 +727,25 @@ void PopulateItemList(int Socket) buf[ matches[3].rm_eo ] = '\0'; + gaItems[i].Ident = strdup( buf + matches[3].rm_so ); gaItems[i].Price = atoi( buf + matches[4].rm_so ); gaItems[i].Desc = strdup( buf + matches[5].rm_so ); free(buf); } + + // Read end of list + buf = ReadLine(Socket); + responseCode = atoi(buf); + + if( responseCode != 200 ) { + fprintf(stderr, "Unknown response from dispense server %i\n'%s'", + responseCode, buf + ); + exit(-1); + } + + free(buf); } /** diff --git a/src/server/server.c b/src/server/server.c index 9cdee83..85e2025 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -395,13 +395,16 @@ void Server_Cmd_ENUMITEMS(tClient *Client, char *Args) { int i; - sendf(Client->Socket, "201 Items %i", giNumItems); + sendf(Client->Socket, "201 Items %i\n", giNumItems); for( i = 0; i < giNumItems; i ++ ) { - sendf(Client->Socket, " %s:%i", gaItems[i].Handler->Name, gaItems[i].ID); + sendf(Client->Socket, + "202 Item %s:%i %i %s\n", + gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name + ); } - sendf(Client->Socket, "\n"); + sendf(Client->Socket, "200 List end\n"); } tItem *_GetItemFromString(char *String)