// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// C++ Source Code File Name: testprog.cpp 
// Compiler Used: MSVC40, egcs-2.90.29, HP CPP 10.24
// Produced By: Doug Gaer   
// File Creation Date: 09/20/1999
// Date Last Modified: 06/21/2000
// Copyright (c) 2000 Dougas M. Gaer
// ----------------------------------------------------------- // 
// ------------- Program Description and Details ------------- // 
// ----------------------------------------------------------- // 
/*
The VBS C++ classes are copyright (c) 2000, by Douglas M. Gaer.
All those who put this code or its derivatives in a commercial
product MUST mention this copyright in their documentation for
users of the products in which this code or its derivative
classes are used. Otherwise, you have the freedom to redistribute
verbatim copies of this source code, adapt it to your specific
needs, or improve the code and release your improvements to the
public provided that the modified files carry prominent notices
stating that you changed the files and the date of any change.

THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
CORRECTION.

This is a test program for the device cache classes.
*/
// ----------------------------------------------------------- // 
#include "vbsfile.h"

void SkipToEol(istream &s)
{
  char c;
  s.clear();
  while(s.get(c) && c != '\n') { ; }
}

void InputString(char *mesg, MemoryBuffer &s)
{
  cout << mesg << ": ";
  char buf[255];
  for(int i = 0; i < 255; i++) buf[i] = 0;
  cin >> buf;
  s.Load(buf, (strlen(buf) + 1));
  s[s.length()] += '\0'; // Null terminate the string
}

void InputInt(char *mesg, int &i)
{
  cout << mesg << ": ";
  cin >> i;
  SkipToEol(cin);
}

void Menu()
{
  cout << endl;
  cout << "(?)   Display this menu" << endl;
  cout << "(D)   Copy a disk file to a Datagram Socket" << endl;
  cout << "(F)   Copy a disk file to another disk file" << endl;
  cout << "(P)   Copy a disk file to a Serial port" << endl;
  cout << "(Q)   Quit this program" << endl;
  cout << "(S)   Copy a disk file to a Stream Socket" << endl;
  cout << endl;
}

void CopyFileToSerialPort(vbsFile *dev) 
{
  MemoryBuffer in, port;
  InputString("Enter the name of the serial device", port);
  InputString("Enter the name of file to copy", in);

  if(!dev->OpenInputFile(in)) {
    cout << "Cannot open the specified file!" << endl;
    return;
  }
  
  unsigned byte_count = 0;
  vbSerialCommServer client;

  int rv = client.InitCommServer(port);
  if(rv != vbSerialComm::scomm_NO_ERROR) {
    cout << client.SerialCommExceptionMessage() << endl;
    return;
  }

  if(!dev->CopyFileToSerialPort(&client, byte_count)) {
    cout << "Error copying file." << endl;
    return;
  }

  cout << "Number of cache buckets in use = " << dev->BucketsInUse() << endl;
  cout << "Flushing the device cache..." << endl;
  dev->Flush();
  dev->CloseInputFile();

  // Shutdown the server
  client.TerminateConnection();

  cout << "Finished processing file." << endl;
  cout << "Byte count = " << byte_count << endl;
}

void CopyFileToStreamSocket(vbsFile *dev)
{
  vbStream client;
  int port;
  MemoryBuffer in, host;

  InputString("Enter the name of the server", host);
  InputInt("Enter the server's port number", port);
  InputString("Enter the name of file to copy", in);

  if(!dev->OpenInputFile(in)) {
    cout << "Cannot open the specified file!" << endl;
    return;
  }

  unsigned byte_count = 0;

  if(client.StreamClient(port, host) != 0) {
    cout << client.SocketExceptionMessage() << endl;
    client.Close();
    return;
  }
  
  if(!dev->CopyFileToStreamSocket(&client, byte_count)) {
    cout << "Error copying file." << endl;
    return;
  }

  cout << "Number of cache buckets in use = " << dev->BucketsInUse() << endl;
  cout << "Flushing the device cache..." << endl;
  dev->Flush();
  dev->CloseInputFile();

  // Shutdown the server
  client.TerminateConnection();
  
  cout << "Finished processing file." << endl;
  cout << "Byte count = " << byte_count << endl;
}

