/[projects]/smsdaemon/SmsDaemon.cpp
ViewVC logotype

Annotation of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 217 - (hide annotations) (download)
Tue Dec 23 14:20:43 2008 UTC (15 years, 4 months ago) by torben
File size: 2933 byte(s)
Solved ToDo item:
'- Create a filtering method:
-  Incoming: which phonenumbers are allowed to invoke which plugins
-  Incoming could be solved with a PluginProxy() which intercepts the ExecutePlugin calls (only 
PluginManager needs to know about this one)'

Move the Access related items to AccessManager.*


1 torben 88 #include "SmsDaemon.h"
2    
3     #include <string>
4    
5     #include <sstream>
6 torben 132 #include <stdlib.h>
7    
8 torben 158 #include "Common.h"
9 torben 88
10 torben 149 #include "ISmsTransceiver.h"
11 torben 88
12     #include "Plugin.h"
13     #include "kbhit.h"
14    
15 torben 158 #include "Util.h"
16 torben 88
17 torben 92 #include "TaskManager.h"
18     #include "PluginManager.h"
19 torben 151 #include "ConfigFile.h"
20 torben 157 #include "Logger.h"
21 torben 217 #include "AccessManager.h"
22 torben 92
23 torben 94 #include "version.h"
24    
25 torben 88 using namespace std;
26    
27     void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin)
28     {
29     ostringstream os;
30 torben 142 os << "Recieved sms from " << sms.GetSender() << " ; command=" << sms.ExtractCommand();
31 torben 88 if (!hasPlugin)
32     os << " -- PLUGIN NOT FOUND";
33    
34 torben 157 Logger::logMessage(os.str());
35 torben 88 }
36    
37    
38     void SmsDaemon::CheckSms()
39     {
40 torben 128 const int INTERVAL = 1000; //no of milliseconds between sms checks
41 torben 95 timeval now = Util::GetTimeOfDay();
42    
43     if ( Util::mTimeDiff(_lastSmsCheck,now) < INTERVAL)
44     return;
45    
46     _lastSmsCheck = now;
47    
48 torben 88 Common* cmn = Common::instance();
49    
50 torben 92 PluginManager* manager = cmn->GetPluginManager();
51 torben 88 vector<SMS> sms = _modem.ReadSms();
52    
53     for (unsigned int i=0; i<sms.size(); ++i)
54     {
55 torben 142 string cmd = sms[i].ExtractCommand();
56 torben 196
57 torben 88 cmd = Util::str_tolower(cmd);
58    
59 torben 92 Plugin* pl = manager->GetPlugin(cmd);
60 torben 88
61     CreateLogMessage(sms[i], pl != 0);
62    
63     if (pl)
64     {
65     pl->Execute(_modem, sms[i]);
66     }
67     else
68     {
69 torben 142 _modem.SendSms(sms[i].GetSender(), "Unknown command!", false);
70 torben 88 }
71    
72     cmn->smsCounter.incomming++;
73     }
74     }
75    
76    
77     void SmsDaemon::MainLoop()
78     {
79     Common* cmn = Common::instance();
80     volatile bool& mainContinue = cmn->mainContinue;
81 torben 208 volatile bool& reloadConfig = cmn->reloadConfig;
82 torben 88
83     mainContinue = true;
84    
85     while (mainContinue)
86     {
87    
88 torben 128 CheckSms();
89 torben 88
90 torben 128 cmn->GetTaskManager()->ExecuteTasks(_modem);
91 torben 88
92     if (cmn->isDebug && kbhit())
93     break;
94    
95 torben 208 if (reloadConfig)
96     ReloadConfig();
97    
98 torben 128 Util::Sleep(100);
99 torben 88
100     }
101     }
102    
103 torben 208 void SmsDaemon::ReloadConfig()
104     {
105     Common* cmn = Common::instance();
106     cmn->reloadConfig = false;
107    
108     cmn->GetConfigfile()->Reload();
109    
110     cmn->GetPluginManager()->DestroyPlugins();
111     cmn->GetPluginManager()->LoadPlugins();
112    
113     cmn->GetTaskManager()->DestroyTasks();
114     cmn->GetTaskManager()->LoadTasks();
115    
116 torben 217 AccessManager::Load(); //reload blaclist & privileged list
117 torben 208 }
118    
119 torben 88 void SmsDaemon::Start()
120     {
121     Common* cmn = Common::instance();
122 torben 151 string transceiver = cmn->GetConfigfile()->GetValue("smsdaemon","transceiver");
123 torben 88
124 torben 132 srand(time(0));
125 torben 88
126     cmn->daemonStart = time(0);
127 torben 95 _lastSmsCheck = Util::GetTimeOfDay();
128 torben 88
129 torben 157 Logger::logMessage("--------------------------------");
130     Logger::logMessage( VERSION );
131     Logger::logMessage( SVNVERSION );
132     Logger::logMessage( string("Transceiver: ") + transceiver );
133 torben 88
134 torben 217 AccessManager::Load(); //reload blaclist & privileged list
135 torben 92 cmn->GetTaskManager()->LoadTasks();
136     cmn->GetPluginManager()->LoadPlugins();
137 torben 157 Logger::logMessage("SMS daemon started");
138 torben 88
139 torben 216
140 torben 88 try
141     {
142     MainLoop();
143     }
144     catch (std::exception& e)
145     {
146 torben 157 Logger::logMessage( e.what() );
147 torben 88 }
148     catch (...)
149     {
150 torben 157 Logger::logMessage( "Caught unknown exception" );
151 torben 88 }
152    
153 torben 205 cmn->GetPluginManager()->DestroyPlugins();
154    
155 torben 157 Logger::logMessage( cmn->getStatusMessage() );
156 torben 88 }

  ViewVC Help
Powered by ViewVC 1.1.20