comparison PalantirServer/PalantirInitialization.cpp @ 0:3959d33612cc

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jul 2012 14:32:22 +0200
parents
children e85455ff6039
comparison
equal deleted inserted replaced
-1:000000000000 0:3959d33612cc
1 /**
2 * Palantir - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "PalantirInitialization.h"
22
23 #include "../Core/PalantirException.h"
24 #include "../Core/Toolbox.h"
25
26 #include <boost/lexical_cast.hpp>
27 #include <boost/filesystem.hpp>
28 #include <curl/curl.h>
29 #include <boost/thread.hpp>
30
31 namespace Palantir
32 {
33 static const char* CONFIGURATION_FILE = "Configuration.json";
34
35 static boost::mutex globalMutex_;
36 static std::auto_ptr<Json::Value> configuration_;
37
38
39 static void ReadGlobalConfiguration(const char* configurationFile)
40 {
41 configuration_.reset(new Json::Value);
42
43 std::string content;
44
45 if (configurationFile)
46 {
47 Toolbox::ReadFile(content, configurationFile);
48 }
49 else
50 {
51 #if PALANTIR_RELEASE == 0
52 boost::filesystem::path p = PALANTIR_PATH;
53 p /= "Resources";
54 p /= CONFIGURATION_FILE;
55 Toolbox::ReadFile(content, p.string());
56 #else
57 Toolbox::ReadFile(content, CONFIGURATION_FILE);
58 #endif
59 }
60
61 Json::Reader reader;
62 if (!reader.parse(content, *configuration_))
63 {
64 throw PalantirException("Unable to read the configuration file");
65 }
66 }
67
68
69 void PalantirInitialize(const char* configurationFile)
70 {
71 boost::mutex::scoped_lock lock(globalMutex_);
72 ReadGlobalConfiguration(configurationFile);
73 curl_global_init(CURL_GLOBAL_ALL);
74 }
75
76
77
78 void PalantirFinalize()
79 {
80 boost::mutex::scoped_lock lock(globalMutex_);
81 curl_global_cleanup();
82 configuration_.reset(NULL);
83 }
84
85
86
87 std::string GetGlobalStringParameter(const std::string& parameter,
88 const std::string& defaultValue)
89 {
90 boost::mutex::scoped_lock lock(globalMutex_);
91
92 if (configuration_->isMember(parameter))
93 {
94 return (*configuration_) [parameter].asString();
95 }
96 else
97 {
98 return defaultValue;
99 }
100 }
101
102
103 int GetGlobalIntegerParameter(const std::string& parameter,
104 int defaultValue)
105 {
106 boost::mutex::scoped_lock lock(globalMutex_);
107
108 if (configuration_->isMember(parameter))
109 {
110 return (*configuration_) [parameter].asInt();
111 }
112 else
113 {
114 return defaultValue;
115 }
116 }
117
118
119
120 void GetDicomModality(const std::string& name,
121 std::string& aet,
122 std::string& address,
123 int& port)
124 {
125 boost::mutex::scoped_lock lock(globalMutex_);
126
127 if (!configuration_->isMember("DicomModalities"))
128 {
129 throw PalantirException("");
130 }
131
132 const Json::Value& modalities = (*configuration_) ["DicomModalities"];
133 if (modalities.type() != Json::objectValue ||
134 !modalities.isMember(name))
135 {
136 throw PalantirException("");
137 }
138
139 try
140 {
141 aet = modalities[name].get(0u, "").asString();
142 address = modalities[name].get(1u, "").asString();
143 port = modalities[name].get(2u, "").asInt();
144 }
145 catch (...)
146 {
147 throw PalantirException("Badly formatted DICOM modality");
148 }
149 }
150
151
152
153 void GetListOfDicomModalities(std::set<std::string>& target)
154 {
155 boost::mutex::scoped_lock lock(globalMutex_);
156
157 target.clear();
158
159 if (!configuration_->isMember("DicomModalities"))
160 {
161 return;
162 }
163
164 const Json::Value& modalities = (*configuration_) ["DicomModalities"];
165 if (modalities.type() != Json::objectValue)
166 {
167 throw PalantirException("Badly formatted list of DICOM modalities");
168 }
169
170 Json::Value::Members members = modalities.getMemberNames();
171 for (size_t i = 0; i < members.size(); i++)
172 {
173 for (size_t j = 0; j < members[i].size(); j++)
174 {
175 if (!isalnum(members[i][j]) && members[i][j] != '-')
176 {
177 throw PalantirException("Only alphanumeric and dash characters are allowed in the names of the modalities");
178 }
179 }
180
181 target.insert(members[i]);
182 }
183 }
184 }