implemented helper class SocketListener; server listeners converted to use SocketListener
[public/netxms.git] / src / server / core / mdconn.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2017 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: mdconn.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24 #include <socket_listener.h>
25
26 /**
27 * Static data
28 */
29 static MobileDeviceSession *s_sessionList[MAX_DEVICE_SESSIONS];
30 static RWLOCK s_sessionListLock;
31
32 /**
33 * Register new session in list
34 */
35 static BOOL RegisterMobileDeviceSession(MobileDeviceSession *pSession)
36 {
37 RWLockWriteLock(s_sessionListLock, INFINITE);
38 for(int i = 0; i < MAX_DEVICE_SESSIONS; i++)
39 if (s_sessionList[i] == NULL)
40 {
41 s_sessionList[i] = pSession;
42 pSession->setId(i + MAX_CLIENT_SESSIONS);
43 RWLockUnlock(s_sessionListLock);
44 return TRUE;
45 }
46
47 RWLockUnlock(s_sessionListLock);
48 nxlog_write(MSG_TOO_MANY_MD_SESSIONS, EVENTLOG_WARNING_TYPE, NULL);
49 return FALSE;
50 }
51
52 /**
53 * Unregister session
54 */
55 void UnregisterMobileDeviceSession(int id)
56 {
57 RWLockWriteLock(s_sessionListLock, INFINITE);
58 s_sessionList[id - MAX_CLIENT_SESSIONS] = NULL;
59 RWLockUnlock(s_sessionListLock);
60 }
61
62 /**
63 * Initialize mobile device listener(s)
64 */
65 void InitMobileDeviceListeners()
66 {
67 memset(s_sessionList, 0, sizeof(s_sessionList));
68 s_sessionListLock = RWLockCreate();
69 }
70
71 /**
72 * Mobile device listener class
73 */
74 class MobileDeviceListener : public SocketListener
75 {
76 protected:
77 virtual ConnectionProcessingResult processConnection(SOCKET s, const InetAddress& peer);
78 virtual bool isStopConditionReached();
79
80 public:
81 MobileDeviceListener(UINT16 port) : SocketListener(port) { setName(_T("MobileDevices")); }
82 };
83
84 /**
85 * Listener stop condition
86 */
87 bool MobileDeviceListener::isStopConditionReached()
88 {
89 return IsShutdownInProgress();
90 }
91
92 /**
93 * Process incoming connection
94 */
95 ConnectionProcessingResult MobileDeviceListener::processConnection(SOCKET s, const InetAddress& peer)
96 {
97 SetSocketNonBlocking(s);
98 MobileDeviceSession *session = new MobileDeviceSession(s, peer);
99 if (RegisterMobileDeviceSession(session))
100 {
101 session->run();
102 }
103 else
104 {
105 delete session;
106 }
107 return CPR_BACKGROUND;
108 }
109
110 /**
111 * Listener thread
112 */
113 THREAD_RESULT THREAD_CALL MobileDeviceListenerThread(void *arg)
114 {
115 UINT16 listenPort = (UINT16)ConfigReadInt(_T("MobileDeviceListenerPort"), SERVER_LISTEN_PORT_FOR_MOBILES);
116 MobileDeviceListener listener(listenPort);
117 listener.setListenAddress(g_szListenAddress);
118 if (!listener.initialize())
119 return THREAD_OK;
120
121 listener.mainLoop();
122 listener.shutdown();
123 return THREAD_OK;
124 }
125
126 /**
127 * Dump client sessions to screen
128 */
129 void DumpMobileDeviceSessions(CONSOLE_CTX pCtx)
130 {
131 int i, iCount;
132 TCHAR szBuffer[256];
133 static const TCHAR *pszStateName[] = { _T("init"), _T("idle"), _T("processing") };
134 static const TCHAR *pszCipherName[] = { _T("NONE"), _T("AES-256"), _T("BLOWFISH"), _T("IDEA"), _T("3DES"), _T("AES-128") };
135
136 ConsolePrintf(pCtx, _T("ID STATE CIPHER USER [CLIENT]\n"));
137 RWLockReadLock(s_sessionListLock, INFINITE);
138 for(i = 0, iCount = 0; i < MAX_DEVICE_SESSIONS; i++)
139 if (s_sessionList[i] != NULL)
140 {
141 ConsolePrintf(pCtx, _T("%-3d %-24s %-8s %s [%s]\n"), i,
142 (s_sessionList[i]->getState() != SESSION_STATE_PROCESSING) ?
143 pszStateName[s_sessionList[i]->getState()] :
144 NXCPMessageCodeName(s_sessionList[i]->getCurrentCmd(), szBuffer),
145 pszCipherName[s_sessionList[i]->getCipher() + 1],
146 s_sessionList[i]->getUserName(),
147 s_sessionList[i]->getClientInfo());
148 iCount++;
149 }
150 RWLockUnlock(s_sessionListLock);
151 ConsolePrintf(pCtx, _T("\n%d active session%s\n\n"), iCount, iCount == 1 ? _T("") : _T("s"));
152 }