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

Annotation of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (hide annotations) (download)
Sun Dec 21 18:41:08 2008 UTC (15 years, 5 months ago) by torben
File size: 2810 byte(s)
Enable dynamic reload of plugins/tasks when recieving a HUP signal

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

  ViewVC Help
Powered by ViewVC 1.1.20