summaryrefslogtreecommitdiff
path: root/src/eHostInfoMgr.cpp
blob: 81f423dd8623a768a878779981cd4638881e8fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * eHostInfoMgr.cpp
 *
 *  Created on: 2013. 11. 14.
 *      Author: kos
 */

#include <sstream>
#include <fstream>

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include "eHostInfoMgr.h"
#include "ePreDefine.h"

#define KILLPROC(PID) { kill(PID, SIGINT); }

using namespace std;
//-------------------------------------------------------------------------------

#ifdef DEBUG_LOG
//#undef LOG
//#define LOG(X,...) { do{}while(0); }
#endif
//-------------------------------------------------------------------------------

eHostInfoMgr::eHostInfoMgr(std::string aName, int aInfoCount)
	: mInfoCount(aInfoCount)
{
	mSemId = 0;
	mSemName = "/" + aName + "_sem";

	mShmFd = 0;
	mShmSize = sizeof(eHostInfo) * aInfoCount;
	mShmName = "/" + aName + "_shm";

	mShmData = 0;
}
//-------------------------------------------------------------------------------

eHostInfoMgr::~eHostInfoMgr()
{
}
//-------------------------------------------------------------------------------

bool eHostInfoMgr::IsTerminated(std::vector<int>& aList, int aPid)
{
	for (int i = 0; i < aList.size(); ++i) {
		if (aList[i] == aPid) {
			return false;
		}
	}
	return true;
}
//-------------------------------------------------------------------------------

bool eHostInfoMgr::Init()
{
	if (Open() == false) {
		return false;
	}
#ifdef DEBUG_LOG
	LOG("shm-info : fd [%d], name [%s], size [%d], data [%p]", mShmFd, mShmName.c_str(), mShmSize, mShmData);
	LOG("sem-info : id [%p], name [%s]", mSemId, mSemName.c_str());
#endif
	std::vector<int> pidlist = FindPid("transtreamproxy", 0);
#ifdef DEBUG_LOG
	Dump("before init.");
#endif
	Wait();
	for (int i = 0; i < mInfoCount; i++) {
		if (mShmData[i].pid != 0) {
			int pid = mShmData[i].pid;
			if(IsTerminated(pidlist, pid)) {
				Erease(pid);
			}
		}
	}
	Post();
	return true;
}
//-------------------------------------------------------------------------------

bool eHostInfoMgr::Register(std::string aIpAddr, int aPid)
{
	bool result = false;

	Wait();
	for (int i = 0; i < mInfoCount; i++) {
		if (mShmData[i].pid == 0) {
			result = true;
			mShmData[i].pid = aPid;
			strcpy(mShmData[i].ip, aIpAddr.c_str());
			break;
		}
	}
	Post();
#ifdef DEBUG_LOG
	Dump("after register.");
#endif
	return result;
}
//-------------------------------------------------------------------------------

void eHostInfoMgr::Unregister(std::string aIpAddr)
{
	Wait();
	for (int i = 0; i < mInfoCount; i++) {
		if (strcmp(mShmData[i].ip, aIpAddr.c_str()) == 0) {
			memset(mShmData[i].ip, 0, 16);
			mShmData[i].pid = 0;
			break;
		}
	}
	Post();
#ifdef DEBUG_LOG
	Dump("after unregister.");
#endif
}
//-------------------------------------------------------------------------------

void eHostInfoMgr::Erease(int aPid)
{
	for (int i = 0; i < mInfoCount; i++) {
		if (mShmData[i].pid == aPid) {
#ifdef DEBUG_LOG
			LOG("erase.. %s : %d", mShmData[i].ip, mShmData[i].pid);
#endif
			memset(mShmData[i].ip, 0, 16);
			mShmData[i].pid = 0;
			break;
		}
	}
}
//-------------------------------------------------------------------------------

void eHostInfoMgr::Update(std::string aIpAddr, int aPid)
{
#ifdef DEBUG_LOG
	Dump("before update.");
#endif
	Wait();
	for (int i = 0; i < mInfoCount; i++) {
		if (strcmp(mShmData[i].ip, aIpAddr.c_str()) == 0) {
			KILLPROC(mShmData[i].pid);
			memset(mShmData[i].ip, 0, 16);
			mShmData[i].pid = 0;
		}
	}
	Post();
	Register(aIpAddr, aPid);
}
//-------------------------------------------------------------------------------

int eHostInfoMgr::IsExist(std::string aIpAddr)
{
	int existCount = 0;

	Wait();
	for (int i = 0; i < mInfoCount; i++) {
		if (strcmp(mShmData[i].ip, aIpAddr.c_str()) == 0) {
			existCount++;
		}
	}
	Post();

	return existCount;
}
//-------------------------------------------------------------------------------

#ifdef DEBUG_LOG
void eHostInfoMgr::Dump(const char* aMessage)
{
	LOG(" >> %s", aMessage);
	LOG("-------- [ DUMP HOST INFO ] ---------");
	for (int i = 0; i < mInfoCount; i++) {
		LOG("%d : ip [%s], pid [%d]", i,  mShmData[i].ip, mShmData[i].pid);
	}
	LOG("-------------------------------------");
	Post();
}
#endif
//-------------------------------------------------------------------------------

std::string eHostInfoMgr::GetHostAddr()
{
	std::stringstream ss;
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);

    getpeername(0, (struct sockaddr*)&addr, &addrlen);
    ss << inet_ntoa(addr.sin_addr);// << ":" << addr.sin_port;

    return ss.str();
}
//-------------------------------------------------------------------------------

std::vector<int> eHostInfoMgr::FindPid(std::string aProcName, int aMyPid)
{
	std::vector<int> pidlist;
	char cmdlinepath[256] = {0};
	DIR* d = opendir("/proc");
	if (d != 0) {
		struct dirent* de;
		while ((de = readdir(d)) != 0) {
			int pid = atoi(de->d_name);
			if (pid > 0) {
				sprintf(cmdlinepath, "/proc/%s/cmdline", de->d_name);

				std::string cmdline;
				std::ifstream cmdlinefile(cmdlinepath);
				std::getline(cmdlinefile, cmdline);
				if (!cmdline.empty()) {
					size_t pos = cmdline.find('\0');
					if (pos != string::npos)
					cmdline = cmdline.substr(0, pos);
					pos = cmdline.rfind('/');
					if (pos != string::npos)
					cmdline = cmdline.substr(pos + 1);
					if ((aProcName == cmdline) && ((aMyPid != pid) || (aMyPid == 0))) {
						pidlist.push_back(pid);
					}
				}
			}
		}
		closedir(d);
	}
	return pidlist;
}
//-------------------------------------------------------------------------------