#ifndef _ILHELPER_H
#define _ILHELPER_H

#ifndef _WENGO
#include "types.h"
#include "msgheader.h"
#include "bigint.h"
#endif

#ifdef _WENGO
#include "p2pp/common/types.h"
#include "p2pp/msg/msgheader.h"
#include "p2pp/bigint/bigint.h"
#endif

#include <map>
using namespace std;


struct ILObject {
  void* pApp;
  void* (*callback)(void*, int32);
  char8* pchKey;
  int32 kLen;
  char8* pchValue;
  int32 vLen;
  uchar pchHash[HASHLEN];
  void* Conference;
  uint32 uiTransId;
  char retIP[IPADDRLEN];

public:
  ILObject();
  ~ILObject();
};


struct InternalResourceObject {

  uchar pchValue[500]; //TODO: change fixed length
  int32 vLen;

  struct timeval tExpires;

};


class Peer;

class ILHelper {

 private:
  Peer* m_pPeer;
  map<uint32, ILObject*> m_IL; //map of outstanding transactions

  map<BigInt, InternalResourceObject> m_objRO;

 public:
  ILHelper();
  ~ILHelper();
  
  void setParams(Peer* pPeer);

  int32 InsertObject(struct message& msg, uint32 uiExpires);
  int32 InsertObject(uchar* pchKey, char8* pchValue, int32 vLen, uint32 uiExpires);

  int32 OnInsertRequest(struct message& msg);

  int32 OnPLookupRequest(struct message& msg, char* pchIP, int32 port, transtype tt);
  int32 OnRLookupRequest(struct message& msg, char* pchIP, int32 port, transtype tt);

  int32 OnInsertResponse(struct message& msg);

  int32 OnRLookupResponse(struct message& msg);
  int32 OnPLookupResponse(struct message& msg);

  int32 Insert(char8* pchKey, int32 kLen, char8* pchValue, int32 vLen, void* (*callback)(void*, int32),  void* pApp);
  int32 RLookup(char8* pchKey, int32 kLen, void* (*callback)(void*, int32), void* pApp, void* pConference );
  int32 PLookup(char8* pchKey, int32 kLen, void* (*callback)(void*, int32), void* pApp, void* Conference);

  int32 OnTimer();
  int32 OnTransactionTimeout(uint32 uiTransId, char* pchIP, int32 port);

  void ShowRO();


};

#endif
