// ------------------------------- //
// -------- 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: 04/28/2000
// Date Last Modified: 05/26/2000
// Copyright (c) 2000 Douglas M. Gaer
// ----------------------------------------------------------- // 
// ------------- Program Description and Details ------------- // 
// ----------------------------------------------------------- // 
/*
The VBD and VBS C++ classes are copyright (c) 1997 and 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 used demonstrate the use of the vbThread
library in multi-threaded server.
*/
// ----------------------------------------------------------- //   
#include <iostream.h>
#include <stdlib.h>
#include "httpserv.h"

int main(int argc, char **argv)
{
  // Check arguments. Should be only one: the port number to bind to.
  if(argc != 2) {
    cerr << "Usage: " << argv[0] << " port" << endl;
    return 1;
  }

  unsigned short port = (unsigned short) atoi(argv[1]);
  vbHTTPServer server;
  
  cout << "Initializing the HTTP server..." << endl;
  int rv = server.InitHTTPServer(port);
  if(rv != 0) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }

  // Get the host name assigned to this machine
  char hostname[vbsMAX_NAME_LEN];
  rv = server.HostName(hostname);
  if(rv != 0) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }

  cout << "Opening HTTP server on host " << hostname << endl;
  cout << "Press Ctrl C to exit" << endl;
  cout << endl;
  
  cout << "Listening on port " << port << endl;

  while(1) { // Loop forever accepting connections

    // Block the server until a client requests service
    if(server.Accept() < 0) {
      cout << server.SocketExceptionMessage() << endl;
      continue;
    }

    // Get the client info
    char *af = server.GetRemoteAddressFamily() == AF_INET?"AF_INET":"UNKNOWN";
    char client_name[vbsMAX_NAME_LEN]; int r_port = -1;
    server.GetClientInfo(client_name, r_port);
    
    cout << "Recieved request from " << client_name 
	 << " remote port " << r_port << endl << flush;

    // Start a client thread
    vbThread_t *client_thread = server.CreateThread();
    if(client_thread->GetThreadError() != vbTHREAD_NO_ERROR) {
      cout << client_thread->ThreadExceptionMessage() << endl;
    }
  }

  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //