From: John Hodge Date: Sat, 19 Feb 2011 07:17:41 +0000 (+0800) Subject: Multiple chanegs, see details X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f2baa7bf5fb9877235369e7bf1abb7cff507dff6;p=tpg%2Fopendispense2.git Multiple chanegs, see details - Added option to hide an item from ENUM_ITEMS (prefix with '-') - Added door server password support (command line) - Non-enabled inotify support for item config --- diff --git a/items.cfg b/items.cfg index 2d9df3e..ef85035 100644 --- a/items.cfg +++ b/items.cfg @@ -18,5 +18,6 @@ pseudo 3 3500 polo postorder # Polo Shirt! (With UCC Sun Logo) pseudo 4 2500 membership # here comes the money! # Snack machine -#snack 13 128 Smiths Salt & Vinegar -#snack 33 128 Smiths Original +-snack 13 128 Smiths Salt & Vinegar +-snack 33 128 Smiths Original +-snack 53 128 Smiths Barbeque diff --git a/src/client/main.c b/src/client/main.c index 3ca9593..9b0f746 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -346,18 +346,16 @@ int main(int argc, char *argv[]) } - // Connect to server again - sock = OpenConnection(gsDispenseServer, giDispensePort); - if( sock < 0 ) return -1; - - // Authenticate - Authenticate(sock); - // Check for a valid item ID if( i >= 0 ) + { + // Connect, Authenticate, dispense and close + sock = OpenConnection(gsDispenseServer, giDispensePort); + if( sock < 0 ) return -1; + Authenticate(sock); DispenseItem(sock, i); - - close(sock); + close(sock); + } return 0; } diff --git a/src/server/common.h b/src/server/common.h index 06a9100..3c117d6 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -32,6 +32,7 @@ struct sItem { char *Name; //!< Display Name int Price; //!< Price + int bHidden; //!< Hidden item? tHandler *Handler; //!< Handler for the item short ID; //!< Item ID diff --git a/src/server/dispense.c b/src/server/dispense.c index 7e80703..c631bed 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -3,6 +3,7 @@ #include "common.h" #include #include +#include int _GetMinBalance(int Account); int _CanTransfer(int Source, int Destination, int Ammount); diff --git a/src/server/itemdb.c b/src/server/itemdb.c index 8b494f7..e503123 100644 --- a/src/server/itemdb.c +++ b/src/server/itemdb.c @@ -13,6 +13,10 @@ #include #include "common.h" #include +#include +#include +#include +#include // === IMPORTS === extern tHandler gCoke_Handler; @@ -20,6 +24,8 @@ extern tHandler gSnack_Handler; extern tHandler gDoor_Handler; // === PROTOTYPES === +void Init_Handlers(void); +void ItemList_Changed(int signum); void Load_Itemlist(void); char *trim(char *__str); @@ -30,6 +36,9 @@ tHandler gPseudo_Handler = {Name:"pseudo"}; tHandler *gaHandlers[] = {&gPseudo_Handler, &gCoke_Handler, &gSnack_Handler, &gDoor_Handler}; int giNumHandlers = sizeof(gaHandlers)/sizeof(gaHandlers[0]); char *gsItemListFile = DEFAULT_ITEM_FILE; +#if USE_INOTIFY + int giItem_INotifyFD; +#endif // === CODE === void Init_Handlers() @@ -40,7 +49,36 @@ void Init_Handlers() if( gaHandlers[i]->Init ) gaHandlers[i]->Init(0, NULL); // TODO: Arguments } + + // Use inotify to watch the snack config file + #if USE_INOTIFY + { + int oflags; + + giItem_INotifyFD = inotify_init(); + inotify_add_watch(giItem_INotifyFD, gsItemListFile, IN_MODIFY); + + // Handle SIGIO + signal(SIGIO, &ItemList_Changed); + + // Fire SIGIO when data is ready on the FD + fcntl(giItem_INotifyFD, F_SETOWN, getpid()); + oflags = fcntl(giItem_INotifyFD, F_GETFL); + fcntl(giItem_INotifyFD, F_SETFL, oflags | FASYNC); + } + #endif +} + +#if USE_INOTIFY +void ItemList_Changed(int signum) +{ + char buf[512]; + read(giItem_INotifyFD, buf, 512); + Load_Itemlist(); + + signum = 0; // Shut up GCC } +#endif /** * \brief Read the item list from disk @@ -55,7 +93,7 @@ void Load_Itemlist(void) regex_t regex; regmatch_t matches[5]; - i = regcomp(®ex, "^([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED); + i = regcomp(®ex, "^-?([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED); if( i ) { size_t len = regerror(i, ®ex, NULL, 0); @@ -142,6 +180,7 @@ void Load_Itemlist(void) gaItems[giNumItems].ID = num; gaItems[giNumItems].Price = price; gaItems[giNumItems].Name = strdup(desc); + gaItems[giNumItems].bHidden = (line[0] == '-'); giNumItems ++; } } diff --git a/src/server/main.c b/src/server/main.c index 0692d6a..6105936 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -28,6 +28,7 @@ extern int giServer_Port; extern char *gsItemListFile; extern char *gsCoke_SerialPort; extern char *gsSnack_SerialPort; +extern char *gsDoor_Password; // === GLOBALS === int giDebugLevel = 0; @@ -52,9 +53,11 @@ int main(int argc, char *argv[]) switch(arg[1]) { case 'p': + if( i + 1 >= argc ) return -1; giServer_Port = atoi(argv[++i]); break; case 'd': + if( i + 1 >= argc ) return -1; giDebugLevel = atoi(argv[++i]); break; default: @@ -64,14 +67,21 @@ int main(int argc, char *argv[]) } else if( arg[0] == '-' && arg[1] == '-' ) { if( strcmp(arg, "--itemsfile") == 0 ) { + if( i + 1 >= argc ) return -1; gsItemListFile = argv[++i]; } else if( strcmp(arg, "--cokeport") == 0 ) { + if( i + 1 >= argc ) return -1; gsCoke_SerialPort = argv[++i]; } else if( strcmp(arg, "--snackport") == 0 ) { + if( i + 1 >= argc ) return -1; gsSnack_SerialPort = argv[++i]; } + else if( strcmp(arg, "--doorpass") == 0 ) { + if( i + 1 >= argc ) return -1; + gsDoor_Password = argv[++i]; + } else { // Usage error? } diff --git a/src/server/server.c b/src/server/server.c index b103e83..4379317 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -490,16 +490,24 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args) */ void Server_Cmd_ENUMITEMS(tClient *Client, char *Args) { - int i; + int i, count; if( Args != NULL && strlen(Args) ) { sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n"); return ; } + + // Count shown items + count = 0; + for( i = 0; i < giNumItems; i ++ ) { + if( gaItems[i].bHidden ) continue; + count ++; + } - sendf(Client->Socket, "201 Items %i\n", giNumItems); + sendf(Client->Socket, "201 Items %i\n", count); for( i = 0; i < giNumItems; i ++ ) { + if( gaItems[i].bHidden ) continue; sendf(Client->Socket, "202 Item %s:%i %i %s\n", gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name