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

#include "table.h"

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

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


class BambooRTable : public Table {

 private:
  map<BigInt, map<BigInt, list< Internalpeerinfo> > > m_RT;
  BigInt m_NodeId;

  uint32 m_HASH_SIZE;
  uint32 m_BASE;

  int32 m_iSize;
  BambooDist m_BambooDist;
  
  //initiliaze m_Max to a large number
  BigInt m_Max;

 public:
  
  BambooRTable();
  ~BambooRTable();
  
  void setNodeId(uchar* peerid, int32 len, int32 base);

  //virtual functions
  uint32 getSize();
  uint32 getNextHop(BigInt, peerinfo&);
  int32 Insert(struct peerinfo& stP, int32 updateKA);
  int32 Insert(struct peerinfo& stP, int32 updateKA, uint16 stunPort);
  int32 Insert(list<peerinfo>& lstP, int32 updateKA);  
  
  uint32 removePeer(char8* pchIP, uint32 port);

  virtual int32 getTable(list<peerinfo> & retval);
  uint32 getTableLevel(list<peerinfo>& retval, uchar level);
  uint32 getPeerLevelDigit(uint32& level, uint32 digit, peerinfo & stP);
  uint32 OnPLookupRequest(peerinfo strequester, list<peerinfo>& reply);
  uint32 getNextHop(uchar* peerid, uint32 len, peerinfo& nextP);
  
  uint32 LocalTuning(uchar level, list<peerinfo> table, peerinfo stP);
  void Show();

  int32 OnTimer();

  void Disconnect(); //called when destroying node object
};


class BambooNTable : public Table {

 private:
  uint32 m_k;
  map< BigInt, Internalpeerinfo > m_NT;
  uint32 m_iSize;
  uint32 m_HASH_SIZE;
  uint32 m_BASE;
  BigInt m_NodeId;
  BambooDist m_BambooDist;
 public:

  BambooNTable();
  ~BambooNTable();
  void setNodeId(uchar* peerid, int32 len, uint32 base1, uint32 base);

  uint32 getSize();
  uint32 getNextHop(BigInt, peerinfo&);
  virtual int32 getTable(list<peerinfo > &lstP);
  int32 Insert(struct peerinfo& stP, int32 updateKA);
  int32 Insert(list<peerinfo>& lstP, int32 updateKA);

  uint32 getNextHop(uchar* peerid, uint32 len, peerinfo& nextP);
  void Show();

  int32 OnTimer();

  void Disconnect();  //called when destroying node object
};

#endif
