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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS Core - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 
60 //== INCLUDES =================================================================
61 
62 // -------------------- mview
63 #include "Core.hh"
64 
65 //== IMPLEMENTATION ==========================================================
66 
67 
68 
69 void Core::slotPluginExists( QString _pluginName , bool& _exists ) {
70 
71  for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
72  if ( plugins_[i].rpcName == _pluginName ) {
73  _exists = true;
74  return;
75  }
76  }
77 
78  _exists = false;
79 }
80 
81 void Core::slotFunctionExists( QString _pluginName , QString _functionName , bool& _exists ) {
82 
83  //Find plugin
84  int plugin = -1;
85  for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
86  if ( plugins_[i].rpcName == _pluginName ) {
87  plugin = i;
88  break;
89  }
90  }
91 
92  if ( plugin == -1 ) {
93  _exists = false;
94  return;
95  }
96 
97  _exists = plugins_[plugin].rpcFunctions.contains(_functionName);
98 }
99 
100 void Core::slotCall( QString _pluginName , QString _functionName , bool& _success ) {
101 
102  //Find plugin
103  int plugin = -1;
104  for ( int i = 0 ; i < (int)plugins_.size(); ++i ) {
105  if ( plugins_[i].rpcName == _pluginName ) {
106  plugin = i;
107  break;
108  }
109  }
110 
111  if ( plugin == -1 ) {
112  _success = false;
113  emit log(LOGERR, tr("Unable to call function from Plugin : ") + _pluginName + tr(" ( Plugin not Found! )"));
114  return;
115  }
116 
117  if ( !plugins_[plugin].rpcFunctions.contains(_functionName) ) {
118  _success = false;
119  emit log(LOGERR, tr("Unable to call function from Plugin : ") + _pluginName);
120  emit log(LOGERR, tr("Function ") + _functionName + tr(" not found!"));
121  return;
122  }
123 
124  scriptEngine_.evaluate(_pluginName + "." + _functionName );
125  if ( scriptEngine_.hasUncaughtException() ) {
126  _success = false;
127  QScriptValue result = scriptEngine_.uncaughtException();
128  QString exception = result.toString();
129  emit log( LOGERR , tr("RPC failed with : ") + exception );
130  return;
131  }
132 
133  _success = true;
134 
135 }
136 
137 void Core::slotCall( QString _expression , bool& _success ) {
138 
139  scriptEngine_.evaluate( _expression );
140  if ( scriptEngine_.hasUncaughtException() ) {
141  _success = false;
142  QScriptValue result = scriptEngine_.uncaughtException();
143  QString exception = result.toString();
144  emit log( LOGERR , tr("RPC failed with : ") + exception );
145  return;
146  }
147 
148  _success = true;
149 
150 }
151 
152 void Core::slotGetValue(QString _expression, QVariant& _result ){
153  //execute the expression
154  bool ok;
155 
156  slotCall("var tmpValue=" + _expression + ";", ok);
157 
158  if (!ok){
159  _result = QVariant();
160  return;
161  }
162 
163  //get the return value
164  QScriptValue val = scriptEngine_.globalObject().property("tmpValue");
165 
166 // std::cerr << "Type:" << val.toVariant().userType() << std::endl;
167 // std::cerr << "Value:" << val.toVariant().toString().toStdString()<< std::endl;
168 
169  _result = val.toVariant();
170 }
171 
172 //=============================================================================
std::vector< PluginInfo > plugins_
List of all loaded plugins_.
Definition: Core.hh:1208
void slotFunctionExists(QString _pluginName, QString _functionName, bool &_exists)
Check if a function exists.
Definition: RPC.cc:81
void slotPluginExists(QString _pluginName, bool &_exists)
Check if a plugin exists.
Definition: RPC.cc:69
void slotGetValue(QString _expression, QVariant &_result)
Definition: RPC.cc:152
void slotCall(QString _pluginName, QString _functionName, bool &_success)
Definition: RPC.cc:100
QScriptEngine scriptEngine_
Core scripting engine.
Definition: Core.hh:1302
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.