/*
 * 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 _KADPEER_H
#define _KADPEER_H

#include "peer.h"

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

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

class KadPeer : public Peer {

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


private:
  KadRTable* m_pKRT;  

private:

  int32 OnACK(char* pchIP, int32 port, transtype tt);
  int32 OnRequest(char* pchIP, int32 port, transtype tt);
  int32 OnResponse(char* pchIP, int32 port, transtype tt);
  int32 OnIndication(char* pchIP, int32 port, transtype tt);

  void OnTransactionTimeout(Transaction* pobjTrans);

  uint32 getNextHop(uchar* peerid, uint32 len, struct peerinfo& stP);


protected:
  //timer management
  struct timeval m_tTT;

  void setTTTimer();
  int32 isTTExpired();

public:
  KadPeer();
  ~KadPeer();

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


  //Insert in routing or neighbor table  
  int32 Insert(struct peerinfo& stP, int32 updateKA);
  int32 Insert(list<peerinfo>& lstP, int32 updateKA);
  int32 Insert(struct peerinfo& stP, int32 updateKA, uint16 stunPort);


  //Debugging functions for checking the local state
  void ShowRT();
  void ShowNT();
  void ShowRO();
  void ShowTURNAlloc();

  //Get SIP object (Default return value =0, i.e. no SIP object)
  virtual int32 GetSIPObject(struct resourceobject& stRo);
  virtual void UpdateRelayList(list<peerinfo>& lstP);

  virtual int GetStunServerPort();
};

#endif
