/* 
 * File: FEPSim.h
 * 
 * The FEP Simulator simulates the Front End Processor(FEP), which has the
 * responsibility of "polling" Loop Detector Stations for highway status data.
 * The actual FEP "polls" actual stations over serial communication lines, whereas
 * the FEP Simulator receives highways status data over a socket from the Java
 * ATMS Driver. The highways status data is then parsed by the Network Reader.
 * 
 * The data is reconfigured into an fep_reply struct, then sent to the ATMS via 
 * RPC Calls. The RPC Calls to the ATMS Server send an the fep_reply structs. 
 * There is one fep_reply structure sent to the ATMS for every station.
 *
 * @author John A. Torres
 * @version 9/8/2017
 */

// Include guard
#ifndef __FEPSIM_H_INCLUDED__
#define __FEPSIM_H_INCLUDED__

// Forward declared dependencies
class HighwaysParser;

// Included dependencies
#include "fep.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#include "HighwaysParser.h"
#include <netdb.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>

// this buffer is the size of the entire highways data message + 1 for the
// appended newline character, when sent over the socket
const int BUFF_SIZE = 1266341;
// Log file for FEPSimulator
static ofstream FEPLogFile;
    
class FEPSim {
public:
    // The RPC Client
    CLIENT *clnt;

    /**
     * Constructor. Sets data values for RPC Client and socket server.
     * 
     * @param ATMSHost The IP of ATMS Server
     * @param FEP RPC program number
     * @param FEP RPC program revision number
     * @param Socket Server listen port
     */
    FEPSim(char * ATMSHost, int FEP_PROG, int FEP_REV, int SOCK_PORT);
    
    /**
     * Creates a socket server and awaits the highway status message from the
     * ATMS Driver. Upon receipt of the highway status message, creates an RPC
     * client and sends the fep_replys to the ATMS.
     */
    void runSockServer();
    
    /**
     * Creates an RPC Client, and on successful creation, sends fep_replys to ATMS.
     * @param The recieved highway status msg.
     */
    void manageClientConnection(char * buffer);
    
    /**
     * Destructor: closes the log file if open
     */
    ~FEPSim(); // Destructor

private:
    // atms ip address
    char * ATMSHost;
    // rpc program number
    int FEP_PROG;
    // rpc revision number
    int FEP_REV;
    // socket port
    int SOCK_PORT;
    // name of logging file
    char * FEPLogFileName;
    
    /**
     * Handler for the ATMS RPC Response (to the client RPC Call)
     * @param response pointer to fep_reply struct
     */
    void handleCallResponse(void *response);
    
    /**
     * Creates an RPC Client to the ATMS Server. If unsuccessful, returns false
     * @return bool success
     */
    bool createClient();
    
    /**
     * Sends an fep_reply for each station on the FEPLine. 
     * Gets highway status from recieved socket msg.
     * 
     * @param buffer The recieved highway status msg.
     */
    void sendReplys(char * buffer);
};

#endif // __FEPSIM_H_INCLUDED__
