Exemple #1
0
std::string ProcessReturnCode::str() const {
  switch (state()) {
  case NOT_STARTED:
    return "not started";
  case RUNNING:
    return "running";
  case EXITED:
    return to<std::string>("exited with status ", exitStatus());
  case KILLED:
    return to<std::string>("killed by signal ", killSignal(),
                           (coreDumped() ? " (core dumped)" : ""));
  }
  CHECK(false);  // unreached
}
// The client communicationg thread
void *vStockPositionTracker(void *vArg)
{
    // The argument is cast back to a STcpThreadStruct instance
    STcpThreadStruct *newConnect = (STcpThreadStruct *)vArg;

    XPCTransaction newTransaction;  // Stores the transaction received from the client.
    XPCStockPosition *individualPosition;// A pointer to an individual ticker
                                         // position
    int iFound;

    // Loop forever processing client position updates until the client disconnects.
    while(1)
    {    
        try
        {
            // Receive the client's transaction message
            int iNumBytes = newConnect->clientConnection->iRecieveMessage((void *)&newTransaction, sizeof(newTransaction));

            if ((iNumBytes == 0) || (errno == ECONNRESET))
            {
                // Kill the price provider
                XPCSignal killSignal(SIGKILL);
                killSignal.vSendSignal(newConnect->iChildPid);

                // Client has disconnected.  Do cleanup and exit the thread
                delete newConnect->clientConnection;
                delete newConnect;    
                return 1;    
            }

            // Lock the semaphore to prevent multiple access problems
            newConnect->positionSem->vLockWait();

            // Display the components of the transaction received.
            cout << "Got New Transaction:" << endl;
            cout << "User: "******"Ticker: " << newTransaction.sGetTicker() << endl;
            cout << "BS: " << newTransaction.cGetBS() << endl;
            cout << "Amount: " << newTransaction.iGetAmount() << endl;

            // Set the user-position found flag to false.
            iFound = 0;

            // Loop through all user positions and locate a position matching
            // the user contained within the transaction record received.
            for (int iUserCount = 0; iUserCount < iNumUsers; iUserCount++)
            {
                // If a position exists for the user, locate the position
                // for the ticker contained within the transaction record.
                if (strcmp(userPositions[iUserCount].sGetUser(), newTransaction.sGetUser()) == 0)
                {
                    // If a ticker position exists add the transaction
                    // amount to the current position.
                    if ((individualPosition = userPositions[iUserCount].FindPosition(newTransaction.sGetTicker())) != NULL)
                    {
                            individualPosition->vAddPosition(newTransaction.iGetAmount(), newTransaction.cGetBS());

                    }
                    // If a position does not exists for the given
                    // ticker, add a new ticker position to the user's
                    // portfolio.
                    else
                    {
                        individualPosition = userPositions[iUserCount].AddNewPosition(newTransaction.sGetTicker(), newTransaction.iGetAmount(), newTransaction.cGetBS());
                    }
                    // Set the user position found flag to true.
                    iFound = 1;
                    break;
                }
            }

            // If the user position found flag is false add a new user
            // position.
            if (iFound == 0)
            {
                // If the number of user positions is currently 50, no more
                // user positions can be added.  Set the individual user
                // position to NULL.
                if (iNumUsers == 50)
                    individualPosition = NULL;
                else
                {
                    // Add a new user position.
                    userPositions[iNumUsers].vSetUser(newTransaction.sGetUser());
                    individualPosition = userPositions[iNumUsers].AddNewPosition(newTransaction.sGetTicker(), newTransaction.iGetAmount(), newTransaction.cGetBS());
                    // Increment the number of user positions by 1.
                    iNumUsers++;
                }
            }
       
            // Display the new position, set the transaction record to reflect
            // the new position, and send the transaction record to the
            // appropriate client.
            cout << "New Position: " << individualPosition->iGetPosition() << endl;
            newTransaction.vSetAmount(individualPosition->iGetPosition());

            // Unlock the semaphore
            newConnect->positionSem->vUnlock();

            // Use the iSendMessage method to send the updated position to the 
            // connected client
            newConnect->clientConnection->iSendMessage((void *)&newTransaction, sizeof(newTransaction));    
       }

       // Catch any exceptions that are caused by the TCP socket related classes.
       // Exit the child process with a status of 0
       catch(XPCException &exceptObject)
       {
            cout << exceptObject.sGetException() << endl;
            exit(0);
       }
    }
}