/* Std include */
#include <stdlib.h>
#include "string.h"

/* SDL include */
#include "scttypes.h"

/* System include */
#include "switch.ifc"

/* Lists defined in the .cod file */
extern tSDLSignalInfo *xSignalList[];
extern xSignalIdNode xSignalIdList[];

/* Data structures to hold the MobileStation addresses */
struct destData{char ipAddr[32]; int portNbr;};
struct destData destination[6];


/* Routing function */
void xGetDestination(xSignalNode *xOutSignal, char *signalName, char *IPAddr, int *Port)
{
  unsigned int receiver;

  /*
    The address structure is initialized with hostname and port number of the mobile stations
    Four mobile stations out of six are used
  */


/* USER CODE: Set hostname and port number of each mobile station binary here --------- */

  /* Marie */
  strcpy(destination[1].ipAddr, "localhost\0");
  destination[1].portNbr = 4949;
  /* John */
  strcpy(destination[2].ipAddr, "localhost\0");
  destination[2].portNbr = 5959;
  /* Parispizza */
  strcpy(destination[4].ipAddr, "localhost\0");
  destination[4].portNbr = 7979;
  /* Lyonpizza */
  strcpy(destination[5].ipAddr, "localhost\0");
  destination[5].portNbr = 8989;

/* END USER CODE ---------------------------------------------------------------------- */


  /* 
     Handle each signal in the BtoM signal list:
     call_conf, call_ind, call_reject, giveReport, initMS, randomNr;
  */
  if (strcmp(signalName, "call_conf") == 0)
  {
    /* call_conf(called_state_t , Mobile_ID_t, BTS_ID_t) */
    receiver = ((yPDP_call_conf)(*xOutSignal))->Param2;
  }
  else if (strcmp(signalName, "call_ind") == 0)
  {
    /* call_ind(Mobile_ID_t [caller], Mobile_ID_t [callee], BTS_ID_t) */
    receiver = ((yPDP_call_ind)(*xOutSignal))->Param2;
  }
  else if (strcmp(signalName, "call_reject") == 0)
  {
    /* call_reject(reason_t, Mobile_ID_t, BTS_ID_T) */
    receiver = ((yPDP_call_reject)(*xOutSignal))->Param2;
  }
  else if (strcmp(signalName, "giveReport") == 0)
  {
    /* giveReport(billing_report_t, Mobile_ID_t, BTS_ID_t) */
    receiver = ((yPDP_giveReport)(*xOutSignal))->Param2;
  }
  else if (strcmp(signalName, "initMS") == 0)
  {
    /* initMS(BTS_ID_t, Mobile_ID_t, IMEI_t) */
    receiver = ((yPDP_initMS)(*xOutSignal))->Param2;
  }
  else if (strcmp(signalName, "randomNr") == 0)
  {
    /* randomNr(NATURAL, Mobile_ID_t) */
    receiver = ((yPDP_randomNr)(*xOutSignal))->Param2;
  }


  /* set destination data */
  strcpy(IPAddr, destination[receiver].ipAddr);
  (*Port) = destination[receiver].portNbr;

  return;  
}
