/**
 * EH_ToolKit.h
 *
 * Copyright by AirDream Co., Ltd. 2010-2011 All right reserved
 *
 * Description:
 * Created on:2011-8-25
 * Modified on:2011-8-25
 *
 */

#ifndef EH_TOOLKIT_H_
#define EH_TOOLKIT_H_

#include <stdint.h>
#include <sstream>
#include <string>
#include <vector>
#include <stdio.h>

#include "ace/Message_Block.h"
#include "ace/Process_Mutex.h"
#include "ace/SOCK_Stream.h"
#include "ace/UUID.h"
#include "ace/OS_NS_time.h"


#include "json/writer.h"
#include "json/reader.h"
#include "json/value.h"

#define COMPARE_VER_MORE_THAN   1
#define COMPARE_VER_LESS_THAN   2
#define COMPARE_VER_EQ_THAN     3

using namespace std;

class EH_ToolKit {
public:
    static void PrintMsg(const uint8_t *msg, uint16_t msg_len,bool isDebug);
    static void Split(const string& src, const string& separator, vector<string>& dest);
    static string Trim(string& s);
    static unsigned short CheckSum(const unsigned char* in,const int len);

    static int StringToHex(const char*input, unsigned char*output,const int bufLen);
    static int StringToHexNoSpace(const char* input, unsigned char* output,const int bufLen);
    static int HexToStringNoSpace(const char* input, char* output, int len);
    static int HexToString(const char* input, char* output, int len,const char* sep);
    static int StringToInt(const string input);
    static uint StringToUInt(const string input);
    static double StringToDouble(const string input);
    static string IntToString(const int input);
    static string UIntToString(const uint input);
    static string UInt64ToString(const ACE_UINT64 input);
    static string DoubleToString(const double input);

    static string JsonToString(const EH::Json::Value& json);
    static string CreateRespJsonMsg(string opcode,string arg,int status,string nodeid = "*");
    static string CreateRespJsonMsgWithErrorCode(string opcode,string arg,int status,string nodeid = "*",
    		                                                                               int error=-1);

    static string CreateReqJsonMsg(string opcode,string arg,string requester,string nodeid = "*");
    static string CreateReqJsonMsgWithReqId(string opcode,string arg,string requester,string nodeid = "*",string reqId = "");
    static string CreateReqJsonMsgWithArgJson(string opcode,EH::Json::Value arg,string requester,string nodeid = "*",string reqId = "");

    static bool CompareNoCase(string str1,string str2);
    static bool HasSpareProcess(const string processName);
    static bool CheckRoot();
    static const char* GetLocalIP(string interface="NOT_SET");

    static void BuildAbsTimeOut(ACE_Time_Value* timeout,int timeoutMS);

    static void ShowHelpInfo();
    static void ShowVersionInfo(string proc,string version);
    static int GetVersionInfo(string version, int& majorV, int& minorV, int& betaV, bool hasVersionPrefix=true);
    static int CompareVersion(string checkVersion, string baseVersion);

    static uint32_t CreateCrc32(const uint8_t *msg, uint32_t len);
    static uint16_t CreateCrc16(const uint8_t *msg, uint32_t len);
    static uint16_t CrcByte(uint16_t crc, uint8_t b);
    static uint16_t Crc16(void* buf, uint8_t len);


    static bool Ping(string destIP);
    static int GetCpuCoreNum();
    static bool MatchVersion(string version,string pattern);
    static void SetCoredumpMsgDump();
    static void PrintBacktrace(int signum);
    static string GetUUID();
    static string GetCurrentTime();
    static ACE_UINT64 getSystemTimeInMs();
    static int DoSystem(string cmd);
    static string GetTimestamp();

    static string GetMac();
    static string GetMacNoSpace();
private:
    static string GetFileContent(string filePath, bool& error);

private:
    static uint32_t ms_uiCrc32Tab[256];
};

#endif /* EH_TOOLKIT_H_ */