void CopyFileToFile(vbsFile *dev)
{
  MemoryBuffer in, out;

  InputString("Enter the name of the input file", in);

  if(!dev->OpenInputFile(in)) {
    cout << "Cannot open the specified file!" << endl;
    return;
  }

  InputString("Enter the name of the output file", out);
  
  if(!dev->OpenOutputFile(out)) {
    cout << "Cannot open the specified file!" << endl;
    return;
  }

  unsigned byte_count = 0;

  if(!dev->CopyFileToFile(byte_count)) {
    cout << "Error copying file." << endl;
    return;
  }

  cout << "Number of cache buckets in use = " << dev->BucketsInUse() << endl;
  cout << "Flushing the device cache..." << endl;
  dev->Flush();
  dev->CloseInputFile(); dev->CloseOutputFile();
  cout << "Finished processing file." << endl;
  cout << "Byte count = " << byte_count << endl;
}

void CopyFileToDatagramSocket(vbsFile *dev)
{
  vbDatagram client;
  int port;
  MemoryBuffer in, host;

  InputString("Enter the name of the server", host);
  InputInt("Enter the server's port number", port);
  InputString("Enter the name of file to copy", in);
  
  if(!dev->OpenInputFile(in)) {
    cout << "Cannot open the specified file!" << endl;
    return;
  }

  unsigned byte_count = 0;

  if(client.DatagramClient(port, host) != 0) {
    cout << client.SocketExceptionMessage() << endl;
    client.Close();
    return;
  }

  if(!dev->CopyFileToDatagramSocket(&client, byte_count)) {
    cout << "Error copying file." << endl;
    return;
  }

  cout << "Number of cache buckets in use = " << dev->BucketsInUse() << endl;
  cout << "Flushing the device cache..." << endl;
  dev->Flush();
  dev->CloseInputFile();

  // Shutdown the server
  client.TerminateConnection();

  cout << "Finished processing file." << endl;
  cout << "Byte count = " << byte_count << endl;
}

void SetupClient(int client_type)
{
  int cache_size = 10;
  vbsFile dev(cache_size); // Device cache used to process a file

  cout << "Creating a device cache using " << cache_size 
       << " cache buckets." << endl;
  cout << "Reserving " << (sizeof(MEMTYPE) * cache_size) 
       << " bytes of memory." << endl;
  cout << "Number of cache buckets in use = " << dev.BucketsInUse() << endl;
  cout << endl;
  
  switch(client_type) {
    case vbSOCKET_STREAM_CLIENT:
      CopyFileToStreamSocket(&dev);
      Menu();
      break;
      
    case vbSOCKET_DATAGRAM_CLIENT:
      CopyFileToDatagramSocket(&dev);
      Menu();
      break;

    case vbSOCKET_SERIAL_PORT_CLIENT:
      CopyFileToSerialPort(&dev);
      Menu();
      break;
      
    case vbSOCKET_LOCAL_FILE_SYSTEM:
      CopyFileToFile(&dev);
      Menu();
      break;
      
    default:
      break;
  }
}

int main()
{
  Menu();
  
  char key;
  int rv = 1;
  while(rv) {
    if (!cin) { // Input is in fail state
      SkipToEol(cin); // Go to end of line
      if (!cin) {  // Can't fix
	cout << "Input stream is broken" << endl;
	return 0;
      }
    }
    cout << endl;
    cout << '>';
    cin >> key;

    if (!cin) continue; // Fix at top of loop
    switch(key) {
      case 'd' : case 'D' : 
	SkipToEol(cin);
	SetupClient(vbSOCKET_DATAGRAM_CLIENT);
	break;
	
      case 'f' : case 'F' : 
	SkipToEol(cin);
	SetupClient(vbSOCKET_LOCAL_FILE_SYSTEM);
	break;
	
      case 'p' : case 'P' :
	SkipToEol(cin);
	SetupClient(vbSOCKET_SERIAL_PORT_CLIENT);
	break;
	
      case 's' : case 'S' :
	SkipToEol(cin);
	SetupClient(vbSOCKET_STREAM_CLIENT);
	break;

      case 'q' : case 'Q' :
	rv = 0;
	break;

      case '?' :
	Menu();
	break;

      default:
        cout << "Unrecognized command" << endl;
	cout << endl;
    }
  }
  return 0;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //