Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,355 people online right now. Registration is fast and FREE... Join Now!




Banking System

 
Reply to this topicStart new topic

Banking System

Gideon_kg
15 Oct, 2008 - 03:06 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2008
Posts: 1

I seek help with updating and deleting an account
CODE

//Copyright: Shoden Data Systems
//Author:Nche Gideon 2008. The Bank Project
//File Name: BankServer.cpp
#include <iostream>
#include <pthread.h>      //POSIX Threads
#include <sys/socket.h> //Sockets
#include <arpa/inet.h>  //Sockets
#include <netinet/in.h> //Sockets
#include <semaphore.h>    //Sempahore Header
#include <unistd.h>
#include <signal.h>        //Signals for timeout
#include <string>
#include <errno.h>     //Error Numbers
// Thread Parameters Definition
sem_t semLock;            // Semaphore
sem_t oneLock;            // Semaphore
struct thread_parm
{
    int    clientSocket;
    int    thredNo;
    string clientIP;
};
//Global Data Definitions
int serverSocket;                                 //Server Socket
sockaddr_in serverAddress;                        //Server Address
unsigned short const hostPort=42001;            //Port Number
char const * const hostIPAddr="127.0.0.1";        //Server IP Address localhost
unsigned short const maxQueue=10;                //Max Listen Queue Length
unsigned int clientSize;
int clientCount=0;                                //Number of clients
struct sigaction timeOut;                        //Timeout Structure
//Function Prototypes
void catchTimeOut(int);                            //Timeout Function
void* VTTServer (void*);
void* ATMServer (void*);
Thread
int main()
{
    int clientSocket;                            //Client Socket Handle
    int threadStatus;
    int socketStatus;
    char* clientIPAddr;
    sockaddr_in clientAddress;                    //Client Address
    thread_parm threadParm;
    threadParm.threadNo=0;
    pthread_t threadID;                            //EchoTask(s) Thread Container
    //Create Server Socket
    serverSocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (serverSocket<0)
    {
        cout << "::Socket Creation Failed Error Code=" << errno <<endl;
        return false;
    }
    // Initialise Server Address
    memset(&serverAddress,0,sieof(serverAddress));
    serverAddress.sin_family=AF_INET;
    serverAddress.sin_addr.s_addr=inet_addr(hostIPAddr);
    serverAddress.sin_port=htons(hostPort);
    //Bind to Port Number
    socketStatus=bind(serverSocket,(sockaddr*) &serverAddress,
                    sizeof(serverAddress));
    if (socketStatus<0)
    {
        cout << "::Bind Failed Error Code=" << errno <<endl;
        return 9;
    }
    //Listen For Clients
    socketStatus=listen(serverSocket,maxQueue);
    if (socketStatus<0)
    {
        cout << "::Listen Failed Error Code=" << errno << endl;
        return 9;
    }
    // Set Timeout Handler & Signal
    timeOut.sa_handler=catchTimeOut;
    sigaction(SIGALRM,&timeOut,0);
    timeOut.sa_flags=0;
    while (true)
    {
        sem_init(&semLock,0,1);            //Initialise Lock to 1
        sem_init(&oneLock,0,1);            //Initialise Lock to 1
        alarm(120); // Set timeout value
        memset (&clientAddress,0,sieof(clientAddress));
        // Accept (Wait) For Client
        clientSocket=accept(serverSocket,(sockaddr*) &clientAddress,
                        &clientSize);
        if (errno==EINTR && clientSocket<0)  //Timeout Detected
        {
            if (clientCount==0)
                break; //=>dNo Connected Clients
            continue;
        }
        if (clientSocket<0)
        {
            cout << "Accept Failed Error Code=" <<errno << enl;
            return 9;
        }
        sem_wait(&oneLock);
        alarm(0);
        clientIPAddr=inet_ntoa(clientAddresss.sin_addr);
        threadParm.clientSocket=clientSocket;
        threadParm.threadNo++;
        threadParm.clientIP=clientIPAddr;
        //Create
        threadStatus=pthread_create(&threadID,NULL,  ,&threadParm);
        sem_post(&semLock);
        if(threadStatus!=0) {
            cout << "Thread Creation Failed Error Code=" << errno <<endl;
            return 9;
            }
            clientCount++;
    }
    close(serverSocket);
    return 0;
}
void Login( const char *username, const char *password)
{
    int threadNo;
    int noBytes;
    string clientIP;
    char msgIn[256];
    int clientSocket;
    int administration;
    thread_parm* threadInfo;
    threadInfo = (thread_parm*) threadParms;
    clientSocket= threadInfo->clientSocket;
    clientIP = threadInfo->threadNo;
    threadNo=threadInfo->clientIP;
    cout << "Client Address=" <<clientIP <<"/"<< threadNo << endl;
    send(clientSocket,"+Connected",7,0);
}
void* VTTServer(void* threadParms)
{
    bool noVendor=false;
    int  clientSocket;
    int  threadNo;
    char clientIP[16];
    char msgIn[256];
    char msgOut[256];
    char vttMsg[64];
    char venno[9];
    char msgID;
    vendors      aVendor;
    MsgServer    msgHangler;
    clients      aClient;
    MsgServer*   vttTokens;
    thread_parm* threadInfo;
    double          cardAmount, avlCredit;
    int             pinNumber;
    // Thread parameters
    threadInfo= (thread_parm*) threadParms;
    clientSocket = threadInfo->clientSocket;
    threadNo = threadInfo->threadNo;
    strcpy(clientIP,threadInfo->clientIP);
    strcpy(venno,threadInfo->venno);
    //Build Message
    sprintf(vttMsg,"+" Thread %d VTT Eded",threadNo);
    // Vendor Code
    aVendor.venno=msgHandler.parseInt(venno);
    // Query Vendor
    if (sqlServer->queryVendor(&aVendor))
    {
        strcpy(msgOut,msg::NAK0); // Ventor not registered
        noVendor=true;
    }
    else
    {
        strcpy(msgOut,msg::ACK0); //Vendor registered
        //Get vendor authorisation code
        strcat (msgOut, msgHandler.toString(avendor.vauth));
        strcat(msgOut,";");
    }
    // Send ACK|NAK message to client
    sendMessage(clientSocket,clientIP,msgOut,strlen(msgOut));
    //Transaction processing loop if vendor valid
    
    while (!noVendor)
    {
        //Receive client message
        recvMessage(clientSocket,clientIP,msgIn,256);
        //Load message tokens
        vttTokens=new MsgServer(msgIn);
        //Get message id
        msgID =vttTokens->getMsgID();
        if (msgID==msg::isHALT) // Client termination request
            break;   //Stop thread
        switch (msgID)  // Select message identification
        {
            case msg::isCCDR:  //Credit card debit
            // Get Acccount number
            strcpy(aClient.actno,vttTokens->nextToken());
            // Get PIN
            pinNumber=msgHandler.parseInt(vttTokens->nextToken());
            // Get Amount
            cardAmount=msgHandler.parseReal(vttTokens->nextToken());
            if (sqlServer->getAccounts(&aClient))  // Valid Client?
            {
                strcpy(msgOut,msg::NAK0); // Client not registered
                break;
            }
            if (aClient.pinno!=pinNumber)  // Valid PIN
            {
                strcpy(msgOut,msg::NAK1);  // Client authorisation failed
                break;
            }
            avlCredit = aClient.calim-aClient.cabal;   //Get credit limit
            if (cardAmount>avlCredit)      //Sufficient funds for purchase
            {
                strcpy(msgOut,msg::NAK2);     //Insufficient funds
                break;
            }
            aClient.cabal +=cardAmount;   //Update balance
            //Update client database
            sqlServer->updateAccount(2,types::drCCP,aVendor.venno,cardAmount,&aClient);
            strcpy(msgOut,msg::ACK0);       //Purchase authorised
        }
        // Send message to client
        sendMessage(clientSocket,clientIP,msgOut,strlen(msgOut));
        delete vttTokens;
    }
        logWriter->logMessage(9,msg::isAMSG,clientIP,vttMsg);
        clientCount--;
        close(clientSocket);     //Close client socket
        return NULL;
}
void* ADTServer(void* threadParms)
{
    int  clientSocket;
    int  threadNo;
    char clientIP[16];
    char msgIn[256];
    char msgOut[256];
    char adtMsg[64];
    char msgID;
    MsgServer  msgHandler;
    users      aUser;
    char       actNumber[9];
    MsgServer* adtTokens;
    thread_parm* threadInfo;
    //Get thread parameters
    threadInfo = (thread_parm*) threadParms;
    clientSocket = threadInfo->clientSocket;
    threadNo = threadInfo->threadNo;
    // Build message
    sprintf(adtMsg,"+* Thread %d ADT Ended",threadNo);
    strcpy(msgOut,msg::ACK0);
    // send connection acknowledgement to client
    send Message(clientSocket,clientIP,msgOut,strlen(msgOut));
    while (true)
    {
        //Receive client messsage
        recvMessage(clientSocket,clientIP,msgIn,256);
        //Build message tokens
        adtTokens = new MsgServer(msgIn);
        //Get message id
        msgID =adtTokens->getMsgID();
        if (msgID==msg::isHALT)
            break; //Terminate transaction server
    }

        
        //*********************************************************************
    // Code to delete or update an account

        switch (msgID)  //Select message id
        {
            case msg::isLOGU:      //Login ADT user
            //Get user name
            strcpy(aUser.uname,adtTokens->nextToken());
            //Query user
            if (sqlServer->queryUser(&aUser))
            {
                strcpy(msgOut,msg::NAK0);       //User not registered
                break;
            }
            strcpy(msgOut,msg::ACK0);
            strcat(msgOut,aUser.upass);        //Insert user password
            strcat(msgOut,";");
            break;
        case msg::isNEWA:        // New account
            sqlServer->insertClient(adtTokens);          //Add account
            strcpy(msgOut,msg::ACK0);
            break;
        case msg::isLOGA:    // Get exisisting account
            strcpy(actNumber,adtTokens->nextToken());    // Query account
            break;
        }
        //send message to user
        sendMessage(clientSocket,clientIP,msgOut,strlen(msgOut));
        delete adtTokens;
    }
    logWriter->logMessage(9,msg::isAMSG,clientIP,adtMsg);
    clientCount--;
    close(clientSocket);
    return NULL;
}

nchek@webmail.co.za
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Banking System
15 Oct, 2008 - 03:19 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
Please always code.gif

and always post your problem with complete details along with your code. That helps us to help you better.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:11PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month