void CAgentService::LoadVariable() { VAROBJECT *pObject; ENVENTRY env_t; char szBuffer[100]; long addr=0; int n; // Initialize Variable VARAPI_Initialize(VARCONF_FILENAME, m_Root_node, TRUE); GetLocalAddress(szBuffer); addr = inet_addr(szBuffer); pObject = VARAPI_GetObjectByName("ethIpAddr"); if (pObject != NULL) memcpy(pObject->var.stream.p, &addr, sizeof(IPADDR)); memset(&env_t, 0, sizeof(ENVENTRY)); GetEnvInfo(env_t.envKernelVersion, env_t.envGccVersion, env_t.envCpuName, &env_t.envCpuMips); pObject = VARAPI_GetObjectByName("envKernelVersion"); if (pObject != NULL) strcpy(pObject->var.stream.p, env_t.envKernelVersion); pObject = VARAPI_GetObjectByName("envGccVersion"); if (pObject != NULL) strcpy(pObject->var.stream.p, env_t.envGccVersion); pObject = VARAPI_GetObjectByName("envCpuName"); if (pObject != NULL) strcpy(pObject->var.stream.p, env_t.envCpuName); pObject = VARAPI_GetObjectByName("envCpuMips"); if (pObject != NULL) pObject->var.stream.u32 = HostToLittleShort(env_t.envCpuMips); /** Rewriter(Q) : testmode가 무엇인지 확인해야 함 */ GetFileValue("/app/conf/testmode", &n); m_bGprsPerfTestMode = (n > 0) ? TRUE : FALSE; if(GetFileString("/app/conf/mcusystype", szBuffer, sizeof(szBuffer), "OUTDOOR", TRUE) && strncasecmp(szBuffer,"INDOOR",strlen("INDOOR"))==0) { m_nSystemType = MCU_TYPE_INDOOR; } /*-- Pulse Meter Type을 읽어온다 --*/ LoadSensorType(); /*-- Patch 정보를 읽어온다 --*/ LoadPatchList(); /*-- Patch 5-1949 --*/ #if defined(__PATCH_5_1949__) m_nDebugLevel &= (*m_pbPatch_5_1949 ? 0x00 : 0xFF); #endif }
DWORD ServiceExecutionThread(LPDWORD param) { #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread\n"); #endif if (IntializeSystemInfo() == FALSE) { return IC_NO_INITIALIZE; } WORD wVersionRequested = MAKEWORD(1, 1); WSADATA wsaData; WSAStartup(wVersionRequested, &wsaData); SOCKET udpSocket = INVALID_SOCKET; int result = IC_NOERROR; bool showHelp = false; do { g_killServiceEvent = CreateEvent(0, TRUE, FALSE, 0); if (!g_killServiceEvent) { OutputError(L"CreateEvent fails! (%d)", GetLastError()); result = IC_EVENT_CREATE_FAIL; break; } wstring apiKey; wstring envInfo; vector<int> intervalTimes; ConnectionInfo connection; g_debugMode = GetDebugMode(g_argc, g_argv); if (cmdOptionExists(g_argv, g_argv + g_argc, L"-h") == true || cmdOptionExists(g_argv, g_argv + g_argc, L"/h") == true) { showHelp = true; break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-unreg") == true) { DoUnregistration(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-start") == true) { DoStartService(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-stop") == true) { DoStopService(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-update") == true) { ProcessLatestUpdate(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-restart") == true) { RestartService(); break; } apiKey = GetApiKey(g_argc, g_argv); envInfo = GetEnvInfo(g_argc, g_argv); intervalTimes = GetIntervalTime(g_argc, g_argv); if (apiKey.length() == 0) { OutputError(L"NO ApiKey\n"); result = IC_NO_APIKEY; showHelp = true; break; } if (envInfo.length() == 0) { OutputError(L"NO AgentID Info\n"); result = IC_NO_AGENTIDINFO; showHelp = true; break; } string address = GetHostAddress(g_argc, g_argv, connection); struct hostent *host = gethostbyname(address.c_str()); if (host == nullptr) { OutputError(L"Can't resolve host address: %s\n", address.c_str()); result = IC_NO_RESOLVE_HOSTADDR; break; } SetupHostPort(g_argc, g_argv, connection); if (cmdOptionExists(g_argv, g_argv + g_argc, L"-regservice") == true) { DoRegistration(apiKey, envInfo, address, connection.Getport(), intervalTimes); break; } struct sockaddr_in remoteServAddr; remoteServAddr.sin_family = host->h_addrtype; memcpy((char *)&remoteServAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); remoteServAddr.sin_port = htons((u_short)connection.Getport()); /* socket creation */ udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpSocket == INVALID_SOCKET) { OutputError(L"socket failed with error: %ld\n", WSAGetLastError()); break; } wstring appVersion = GetAppVersion(g_moduleFilePath.c_str(), NULL, NULL, NULL, NULL); OutputConsole(L"(%s) ServiceExecutionThread - data collect thread - start\n", appVersion.c_str()); { thread processCpuMemThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]() { ProcessCpuMemInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[0]); }); thread processDiskThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]() { ProcessDiskInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[1]); }); thread updateThread([]() { DWORD oneday = 1000 * 60 * 60 * 24; while (true) { ProcessLatestUpdate(); if (::WaitForSingleObject(g_killServiceEvent, oneday) == WAIT_TIMEOUT) { continue; } break; } }); if (g_isConsoleApp == TRUE) { printf("Press any key to exit...\n"); getchar(); } else { #if _DEBUG ::OutputDebugString(L"Service thread - WaitForSingleObject...\n"); #endif WaitForSingleObject(g_killServiceEvent, INFINITE); } #if _DEBUG ::OutputDebugString(L"Service thread waiting...\n"); Sleep(1000); #endif if (processCpuMemThread.joinable() == true) { processCpuMemThread.join(); } if (processDiskThread.joinable() == true) { processDiskThread.join(); } if (updateThread.joinable() == true) { updateThread.join(); } #if _DEBUG ::OutputDebugString(L"All Service-threads exited...\n"); #endif } ::SetEvent(g_killSafeExitEvent); #if _DEBUG ::OutputDebugString(L"Service thread detached\n"); #endif } while (false); #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ending\n"); #endif if (udpSocket != INVALID_SOCKET) { closesocket(udpSocket); } if (showHelp == true) { ShowHelp(); } WSACleanup(); #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ended\n"); #endif return result; }