annotate OrthancServer/Sources/LuaScripting.cpp @ 5037:3444990cf295

cleanup
author Alain Mazy <am@osimis.io>
date Fri, 24 Jun 2022 16:16:43 +0200
parents 06ffe03d008a
children 0ea402b4d901
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1698
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
4870
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU General Public License as
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * published by the Free Software Foundation, either version 3 of the
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * License, or (at your option) any later version.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * General Public License for more details.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "PrecompiledHeadersServer.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "LuaScripting.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
26 #include "OrthancConfiguration.h"
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
27 #include "OrthancRestApi/OrthancRestApi.h"
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "ServerContext.h"
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
29
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
30 #include "../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h"
4045
05b8fd21089c fix path
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
31 #include "../../OrthancFramework/Sources/HttpServer/StringHttpOutput.h"
05b8fd21089c fix path
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
32 #include "../../OrthancFramework/Sources/Logging.h"
05b8fd21089c fix path
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
33 #include "../../OrthancFramework/Sources/Lua/LuaFunctionCall.h"
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
4032
058b5ade8acd renaming embedded resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4031
diff changeset
35 #include <OrthancServerResources.h>
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
5023
c8c98389080f use constant for multiply occurring string
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5022
diff changeset
37 static const char* ON_HEART_BEAT = "OnHeartBeat";
c8c98389080f use constant for multiply occurring string
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5022
diff changeset
38
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 namespace Orthanc
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
42 class LuaScripting::IEvent : public IDynamicObject
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
43 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
44 public:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
45 virtual void Apply(LuaScripting& lock) = 0;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
46 };
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
47
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
48
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
49 class LuaScripting::OnStoredInstanceEvent : public LuaScripting::IEvent
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
50 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
51 private:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
52 std::string instanceId_;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
53 Json::Value simplifiedTags_;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
54 Json::Value metadata_;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
55 Json::Value origin_;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
56
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
57 public:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
58 OnStoredInstanceEvent(const std::string& instanceId,
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
59 const Json::Value& simplifiedTags,
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
60 const Json::Value& metadata,
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
61 const DicomInstanceToStore& instance) :
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
62 instanceId_(instanceId),
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
63 simplifiedTags_(simplifiedTags),
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
64 metadata_(metadata)
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
65 {
2640
c691fcf66071 ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2638
diff changeset
66 instance.GetOrigin().Format(origin_);
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
67 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
68
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
69 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
70 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
71 static const char* NAME = "OnStoredInstance";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
72
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
73 LuaScripting::Lock lock(that);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
74
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
75 if (lock.GetLua().IsExistingFunction(NAME))
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
76 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
77 that.InitializeJob();
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
78
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
79 LuaFunctionCall call(lock.GetLua(), NAME);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
80 call.PushString(instanceId_);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
81 call.PushJson(simplifiedTags_);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
82 call.PushJson(metadata_);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
83 call.PushJson(origin_);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
84 call.Execute();
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
85
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
86 that.SubmitJob();
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
87 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
88 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
89 };
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
90
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
91
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
92 class LuaScripting::ExecuteEvent : public LuaScripting::IEvent
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
93 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
94 private:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
95 std::string command_;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
96
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
97 public:
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
98 explicit ExecuteEvent(const std::string& command) :
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
99 command_(command)
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
100 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
101 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
102
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
103 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
104 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
105 LuaScripting::Lock lock(that);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
106
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
107 if (lock.GetLua().IsExistingFunction(command_.c_str()))
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
108 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
109 LuaFunctionCall call(lock.GetLua(), command_.c_str());
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
110 call.Execute();
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
111 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
112 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
113 };
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
114
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
115
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
116 class LuaScripting::StableResourceEvent : public LuaScripting::IEvent
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
117 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
118 private:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
119 ServerIndexChange change_;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
120
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
121 class GetInfoOperations : public ServerIndex::IReadOnlyOperations
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
122 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
123 private:
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
124 const ServerIndexChange& change_;
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
125 bool ok_;
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
126 DicomMap tags_;
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
127 std::map<MetadataType, std::string> metadata_;
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
128
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
129 public:
4555
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4554
diff changeset
130 explicit GetInfoOperations(const ServerIndexChange& change) :
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
131 change_(change),
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
132 ok_(false)
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
133 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
134 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
135
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
136 virtual void Apply(ServerIndex::ReadOnlyTransaction& transaction) ORTHANC_OVERRIDE
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
137 {
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
138 int64_t internalId;
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
139 ResourceType level;
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
140 if (transaction.LookupResource(internalId, level, change_.GetPublicId()) &&
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
141 level == change_.GetResourceType())
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
142 {
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
143 transaction.GetMainDicomTags(tags_, internalId);
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
144 transaction.GetAllMetadata(metadata_, internalId);
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
145 ok_ = true;
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
146 }
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
147 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
148
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
149 void CallLua(LuaScripting& that,
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
150 const char* name) const
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
151 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
152 if (ok_)
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
153 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
154 Json::Value formattedMetadata = Json::objectValue;
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
155
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
156 for (std::map<MetadataType, std::string>::const_iterator
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
157 it = metadata_.begin(); it != metadata_.end(); ++it)
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
158 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
159 std::string key = EnumerationToString(it->first);
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
160 formattedMetadata[key] = it->second;
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
161 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
162
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
163 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
164 LuaScripting::Lock lock(that);
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
165
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
166 if (lock.GetLua().IsExistingFunction(name))
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
167 {
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
168 that.InitializeJob();
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
169
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
170 Json::Value json = Json::objectValue;
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
171
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
172 if (change_.GetResourceType() == ResourceType_Study)
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
173 {
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
174 DicomMap t;
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
175 tags_.ExtractStudyInformation(t); // Discard patient-related tags
4697
569d9ef165b1 Added "short", "simplify" and/or "full" options to control the format of DICOM tags wherever possible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4606
diff changeset
176 FromDcmtkBridge::ToJson(json, t, DicomToJsonFormat_Human);
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
177 }
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
178 else
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
179 {
4697
569d9ef165b1 Added "short", "simplify" and/or "full" options to control the format of DICOM tags wherever possible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4606
diff changeset
180 FromDcmtkBridge::ToJson(json, tags_, DicomToJsonFormat_Human);
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
181 }
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
182
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
183 LuaFunctionCall call(lock.GetLua(), name);
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
184 call.PushString(change_.GetPublicId());
4554
efd90f778cd2 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4551
diff changeset
185 call.PushJson(json);
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
186 call.PushJson(formattedMetadata);
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
187 call.Execute();
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
188
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
189 that.SubmitJob();
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
190 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
191 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
192 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
193 }
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
194 };
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
195
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
196
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
197 public:
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
198 explicit StableResourceEvent(const ServerIndexChange& change) :
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
199 change_(change)
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
200 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
201 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
202
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
203 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
204 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
205 const char* name;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
206
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
207 switch (change_.GetChangeType())
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
208 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
209 case ChangeType_StablePatient:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
210 name = "OnStablePatient";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
211 break;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
212
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
213 case ChangeType_StableStudy:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
214 name = "OnStableStudy";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
215 break;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
216
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
217 case ChangeType_StableSeries:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
218 name = "OnStableSeries";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
219 break;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
220
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
221 default:
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
222 throw OrthancException(ErrorCode_InternalError);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
223 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
224
2638
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
225 {
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
226 // Avoid unnecessary calls to the database if there's no Lua callback
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
227 LuaScripting::Lock lock(that);
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
228
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
229 if (!lock.GetLua().IsExistingFunction(name))
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
230 {
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
231 return;
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
232 }
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
233 }
25ae209dcea9 Avoid unnecessary calls to the database if there is no Lua callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
234
4551
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
235 GetInfoOperations operations(change_);
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
236 that.context_.GetIndex().Apply(operations);
350a22c094f2 testing replay of transactions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
237 operations.CallLua(that, name);
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
238 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
239 };
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
240
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
241
2675
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
242 class LuaScripting::JobEvent : public LuaScripting::IEvent
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
243 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
244 public:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
245 enum Type
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
246 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
247 Type_Failure,
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
248 Type_Submitted,
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
249 Type_Success
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
250 };
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
251
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
252 private:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
253 Type type_;
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
254 std::string jobId_;
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
255
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
256 public:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
257 JobEvent(Type type,
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
258 const std::string& jobId) :
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
259 type_(type),
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
260 jobId_(jobId)
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
261 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
262 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
263
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
264 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
2675
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
265 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
266 std::string functionName;
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
267
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
268 switch (type_)
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
269 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
270 case Type_Failure:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
271 functionName = "OnJobFailure";
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
272 break;
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
273
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
274 case Type_Submitted:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
275 functionName = "OnJobSubmitted";
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
276 break;
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
277
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
278 case Type_Success:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
279 functionName = "OnJobSuccess";
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
280 break;
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
281
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
282 default:
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
283 throw OrthancException(ErrorCode_InternalError);
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
284 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
285
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
286 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
287 LuaScripting::Lock lock(that);
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
288
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
289 if (lock.GetLua().IsExistingFunction(functionName.c_str()))
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
290 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
291 LuaFunctionCall call(lock.GetLua(), functionName.c_str());
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
292 call.PushString(jobId_);
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
293 call.Execute();
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
294 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
295 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
296 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
297 };
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
298
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
299
3676
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
300 class LuaScripting::DeleteEvent : public LuaScripting::IEvent
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
301 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
302 private:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
303 ResourceType level_;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
304 std::string publicId_;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
305
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
306 public:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
307 DeleteEvent(ResourceType level,
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
308 const std::string& publicId) :
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
309 level_(level),
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
310 publicId_(publicId)
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
311 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
312 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
313
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
314 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
3676
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
315 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
316 std::string functionName;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
317
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
318 switch (level_)
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
319 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
320 case ResourceType_Patient:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
321 functionName = "OnDeletedPatient";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
322 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
323
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
324 case ResourceType_Study:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
325 functionName = "OnDeletedStudy";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
326 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
327
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
328 case ResourceType_Series:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
329 functionName = "OnDeletedSeries";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
330 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
331
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
332 case ResourceType_Instance:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
333 functionName = "OnDeletedInstance";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
334 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
335
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
336 default:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
337 throw OrthancException(ErrorCode_InternalError);
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
338 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
339
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
340 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
341 LuaScripting::Lock lock(that);
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
342
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
343 if (lock.GetLua().IsExistingFunction(functionName.c_str()))
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
344 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
345 LuaFunctionCall call(lock.GetLua(), functionName.c_str());
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
346 call.PushString(publicId_);
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
347 call.Execute();
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
348 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
349 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
350 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
351 };
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
352
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
353
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
354 class LuaScripting::UpdateEvent : public LuaScripting::IEvent
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
355 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
356 private:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
357 ResourceType level_;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
358 std::string publicId_;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
359
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
360 public:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
361 UpdateEvent(ResourceType level,
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
362 const std::string& publicId) :
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
363 level_(level),
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
364 publicId_(publicId)
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
365 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
366 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
367
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
368 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
3676
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
369 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
370 std::string functionName;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
371
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
372 switch (level_)
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
373 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
374 case ResourceType_Patient:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
375 functionName = "OnUpdatedPatient";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
376 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
377
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
378 case ResourceType_Study:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
379 functionName = "OnUpdatedStudy";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
380 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
381
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
382 case ResourceType_Series:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
383 functionName = "OnUpdatedSeries";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
384 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
385
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
386 case ResourceType_Instance:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
387 functionName = "OnUpdatedInstance";
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
388 break;
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
389
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
390 default:
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
391 throw OrthancException(ErrorCode_InternalError);
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
392 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
393
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
394 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
395 LuaScripting::Lock lock(that);
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
396
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
397 if (lock.GetLua().IsExistingFunction(functionName.c_str()))
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
398 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
399 LuaFunctionCall call(lock.GetLua(), functionName.c_str());
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
400 call.PushString(publicId_);
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
401 call.Execute();
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
402 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
403 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
404 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
405 };
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
406
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
407
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
408 ServerContext* LuaScripting::GetServerContext(lua_State *state)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
409 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
410 const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext");
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
411 return const_cast<ServerContext*>(reinterpret_cast<const ServerContext*>(value));
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
412 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
413
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
414
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
415 // Syntax in Lua: RestApiGet(uri, builtin)
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
416 int LuaScripting::RestApiGet(lua_State *state)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
417 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
418 ServerContext* serverContext = GetServerContext(state);
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
419 if (serverContext == NULL)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
420 {
1440
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1437
diff changeset
421 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
422 lua_pushnil(state);
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
423 return 1;
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
424 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
425
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
426 // Check the types of the arguments
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
427 int nArgs = lua_gettop(state);
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
428 if (nArgs < 1 || nArgs > 3 ||
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
429 !lua_isstring(state, 1) || // URI
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
430 (nArgs >= 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
431 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
432 LOG(ERROR) << "Lua: Bad parameters to RestApiGet()";
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
433 lua_pushnil(state);
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
434 return 1;
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
435 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
436
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
437 const char* uri = lua_tostring(state, 1);
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1486
diff changeset
438 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
439
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
440 std::map<std::string, std::string> headers;
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
441 LuaContext::GetDictionaryArgument(headers, state, 3, true /* HTTP header key to lower case */);
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
442
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
443 try
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
444 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
445 std::string result;
4605
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
446 if (IHttpHandler::SimpleGet(result, NULL, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
447 RequestOrigin_Lua, uri, headers) == HttpStatus_200_Ok)
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
448 {
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
449 lua_pushlstring(state, result.c_str(), result.size());
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
450 return 1;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
451 }
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
452 }
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
453 catch (OrthancException& e)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
454 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
455 LOG(ERROR) << "Lua: " << e.What();
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
456 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
457
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
458 LOG(ERROR) << "Lua: Error in RestApiGet() for URI: " << uri;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
459 lua_pushnil(state);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
460 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
461 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
462
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
463
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
464 int LuaScripting::RestApiPostOrPut(lua_State *state,
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
465 bool isPost)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
466 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
467 ServerContext* serverContext = GetServerContext(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
468 if (serverContext == NULL)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
469 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
470 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
471 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
472 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
473 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
474
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
475 // Check the types of the arguments
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
476 int nArgs = lua_gettop(state);
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
477 if (nArgs < 2 || nArgs > 4 ||
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
478 !lua_isstring(state, 1) || // URI
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
479 !lua_isstring(state, 2) || // Body
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
480 (nArgs >= 3 && !lua_isboolean(state, 3))) // Restrict to built-in API?
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
481 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
482 LOG(ERROR) << "Lua: Bad parameters to " << (isPost ? "RestApiPost()" : "RestApiPut()");
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
483 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
484 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
485 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
486
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
487 const char* uri = lua_tostring(state, 1);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
488 size_t bodySize = 0;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
489 const char* bodyData = lua_tolstring(state, 2, &bodySize);
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1486
diff changeset
490 bool builtin = (nArgs == 3 ? lua_toboolean(state, 3) != 0 : false);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
491
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
492 std::map<std::string, std::string> headers;
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
493 LuaContext::GetDictionaryArgument(headers, state, 4, true /* HTTP header key to lower case */);
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
494
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
495 try
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
496 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
497 std::string result;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
498 if (isPost ?
4605
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
499 IHttpHandler::SimplePost(result, NULL,
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
500 serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
501 RequestOrigin_Lua, uri, bodyData, bodySize, headers) == HttpStatus_200_Ok :
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
502 IHttpHandler::SimplePut(result, NULL,
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
503 serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
504 RequestOrigin_Lua, uri, bodyData, bodySize, headers) == HttpStatus_200_Ok)
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
505 {
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
506 lua_pushlstring(state, result.c_str(), result.size());
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
507 return 1;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
508 }
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
509 }
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
510 catch (OrthancException& e)
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
511 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
512 LOG(ERROR) << "Lua: " << e.What();
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
513 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
514
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
515 LOG(ERROR) << "Lua: Error in " << (isPost ? "RestApiPost()" : "RestApiPut()") << " for URI: " << uri;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
516 lua_pushnil(state);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
517 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
518 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
519
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
520
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
521 // Syntax in Lua: RestApiPost(uri, body, builtin)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
522 int LuaScripting::RestApiPost(lua_State *state)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
523 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
524 return RestApiPostOrPut(state, true);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
525 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
526
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
527
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
528 // Syntax in Lua: RestApiPut(uri, body, builtin)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
529 int LuaScripting::RestApiPut(lua_State *state)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
530 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
531 return RestApiPostOrPut(state, false);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
532 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
533
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
534
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
535 // Syntax in Lua: RestApiDelete(uri, builtin)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
536 int LuaScripting::RestApiDelete(lua_State *state)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
537 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
538 ServerContext* serverContext = GetServerContext(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
539 if (serverContext == NULL)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
540 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
541 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
542 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
543 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
544 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
545
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
546 // Check the types of the arguments
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
547 int nArgs = lua_gettop(state);
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
548 if (nArgs < 1 || nArgs > 3 ||
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
549 !lua_isstring(state, 1) || // URI
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
550 (nArgs >= 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
551 {
1449
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1447
diff changeset
552 LOG(ERROR) << "Lua: Bad parameters to RestApiDelete()";
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
553 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
554 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
555 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
556
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
557 const char* uri = lua_tostring(state, 1);
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1486
diff changeset
558 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
559
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
560 std::map<std::string, std::string> headers;
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
561 LuaContext::GetDictionaryArgument(headers, state, 3, true /* HTTP header key to lower case */);
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3187
diff changeset
562
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
563 try
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
564 {
4605
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
565 if (IHttpHandler::SimpleDelete(NULL, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
c8f444e8556d new function in the plugin SDK: OrthancPluginCallRestApi()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4507
diff changeset
566 RequestOrigin_Lua, uri, headers) == HttpStatus_200_Ok)
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
567 {
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
568 lua_pushboolean(state, 1);
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
569 return 1;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
570 }
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
571 }
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
572 catch (OrthancException& e)
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
573 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
574 LOG(ERROR) << "Lua: " << e.What();
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
575 }
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
576
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
577 LOG(ERROR) << "Lua: Error in RestApiDelete() for URI: " << uri;
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
578 lua_pushnil(state);
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
579
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
580 return 1;
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
581 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
582
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
583
1587
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
584 // Syntax in Lua: GetOrthancConfiguration()
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
585 int LuaScripting::GetOrthancConfiguration(lua_State *state)
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
586 {
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
587 Json::Value configuration;
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
588
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
589 {
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
590 OrthancConfiguration::ReaderLock lock;
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
591 configuration = lock.GetJson();
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
592 }
1587
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
593
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
594 LuaContext::GetLuaContext(state).PushJson(configuration);
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
595
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
596 return 1;
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
597 }
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
598
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
599
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
600 size_t LuaScripting::ParseOperation(LuaJobManager::Lock& lock,
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
601 const std::string& operation,
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
602 const Json::Value& parameters)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
603 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
604 if (operation == "delete")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
605 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
606 LOG(INFO) << "Lua script to delete resource " << parameters["Resource"].asString();
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
607 return lock.AddDeleteResourceOperation(context_);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 if (operation == "store-scu")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
612 std::string localAet;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
613 if (parameters.isMember("LocalAet"))
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
614 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
615 localAet = parameters["LocalAet"].asString();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
616 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
617 else
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
618 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
619 localAet = context_.GetDefaultLocalApplicationEntityTitle();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
620 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
621
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
622 std::string name = parameters["Modality"].asString();
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
623 RemoteModalityParameters modality;
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
624
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
625 {
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
626 OrthancConfiguration::ReaderLock configLock;
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
627 modality = configLock.GetConfiguration().GetModalityUsingSymbolicName(name);
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
628 }
2222
21713ce8717b Fix handling of Move Originator AET and ID in C-MOVE SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
629
21713ce8717b Fix handling of Move Originator AET and ID in C-MOVE SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
630 // This is not a C-MOVE: No need to call "StoreScuCommand::SetMoveOriginator()"
3894
8f7ad4989fec transcoding to uncompressed transfer syntaxes over DICOM protocol is implemented
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3851
diff changeset
631 return lock.AddStoreScuOperation(context_, localAet, modality);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 if (operation == "store-peer")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 {
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
636 OrthancConfiguration::ReaderLock configLock;
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
637 std::string name = parameters["Peer"].asString();
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
639 WebServiceParameters peer;
2944
f395460af74d simplifying OrthancConfiguration for modalities/peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2940
diff changeset
640 if (configLock.GetConfiguration().LookupOrthancPeer(peer, name))
2799
6e3a60b85da6 New primitives to access Orthanc peers from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2738
diff changeset
641 {
6e3a60b85da6 New primitives to access Orthanc peers from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2738
diff changeset
642 return lock.AddStorePeerOperation(peer);
6e3a60b85da6 New primitives to access Orthanc peers from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2738
diff changeset
643 }
6e3a60b85da6 New primitives to access Orthanc peers from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2738
diff changeset
644 else
6e3a60b85da6 New primitives to access Orthanc peers from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2738
diff changeset
645 {
2955
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2944
diff changeset
646 throw OrthancException(ErrorCode_UnknownResource,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2944
diff changeset
647 "No peer with symbolic name: " + name);
2799
6e3a60b85da6 New primitives to access Orthanc peers from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2738
diff changeset
648 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 if (operation == "modify")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3676
diff changeset
653 std::unique_ptr<DicomModification> modification(new DicomModification);
2519
2e6b7862ccf2 ParseAnonymizationRequest/ParseModifyRequest now in DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
654 modification->ParseModifyRequest(parameters);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
656 return lock.AddModifyInstanceOperation(context_, modification.release());
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
658
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 if (operation == "call-system")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
661 LOG(INFO) << "Lua script to call system command on " << parameters["Resource"].asString();
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
663 const Json::Value& argsIn = parameters["Arguments"];
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
664 if (argsIn.type() != Json::arrayValue)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
665 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
666 throw OrthancException(ErrorCode_BadParameterType);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
667 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
668
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
669 std::vector<std::string> args;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
670 args.reserve(argsIn.size());
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
671 for (Json::Value::ArrayIndex i = 0; i < argsIn.size(); ++i)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
672 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
673 // http://jsoncpp.sourceforge.net/namespace_json.html#7d654b75c16a57007925868e38212b4e
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
674 switch (argsIn[i].type())
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
675 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
676 case Json::stringValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
677 args.push_back(argsIn[i].asString());
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
678 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
679
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
680 case Json::intValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
681 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asInt()));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
682 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
683
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
684 case Json::uintValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
685 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asUInt()));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
686 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
687
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
688 case Json::realValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
689 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asFloat()));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
690 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
691
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
692 default:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
693 throw OrthancException(ErrorCode_BadParameterType);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
694 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
695 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
696
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
697 std::string command = parameters["Command"].asString();
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
698 std::vector<std::string> postArgs;
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
699
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
700 return lock.AddSystemCallOperation(command, args, postArgs);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
701 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
702
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
703 throw OrthancException(ErrorCode_ParameterOutOfRange);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
704 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
705
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
706
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
707 void LuaScripting::InitializeJob()
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
708 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
709 lua_.Execute("_InitializeJob()");
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
710 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
711
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
712
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
713 void LuaScripting::SubmitJob()
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
714 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
715 Json::Value operations;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
716 LuaFunctionCall call2(lua_, "_AccessJob");
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
717 call2.ExecuteToJson(operations, false);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
718
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
719 if (operations.type() != Json::arrayValue)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
720 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
721 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
722 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
723
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
724 LuaJobManager::Lock lock(jobManager_, context_.GetJobsEngine());
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
725
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
726 bool isFirst = true;
2727
815e30657dad fix warning
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2675
diff changeset
727 size_t previous = 0; // Dummy initialization to avoid warning
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
728
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
729 for (Json::Value::ArrayIndex i = 0; i < operations.size(); ++i)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
730 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
731 if (operations[i].type() != Json::objectValue ||
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
732 !operations[i].isMember("Operation"))
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
733 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
734 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
735 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
736
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
737 const Json::Value& parameters = operations[i];
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
738 if (!parameters.isMember("Resource"))
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
739 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
740 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
741 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
742
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
743 std::string operation = parameters["Operation"].asString();
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
744 size_t index = ParseOperation(lock, operation, operations[i]);
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
745
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
746 std::string resource = parameters["Resource"].asString();
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
747 if (!resource.empty())
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
748 {
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
749 lock.AddDicomInstanceInput(index, context_, resource);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
750 }
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
751 else if (!isFirst)
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
752 {
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
753 lock.Connect(previous, index);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
754 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
755
2609
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
756 isFirst = false;
f7a84b551ee4 switch Lua to new jobs engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2519
diff changeset
757 previous = index;
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
758 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
759 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
760
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
761
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
762 LuaScripting::LuaScripting(ServerContext& context) :
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
763 context_(context),
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
764 state_(State_Setup),
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
765 heartBeatPeriod_(0)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
766 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
767 lua_.SetGlobalVariable("_ServerContext", &context);
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
768 lua_.RegisterFunction("RestApiGet", RestApiGet);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
769 lua_.RegisterFunction("RestApiPost", RestApiPost);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
770 lua_.RegisterFunction("RestApiPut", RestApiPut);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
771 lua_.RegisterFunction("RestApiDelete", RestApiDelete);
1587
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
772 lua_.RegisterFunction("GetOrthancConfiguration", GetOrthancConfiguration);
2738
3a55b77339ff fix deadlock in Lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2727
diff changeset
773
3a55b77339ff fix deadlock in Lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2727
diff changeset
774 LOG(INFO) << "Initializing Lua for the event handler";
3a55b77339ff fix deadlock in Lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2727
diff changeset
775 LoadGlobalConfiguration();
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
776 }
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
777
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
778
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
779 LuaScripting::~LuaScripting()
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
780 {
2651
1da5a052c777 testing value serialization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2649
diff changeset
781 if (state_ == State_Running)
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
782 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
783 LOG(ERROR) << "INTERNAL ERROR: LuaScripting::Stop() should be invoked manually to avoid mess in the destruction order!";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
784 Stop();
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
785 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
786 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
787
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
788 void LuaScripting::HeartBeatThread(LuaScripting* that)
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
789 {
5022
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
790 static const unsigned int GRANULARITY = 100; // In milliseconds
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
791
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
792 const boost::posix_time::time_duration PERIODICITY =
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
793 boost::posix_time::seconds(that->heartBeatPeriod_);
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
794
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
795 boost::posix_time::ptime next =
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
796 boost::posix_time::microsec_clock::universal_time() + PERIODICITY;
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
797
5018
eb8ca3403983 fix: lock mutex when accessing state in HeartBeatThread
Alain Mazy <am@osimis.io>
parents: 5017
diff changeset
798 bool shouldStop = false;
eb8ca3403983 fix: lock mutex when accessing state in HeartBeatThread
Alain Mazy <am@osimis.io>
parents: 5017
diff changeset
799
eb8ca3403983 fix: lock mutex when accessing state in HeartBeatThread
Alain Mazy <am@osimis.io>
parents: 5017
diff changeset
800 while (!shouldStop)
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
801 {
5022
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
802 boost::this_thread::sleep(boost::posix_time::milliseconds(GRANULARITY));
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
803
5018
eb8ca3403983 fix: lock mutex when accessing state in HeartBeatThread
Alain Mazy <am@osimis.io>
parents: 5017
diff changeset
804 if (boost::posix_time::microsec_clock::universal_time() >= next)
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
805 {
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
806 LuaScripting::Lock lock(*that);
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
807
5023
c8c98389080f use constant for multiply occurring string
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5022
diff changeset
808 if (lock.GetLua().IsExistingFunction(ON_HEART_BEAT))
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
809 {
5023
c8c98389080f use constant for multiply occurring string
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5022
diff changeset
810 LuaFunctionCall call(lock.GetLua(), ON_HEART_BEAT);
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
811 call.Execute();
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
812 }
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
813
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
814 next = boost::posix_time::microsec_clock::universal_time() + PERIODICITY;
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
815 }
5018
eb8ca3403983 fix: lock mutex when accessing state in HeartBeatThread
Alain Mazy <am@osimis.io>
parents: 5017
diff changeset
816
5022
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
817 {
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
818 boost::recursive_mutex::scoped_lock lock(that->mutex_);
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
819 shouldStop = (that->state_ == State_Done);
3d0a084e1ace fix to non-static variable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5018
diff changeset
820 }
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
821 }
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
822 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
823
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
824 void LuaScripting::EventThread(LuaScripting* that)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
825 {
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
826 for (;;)
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
827 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3676
diff changeset
828 std::unique_ptr<IDynamicObject> event(that->pendingEvents_.Dequeue(100));
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
829
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
830 if (event.get() == NULL)
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
831 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
832 // The event queue is empty, check whether we should stop
2618
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2616
diff changeset
833 boost::recursive_mutex::scoped_lock lock(that->mutex_);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
834
2651
1da5a052c777 testing value serialization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2649
diff changeset
835 if (that->state_ != State_Running)
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
836 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
837 return;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
838 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
839 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
840 else
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
841 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
842 try
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
843 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
844 dynamic_cast<IEvent&>(*event).Apply(*that);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
845 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
846 catch (OrthancException& e)
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
847 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
848 LOG(ERROR) << "Error while processing Lua events: " << e.What();
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
849 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
850 }
3851
6498739a3c3c refactoring: TimeoutDicomConnectionManager is now only used by Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3845
diff changeset
851
6498739a3c3c refactoring: TimeoutDicomConnectionManager is now only used by Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3845
diff changeset
852 that->jobManager_.GetDicomConnectionManager().CloseIfInactive();
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
853 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
854 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
855
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
856
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
857 void LuaScripting::Start()
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
858 {
2618
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2616
diff changeset
859 boost::recursive_mutex::scoped_lock lock(mutex_);
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
860
2651
1da5a052c777 testing value serialization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2649
diff changeset
861 if (state_ != State_Setup ||
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
862 eventThread_.joinable() /* already started */)
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
863 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
864 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
865 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
866 else
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
867 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
868 LOG(INFO) << "Starting the Lua engine";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
869 eventThread_ = boost::thread(EventThread, this);
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
870
5026
bafef43b6f37 cppcheck
Alain Mazy <am@osimis.io>
parents: 5018
diff changeset
871 LuaScripting::Lock luaLock(*this);
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
872
5027
Alain Mazy <am@osimis.io>
parents: 5026 5023
diff changeset
873 if (heartBeatPeriod_ > 0 && luaLock.GetLua().IsExistingFunction(ON_HEART_BEAT))
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
874 {
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
875 LOG(INFO) << "Starting the Lua HeartBeat thread with a period of " << heartBeatPeriod_ << " seconds";
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
876 heartBeatThread_ = boost::thread(HeartBeatThread, this);
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
877 }
2665
389d050a2e66 fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2651
diff changeset
878 state_ = State_Running;
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
879 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
880 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
881
1572
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
882
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
883 void LuaScripting::Stop()
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
884 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
885 {
2618
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2616
diff changeset
886 boost::recursive_mutex::scoped_lock lock(mutex_);
1572
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
887
2651
1da5a052c777 testing value serialization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2649
diff changeset
888 if (state_ != State_Running)
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
889 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
890 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
891 }
1572
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
892
2651
1da5a052c777 testing value serialization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2649
diff changeset
893 state_ = State_Done;
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
894 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
895
2620
1232922c8793 speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2618
diff changeset
896 jobManager_.AwakeTrailingSleep();
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
897
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
898 if (eventThread_.joinable())
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
899 {
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
900 LOG(INFO) << "Stopping the Lua engine";
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
901 eventThread_.join();
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
902 if (heartBeatThread_.joinable())
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
903 {
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
904 heartBeatThread_.join();
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
905 }
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
906 LOG(INFO) << "The Lua engine has stopped";
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
907 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
908 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
909
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
910
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
911 void LuaScripting::SignalStoredInstance(const std::string& publicId,
3916
0e3849268a55 new plugin SDK primitives related to OrthancPluginDicomInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3894
diff changeset
912 const DicomInstanceToStore& instance,
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
913 const Json::Value& simplifiedTags)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
914 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
915 Json::Value metadata = Json::objectValue;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
916
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
917 for (ServerIndex::MetadataMap::const_iterator
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
918 it = instance.GetMetadata().begin();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
919 it != instance.GetMetadata().end(); ++it)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
920 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
921 if (it->first.first == ResourceType_Instance)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
922 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
923 metadata[EnumerationToString(it->first.second)] = it->second;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
924 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
925 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
926
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
927 pendingEvents_.Enqueue(new OnStoredInstanceEvent(publicId, simplifiedTags, metadata, instance));
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
928 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
929
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
930
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
931 void LuaScripting::SignalChange(const ServerIndexChange& change)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
932 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
933 if (change.GetChangeType() == ChangeType_StablePatient ||
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
934 change.GetChangeType() == ChangeType_StableStudy ||
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
935 change.GetChangeType() == ChangeType_StableSeries)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
936 {
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
937 pendingEvents_.Enqueue(new StableResourceEvent(change));
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
938 }
3676
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
939 else if (change.GetChangeType() == ChangeType_Deleted)
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
940 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
941 pendingEvents_.Enqueue(new DeleteEvent(change.GetResourceType(), change.GetPublicId()));
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
942 }
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
943 else if (change.GetChangeType() == ChangeType_UpdatedAttachment ||
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
944 change.GetChangeType() == ChangeType_UpdatedMetadata)
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
945 {
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
946 pendingEvents_.Enqueue(new UpdateEvent(change.GetResourceType(), change.GetPublicId()));
231b46ce1984 Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
947 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
948 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
949
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
950
1574
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
951 bool LuaScripting::FilterIncomingInstance(const DicomInstanceToStore& instance,
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
952 const Json::Value& simplified)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
953 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
954 static const char* NAME = "ReceivedInstanceFilter";
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
955
1456
68827c07e683 fix recursive calls with Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
956 boost::recursive_mutex::scoped_lock lock(mutex_);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
957
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
958 if (lua_.IsExistingFunction(NAME))
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
959 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
960 LuaFunctionCall call(lua_, NAME);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
961 call.PushJson(simplified);
1574
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
962
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
963 Json::Value origin;
2640
c691fcf66071 ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2638
diff changeset
964 instance.GetOrigin().Format(origin);
1574
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
965 call.PushJson(origin);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
966
3845
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
967 Json::Value info = Json::objectValue;
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
968 info["HasPixelData"] = instance.HasPixelData();
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
969
4507
b4c58795f3a8 widening the use of DicomTransferSyntax enum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4460
diff changeset
970 DicomTransferSyntax s;
3845
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
971 if (instance.LookupTransferSyntax(s))
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
972 {
4507
b4c58795f3a8 widening the use of DicomTransferSyntax enum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4460
diff changeset
973 info["TransferSyntaxUID"] = GetTransferSyntaxUid(s);
3845
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
974 }
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
975
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
976 call.PushJson(info);
281045a1e6db new "info" field in "ReceivedInstanceFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
977
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
978 if (!call.ExecutePredicate())
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
979 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
980 return false;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
981 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
982 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
983
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
984 return true;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
985 }
1455
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
986
4921
309fb4f02704 fix signature of OrthancPluginIncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4892
diff changeset
987 bool LuaScripting::FilterIncomingCStoreInstance(uint16_t& dimseStatus,
309fb4f02704 fix signature of OrthancPluginIncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4892
diff changeset
988 const DicomInstanceToStore& instance,
309fb4f02704 fix signature of OrthancPluginIncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4892
diff changeset
989 const Json::Value& simplified)
4796
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
990 {
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
991 static const char* NAME = "ReceivedCStoreInstanceFilter";
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
992
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
993 boost::recursive_mutex::scoped_lock lock(mutex_);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
994
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
995 if (lua_.IsExistingFunction(NAME))
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
996 {
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
997 LuaFunctionCall call(lua_, NAME);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
998 call.PushJson(simplified);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
999
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1000 Json::Value origin;
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1001 instance.GetOrigin().Format(origin);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1002 call.PushJson(origin);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1003
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1004 Json::Value info = Json::objectValue;
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1005 info["HasPixelData"] = instance.HasPixelData();
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1006
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1007 DicomTransferSyntax s;
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1008 if (instance.LookupTransferSyntax(s))
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1009 {
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1010 info["TransferSyntaxUID"] = GetTransferSyntaxUid(s);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1011 }
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1012
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1013 call.PushJson(info);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1014
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1015 int result;
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1016 call.ExecuteToInt(result);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1017 return static_cast<uint16_t>(result);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1018 }
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1019
4921
309fb4f02704 fix signature of OrthancPluginIncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4892
diff changeset
1020 return true;
4796
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1021 }
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4697
diff changeset
1022
1455
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
1023
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
1024 void LuaScripting::Execute(const std::string& command)
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
1025 {
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1026 pendingEvents_.Enqueue(new ExecuteEvent(command));
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1027 }
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1028
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1029
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1030 void LuaScripting::LoadGlobalConfiguration()
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1031 {
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
1032 OrthancConfiguration::ReaderLock configLock;
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
1033
4031
e3b3af80732d ServerResources, and moving EmbeddedResourceHttpHandler from Core to OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4030
diff changeset
1034 {
e3b3af80732d ServerResources, and moving EmbeddedResourceHttpHandler from Core to OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4030
diff changeset
1035 std::string command;
e3b3af80732d ServerResources, and moving EmbeddedResourceHttpHandler from Core to OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4030
diff changeset
1036 Orthanc::ServerResources::GetFileResource(command, Orthanc::ServerResources::LUA_TOOLBOX);
e3b3af80732d ServerResources, and moving EmbeddedResourceHttpHandler from Core to OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4030
diff changeset
1037 lua_.Execute(command);
e3b3af80732d ServerResources, and moving EmbeddedResourceHttpHandler from Core to OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4030
diff changeset
1038 }
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1039
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1040 std::list<std::string> luaScripts;
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
1041 configLock.GetConfiguration().GetListOfStringsParameter(luaScripts, "LuaScripts");
5017
0d61efc6256c Added Lua OnHeartBeat()
Alain Mazy <am@osimis.io>
parents: 4921
diff changeset
1042 heartBeatPeriod_ = configLock.GetConfiguration().GetIntegerParameter("LuaHeartBeatPeriod", 0);
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1043
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1044 LuaScripting::Lock lock(*this);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1045
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1046 for (std::list<std::string>::const_iterator
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1047 it = luaScripts.begin(); it != luaScripts.end(); ++it)
1455
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
1048 {
2940
4767d36679ed refactoring access to Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2799
diff changeset
1049 std::string path = configLock.GetConfiguration().InterpretStringParameterAsPath(*it);
2616
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1050 LOG(INFO) << "Installing the Lua scripts from: " << path;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1051 std::string script;
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1052 SystemToolbox::ReadFile(script, path);
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1053
2f3007bf0708 event queues in Lua, serialization of sequence of operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2609
diff changeset
1054 lock.GetLua().Execute(script);
1455
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
1055 }
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
1056 }
2675
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1057
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1058
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1059 void LuaScripting::SignalJobSubmitted(const std::string& jobId)
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1060 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1061 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Submitted, jobId));
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1062 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1063
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1064
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1065 void LuaScripting::SignalJobSuccess(const std::string& jobId)
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1066 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1067 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Success, jobId));
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1068 }
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1069
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1070
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1071 void LuaScripting::SignalJobFailure(const std::string& jobId)
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1072 {
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1073 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Failure, jobId));
3fc310ceb6d4 lua callbacks for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2665
diff changeset
1074 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1075 }