Mercurial > hg > orthanc
annotate Core/Lua/LuaFunctionCall.cpp @ 2425:59b7aa77a8da
compatibility of GDCM sample with Orthanc SDK <= 1.3.0
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Oct 2017 12:44:58 +0200 |
parents | a3a65de1840f |
children | 878b59270859 |
rev | line source |
---|---|
386 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2218
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
386 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
34 #include "../PrecompiledHeaders.h" |
386 | 35 #include "LuaFunctionCall.h" |
36 | |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
37 #include "../OrthancException.h" |
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
38 #include "../Logging.h" |
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
39 |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
40 #include <cassert> |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
41 #include <stdio.h> |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
42 #include <boost/lexical_cast.hpp> |
386 | 43 |
44 namespace Orthanc | |
45 { | |
46 void LuaFunctionCall::CheckAlreadyExecuted() | |
47 { | |
48 if (isExecuted_) | |
49 { | |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
50 throw OrthancException(ErrorCode_LuaAlreadyExecuted); |
386 | 51 } |
52 } | |
53 | |
54 LuaFunctionCall::LuaFunctionCall(LuaContext& context, | |
55 const char* functionName) : | |
56 context_(context), | |
57 isExecuted_(false) | |
58 { | |
59 // Clear the stack to fulfill the invariant | |
60 lua_settop(context_.lua_, 0); | |
61 lua_getglobal(context_.lua_, functionName); | |
62 } | |
63 | |
64 void LuaFunctionCall::PushString(const std::string& value) | |
65 { | |
66 CheckAlreadyExecuted(); | |
1465
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1448
diff
changeset
|
67 lua_pushlstring(context_.lua_, value.c_str(), value.size()); |
386 | 68 } |
69 | |
70 void LuaFunctionCall::PushBoolean(bool value) | |
71 { | |
72 CheckAlreadyExecuted(); | |
73 lua_pushboolean(context_.lua_, value); | |
74 } | |
75 | |
76 void LuaFunctionCall::PushInteger(int value) | |
77 { | |
78 CheckAlreadyExecuted(); | |
79 lua_pushinteger(context_.lua_, value); | |
80 } | |
81 | |
82 void LuaFunctionCall::PushDouble(double value) | |
83 { | |
84 CheckAlreadyExecuted(); | |
85 lua_pushnumber(context_.lua_, value); | |
86 } | |
87 | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
88 void LuaFunctionCall::PushJson(const Json::Value& value) |
386 | 89 { |
90 CheckAlreadyExecuted(); | |
1051 | 91 context_.PushJson(value); |
386 | 92 } |
93 | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
94 void LuaFunctionCall::ExecuteInternal(int numOutputs) |
386 | 95 { |
96 CheckAlreadyExecuted(); | |
97 | |
98 assert(lua_gettop(context_.lua_) >= 1); | |
99 int nargs = lua_gettop(context_.lua_) - 1; | |
100 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0); | |
101 | |
102 if (error) | |
103 { | |
104 assert(lua_gettop(context_.lua_) >= 1); | |
105 | |
106 std::string description(lua_tostring(context_.lua_, -1)); | |
107 lua_pop(context_.lua_, 1); /* pop error message from the stack */ | |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
108 LOG(ERROR) << description; |
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
109 |
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
110 throw OrthancException(ErrorCode_CannotExecuteLua); |
386 | 111 } |
112 | |
113 if (lua_gettop(context_.lua_) < numOutputs) | |
114 { | |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
115 throw OrthancException(ErrorCode_LuaBadOutput); |
386 | 116 } |
117 | |
118 isExecuted_ = true; | |
119 } | |
120 | |
121 bool LuaFunctionCall::ExecutePredicate() | |
122 { | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
123 ExecuteInternal(1); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
124 |
386 | 125 if (!lua_isboolean(context_.lua_, 1)) |
126 { | |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
127 throw OrthancException(ErrorCode_NotLuaPredicate); |
386 | 128 } |
129 | |
130 return lua_toboolean(context_.lua_, 1) != 0; | |
131 } | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
132 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
133 |
1658
54bafe0e7e7b
Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1583
diff
changeset
|
134 void LuaFunctionCall::ExecuteToJson(Json::Value& result, |
54bafe0e7e7b
Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1583
diff
changeset
|
135 bool keepStrings) |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
136 { |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
137 ExecuteInternal(1); |
1658
54bafe0e7e7b
Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1583
diff
changeset
|
138 context_.GetJson(result, lua_gettop(context_.lua_), keepStrings); |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
139 } |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
140 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
141 |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
142 void LuaFunctionCall::ExecuteToString(std::string& result) |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
143 { |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
144 ExecuteInternal(1); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
145 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
146 int top = lua_gettop(context_.lua_); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
147 if (lua_isstring(context_.lua_, top)) |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
148 { |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
149 result = lua_tostring(context_.lua_, top); |
1010 | 150 } |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
151 else |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
152 { |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
153 throw OrthancException(ErrorCode_LuaReturnsNoString); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
154 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
155 } |
1959
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
156 |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
157 |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
158 void LuaFunctionCall::PushStringMap(const std::map<std::string, std::string>& value) |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
159 { |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
160 Json::Value json = Json::objectValue; |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
161 |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
162 for (std::map<std::string, std::string>::const_iterator |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
163 it = value.begin(); it != value.end(); ++it) |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
164 { |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
165 json[it->first] = it->second; |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
166 } |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
167 |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
168 PushJson(json); |
45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
169 } |
2218
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
170 |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
171 |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
172 void LuaFunctionCall::PushDicom(const DicomMap& dicom) |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
173 { |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
174 DicomArray a(dicom); |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
175 PushDicom(a); |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
176 } |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
177 |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
178 |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
179 void LuaFunctionCall::PushDicom(const DicomArray& dicom) |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
180 { |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
181 Json::Value value = Json::objectValue; |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
182 |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
183 for (size_t i = 0; i < dicom.GetSize(); i++) |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
184 { |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
185 const DicomValue& v = dicom.GetElement(i).GetValue(); |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
186 std::string s = (v.IsNull() || v.IsBinary()) ? "" : v.GetContent(); |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
187 value[dicom.GetElement(i).GetTag().Format()] = s; |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
188 } |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
189 |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
190 PushJson(value); |
3eefb84ac0bd
dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1959
diff
changeset
|
191 } |
386 | 192 } |