/* 
 * 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 real FEP "polls" real stations over serial communication lines, whereas
 * the FEP Simulator recieves highway status data through a socket from the Java
 * ATMS Driver.
 * 
 * Highway status data is transmitted to the FEP Simulator over the socket in
 * XML Form. The XML highway status data is then parsed by the Network Reader.
 * 
 * The data is then sent to the ATMS, using RPC Calls. The RPC Calls to the
 * ATMS Server send an fep_reply structure. There is one fep_reply structure
 * sent to the ATMS for every FEP_LINE.
 *
 * The FEP Simulator is a socket server that runs persistently and awaits the
 * XML highway status data over the socket. When the XML highway status data is
 * recieved, it executes the RPC Calls to update the ATMS.
 *
 * @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 <stdio.h>
#include <stdlib.h>
#include "time.h"
#include "NetworkReader.h"
#include <netdb.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

const int BUFF_SIZE = 5500000;

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 XML responses from the
     * ATMS Driver. Upon reciept of the highway status XML 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 xml.
     */
    void manageClientConnection(char * xml);
    
    /**
     * Destructor: Does nothing, no cleaning necessary
     */
    ~FEPSim(); // Destructor

private:
    /* members */
    char * ATMSHost;
    int FEP_PROG;
    int FEP_REV;
    int SOCK_PORT;

    /**
     * Handler for the ATMS RPC Response (to the client RPC Call)
     * @param response pointer to fep_reply struct
     */
    void handleCallResponse(void *response);
    
    /**
     * Creates the RPC Client. If not successful, returns false.
     */
    bool createClient();
    
    /**
     * Sends an fep_reply for each FEP_LINE. Gets highway status from recieved XML
     * data.
     * @param xml The recieved highway status XML.
     */
    void sendReplys(char * xml);

};

#endif // __FEPSIM_H_INCLUDED__
