]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Replcae GetStore with new GetPlugin method from plugin creator
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <signal.h>
4
5 #include <algorithm>
6
7 #include "stg/tariffs.h"
8 #include "stg/admins.h"
9 #include "stg/users.h"
10 #include "stg/plugin_creator.h"
11 #include "stgconfig.h"
12
13 //-----------------------------------------------------------------------------
14 //-----------------------------------------------------------------------------
15 //-----------------------------------------------------------------------------
16 PLUGIN_CREATOR<STG_CONFIG> stgc;
17 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
20 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
21 {
22 int p;
23 PARAM_VALUE pv;
24 vector<PARAM_VALUE>::const_iterator pvi;
25 ///////////////////////////
26 pv.param = "Port";
27 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
28 if (pvi == s.moduleParams.end())
29     {
30     errorStr = "Parameter \'Port\' not found.";
31     printfd(__FILE__, "Parameter 'Port' not found\n");
32     return -1;
33     }
34 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
35     {
36     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
37     printfd(__FILE__, "%s\n", errorStr.c_str());
38     return -1;
39     }
40 port = p;
41
42 return 0;
43 }
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 PLUGIN * GetPlugin()
48 {
49 return stgc.GetPlugin();
50 }
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 const std::string STG_CONFIG::GetVersion() const
55 {
56 return "Stg configurator v.0.08";
57 }
58 //-----------------------------------------------------------------------------
59 STG_CONFIG::STG_CONFIG()
60     : nonstop(false),
61       isRunning(false),
62       users(NULL),
63       admins(NULL),
64       tariffs(NULL),
65       store(NULL),
66       stgSettings(NULL)
67 {
68 }
69 //-----------------------------------------------------------------------------
70 int STG_CONFIG::ParseSettings()
71 {
72 int ret = stgConfigSettings.ParseSettings(settings);
73 if (ret)
74     errorStr = stgConfigSettings.GetStrError();
75 return ret;
76 }
77 //-----------------------------------------------------------------------------
78 int STG_CONFIG::Start()
79 {
80 if (isRunning)
81     return 0;
82
83 nonstop = true;
84
85 config.SetPort(stgConfigSettings.GetPort());
86 config.SetAdmins(admins);
87 config.SetUsers(users);
88 config.SetTariffs(tariffs);
89 config.SetStgSettings(stgSettings);
90 config.SetStore(store);
91
92 if (config.Prepare())
93     {
94     errorStr = config.GetStrError();
95     return -1;
96     }
97
98 if (pthread_create(&thread, NULL, Run, this))
99     {
100     errorStr = "Cannot create thread.";
101     printfd(__FILE__, "Cannot create thread\n");
102     return -1;
103     }
104 errorStr = "";
105 return 0;
106 }
107 //-----------------------------------------------------------------------------
108 int STG_CONFIG::Stop()
109 {
110 if (!isRunning)
111     return 0;
112
113 config.Stop();
114
115 //5 seconds to thread stops itself
116 int i;
117 for (i = 0; i < 25; i++)
118     {
119     if (!isRunning)
120         break;
121
122     usleep(200000);
123     }
124
125 //after 5 seconds waiting thread still running. now killing it
126 if (isRunning)
127     {
128     //TODO pthread_cancel()
129     if (pthread_kill(thread, SIGINT))
130         {
131         errorStr = "Cannot kill thread.";
132         printfd(__FILE__, "Cannot kill thread\n");
133         return -1;
134         }
135     printfd(__FILE__, "STG_CONFIG killed\n");
136     }
137
138 return 0;
139 }
140 //-----------------------------------------------------------------------------
141 void * STG_CONFIG::Run(void * d)
142 {
143 STG_CONFIG * stgConf = (STG_CONFIG *)d;
144 stgConf->isRunning = true;
145
146 stgConf->config.Run(&stgConf->config);
147
148 stgConf->isRunning = false;
149 return NULL;
150 }
151 //-----------------------------------------------------------------------------