-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyw_configuration.cpp
More file actions
210 lines (167 loc) · 7.08 KB
/
yw_configuration.cpp
File metadata and controls
210 lines (167 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "yw_configuration.h"
#include "yw_global.h"
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/exception/all.hpp>
#include <boost/throw_exception.hpp>
#include <boost/exception/error_info.hpp>
#include <errno.h>
#include <iostream>
namespace fs = boost::filesystem;
ywConfiguration::ywConfiguration()
{
configurationValid=false;
serverName="Unknown";
port="8080";
yarraPath =".";
disableModuleInstallation=false;
disableModeEditing =false;
disableSupportForum =false;
disableYarraNews =false;
initServerConfiguration();
}
void ywConfiguration::initServerConfiguration()
{
yarraLogPath ="/log";
yarraModesPath ="/modes";
yarraQueuePath ="/queue";
yarraWorkPath ="/work";
yarraFailPath ="/fail";
yarraStoragePath ="/finished";
yarraModulesPath ="/modules";
yarraModulesUserPath="/modules_user";
matlabBinaryPath ="/usr/local/bin/matlab";
yarraResumePath ="/resume";
yarraEnableResume =false;
}
ywConfiguration::~ywConfiguration()
{
while (!users.empty())
{
ywUser* item=users.back();
users.pop_back();
delete item;
item=0;
}
std::cout << std::endl << "YarraServer WebGUI is going down now." << std::endl << std::endl;
}
void ywConfiguration::loadConfiguration()
{
std::cout << std::endl << "Reading YarraServer WebGUI configuration..." << std::endl;
int maxUser=100;
int userCount=0;
WString userName="";
WString userPassword="";
int userLevel=0;
try
{
boost::property_tree::ptree inifile;
boost::property_tree::ini_parser::read_ini(YW_CONFIGFILE, inifile);
userName=WString::fromUTF8(inifile.get<std::string>( WString("User{1}.Name").arg(userCount+1).toUTF8(),""));
while ((userCount<maxUser) && (userName!=""))
{
userPassword=WString::fromUTF8(inifile.get<std::string>( WString("User{1}.Password").arg(userCount+1).toUTF8(),""));
userLevel=inifile.get<int>( WString("User{1}.Level").arg(userCount+1).toUTF8(),1);
// Check if userLevel has a valid range
if ((userLevel<1) || (userLevel>3))
{
userLevel=1;
}
ywUser* userEntry=new ywUser;
userEntry->name=userName;
userEntry->password=userPassword;
userEntry->level=userLevel;
users.push_back(userEntry);
userCount++;
userName=WString::fromUTF8(inifile.get<std::string>( WString("User{1}.Name").arg(userCount+1).toUTF8(),""));
}
// Read main settings
yarraPath=WString::fromUTF8(inifile.get<std::string>("Setup.YarraPath","."));
port=WString::fromUTF8(inifile.get<std::string>("Setup.Port","8080"));
// Read optional security settings
disableModuleInstallation=inifile.get<bool>("Setup.DisableModuleInstallation",disableModuleInstallation);
disableModeEditing =inifile.get<bool>("Setup.DisableModeEditing" ,disableModeEditing);
disableSupportForum =inifile.get<bool>("Setup.DisableSupportForum" ,disableSupportForum);
disableYarraNews =inifile.get<bool>("Setup.DisableYarraNews" ,disableYarraNews);
// Now try to read the yarra config file
// If the yarra config file cannot be loaded, stop the webgui
if (!loadServerConfiguration())
{
configurationValid=false;
return;
}
configurationValid=true;
}
catch(const boost::property_tree::ptree_error &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
configurationValid=false;
}
std::cout << std::endl;
}
bool ywConfiguration::loadServerConfiguration()
{
// Important: This will reset the server paths to the default values, which will merged with the server location
// below. It's important to reinitialize whenever the configuration is refreshed.
initServerConfiguration();
try
{
WString serverIniFilename=yarraPath+"/"+YW_YARRACONFIG;
if (!fs::exists(serverIniFilename.toUTF8()))
{
std::cout << "ERROR: Can't find YarraServer configuration file " << serverIniFilename << std::endl;
std::cout << " Has the path to the YarraServer installation been set correctly?" << std::endl;
return false;
}
boost::property_tree::ptree serverIni;
boost::property_tree::ini_parser::read_ini(serverIniFilename.toUTF8(), serverIni);
serverName=WString::fromUTF8(serverIni.get<std::string>("Server.Name","Unknown"));
// Note: Here the full path is assembled as default value (base path + subfolder)
yarraLogPath =WString::fromUTF8(serverIni.get<std::string>("Paths.Log" ,WString(yarraPath+yarraLogPath ).toUTF8()));
yarraModesPath =WString::fromUTF8(serverIni.get<std::string>("Paths.Modes" ,WString(yarraPath+yarraModesPath ).toUTF8()));
yarraQueuePath =WString::fromUTF8(serverIni.get<std::string>("Paths.Queue" ,WString(yarraPath+yarraQueuePath ).toUTF8()));
yarraWorkPath =WString::fromUTF8(serverIni.get<std::string>("Paths.Work" ,WString(yarraPath+yarraWorkPath ).toUTF8()));
yarraFailPath =WString::fromUTF8(serverIni.get<std::string>("Paths.Fail" ,WString(yarraPath+yarraFailPath ).toUTF8()));
yarraStoragePath =WString::fromUTF8(serverIni.get<std::string>("Paths.Storage" ,WString(yarraPath+yarraStoragePath ).toUTF8()));
yarraModulesPath =WString::fromUTF8(serverIni.get<std::string>("Paths.Modules" ,WString(yarraPath+yarraModulesPath ).toUTF8()));
yarraModulesUserPath=WString::fromUTF8(serverIni.get<std::string>("Paths.ModulesUser",WString(yarraPath+yarraModulesUserPath).toUTF8()));
yarraResumePath =WString::fromUTF8(serverIni.get<std::string>("Paths.Resume" ,WString(yarraPath+yarraResumePath ).toUTF8()));
yarraEnableResume =serverIni.get<bool>("Options.ResumeTasks",yarraEnableResume);
matlabBinaryPath=WString::fromUTF8(serverIni.get<std::string>("Paths.MatlabBinary",WString(matlabBinaryPath).toUTF8()));
}
catch(const boost::property_tree::ptree_error &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
return false;
}
return true;
}
ywUser* ywConfiguration::validateUser(WString name, WString password)
{
// Function will return a null pointer if the user does not exists
// or the password is wrong
ywUser* userEntry=0;
for (int i=0; i<users.size(); i++)
{
if (users.at(i)->name==name)
{
userEntry=users.at(i);
break;
}
}
if (userEntry==0)
{
// A user with this name does not exist
return 0;
}
if (userEntry->password==password)
{
return userEntry;
}
else
{
// Password is incorrect
return 0;
}
}