Developer Documentation

RPC.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44
45
46
47 //=============================================================================
48 //
49 // CLASS Core - IMPLEMENTATION
50 //
51 //=============================================================================
52
53
54 //== INCLUDES =================================================================
55
56 // -------------------- mview
57 #include "Core.hh"
58
59 //== IMPLEMENTATION ==========================================================
60
61
62
63 void Core::slotPluginExists( QString _pluginName , bool& _exists ) {
64
65 for ( int i = 0 ; i < (int)plugins().size(); ++i ) {
66 if ( plugins()[i].rpcName == _pluginName ) {
67 _exists = true;
68 return;
69 }
70 }
71
72 _exists = false;
73 }
74
75 void Core::slotFunctionExists( QString _pluginName , QString _functionName , bool& _exists ) {
76
77 //Find plugin
78 int plugin = -1;
79 for ( int i = 0 ; i < (int)plugins().size(); ++i ) {
80 if ( plugins()[i].rpcName == _pluginName ) {
81 plugin = i;
82 break;
83 }
84 }
85
86 if ( plugin == -1 ) {
87 _exists = false;
88 return;
89 }
90
91 _exists = plugins()[plugin].rpcFunctions.contains(_functionName);
92 }
93
94 void Core::slotCall( QString _pluginName , QString _functionName , bool& _success ) {
95
96 //Find plugin
97 int plugin = -1;
98 for ( int i = 0 ; i < (int)plugins().size(); ++i ) {
99 if ( plugins()[i].rpcName == _pluginName ) {
100 plugin = i;
101 break;
102 }
103 }
104
105 if ( plugin == -1 ) {
106 _success = false;
107 emit log(LOGERR, tr("Unable to call function from Plugin : ") + _pluginName + tr(" ( Plugin not Found! )"));
108 return;
109 }
110
111 if ( !plugins()[plugin].rpcFunctions.contains(_functionName) ) {
112 _success = false;
113 emit log(LOGERR, tr("Unable to call function from Plugin : ") + _pluginName);
114 emit log(LOGERR, tr("Function ") + _functionName + tr(" not found!"));
115 return;
116 }
117
118 scriptEngine_.evaluate(_pluginName + "." + _functionName );
119 if ( scriptEngine_.hasUncaughtException() ) {
120 _success = false;
121 QScriptValue result = scriptEngine_.uncaughtException();
122 QString exception = result.toString();
123 emit log( LOGERR , tr("RPC failed with : ") + exception );
124 return;
125 }
126
127 _success = true;
128
129 }
130
131 void Core::slotCall( QString _expression , bool& _success ) {
132
133 scriptEngine_.evaluate( _expression );
134 if ( scriptEngine_.hasUncaughtException() ) {
135 _success = false;
136 QScriptValue result = scriptEngine_.uncaughtException();
137 QString exception = result.toString();
138 emit log( LOGERR , tr("RPC failed with : ") + exception );
139 return;
140 }
141
142 _success = true;
143
144 }
145
146 void Core::slotGetValue(QString _expression, QVariant& _result ){
147 //execute the expression
148 bool ok;
149
150 slotCall("var tmpValue=" + _expression + ";", ok);
151
152 if (!ok){
153 _result = QVariant();
154 return;
155 }
156
157 //get the return value
158 QScriptValue val = scriptEngine_.globalObject().property("tmpValue");
159
160 // std::cerr << "Type:" << val.toVariant().userType() << std::endl;
161 // std::cerr << "Value:" << val.toVariant().toString().toStdString()<< std::endl;
162
163 _result = val.toVariant();
164 }
165
166 //=============================================================================
Core::slotFunctionExists
void slotFunctionExists(QString _pluginName, QString _functionName, bool &_exists)
Check if a function exists.
Definition: RPC.cc:75
Core.hh
Core::log
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
LOGERR
Definition: LoggingInterface.hh:96
Core::scriptEngine_
QScriptEngine scriptEngine_
Core scripting engine.
Definition: Core.hh:1342
Core::slotGetValue
void slotGetValue(QString _expression, QVariant &_result)
Definition: RPC.cc:146
Core::plugins
std::vector< PluginInfo > & plugins()
Index of Plugins toolbox widget.
Definition: Core.cc:720
Core::slotCall
void slotCall(QString _pluginName, QString _functionName, bool &_success)
Definition: RPC.cc:94
Core::slotPluginExists
void slotPluginExists(QString _pluginName, bool &_exists)
Check if a plugin exists.
Definition: RPC.cc:63