/*
 * Copyright (C) 2008 The Trustees of Columbia University in the City of New York. 
 * All rights reserved.
 *
 * See the file LICENSE included in this distribution for details.
 */

#ifndef _CHORDPEER_H
#define _CHORDPEER_H

#include "peer.h"

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

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

class ChordPeer : public Peer {

  //Insert and lookup helper class
  //friend class ILHelper;

private:
  ChordRTable* m_pChordRT;
  ChordNTable* m_pChordNT;
  //timer management
  struct timeval m_tKA;
  struct timeval m_tU;

private:
  int32 OnRequest(char* pchIP, int32 port, transtype tt);
  int32 OnResponse(char* pchIP, int32 port, transtype tt);
  void OnTransactionTimeout(Transaction* pobjTrans);

  void setKeepAliveTimer();
  int32 isKAExpired();
  void setUpdateTimer();
  int32 isUExpired();
  
  uint32 getNextHop(uchar* peerid, uint32 len, struct peerinfo& stP);

public:
  ChordPeer();
  ~ChordPeer();

  void Initialize();
  void OnTimer();
  void OnReceive(uchar* buf, int len, char* pchIP, int32 port, transtype tt);

  void ShowRT();
  void ShowNT();
  void ShowRO();

  int32 Insert (char8* pchKey, int32 klen, char8* pchValue, int32 vlen, void* (*)(void*, int32), void* pApp);
  int32 RLookup (char8* pchKey, int32 klen, void* (*)(void*, int32), void* pApp, void * Conference );
  int32 PLookup (char8* pchKey, int32 klen, void* (*)(void*, int32), void* pApp, void * Conference);

};

#endif
