Previous Section  < Free Open Study >  Next Section

The Network.cpp File


// Network.cpp: The implementation of the Network class for this 

// application. The Network class, like the Transaction class, is

// used on both sides of the application. It uses ATM_SIDE and

// BANK_SIDE macros to distinguish which side is being compiled.

// Each side of the network class has both a send and receive pair,

// which match the formats of the corresponding application side.



#include ''network.hpp''

#include ''trans.hpp''



// The send method, which takes a Transaction, is used by the ATM

// side of the application to send a transaction to the Bank side of

// the application. It asks the Transaction to packetize itself.

// The Transaction will build a string in its argument address

// which has the format of a four-character type field (first four

// letters of the class's name, a seven-digit source account field,

// a space, a four-digit PIN number, a space, a nine-digit

// floating-point field (stored in ASCII), and a NULL terminator).

// Some transactions may add additional info to the end of the

// record, e.g., the Transfer transaction adds the target account

// to the end of the string for later retrieval by the Network::

// receive() method below (which takes zero arguments).

// In this simulation, the method simply prints the packet it would

// send through the reader's favorite mechanism.



#ifdef ATM_SIDE

int

Network::send(Transaction* t)

{

   char buffer[large_string];



   t->packetize(buffer);

   cout << ''@Network Simulation@ Sending from the ATM to the

Bank: \'' '' << buffer;

   cout << ''\''\n'';

// The reader would now send this string through there favorite

// byte sending mechanism.

    return(0);

}



// The receive method for the Network class on the ATM side of the

// application waits for a buffer to be sent from the Bank. This

// buffer is expected to have the return status (0 or 1) in the first

// four bytes, followed by any transaction-specific information in

// the remaining characters. It is assumed that a space separates

// the status from the additional info. If the fifth character is a

// NULL terminator, then there is no additional info. The method

// returns the number of bytes existing, aside from the status

// field.

int

Network::receive(int& status, char* buf)

{

   char buffer[large_string];



// The reader would place his or her favorite byte-exchange

// mechanism here and ask it to receive a byte string from the Bank

// side of the application. This byte string is assumed to be a

// four-character status field followed by an indeterminant number

// of transaction-specific information. In this simulation, the

// balance of the account is passed as additional information.



   cout << ''@Network Simulation@ Enter Status (4 characters), a

space, '';

   cout << ''and the account balance: '';

   cin.getline(buffer, large_string, '\n');

   if (buffer[4] == '\0') {

      status = atoi(buffer);

      buf[0]= '\0';

      return(0);

   }

   else if (buffer[4] == '') {

      buffer[4] = '\0';

      status = atoi(buffer);

      strcpy(buf, &buffer[5]);

      return(strlen(&buffer[5]));

   }

   else {

      cout << ''@Network Simulation@ Bad packet received at the

ATM!!!\n'';

      Status = 1;

   }

   return(0);

}



#endif



#ifdef BANK_SIDE

// The receive method on the Bank side of the application receives

// a string of the byte-transfer mechanism (in this case, a string

// typed by the user). The string is then parsed by the Network

// class, and an appropriate Transaction object is built. The

// explicit case analysis on the type of the transaction is

// necessary due to the absence of an object-oriented network. The

// Network class provides the object-oriented interface so that

// the rest of our model sees nothing but objects. The case analysis

// is hidden within this method.



Transaction*

Network::receive()

{

   char buffer[large_string];



   // The reader may replace this call to getline with any

   // appropriate byte-transfer mechanism.

   cout << ''@Network Simulation@ Enter type (4 characters), '';

   cout << ''an account (7 digits), \n'';

   cout << ''@Network Simulation@ a space, a pin (4 characters), a

space, '';

   cout << ''and an amount: \n'';

   cin.getline(buffer, large_string);



// We parse the string by turning the whitespace into NULL

// terminators. We then build the appropriate object denoted by

// the first four characters of the buffer. This is the inverse

// routine for the send method on the ATM side of the application.



   buffer[11] = buffer[16] = buffer[26] = '\0';

   if (!strncmp(buffer, ''With'', 4)) {

      return(newWithdraw(&buffer[4], &buffer[12], atof

(&buffer[17])));

   }

   else if (!strncmp(buffer, ''Depo'', 4)) {

     return(new Deposit(&buffer[4], &buffer[12], atof

(&buffer[17])));

   }

   else if (!strncmp{buffer, ''Bala'', 4)) {

     return (new Balance(&buffer[4], &buffer[12]));

   }

   else if (!strncmp(buffer, ''Tran'', 4)) {

     return(newTransfer(&buffer[4], &buffer[12], &buffer

[27], atof(&buffer[17])));

   }

   else {

     cout << ''@Bank Application@ Unknown packet type!!!\n'';

   }

   return(NULL);

}



// The send method of the Bank side of the application uses the

// transaction to packetize the data. The buffer created will be a

// four-digit status field, followed by a space, and the amount of

// the transaction. This amount is then given to the Balance

// transaction on the other side of the application (the ATM side).

void

Network::send(int status, Transaction* t)

{

   char buffer[large_string];



   t->packetize(status, buffer);

// The reader can replace this output with the appropriate

// byte-transfer mechanism.

    cout << ''@Network Simulation@ Packet Sent to ATM: \'''' << 

buffer << ''\''\n";

}



#endif

    Previous Section  < Free Open Study >  Next Section