// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// C++ Source Code File Name: server.cpp
// C++ Compiler Used: MSVC40, HP CPP 10.24, egcs-2.90.29 
// Produced By: Doug Gaer   
// File Creation Date: 09/20/1999
// Date Last Modified: 06/21/2000
// Copyright (c) 2000 Douglas 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 demonstrate how to stream variable blocks
across a network connection.
*/
// ----------------------------------------------------------- // 
#include <iostream.h>
#include <stdlib.h>
#include "vbstream.h"
#include "part.h"

int FindObject(vbStream *server, vbBlockHeader &vb, PartDB &partdb)
{
  cout << "Client has queried the database" << endl << flush;
 
  if(server->ReadRemoteBlock((char *)partdb->p, vb) != 0) return 1;
  cout << "Searching for: " << partdb->p->name << endl << flush;
  
  FAU addr = partdb->FindObject();
  if(!addr) {
    cout << "Did not find the object" << endl << flush;
    partdb->p->name[0] = 0;
    partdb->p->id = (ID)0;
    partdb->p->price = (Price)0;
  }
  else
    cout << "Found the requested object" << endl << flush;

  cout << "Sending search results to the client" << endl << flush;
  if(server->WriteRemoteBlock((char *)partdb->p, sizeof(PartData))
     != 0) return 1;

  return 0;
}

int ChangeObject(vbStream *server, vbBlockHeader &vb, PartDB &partdb)
{
  cout << "Received a change object request" << endl << flush;
 
  // Read the name of the object to be changed
  if(server->ReadRemoteBlock((char *)partdb->p, vb) != 0) return 1;

  cout << "Client requested a change to object: " << partdb->p->name << endl << flush;

  PartData new_part_info;
  vbBlockHeader block_header;

  // Get the object's new info from the client 
  if(server->ReadClientHeader(block_header) != 0) return 1;
  if(server->ReadRemoteBlock((char *)&new_part_info, block_header) != 0) return 1;

  FAU addr = partdb->ChangeObject(new_part_info);
  if(!addr) {
    cout << "Change request denied: Did not find object in the database" 
	 << endl << flush;
    partdb->p->name[0] = 0;
    partdb->p->id = (ID)0;
    partdb->p->price = (Price)0;
  }
  else {
    cout << "Object changed to: " << partdb->p->name << ", " << partdb->p->id 
	 << ", " << partdb->p->price << endl << flush;
  }
  return 0;
}

int AddObject(vbStream *server, vbBlockHeader &vb, PartDB &partdb)
{
  cout << "Adding an object" << endl << flush;
  if(server->ReadRemoteBlock(partdb->p, vb) != 0) return 1;
  partdb->WriteObject();
  cout << partdb->p->name << " added to the database" << endl << flush;
  return 0;
}

int DeleteObject(vbStream *server, vbBlockHeader &vb, PartDB &partdb)
{
  cout << "Deleting an object" << endl << flush;
  if(server->ReadRemoteBlock(partdb->p, vb) != 0) return 1;
  FAU addr = partdb->DeleteObject();
  if(!addr) 
    cout << "Could not find \"" << partdb->p->name << "\" in the database" 
	 << endl << flush;
  else
    cout << "Deleted: " << partdb->p->name << endl << flush;
  return 0;
}

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 << flush;
    return 1;
  }
  
  PartDB partdb(new Part); 
  cout << "Initializing the database..." << endl << flush;
  const char *fname = "part.vbd";

  if(!VBDFile::Exists(fname)) {
    cout << "Creating new file..." << endl << flush;
    partdb->Create(fname);
  }
  else {
    cout << "Opening existing file..." << endl << flush;
    partdb->Open(fname);
  }

  vbStream server;
  vbsSocket_t remote_socket;
  unsigned short port = (unsigned short) atoi(argv[1]);

  cout << "Initializing the VB stream server..." << endl << flush;
  if(server.StreamServer(port) != 0) {
    cout << server.SocketExceptionMessage() << endl << flush;
    return 1;
  }
  
  // Get the host name assigned to this machine
  char hostname[vbsMAX_NAME_LEN];
  if(server.HostName(hostname) != 0) {
    cout << server.SocketExceptionMessage() << endl << flush;
    return 1;
  }
  cout << "Opening VBD stream server on host " << hostname << endl << flush;

  int server_up = 1;
  while(server_up) { // Loop until signaled to exit 
    cout << "Listening on port " << port << endl << flush;
    remote_socket = server.Accept(); // Block until the next read.
    if(remote_socket < 0) {
      cout << server.SocketExceptionMessage() << endl << flush;
      return 1;
    }

    // Read the block following a client connection
    vbBlockHeader vb;
    if(server.ReadClientHeader(vb) != 0) {
      cout << server.SocketExceptionMessage() << endl << flush;
      return 1;
    }

    cout << flush;
    
    // Get the client info
    char client_name[vbsMAX_NAME_LEN]; int r_port = -1;
    server.GetClientInfo(client_name, r_port);
    cout << client_name << " connecting on port " << r_port << endl << flush;

    // Read the status byte to determine what to do with this block
    __ULWORD__ block_status = vb.Status;
    __SBYTE__ status = (__SBYTE__)((block_status & 0xFF00)>>8);

    switch(status) { 
      // Process each block of data
      case vbAcknowledgeBlock:
	cout << "Received an acknowledge block command" << endl << flush;
	cout << "Sending acknowledgment" << endl << flush;
	if(server.WriteRemoteAckBlock() != 0)
	  cout << server.SocketExceptionMessage() << endl << flush;	  
	server.CloseRemoteSocket();
	break;

      case vbAddRemoteBlock:
	if(AddObject(&server, vb, partdb) != 0) 
	  cout << server.SocketExceptionMessage() << endl << flush;
	server.CloseRemoteSocket();
	break;

      case vbChangeRemoteBlock:
	if(ChangeObject(&server, vb, partdb) != 0) 
	  cout << server.SocketExceptionMessage() << endl << flush;
	server.CloseRemoteSocket();
	break;

      case vbRequestBlock:
	if(FindObject(&server, vb, partdb) != 0) 
	  cout << server.SocketExceptionMessage() << endl << flush;
	server.CloseRemoteSocket();
	break;

      case vbDeleteRemoteBlock:
	if(DeleteObject(&server, vb, partdb) != 0) 
	  cout << server.SocketExceptionMessage() << endl << flush;
	server.CloseRemoteSocket();
	break;

      case vbSendBlock :  
	cout << "Not excepting raw data blocks" << endl << flush;
	server.CloseRemoteSocket();
	break;

      case vbCloseConnection : 
	cout << "Client sent a close connection command" << endl << flush;
	server.CloseRemoteSocket();
	break;

      case vbKillServer:
	cout << "Client shutdown the server" << endl << flush;
	server_up = 0;
	break;
	
      default:
	cout << "Received bad block command from client" << endl << flush;
	server.CloseRemoteSocket();
	break;
    }
  }

  partdb->Close();
  server.Close();
  cout << "Exiting..." << endl << flush;
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //