#ifndef _DECLAH
#define _DECLAH

// ensemble imports
#include "hot_sys.h"
#include "hot_error.h"
#include "hot_msg.h"
#include "hot_ens.h"
#include "hot_thread.h"

#include "hashtable.h"

#define MAX_PETITION_PER_CLIENT 1000000

// set this option for optimistic multicast
// when unset non-optimistic multicast is used
//#define OPTIMISTIC

// set this option for asynchronous clients
// sending messages asynchronously every interval
#define ASYNC
// set this together with SYNC in declarations
// to have multiples asynchronous clients, one per replica
//#define MASYNC

// number of transactions sent
#define N_TXN  1000
// number of warmup transactions
#define  WARMUP 500

// stored procedures identifiers
/*enum ST_PROC_IDS {UPD1, UPD1SQ1, LQ1, UPD3SQ1, UPD6SQ1, SQ1SQ1,
  UPD8, UPD9SQ1, UPD4SQ2, UPD3SQ3, SQ4, SQ1, UPD5};*/

// dcadena
enum ST_PROC_IDS {UPD1, UPD1SQ1, LQ1, UPD3SQ1, UPD6SQ1, SQ1SQ1,
		  UPD8, UPD9SQ1, UPD4SQ2, UPD3SQ3, SQ4, SQ1, UPD5,
		  SQG, UPDG, CDB, BEG, END, ABO, CON, DISCON, INI, 
		  DELAY_DISCON, EARLY_DISCON, DELAY_EARLY_DISCON, CON_RESPONSE, DISCON_RESPONSE, RECON, RECON_RESPONSE};
// SQG : query generic
// UPDG : update generic
// CDB : change database query generic
// BEG : begin transaction
// END : end transaction
// ABO : abort transaction
// CON : connection
// DISCON : disconnectation
// INI: initialize QM Special
// DELAY_DISCON : delay disconnect in QM
// EARLY_DISCON : Connection broken down between driver JDBC and middleware
typedef int bool;

// transaction identifier
typedef hot_uint32  T_Tid;

// type of client
// 0 old client
// 1 pgserver

#define TYPECLIENT 1
#define INTASCHAR 20 /* Hacerlo (sizeof(char)*numdigitos(max(int))) */
#define MAX_TXT 512

/* PGSERVER */
#define TAMBUFFER 2048

/* QMSOCKETS */
#define STPROCS  256 // Max stored procedures
#define COMMANDS 256 // Max commands per stored proc
//Replace for MAX_TXT #define MAXTXT 200  // Max text per command
#define PARAMS 256   // Max params per command
#define MAXDBNAME MAX_TXT //Max lenght database name 100
#define MAXTABLENAME MAX_TXT //Max lenght table name 100
#define TAMBUFFERJDBC 512
#define LENGTHSQLCODE 3

#define EOF_SIZE 1

#define MAXSERVERPATH 200 //Max lenght table name

// write set type
#define MAX_WS 900
#define EXTRASIZE 0
#define RMEXTRASIZE 0

typedef char  T_writeset[MAX_WS /*+ EXTRASIZE - RMEXTRASIZE*/];

#define MAX_CC 900  // max. number of cc


//#define TEST_ADDR "234.5.6.7"
//#define TEST_PORT 3456
char *TEST_ADDR;
int TEST_PORT;

//int MAXQM = 0;
//int MAXCLIENTS = 0;

#define MAXQM 30 // 0 < MAXQM < 31 with postgres 7.2
#define MAXCLIENTS 100 // Maximum number of clients per replica

enum message_kind {OPT, TO, COMMIT, QUERY, REPLY, VIEW, CONNECTION, RESPONSE, INIT, DISCOVERY, DISC_RESP};


// socket to comunicate Groupcomm whith Threads
int sockResGC[MAXCLIENTS/*MAXQM*//*JSalas -1*/];

// Hash table
/*HASHTABLE hMiddler;
HASHTABLE hCC;
HASHTABLE hPetition;*/

// Hash table
struct hashtable hMiddler;
struct hashtable hCC;
//struct hashtable hClient;
struct hashtable hPetition;
struct hashtable hOrderTotal;
struct hashtable hOrderFifo;
struct hashtable hDBConns;
//struct hashtable hQMClient;

// transaction information
typedef struct{
  char dbname[MAXDBNAME];	
  char sqlcode[MAX_TXT /*+ EXTRASIZE - RMEXTRASIZE*/];      // transaction
  T_Tid tid;                  // transaction identifier
  //hot_uint32 qid;           // 250603 T_trans is executed by QM qid
  hot_endpt_t endpoint;       // client identifier
  hot_uint32 isQuery;         // indicates if the transaction is a query
  char typeSQL;               // indicate the type of sql that the transaccion is executing
  hot_uint32 noCC;            // number of conflict classes of the transaction
  // 280803 [MAX_CC] por *
  hot_uint32 *transCC; // conflict classes of the transaction
} T_Trans;

typedef struct{
  hot_uint32 type;         // value of message_kind
  hot_uint32 mclientId;    // dcadena. Thread id
  hot_uint32 qmId;    // dcadena. Thread id
  T_Trans trans;           // the transaction
  // 080903
  hot_uint32 idConnection; // connection id
  hot_uint32 idPetition;   // petition id
  hot_endpt_t endpoint;    // client identifier
} Opt_M;                   // these messages are also used for queries


typedef struct{
  hot_uint32 type;      // value of message_kind
  T_Tid tid;            //  transaction identifier
  hot_uint32 mclientId;      // dcadena. Thread id
  hot_endpt_t endpoint; // client identifier
}To_M;

typedef struct{
  hot_uint32 type;            // value of message_kind
  T_Tid tid;                  //  transaction identifier
  hot_uint32 mclientId;      // dcadena. Thread id
  hot_endpt_t endpoint;       // client identifier
  hot_uint32 writeset_length; // writeset length
  T_writeset writeset;        //
  hot_uint32 recordsUpdates;   // number of records updated, inserted, deleted...
  long response_length; // length of response
  char response[TAMBUFFERJDBC]; //response
}Commit_M;

typedef struct{
  hot_uint32 type;                 // value of message_kind
  hot_endpt_t endpoint;           //  client address
  char result[MAX_TXT /*+ EXTRASIZE - RMEXTRASIZE*/];           // check whether this ok!!!!
}Reply_M;

typedef struct{
  hot_uint32 type;
  char msg[MAX_TXT /*+ EXTRASIZE - RMEXTRASIZE*/];
}Discovery_m;

typedef union {
  hot_uint32 type;    // value of message_kind
  Opt_M opt;
  To_M  to;
  Commit_M commit;
  Opt_M query;
  // 030903
  //char *discovery_m;
  Discovery_m discovery;
}T_Mess;

typedef struct{
  int early; //Only for Discon transactions
  int connection_petition_id; //Only for Discon transactions
  char dbname[MAXDBNAME];
} Conns_M;

bool isSmallerId(T_Tid, T_Tid);
bool isEqualId(T_Tid, T_Tid);
#endif

