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

//do not interchange the ordering of ID_MAX
#define ID_MAX 100

#ifndef _WENGO
#include "msgheader.h"
#include "types.h"
#include "nodetypes.h"
#include "transport.h"
#include "p2pconfig.h"
#include "table.h"
//#include "chordrtable.h"
//#include "chordntable.h"
#include "sysutils.h"
#endif

#include "transaction.h"

#ifdef _WENGO
#include "p2pp/msg/msgheader.h"
#include "p2pp/net/transport.h"
#include "p2pp/p2pconfig/p2pconfig.h"
#include "p2pp/table/table.h"
//#include "p2pp/table/chordrtable.h"
//#include "p2pp/table/chordntable.h"
#include "p2pp/common/types.h"
#include "p2pp/net/transport.h"
#include "p2pp/sys/sysutils.h"
#endif


enum NodeType {eCLIENT=0, ePEER=1, eBOOTSTRAP_SERVER=2, eDIAGNOSTIC_SERVER, eAUTO};


class P2PConfig;
class Transport;
class Transaction;


struct port_trans {
  uint16 port;
  transtype tt;  
  addrtype ht;
};


class Node {
  
 protected:
  //Transaction map
  struct message m_msg;
  uchar m_buf[BUFSIZE];

  //Config file parameters
  uint16 m_TCP_PORT; //TCP listening port
  uint16 m_UDP_PORT; //UDP listening port
  enum p2palg m_P2P; //P2P algorithm
  enum halg m_HASH_ALG;
  int32 m_HASH_SIZE; //size in bytes.
  int32 m_BASE;      //base 2, 4, 16, 32 etc.
  int32 m_REC;       //recursive / iterative flag recursive=1, iterative=0
  int32 m_PARALLEL;
  
  int32 m_NODE_TYPE;
  
  char8 m_BOOT_IP[IPADDRLEN];
  uint16 m_BOOT_PORT;
  uchar m_USERID[ID_MAX];

 public:
  static uchar  m_ID[ID_MAX];

  //Transport object
  static Transport* m_pobjTransport;

  //Routing and neighbor table manipulation
  static list<peerinfo> m_tempNT;		//Temporary NT. Used during maintainance.
  static list<peerinfo> m_tempRT;		//Temporary RT. Used during maintainance.
  static Table* m_pRT;			        //Pointer to the local Routing table
  static Table* m_pNT;		                //Pointer to the local Neighbor table.

  //Application call back
  static void* m_pApp;
  static void* (*appCallBack)(void*, int32);
  static void* (*onTimerCallback)(void*);

  //transaction map
  static map<string, Transaction*> m_TMap;

 public:
  Node();
  virtual ~Node();
  
  void setID(char* pchID); 
  void setUSERID(uchar* pchUID);
  void setTCPPort(int32 iPort);
  void setUDPPort(int32 iPort);
  void setP2P(char* pchP2P);
  p2palg getP2P();
  void setHashAlg(char* pchAlg);
  void setHashSize(int32 iSize);
  void setRec(int32 iRec);
  void setParallel(int32 iParallel);
  static void setTransport(Transport* pobjTransport);
  Transport* getTransport();
  char* getUSERID();
  void setMode(char* pchMode);
  int32 getNodeType();

  void setConfigParams(const P2PConfig& objConfig);


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

  virtual int32 PublishObject (char8* pchKey, uint32 klen, char8* pchValue, uint32 vlen, transtype tt, void* (*)(void*, int32 respcode), void* pApp ) {return 1;}
  virtual int32 LookupObject (char8* pchKey, uint32 klen, transtype tt, void* (*)(void*, int32 respcode), void* pApp, void *pConference ) {return 1;}
  virtual int32 LookupUser (char8* pchKey, uint32 klen, transtype tt, void* (*)(void*, int32 respcode), void* pApp, void* Conference ) {return 1;}
  virtual int32 LookupSIP (char8* pchKey, uint32 klen, transtype tt, void* (*)(void*, int32 respcode), void* pApp, void* Conference ) {return 1;}

  //remove peer from the appropriate list
  virtual int32 RemovePeer(const char* pchIP, const uint32 port) {return 1;}

  //Debugging functions for remotely obtaining state of a node
  virtual int32 GetRT(char8* pchIP, uint32 ilen, uint16 port, transtype tt, void* (*)(void*, int32 respcode), void* pApp) {return 1;}
  virtual int32 GetNT(char8* pchIP, uint32 ilen, uint16 port, transtype tt, void* (*)(void*, int32 respcode), void* pApp) {return 1;}
  virtual int32 GetRO(char8* pchIP, uint32 ilen, uint16 port, transtype tt, void* (*)(void*, int32 respcode), void* pApp) {return 1;}
  virtual int32 GetRelayList(char8* pchIP, uint32 ilen, uint16 port, transtype tt, void* (*)(void*, int32 respcode), void* pApp) {return 1;}
  virtual int32 GetTurnAlloc(char8* pchIP, uint32 ilen, uint16 port, transtype tt, void* (*)(void*, int32 respcode), void* pApp) {return 1;}

  //Change user ID
  virtual int32 ChangeUserID(char8* pchID, uint32 ilen, void* (*)(void*, int32 respcode), void* pApp) {return 1;}

  //Set specific values such as bw, cpuutil, memory for program and machine
  virtual int32 Set(char8* pchIP, uint32 ilen, uint16 port, transtype tt, struct nodeval& stnv, int32 isSet, void* (*)(void*, int32 respcode), void* pApp) {return 1;}

  //Debugging functions for checking the local state
  virtual void ShowRT() {};
  virtual void ShowNT() {};
  virtual void ShowRO() {};  
  virtual void ShowCP() {};  

  virtual void ShowCPlist() {};  
  virtual void ShowRelayServerList() {};  
  virtual void ShowTURNAlloc() {};
  virtual void ShowRelayAllocations() {};
  
  virtual int32 Disconnect(void* (*callback)(void*, int32 respcode), void* pApp);

  static void setCallBack(void* (*callback)(void*, int32), void* pApp);
  static void setOnTimerCallback(void* (*callback)(void*) );
};


#endif
