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

#ifndef _WENGO
#include "types.h"
#include "node.h"
#include "peer.h"
#include "msgheader.h"
#endif

#ifdef _WENGO
#include "p2pp/common/types.h"
#include "p2pp/msg/msgheader.h"
#include "p2pp/bigint/bigint.h"
#include "p2pp/msg/node.h"
#include "p2pp/msg/peer.h"
#endif


class Client : public Peer {
  
 private:
  uint32 m_uiTransId;
  list<Internalpeerinfo> m_CP;
  uint32 m_CPsize;

  //connectivity maintenance timers
  struct timeval m_tCP;
  uint32 m_CPrefresh; //check connected peers list every CPrefresh seconds if sending a keepalive is needed
  uint32 m_CPka;      //base keep-alive interval
  uint32 m_CPrand;    //random interval


 private:
  void OnTransactionTimeout(Transaction* pobjTrans);

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

  void setKATimer(struct timeval& stT);
  void setCPrefreshtimer(struct timeval& stT);
  int32 isTimerExpired(struct timeval& stT);
  

 public:
  Client();
  virtual ~Client();
  
  virtual void Initialize();
  virtual void OnTimer();
  virtual int32 OnReceive(uchar* buf, int len, char* pchIP, int32 port, transtype tt);

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

  virtual int32 RemovePeer(const char* pchIP, const uint32 port);

  virtual void ShowRT();
  virtual void ShowNT();
  virtual void ShowRO();
  virtual void ShowCP();

  virtual void ShowCPlist();  
  virtual void ShowRelayServerList();  

  virtual int32 GetSIPObject(struct resourceobject& stRo) ;
  virtual void UpdateRelayList(list<peerinfo>& lstP);
  void ShowRelayAllocations();

  int32 UpdateCPList(Internalpeerinfo& stP, bool pushfront);
  int32 UpdateCPList(list<peerinfo>& lstP);
};

#endif
