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

Annotation of /smsdaemon/SmsDaemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (hide annotations) (download)
Mon Jun 16 09:04:05 2008 UTC (15 years, 11 months ago) by torben
File size: 1767 byte(s)
Refactored some of the main.cpp code into SmsDaemon class.


1 torben 88 #include "SmsDaemon.h"
2    
3     #include <string>
4    
5     #include <sstream>
6     #include "common.h"
7    
8     #include "GsmModem.h"
9    
10     #include "Plugin.h"
11     #include "kbhit.h"
12    
13     #include "util.h"
14    
15     using namespace std;
16    
17    
18     void SmsDaemon::CreateLogMessage(SMS& sms,bool hasPlugin)
19     {
20     ostringstream os;
21     os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);
22     if (!hasPlugin)
23     os << " -- PLUGIN NOT FOUND";
24    
25     Common::instance()->logMessage(os.str());
26     }
27    
28    
29     void SmsDaemon::CheckSms()
30     {
31     Common* cmn = Common::instance();
32    
33     PluginManager& manager = cmn->pluginManager;
34     vector<SMS> sms = _modem.ReadSms();
35    
36     for (unsigned int i=0; i<sms.size(); ++i)
37     {
38     string cmd = GetSmsCommand(sms[i]);
39    
40     cmd = Util::str_tolower(cmd);
41    
42     Plugin* pl = manager.GetPlugin(cmd);
43    
44     CreateLogMessage(sms[i], pl != 0);
45    
46     if (pl)
47     {
48     pl->Execute(_modem, sms[i]);
49     }
50     else
51     {
52     _modem.SendSms(sms[i].sender, "Unknown command!", false);
53     }
54    
55     _modem.DeleteSms(sms[i].sms_index);
56     cmn->smsCounter.incomming++;
57     }
58     }
59    
60    
61     void SmsDaemon::MainLoop()
62     {
63     Common* cmn = Common::instance();
64     volatile bool& mainContinue = cmn->mainContinue;
65    
66    
67     mainContinue = true;
68    
69     while (mainContinue)
70     {
71    
72     CheckSms();
73    
74     cmn->taskManager.ExecuteTasks();
75    
76     if (cmn->isDebug && kbhit())
77     break;
78    
79     Util::Sleep(10);
80    
81     }
82     }
83    
84     void SmsDaemon::Start()
85     {
86     Common* cmn = Common::instance();
87    
88    
89     cmn->daemonStart = time(0);
90    
91     cmn->logMessage("--------------------------------");
92    
93     cmn->taskManager.LoadTasks();
94     cmn->pluginManager.LoadPlugins();
95     cmn->logMessage("SMS daemon started");
96    
97     _modem.DeleteAllSms();
98    
99     try
100     {
101     MainLoop();
102     }
103     catch (std::exception& e)
104     {
105     cmn->logMessage( e.what() );
106     }
107     catch (...)
108     {
109     cmn->logMessage( "Caught unknown exception" );
110     }
111    
112     cmn->logMessage( cmn->getStatusMessage() );
113     }

  ViewVC Help
Powered by ViewVC 1.1.20