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

#include "table.h"

#ifndef _WENGO
#include "types.h"
#include "bigint.h"
#include "distance.h"
#include "chorddist.h"
#endif

#ifdef _WENGO
#include "p2pp/common/types.h"
#include "p2pp/bigint/bigint.h"
#include "p2pp/distance/distance.h"
#include "p2pp/distance/chorddist.h"
#endif


#include <map>

using namespace std;

class ChordRTable : public Table {

private:
  map <BigInt , list<Internalpeerinfo> > RTable;	//finger = (nodeID + 2^i)mod(2^HASH_SIZE)
  BigInt m_NodeId;						//the Id of the node whose routing table it is.
  uint32 m_HASH_SIZE;					//the size in bytes of the ID. Determines the number of fingers in the table.
  uint32 m_Base;                                        //
  BigInt m_Lastrowid;
 public:
  ChordRTable();
  ~ChordRTable();
  uint32 setParams(uchar*, uint32 HASHSIZE, uint32 BASE);
  uint32 getRow(BigInt lId, BigInt& rowid, int& finger); //sets rowid to the rowid where lId is a candidate.
  uint32 Insert(struct peerinfo stP);			//Inserts the entry in the appropriate row.
  
  bool Iamcloser(BigInt lKey, peerinfo& stP);           //Tests if I am closer to lKey than stP.
  uint32 getNextHop(BigInt lkey, peerinfo&);		//Sets
  //the
  //peerinfo object to the peer which should be the next hop in the lookup for lkey.
  uint32 getNextHop(uchar* peerid, uint32 len, struct peerinfo& stP);
  
  bool isPresent(BigInt bKey, list<Internalpeerinfo>& row );
  uint32 Remove(BigInt lkey);				//Removes the peerinfo object with this lkey.
  uint32 RemovePeer(char* IP, uint32 uiPort);
  uint32 DumpTable(list<peerinfo> &);			//dump the Routing table entries to the peerinfo list.
  uint32 getNumRow(uint32 row);				//Return the number of peerinfo objects for this row (NOT rowid). 
  uint32 getSize();					//Return the total number of peerinfo objects in the table.
  uint32 getRowEntries(uint32 row, list<peerinfo> &);	//Set the list
                                                        //to contain
                                                        //the list of
                                                        //peerinfo
                                                        //objects in
                                                        //this row.

  void Show();

};
#endif

