comparison Plugins/Engine/OrthancPlugins.cpp @ 1310:61ce8147f30d db-changes

custom database back-end
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 11 Feb 2015 10:40:08 +0100
parents 179a3049b942
children b7351ecb79b4
comparison
equal deleted inserted replaced
1309:8f4487d8f79e 1310:61ce8147f30d
178 typedef std::list<RestCallback> RestCallbacks; 178 typedef std::list<RestCallback> RestCallbacks;
179 typedef std::list<OrthancPluginOnStoredInstanceCallback> OnStoredCallbacks; 179 typedef std::list<OrthancPluginOnStoredInstanceCallback> OnStoredCallbacks;
180 typedef std::list<OrthancPluginOnChangeCallback> OnChangeCallbacks; 180 typedef std::list<OrthancPluginOnChangeCallback> OnChangeCallbacks;
181 typedef std::map<Property, std::string> Properties; 181 typedef std::map<Property, std::string> Properties;
182 182
183 ServerContext& context_; 183 ServerContext* context_;
184 RestCallbacks restCallbacks_; 184 RestCallbacks restCallbacks_;
185 OrthancRestApi* restApi_; 185 OrthancRestApi* restApi_;
186 OnStoredCallbacks onStoredCallbacks_; 186 OnStoredCallbacks onStoredCallbacks_;
187 OnChangeCallbacks onChangeCallbacks_; 187 OnChangeCallbacks onChangeCallbacks_;
188 bool hasStorageArea_; 188 bool hasStorageArea_;
192 boost::thread changeThread_; 192 boost::thread changeThread_;
193 bool done_; 193 bool done_;
194 Properties properties_; 194 Properties properties_;
195 int argc_; 195 int argc_;
196 char** argv_; 196 char** argv_;
197 197 std::auto_ptr<OrthancPluginDatabase> database_;
198 PImpl(ServerContext& context) : 198
199 context_(context), 199 PImpl() :
200 context_(NULL),
200 restApi_(NULL), 201 restApi_(NULL),
201 hasStorageArea_(false), 202 hasStorageArea_(false),
202 done_(false), 203 done_(false),
203 argc_(1), 204 argc_(1),
204 argv_(NULL) 205 argv_(NULL)
244 245
245 return result; 246 return result;
246 } 247 }
247 248
248 249
249 OrthancPlugins::OrthancPlugins(ServerContext& context) 250 OrthancPlugins::OrthancPlugins()
250 { 251 {
251 pimpl_.reset(new PImpl(context)); 252 pimpl_.reset(new PImpl());
252 pimpl_->changeThread_ = boost::thread(PImpl::ChangeThread, pimpl_.get()); 253 pimpl_->changeThread_ = boost::thread(PImpl::ChangeThread, pimpl_.get());
253 } 254 }
255
256
257 void OrthancPlugins::SetServerContext(ServerContext& context)
258 {
259 pimpl_->context_ = &context;
260 }
261
254 262
255 263
256 OrthancPlugins::~OrthancPlugins() 264 OrthancPlugins::~OrthancPlugins()
257 { 265 {
258 Stop(); 266 Stop();
640 } 648 }
641 649
642 650
643 void OrthancPlugins::GetDicomForInstance(const void* parameters) 651 void OrthancPlugins::GetDicomForInstance(const void* parameters)
644 { 652 {
653 assert(pimpl_->context_ != NULL);
654
645 const _OrthancPluginGetDicomForInstance& p = 655 const _OrthancPluginGetDicomForInstance& p =
646 *reinterpret_cast<const _OrthancPluginGetDicomForInstance*>(parameters); 656 *reinterpret_cast<const _OrthancPluginGetDicomForInstance*>(parameters);
647 657
648 std::string dicom; 658 std::string dicom;
649 pimpl_->context_.ReadFile(dicom, p.instanceId, FileContentType_Dicom); 659 pimpl_->context_->ReadFile(dicom, p.instanceId, FileContentType_Dicom);
650 CopyToMemoryBuffer(*p.target, dicom); 660 CopyToMemoryBuffer(*p.target, dicom);
651 } 661 }
652 662
653 663
654 void OrthancPlugins::RestApiGet(const void* parameters, 664 void OrthancPlugins::RestApiGet(const void* parameters,
827 837
828 default: 838 default:
829 throw OrthancException(ErrorCode_InternalError); 839 throw OrthancException(ErrorCode_InternalError);
830 } 840 }
831 841
842 assert(pimpl_->context_ != NULL);
843
832 std::list<std::string> result; 844 std::list<std::string> result;
833 pimpl_->context_.GetIndex().LookupIdentifier(result, tag, p.argument, level); 845 pimpl_->context_->GetIndex().LookupIdentifier(result, tag, p.argument, level);
834 846
835 if (result.size() == 1) 847 if (result.size() == 1)
836 { 848 {
837 *p.result = CopyString(result.front()); 849 *p.result = CopyString(result.front());
838 } 850 }
1079 AccessDicomInstance(service, parameters); 1091 AccessDicomInstance(service, parameters);
1080 return true; 1092 return true;
1081 1093
1082 case _OrthancPluginService_RegisterStorageArea: 1094 case _OrthancPluginService_RegisterStorageArea:
1083 { 1095 {
1096 LOG(INFO) << "Plugin has registered a custom storage area";
1084 const _OrthancPluginRegisterStorageArea& p = 1097 const _OrthancPluginRegisterStorageArea& p =
1085 *reinterpret_cast<const _OrthancPluginRegisterStorageArea*>(parameters); 1098 *reinterpret_cast<const _OrthancPluginRegisterStorageArea*>(parameters);
1086 1099
1087 pimpl_->storageArea_ = p; 1100 pimpl_->storageArea_ = p;
1088 pimpl_->hasStorageArea_ = true; 1101 pimpl_->hasStorageArea_ = true;
1105 { 1118 {
1106 return false; 1119 return false;
1107 } 1120 }
1108 else 1121 else
1109 { 1122 {
1110 pimpl_->context_.GetIndex().SetGlobalProperty(static_cast<GlobalProperty>(p.property), p.value); 1123 assert(pimpl_->context_ != NULL);
1124 pimpl_->context_->GetIndex().SetGlobalProperty(static_cast<GlobalProperty>(p.property), p.value);
1111 return true; 1125 return true;
1112 } 1126 }
1113 } 1127 }
1114 1128
1115 case _OrthancPluginService_GetGlobalProperty: 1129 case _OrthancPluginService_GetGlobalProperty:
1116 { 1130 {
1131 assert(pimpl_->context_ != NULL);
1132
1117 const _OrthancPluginGlobalProperty& p = 1133 const _OrthancPluginGlobalProperty& p =
1118 *reinterpret_cast<const _OrthancPluginGlobalProperty*>(parameters); 1134 *reinterpret_cast<const _OrthancPluginGlobalProperty*>(parameters);
1119 std::string result = pimpl_->context_.GetIndex().GetGlobalProperty(static_cast<GlobalProperty>(p.property), p.value); 1135 std::string result = pimpl_->context_->GetIndex().GetGlobalProperty(static_cast<GlobalProperty>(p.property), p.value);
1120 *(p.result) = CopyString(result); 1136 *(p.result) = CopyString(result);
1121 return true; 1137 return true;
1122 } 1138 }
1123 1139
1124 case _OrthancPluginService_GetCommandLineArgumentsCount: 1140 case _OrthancPluginService_GetCommandLineArgumentsCount:
1144 *(p.result) = CopyString(arg); 1160 *(p.result) = CopyString(arg);
1145 return true; 1161 return true;
1146 } 1162 }
1147 } 1163 }
1148 1164
1165 case _OrthancPluginService_RegisterDatabaseBackend:
1166 {
1167 LOG(INFO) << "Plugin has registered a custom database back-end";
1168 const _OrthancPluginRegisterDatabaseBackend& p =
1169 *reinterpret_cast<const _OrthancPluginRegisterDatabaseBackend*>(parameters);
1170
1171 pimpl_->database_.reset(new OrthancPluginDatabase(*p.backend, p.payload));
1172 *(p.result) = reinterpret_cast<OrthancPluginDatabaseContext*>(pimpl_->database_.get());
1173
1174 return true;
1175 }
1176
1177 case _OrthancPluginService_DatabaseAnswer:
1178 {
1179 const _OrthancPluginDatabaseAnswer& p =
1180 *reinterpret_cast<const _OrthancPluginDatabaseAnswer*>(parameters);
1181 if (pimpl_->database_.get() != NULL)
1182 {
1183 pimpl_->database_->AnswerReceived(p);
1184 return true;
1185 }
1186 else
1187 {
1188 LOG(ERROR) << "Cannot invoke this service without a custom database back-end";
1189 throw OrthancException(ErrorCode_BadRequest);
1190 }
1191 }
1192
1149 default: 1193 default:
1150 return false; 1194 return false;
1151 } 1195 }
1152 } 1196 }
1153 1197
1160 1204
1161 bool OrthancPlugins::HasStorageArea() const 1205 bool OrthancPlugins::HasStorageArea() const
1162 { 1206 {
1163 return pimpl_->hasStorageArea_; 1207 return pimpl_->hasStorageArea_;
1164 } 1208 }
1209
1210 bool OrthancPlugins::HasDatabase() const
1211 {
1212 return pimpl_->database_.get() != NULL;
1213 }
1214
1165 1215
1166 1216
1167 namespace 1217 namespace
1168 { 1218 {
1169 class PluginStorageArea : public IStorageArea 1219 class PluginStorageArea : public IStorageArea
1261 1311
1262 return new PluginStorageArea(pimpl_->storageArea_); 1312 return new PluginStorageArea(pimpl_->storageArea_);
1263 } 1313 }
1264 1314
1265 1315
1316 IDatabaseWrapper& OrthancPlugins::GetDatabase()
1317 {
1318 if (!HasDatabase())
1319 {
1320 throw OrthancException(ErrorCode_BadSequenceOfCalls);
1321 }
1322
1323 return *pimpl_->database_;
1324 }
1325
1326
1327
1328
1266 1329
1267 const char* OrthancPlugins::GetProperty(const char* plugin, 1330 const char* OrthancPlugins::GetProperty(const char* plugin,
1268 _OrthancPluginProperty property) const 1331 _OrthancPluginProperty property) const
1269 { 1332 {
1270 PImpl::Property p = std::make_pair(plugin, property); 1333 PImpl::Property p = std::make_pair(plugin, property);