#ifndef _TRANSACTION_H
#define _TRANSACTION_H


#ifndef _WENGO
#include "types.h"
#include "node.h"
#include "transport.h"
#include "gettimeofday.h"
#include "msgheader.h"
#endif

#ifdef _WENGO
#include "p2pp/common/types.h"
#include "p2pp/node/node.h"
#include "p2pp/net/transport.h"
#include "p2pp/sys/gettimeofday.h"
#include "p2pp/msg/msgheader.h"
#endif


#ifdef _WENGO 
#define _T0 500
#define _T1 500
#define _T2 10
#define _T3 10
#endif

#ifndef _WENGO
#define _T0 500
#define _T1 500
#define _T2 10
#define _T3 10
#endif

class Transport;


enum trans_state {eT_INITIAL=0, eT_TRANS_MSG, eT_WAIT_RESP, eT_TERMINATED, eT_FAILURE};

class Transaction {

private:

  //timer management
  struct timeval m_nTime;
  struct timeval m_eTime;  

  int32 m_iRetrans;

  //transport object
  Transport* m_pobjTransport;
  
  //The buffer into which the data is copied for retransmissions
  uchar m_buf[BUFSIZE];
  int32 m_iSize;

  //Transaction state
  enum trans_state m_State;

public:
  //IP and port to which the transaction was sent
  char8 m_pchIP[IPADDRLEN];
  uint32 m_uiPort;

public:
  uint32 m_uiTransId;       //Transaction Id  
  string m_srcId;           //Source-Id

  //JUGAR variables
  string m_dstId;           //dst-Id
  uchar  m_uchdstId[2*HASHLEN]; 

  enum msgtype m_msgtype;  
  enum reqoritype m_reqoritype;
  transtype m_eTransport;  

public:

  Transaction();
  ~Transaction();

  void setParams(Transport* objTransport, uint32 transId, uchar* srcId, uint32 srcIdLen, enum msgtype tmsg, enum reqoritype treqori, enum transtype eTrans);

  int32 OnTimer();
  int32 OnReceive(struct message& msg);
  int32 OnACK(struct message& msg);


  int32 Send(uchar* buf, int32 size, char8* pchIP, uint32 port);
  string getTransId();

  void setDestId(uchar* dstId, uint32 dstIdLen);
  
};

#endif

