Start changing client library to allow multiple sessions within one process
[public/netxms.git] / src / libnxcl / session.cpp
CommitLineData
41213c24
VK
1/*
2** NetXMS - Network Management System
3** Client Library
4** Copyright (C) 2004 Victor Kirhenshtein
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** $module: session.cpp
21**
22**/
23
24#include "libnxcl.h"
25
26
27//
28// Session class constructor
29//
30
31NXCL_Session::NXCL_Session()
32{
33 m_pEventHandler = NULL;
34 m_dwState = STATE_DISCONNECTED;
35 m_dwCommandTimeout = 10000; // Default timeout is 10 seconds
36 m_dwNumObjects = 0;
37 m_pIndexById = NULL;
38 m_mutexIndexAccess = MutexCreate();
39 m_dwReceiverBufferSize = 4194304; // 4MB
40 m_hSocket = -1;
41}
42
43
44//
45// Session class destructor
46//
47
48NXCL_Session::~NXCL_Session()
49{
50 Disconnect();
51 MutexDestroy(m_mutexIndexAccess);
52 safe_free(m_pIndexById);
53}
54
55
56//
57// Disconnect session
58//
59
60void NXCL_Session::Disconnect(void)
61{
62 // Close socket
63 shutdown(m_hSocket, SHUT_RDWR);
64 closesocket(m_hSocket);
65
66 // Clear message wait queue
67 m_msgWaitQueue.Clear();
68
69 // Cleanup
70 DestroyAllObjects();
71}
72
73
74//
75// Destroy all objects
76//
77
78void NXCL_Session::DestroyAllObjects(void)
79{
80 DWORD i;
81
82 MutexLock(m_mutexIndexAccess, INFINITE);
83 for(i = 0; i < m_dwNumObjects; i++)
84 DestroyObject(m_pIndexById[i].pObject);
85 m_dwNumObjects = 0;
86 free(m_pIndexById);
87 m_pIndexById = NULL;
88 MutexUnlock(m_mutexIndexAccess);
89}
90
91
92//
93// Wait for specific message
94//
95
96CSCPMessage *NXCL_Session::WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
97{
98 return m_msgWaitQueue.WaitForMessage(wCode, dwId,
99 dwTimeOut == 0 ? m_dwCommandTimeout : dwTimeOut);
100}
101
102
103//
104// Wait for specific raw message
105//
106
107CSCP_MESSAGE *NXCL_Session::WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
108{
109 return m_msgWaitQueue.WaitForRawMessage(wCode, dwId,
110 dwTimeOut == 0 ? m_dwCommandTimeout : dwTimeOut);
111}
112
113
114//
115// Wait for request completion notification and extract result code
116// from recived message
117//
118
119DWORD NXCL_Session::WaitForRCC(DWORD dwRqId)
120{
121 CSCPMessage *pResponce;
122 DWORD dwRetCode;
123
124 pResponce = WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId, m_dwCommandTimeout);
125 if (pResponce != NULL)
126 {
127 dwRetCode = pResponce->GetVariableLong(VID_RCC);
128 delete pResponce;
129 }
130 else
131 {
132 dwRetCode = RCC_TIMEOUT;
133 }
134 return dwRetCode;
135}
136
137
138//
139// Send CSCP message
140//
141
142BOOL NXCL_Session::SendMsg(CSCPMessage *pMsg)
143{
144 CSCP_MESSAGE *pRawMsg;
145 BOOL bResult;
146 TCHAR szBuffer[128];
147
148 DebugPrintf(_T("SendMsg(\"%s\"), id:%ld)"), CSCPMessageCodeName(pMsg->GetCode(), szBuffer), pMsg->GetId());
149 pRawMsg = pMsg->CreateMessage();
150 bResult = (send(m_hSocket, (char *)pRawMsg, ntohl(pRawMsg->dwSize), 0) == (int)ntohl(pRawMsg->dwSize));
151 free(pRawMsg);
152 return bResult;
153}