From: John Hodge Date: Sat, 20 Nov 2010 17:34:31 +0000 (+0800) Subject: Fixing basic cokebank to actually work X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=650ed689f2d8c30fe9c448fbec0e58037cc2ce25;p=tpg%2Fopendispense2.git Fixing basic cokebank to actually work --- diff --git a/RunServerTest b/RunServerTest new file mode 100755 index 0000000..d0c2fa3 --- /dev/null +++ b/RunServerTest @@ -0,0 +1,3 @@ +#!/bin/sh + +LD_LIBRARY_PATH=. ./dispsrv --itemsfile items.cfg -p 11020 diff --git a/src/cokebank b/src/cokebank new file mode 120000 index 0000000..eff8399 --- /dev/null +++ b/src/cokebank @@ -0,0 +1 @@ +cokebank_basic \ No newline at end of file diff --git a/src/cokebank_basic/Makefile b/src/cokebank_basic/Makefile index 8e6ea60..6aa55df 100644 --- a/src/cokebank_basic/Makefile +++ b/src/cokebank_basic/Makefile @@ -1,6 +1,6 @@ BIN := ../../cokebank.so -OBJ := main.o +OBJ := main.o bank.o CPPFLAGS := CFLAGS := -Wall -Werror -g -fPIC diff --git a/src/cokebank_basic/bank.c b/src/cokebank_basic/bank.c index c9aa6b3..cc09fa5 100644 --- a/src/cokebank_basic/bank.c +++ b/src/cokebank_basic/bank.c @@ -9,18 +9,27 @@ * for full details. */ #include +#include +#include +#include "common.h" enum { FLAG_TYPEMASK = 0x03, - USER_FLAG_NORMAL = 0x00, - USER_FLAG_COKE = 0x01, - USER_FLAG_WHEEL = 0x02, - USER_FLAG_GOD = 0x03 + USER_TYPE_NORMAL = 0x00, + USER_TYPE_COKE = 0x01, + USER_TYPE_WHEEL = 0x02, + USER_TYPE_GOD = 0x03 }; +// === GLOBALS === +tUser *gaBank_Users; + int giBank_NumUsers; +FILE *gBank_File; + // === CODE === -int Bank_GetUserByUnixID(int UnixUID) +int Bank_GetUserByUnixID(int UnixID) { + int i; // Expensive search :( for( i = 0; i < giBank_NumUsers; i ++ ) { @@ -86,6 +95,14 @@ int Bank_GetMinAllowedBalance(int ID) } } +int Bank_GetUserUnixID(int ID) +{ + if( ID < 0 || ID >= giBank_NumUsers ) + return -1; + + return gaBank_Users[ID].UnixID; +} + /** * \brief Create a new user in our database */ @@ -105,7 +122,7 @@ int Bank_AddUser(int UnixID) // Commit to file fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET); - fwrite(gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File); + fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File); // Increment count giBank_NumUsers ++; diff --git a/src/cokebank_basic/common.h b/src/cokebank_basic/common.h new file mode 100644 index 0000000..e6e37c6 --- /dev/null +++ b/src/cokebank_basic/common.h @@ -0,0 +1,19 @@ +/* + * OpenDispense 2 + * UCC (University [of WA] Computer Club) Electronic Accounting System + * + * cokebank.c - Coke-Bank management + * + * This file is licenced under the 3-clause BSD Licence. See the file COPYING + * for full details. + */ +#ifndef _COKEBANK_COMMON_H_ +#define _COKEBANK_COMMON_H_ + +typedef struct sUser { + int UnixID; + int Balance; + int Flags; +} tUser; + +#endif diff --git a/src/cokebank_basic/main.c b/src/cokebank_basic/main.c index 2388be8..11b822c 100644 --- a/src/cokebank_basic/main.c +++ b/src/cokebank_basic/main.c @@ -11,17 +11,21 @@ #include #include #include +#include "common.h" // === IMPORTS === - int Bank_GetMinAllowedBalance(int ID); - int Bank_GetUserBalance(int ID); - int Bank_AlterUserBalance(int ID, int Delta); - int Bank_GetUserByUnixID(int UnixID); - int Bank_GetUserByName(const char *Name); - int Bank_AddUser(int UnixID); +extern int Bank_GetMinAllowedBalance(int ID); +extern int Bank_GetUserBalance(int ID); +extern int Bank_AlterUserBalance(int ID, int Delta); +extern int Bank_GetUserByUnixID(int UnixID); +extern int Bank_GetUserUnixID(int ID); +extern int Bank_AddUser(int UnixID); +extern FILE *gBank_File; +extern tUser *gaBank_Users; +extern int giBank_NumUsers; // === PROTOTYPES === -void Init_Cokebank(void); +void Init_Cokebank(const char *Argument); int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); int GetBalance(int User); char *GetUserName(int User); @@ -32,9 +36,21 @@ char *GetUserName(int User); /** * \brief Load the cokebank database */ -void Init_Cokebank(void) +void Init_Cokebank(const char *Argument) { - + gBank_File = fopen(Argument, "rb+"); + if( !gBank_File ) { + gBank_File = fopen(Argument, "wb+"); + } + if( !gBank_File ) { + perror("Opening coke bank"); + } + + fseek(gBank_File, 0, SEEK_END); + giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]); + fseek(gBank_File, 0, SEEK_SET); + gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) ); + fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File); } /** @@ -69,7 +85,19 @@ int GetBalance(int User) */ char *GetUserName(int User) { - return NULL; + struct passwd *pwd; + int unixid = Bank_GetUserUnixID(User); + + if( unixid == -1 ) + return strdup(">sales"); + + if( unixid == -2 ) + return strdup(">liability"); + + pwd = getpwuid(unixid); + if( !pwd ) return NULL; + + return strdup(pwd->pw_name); } /** @@ -77,19 +105,26 @@ char *GetUserName(int User) */ int GetUserID(const char *Username) { - struct passwd *pwd; - int ret; + int ret, uid; - // Get user ID - pwd = getpwnam(Username); - if( !pwd ) { - return -1; + if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into + uid = -1; + } + else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from + uid = -2; + } + else { + struct passwd *pwd; + // Get user ID + pwd = getpwnam(Username); + if( !pwd ) return -1; + uid = pwd->pw_uid; } // Get internal ID (or create new user) - ret = Bank_GetUserByUnixID(pwd->pw_uid); + ret = Bank_GetUserByUnixID(uid); if( ret == -1 ) { - ret = Bank_AddUser(pwd->pw_uid); + ret = Bank_AddUser(uid); } return ret; @@ -101,8 +136,10 @@ int GetUserID(const char *Username) */ int GetUserAuth(const char *Username, const char *Password) { - if( strcmp(Username, "test") == 0 ) - return Bank_GetUserByName("test"); + #if HACK_TPG_NOAUTH + if( strcmp(Username, "tpg") == 0 ) + return GetUserID("tpg"); + #endif return -1; } diff --git a/src/server/dispense.c b/src/server/dispense.c index 32e6647..f851731 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -29,7 +29,7 @@ int DispenseItem(int User, int Item) if(!ret) return ret; // Subtract the balance - ret = AlterBalance( User, -item->Price ); + ret = Transfer( User, GetUserID(">sales"), item->Price, "" ); // What value should I use for this error? // AlterBalance should return the final user balance if(ret == 0) return 1; @@ -42,7 +42,7 @@ int DispenseItem(int User, int Item) if(ret) { Log_Error("Dispense failed after deducting cost (%s dispensing %s - %ic)", username, item->Name, item->Price); - AlterBalance( User, item->Price ); + Transfer( GetUserID(">sales"), User, item->Price, "rollback" ); free( username ); return 1; } diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index f8ed168..493212e 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -36,7 +36,7 @@ regex_t gCoke_StatusRegex; int Coke_InitHandler() { giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR); - regexc(&gCoke_StatusRegex, "^$", REG_EXTENDED); + regcomp(&gCoke_StatusRegex, "^$", REG_EXTENDED); return 0; } @@ -54,7 +54,7 @@ int Coke_CanDispense(int User, int Item) // Read the response read(giCoke_SerialFD, tmp, sizeof(tmp)-1); - regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches); + regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0); printf("s%i response '%s'\n", Item, tmp); @@ -67,6 +67,7 @@ int Coke_CanDispense(int User, int Item) int Coke_DoDispense(int User, int Item) { char tmp[32]; + regmatch_t matches[4]; // Sanity please if( Item < 0 || Item > 6 ) return -1; @@ -77,7 +78,7 @@ int Coke_DoDispense(int User, int Item) // Get status read(giCoke_SerialFD, tmp, sizeof(tmp)-1); - regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches); + regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0); printf("d%i response '%s'\n", Item, tmp); diff --git a/src/server/main.c b/src/server/main.c index e789b13..d327db3 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -13,7 +13,7 @@ #include "common.h" // === IMPORTS === -extern void Init_Cokebank(void); // cokebank.c +extern void Init_Cokebank(const char *Argument); // cokebank.c extern void Load_Itemlist(void); extern void Server_Start(void); extern int giServer_Port; @@ -22,6 +22,7 @@ extern char* gsCoke_SerialPort; // === GLOBALS === int giDebugLevel = 0; +char *gsCokebankPath = "cokebank.db"; // === CODE === int main(int argc, char *argv[]) @@ -63,7 +64,7 @@ int main(int argc, char *argv[]) } } - Init_Cokebank(); + Init_Cokebank(gsCokebankPath); Load_Itemlist();