// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// C++ Source Code File Name: comsend.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 program is used to send disk files to the ComSock server
via stream socket.
*/
// ----------------------------------------------------------- // 
#include <iostream.h>
#include <stdlib.h>
#include "comfile.h"
#include "systime.h"

// --------------------------------------------------------------
// Constants 
// --------------------------------------------------------------
const double comsendVersionNumber = 1034.101;
const char *comsendProgramName = "ComSock Client";
// --------------------------------------------------------------

void CopyFileToStreamSocket(vbsCommFileCache *dev,
			    char *in, char *host, int port)
{
  vbStream client;

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

  unsigned byte_count = 0;
  cout << "Connecting to " << host << " on port " << port << endl;

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

  cout << "Writing the \"" << in << "\" file to " << host << " port "
       << port << endl;
  if(!dev->CopyFileToStreamSocket(&client, byte_count)) {
    cout << "Error copying file!" << endl;
    return;
  }

  cout << "Flushing the device cache..." << endl;
  dev->Flush();
  cout << "Wrote " << byte_count << " bytes." << endl;
  dev->CloseInputFile();
  cout << "Finished processing file." << endl;
  cout << "Closing the connection..." << endl;
  client.CloseConnection();
}

// Program's main thread of execution
int main(int argc, char **argv)
{
  if(argc != 4) {
    cout << endl;
    cout.setf(ios::showpoint | ios::fixed);
    cout.precision(3);
    cout << comsendProgramName << " version " << comsendVersionNumber << endl;
    cout << "Usage: " << argv[0] << " hostname port_number file_name" << endl;
    cout << endl;
    cout << "host_name   = Hostname or IP address of the ComSock server."
	 << endl;
    cout << "port_number = Port number the ComSock server is listening."
	 << endl;
    cout << "file_name   = File to copy to the ComSock server."
	 << endl; 
    cout << endl;
    return 0;
  }

  int cache_size = 1024;
  vbsCommFileCache dev(cache_size); // Device cache used to process a file

  unsigned short port = (unsigned short) atoi(argv[2]);
  
  SysTime systime;
  char *host = argv[1];
  char *fname = argv[3];
    
  cout << endl;
  cout << "Starting the " << comsendProgramName << " client at: " 
       << systime.GetSystemDateTime() << endl;

  CopyFileToStreamSocket(&dev, fname, host, port);

  cout << "Exiting..." << endl;
  cout << endl;
  return 0;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //