/* 
 * File         : main.cpp
 * Author       : Joe Churchwell
 * Purpose      : This package is developed for the Smart Terminal DLL to be
 *                able to include Ethernet.  This file serves as a test bed for
 *                the Ethernet package provided as a .OBJ file for later 
 *                implementation.
 *              
 * Created on February 25, 2013, 8:34 AM
 * 
 */

#include "stdafx.h"
#include<stdio.h>

#include <cstdlib>
#include "epackage.h"
#include "EtherServer.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    
    char msg[MAX_BUFFER_SIZE];
    signed int retVal;
    
    // Open up the server
    EtherServer e;
    EtherServer dhcp_c; // Client Receive Port
    EtherServer dhcp_s; // Server Receive Port
    
    // Open up the server port for normal communication
    if(e.OpenEthernetPort(COMMUNICATION_SOCKET) != EXIT_SUCCESS)
    {
        printf("Failed to open Ethernet port");
        return EXIT_FAILURE;
    }
    
    // Open up a DHCP Ethernet port to setup the client with the IP address
    if(dhcp_c.OpenEthernetPort(DHCP_PORT_TO_CLIENT) != EXIT_SUCCESS)
    {
        printf("Failed to open Ethernet port to client (DHCP)");
        return EXIT_FAILURE;
    }    
    
    // Open up a DHCP Ethernet port to setup the client with the IP address
    if(dhcp_s.OpenEthernetPort(DHCP_PORT_TO_SERVER) != EXIT_SUCCESS)
    {
        printf("Failed to open Ethernet port to server (DHCP)");
        return EXIT_FAILURE;
    }       
        
    // Continuously loop looking for messages
    while(1)
    {
        // Wait to receive something
		if(e.RetrieveWait(msg, &retVal) == EXIT_SUCCESS)
		{

			msg[retVal] = NULL;
	        
			// Print out what we received
			printf("Size = %i; Here is the message: %s\n", retVal, msg);
	        
			// Send out the data as well
			e.Write((const char *)msg, retVal);
		}
    }
    

    return EXIT_SUCCESS;
}

