Multiple chanegs, see details
authorJohn Hodge <[email protected]>
Sat, 19 Feb 2011 07:17:41 +0000 (15:17 +0800)
committerJohn Hodge <[email protected]>
Sat, 19 Feb 2011 07:17:41 +0000 (15:17 +0800)
- 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

items.cfg
src/client/main.c
src/server/common.h
src/server/dispense.c
src/server/itemdb.c
src/server/main.c
src/server/server.c

index 2d9df3e..ef85035 100644 (file)
--- 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
index 3ca9593..9b0f746 100644 (file)
@@ -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;
 }
index 06a9100..3c117d6 100644 (file)
@@ -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
index 7e80703..c631bed 100644 (file)
@@ -3,6 +3,7 @@
 #include "common.h"
 #include <stdlib.h>
 #include <limits.h>
+#include <string.h>
 
  int   _GetMinBalance(int Account);
  int   _CanTransfer(int Source, int Destination, int Ammount);
index 8b494f7..e503123 100644 (file)
 #include <ctype.h>
 #include "common.h"
 #include <regex.h>
+#include <sys/inotify.h>
+#include <signal.h>
+#include <sys/fcntl.h>
+#include <unistd.h>
 
 // === 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(&regex, "^([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED);
+       i = regcomp(&regex, "^-?([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED);
        if( i )
        {
                size_t  len = regerror(i, &regex, 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 ++;
        }       
 }
index 0692d6a..6105936 100644 (file)
@@ -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?
                        }
index b103e83..4379317 100644 (file)
@@ -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

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