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

#include "peer.h"

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

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

#include <list>
using namespace std;

#define ROUTE_TIMER 120
#define RESOURCE_TIMER 45

class Diagnostics : public Peer {

private:
  struct peerinfo m_stPeer;

private:
  void ComposeDiagnosticsToSites(enum diag_type diagtype);
  void ComposeDiagnosticsToBoot();
  void AppendPeerList(list<peerinfo>& lstP);

  char8 m_ID[255];
  list<message> m_DL;

  //for timer management
  struct timeval m_tRoute;       //send a get routing table request
  struct timeval m_tResource;    //send a neighbor table request

  FILE *m_pData;
  char m_Diagnosticsfile[ID_MAX];

public:
  Diagnostics();
  virtual ~Diagnostics();

  void setDiagnosticsFile(const char *filename);

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

  int32 Insert (char8* pchKey, int32 klen, char8* pchValue, int32 vlen, void* (*)(void*, int32), void* pApp);
  int32 RLookup (char8* pchKey, int32 klen, void* (*)(void*, int32), void* pApp );
  int32 PLookup (char8* pchKey, int32 klen, void* (*)(void*, int32), void* pApp );

  int32 isRouteTimerExpired();
  void setRouteTimer();
  void SaveRouteData();

  int32 isResourceTimerExpired();
  void setResourceTimer();
  void SaveResourceData();

  int32 InsertRT(char* pchIP, int32 port, transtype tt, list<message>::iterator& mitr);
  int32 InsertNT(char* pchIP, int32 port, transtype tt, list<message>::iterator& mitr);
  int32 InsertRes(char* pchIP, int32 port, transtype tt, list<message>::iterator& mitr);

  
};

#endif

