// ------------------------------- //
// -------- 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 the vbDatagram class.
*/
// ----------------------------------------------------------- // 
#include <iostream.h>
#include <stdlib.h>
#include "vbdatagm.h"

int ReadBlock(vbDatagram *server, vbBlockHeader &vb)
{
  cout << "Reading a raw block of data" << endl;
  
  char *buf = new char[(__ULWORD__)vb.Length];
  if(server->ReadRemoteBlock(buf, vb) != 0) return 1;

  buf[(__ULWORD__)vb.Length] = 0;
  cout << buf;
  cout << server->BytesRead() << " bytes received." << endl;
  
  delete buf;
  return 0;
}

int AnswerRequest(vbDatagram *server, vbBlockHeader &vb)
{
  cout << "Client has requested a block" << endl;
 
  char *test_block = "The quick brown fox jumps over the lazy dog \
0123456789\n";

  char *buf = new char[(__ULWORD__)vb.Length];
  if(server->ReadRemoteBlock(buf, vb) != 0) return 1;

  buf[(__ULWORD__)vb.Length] = 0;
  cout << "Client has requested block: " << buf << endl;
  delete buf;

  cout << "Answering request" << endl;
  
  if(server->WriteRemoteBlock((char *)test_block, strlen(test_block))
			      != 0) return 1;
  return 0;
}

int ChangeBlock(vbDatagram *server, vbBlockHeader &vb)
{
  cout << "Received a change block request" << endl;
 
  char *buf = new char[(__ULWORD__)vb.Length];
  if(server->ReadRemoteBlock(buf, vb) != 0) return 1;

  buf[(__ULWORD__)vb.Length] = 0;
  cout << "Client requested that block \"" << buf << "\" be changed to:" 
       << endl << flush;
  delete buf;

  vbBlockHeader block_header;
  if(server->ReadClientHeader(block_header) != 0) return 1;

  char *block = new char[(__ULWORD__)block_header.Length];
  if(server->ReadRemoteBlock((char *)block, block_header) != 0) return 1;
  block[(__ULWORD__)block_header.Length] = 0;
  cout << block << flush;

  delete block;
  return 0;
}

int AddBlock(vbDatagram *server, vbBlockHeader &vb)
{
  cout << "Received an add block request" << endl;
  
  char *block = new char[(__ULWORD__)vb.Length];
  if(server->ReadRemoteBlock(block, vb) != 0) return 1;

  block[(__ULWORD__)vb.Length] = 0;
  cout << "Client has requested the following block be added:" << endl;
  cout << block << flush;
  delete block;
  
  return 0;
}

int DeleteBlock(vbDatagram *server, vbBlockHeader &vb)
{
  cout << "Received a delete block request" << endl;
  
  char *buf = new char[(__ULWORD__)vb.Length];
  if(server->ReadRemoteBlock(buf, vb) != 0) return 1;

  buf[(__ULWORD__)vb.Length] = 0;
  cout << "Client has requested block \"" << buf << "\" be deleted" << endl;
  delete buf;
  
  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;
    return 1;
  }

  vbDatagram server;
  unsigned short port = (unsigned short) atoi(argv[1]);

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

  // Find out what port was really assigned 
  int assigned_port;
  if(server.PortNumber(assigned_port) != 0) {
    cout << server.SocketExceptionMessage() << endl;
    return 1;
  }
  cout << "Port assigned is " << assigned_port << endl;
    
  while(1) { // Block until the next read. 
    // Read the block following a client connection
    vbBlockHeader vb;
    if(server.ReadClientHeader(vb) != 0) {
      cout << server.SocketExceptionMessage() << endl;
      return 1;
    }

    // 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;

    // 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;
	cout << "Sending acknowledgment" << endl;
	if(server.WriteRemoteAckBlock() != 0)
	  cout << server.SocketExceptionMessage() << endl;	  
	break;

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

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

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

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

      case vbSendBlock :  
	if(ReadBlock(&server, vb) != 0) 
	  cout << server.SocketExceptionMessage() << endl;
	break;

      case vbCloseConnection : 
	cout << "Client sent a close connection command" << endl;
	break;

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

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