- Implemented interface for SMS drivers
[public/netxms.git] / src / server / core / sms.cpp
CommitLineData
5795bf90
VK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003,2004,2005 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** $module: sms.cpp
20**
21**/
22
23#include "nxcore.h"
24
25
a3f23550
VK
26//
27// SMS structure
28//
29
30typedef struct
31{
32 char szRcpt[MAX_RCPT_ADDR_LEN];
33 char szText[160];
34} SMS;
35
36
5795bf90
VK
37//
38// Static data
39//
40
41static Queue *m_pMsgQueue;
a3f23550
VK
42static BOOL (* m_DrvSendMsg)(TCHAR *, TCHAR *);
43static void (* m_DrvUnload)(void);
44static THREAD m_hThread = INVALID_THREAD_HANDLE;
45
46
47//
48// Sender thread
49//
50
51static THREAD_RESULT THREAD_CALL SenderThread(void *pArg)
52{
53 SMS *pMsg;
54
55 while(1)
56 {
57 pMsg = (SMS *)m_pMsgQueue->GetOrBlock();
58 if (pMsg == INVALID_POINTER_VALUE)
59 break;
60
61 if (!m_DrvSendMsg(pMsg->szRcpt, pMsg->szText))
62 PostEvent(EVENT_SMS_FAILURE, g_dwMgmtNode, "s", pMsg->szRcpt);
63
64 free(pMsg);
65 }
66 return THREAD_OK;
67}
5795bf90
VK
68
69
70//
71// Initialize SMS subsystem
72//
73
74void InitSMSSender(void)
75{
76 TCHAR szDriver[MAX_PATH], szDrvConfig[MAX_PATH];
77
78 ConfigReadStr(_T("SMSDriver"), szDriver, MAX_PATH, _T("none"));
79 ConfigReadStr(_T("SMSDrvConfig"), szDrvConfig, MAX_DB_STRING, _T(""));
80 if (_tcsicmp(szDriver, _T("none")))
81 {
82 TCHAR szErrorText[256];
83 HMODULE hModule;
84
85 hModule = DLOpen(szDriver, szErrorText);
86 if (hModule != NULL)
87 {
a3f23550 88 BOOL (* DrvInit)(TCHAR *);
5795bf90 89
a3f23550
VK
90 DrvInit = (BOOL (*)(TCHAR *))DLGetSymbolAddr(hModule, "SMSDriverInit", szErrorText);
91 m_DrvSendMsg = (BOOL (*)(TCHAR *, TCHAR *))DLGetSymbolAddr(hModule, "SMSDriverSend", szErrorText);
92 m_DrvUnload = (void (*)(void))DLGetSymbolAddr(hModule, "SMSDriverUnload", szErrorText);
93 if ((DrvInit != NULL) && (m_DrvSendMsg != NULL) && (m_DrvUnload != NULL))
5795bf90 94 {
a3f23550
VK
95 if (DrvInit(szDrvConfig))
96 {
97 m_pMsgQueue = new Queue;
98
99 m_hThread = ThreadCreateEx(SenderThread, 0, NULL);
100 }
101 else
102 {
103 WriteLog(MSG_SMSDRV_INIT_FAILED, EVENTLOG_ERROR_TYPE, "s", szDriver);
104 DLClose(hModule);
105 }
106 }
107 else
108 {
109 WriteLog(MSG_SMSDRV_NO_ENTRY_POINTS, EVENTLOG_ERROR_TYPE, "s", szDriver);
110 DLClose(hModule);
5795bf90
VK
111 }
112 }
113 else
114 {
115 WriteLog(MSG_DLOPEN_FAILED, EVENTLOG_ERROR_TYPE,
116 _T("ss"), szDriver, szErrorText);
117 }
118 }
119}
120
121
122//
a3f23550 123// Shutdown SMS sender
5795bf90
VK
124//
125
126void ShutdownSMSSender(void)
127{
128 m_pMsgQueue->Clear();
129 m_pMsgQueue->Put(INVALID_POINTER_VALUE);
a3f23550
VK
130 if (m_hThread != INVALID_THREAD_HANDLE)
131 ThreadJoin(m_hThread);
132 delete m_pMsgQueue;
5795bf90
VK
133}
134
135
136//
a3f23550 137// Post SMS to queue
5795bf90
VK
138//
139
a3f23550 140void NXCORE_EXPORTABLE PostSMS(TCHAR *pszRcpt, TCHAR *pszText)
5795bf90 141{
a3f23550 142 SMS *pMsg;
5795bf90 143
a3f23550
VK
144 pMsg = (SMS *)malloc(sizeof(SMS));
145 _tcsncpy(pMsg->szRcpt, pszRcpt, MAX_RCPT_ADDR_LEN);
146 _tcsncpy(pMsg->szText, pszText, 160);
147 m_pMsgQueue->Put(pMsg);
5795bf90 148}