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

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

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

#include <map>

using namespace std;

class ChordNTable : public Table {

private:
  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;
  list<Internalpeerinfo> m_Succ_list;			//successor list;
  list<Internalpeerinfo> m_Pred_list;			//predecessor list
public:
  ChordNTable();
  ~ChordNTable();
  uint32 setParams(uchar*, uint32, uint32);             //peerid, Hashsize, base
  uint32 Insert(struct peerinfo objP);
  uint32 Insert_Pred(struct peerinfo objP);               //Inserts the entry in the predecessor list.
  uint32 Insert_Succ(struct peerinfo objP);               //Inserts the entry in the successor list.
  bool Iamcloser(BigInt, peerinfo&);
  bool isPresent(Internalpeerinfo stIkey, list<Internalpeerinfo>& n_list );
  uint32 getNextHop(uchar* peerid, uint32 len, struct peerinfo& stP);  //Return 0 if stP is valid, return 1 if Iamcloser.
  uint32 getNextHop(BigInt, peerinfo&);	                 //Use the above interface.
  uint32 Remove_Succ(BigInt key);                        //Removes the peerinfo object with this key.
  uint32 Remove_Pred(BigInt key);                        //Removes the peerinfo object with this key.
  uint32 RemovePeer(char* IP, uint32 uiPort);
  uint32 DumpSuccList(list<peerinfo> &);                 //dump the Successor list entries to the peerinfo list.
  uint32 DumpPredList(list<peerinfo> &);                 //dump the Predecessor list entries to the peerinfo list.
  uint32 getSize();
  void Show();
};
#endif
