#ifndef _QMSOCKETSH
#define _QMSOCKETSH

#include "declarations.h"
#include "transmng.h"
// 230603
#include "pgserver.h"

//threads
#include "threads.h"


//process
#include "process.h"

#define PORT 6014 // port for socket connection with the local QMs

// dcadena. Add NOT_connection and NOT_cdb
enum kind_of_QM_message
  {QM_execute, QM_commit, QM_abort, QM_apply_ws_and_commit,
     NOT_exec, NOT_commit, NOT_abort, NOT_connection, NOT_cdb,
     NOT_disconnect, NOT_response, QM_empty, NOT_freeqm, 
     QM_connect, QM_disconnect, QM_reconnect};

typedef struct {
  int tid;
  int idConnection;
  int idPetition;	
  //int last_petition_executed;
  int is_delay_disconnect;
  int send_discon_message;
  int backup_stp;
} QM_message_Disconnect;

typedef struct {
  enum kind_of_QM_message type;
  QM_message_Disconnect discon;
  //hot_uint32 mclientId;   // Use trans.mclientId
  //hot_uint32 last_petition_executed;
  T_Transaction trans;
  hot_uint32 size;
  char buffer[TAMBUFFER /*+ EXTRASIZE - RMEXTRASIZE*/];
} QM_message;

enum kind_QM_state {BUSY, FREE, FREECON};

/*
typedef struct {  
  int qmId;
  Dllist_item_ptr *reuselink;
} QMToken;
*/

typedef struct{
  int stateId;
  char dbname[MAXDBNAME];
  enum kind_QM_state state;    
  Dllist_item_ptr token;
  Dllist_item_ptr linkstate;  
  T_Dllist_ptr linkstatequeue;
}QMState;

typedef struct{
  int stateId;  
  struct itemhDBConns *item;  
}QMStateLink;

//enum kind_of_NOT_message {NOT_commit, NOT_exec};
// this type is not used yet
/*
typedef struct {
  enum kind_of_NOT_message type;
  T_Tid tid;
  T_writeset ws;
  long writeset_length;
} thread_message;
*/

// 230603
//static int sock[MAX_CC];
int sock[MAXQM+1];
// dcadena
// 250603
//void SendMessageToQM(T_Transaction*, QM_message, hot_uint32);
void SendMessageToQM(T_Transaction*, QM_message*);
void SendMessageToQMPriority(T_Transaction*, QM_message*, int);
void StopQMs();
// 280803
//void StartQMs(int, int);
void StartQMs();

#endif
