issue #48
[public/netxms.git] / src / server / core / sms.cpp
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
26 //
27 // SMS structure
28 //
29
30 typedef struct
31 {
32 char szRcpt[MAX_RCPT_ADDR_LEN];
33 char szText[160];
34 } SMS;
35
36
37 //
38 // Static data
39 //
40
41 static Queue *m_pMsgQueue = NULL;
42 static BOOL (* m_DrvSendMsg)(TCHAR *, TCHAR *);
43 static void (* m_DrvUnload)(void);
44 static THREAD m_hThread = INVALID_THREAD_HANDLE;
45
46
47 //
48 // Sender thread
49 //
50
51 static 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 }
68
69
70 //
71 // Initialize SMS subsystem
72 //
73
74 void 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 {
88 BOOL (* DrvInit)(TCHAR *);
89
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))
94 {
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);
111 }
112 }
113 else
114 {
115 WriteLog(MSG_DLOPEN_FAILED, EVENTLOG_ERROR_TYPE,
116 _T("ss"), szDriver, szErrorText);
117 }
118 }
119 }
120
121
122 //
123 // Shutdown SMS sender
124 //
125
126 void ShutdownSMSSender(void)
127 {
128 if (m_pMsgQueue != NULL)
129 {
130 m_pMsgQueue->Clear();
131 m_pMsgQueue->Put(INVALID_POINTER_VALUE);
132 if (m_hThread != INVALID_THREAD_HANDLE)
133 ThreadJoin(m_hThread);
134 delete m_pMsgQueue;
135 }
136 }
137
138
139 //
140 // Post SMS to queue
141 //
142
143 void NXCORE_EXPORTABLE PostSMS(TCHAR *pszRcpt, TCHAR *pszText)
144 {
145 SMS *pMsg;
146
147 if (m_pMsgQueue != NULL)
148 {
149 pMsg = (SMS *)malloc(sizeof(SMS));
150 _tcsncpy(pMsg->szRcpt, pszRcpt, MAX_RCPT_ADDR_LEN);
151 _tcsncpy(pMsg->szText, pszText, 160);
152 m_pMsgQueue->Put(pMsg);
153 }
154 }