casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QtDBusXmlApp.qo.h
Go to the documentation of this file.
1 //# QtDBusXmlApp.qo.h: Abstract parent to use the CASA DBus server.
2 //# Copyright (C) 2009
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id: $
27 #ifndef QTDBUSXMLAPP_QO_H_
28 #define QTDBUSXMLAPP_QO_H_
29 
32 
33 #include <QDBusAbstractAdaptor>
34 #include <QDBusConnection>
35 
36 namespace casa {
37 
38 //# Forward Declarations.
39 class QtDBusXmlAppAdaptor;
40 
41 
42 // Abstract parent of classes that want to register themselves with CASA's
43 // DBus server. The class also provides public static methods for other code
44 // to send DBus messages without needing to subclass and register. This class
45 // hides the details of how the DBus communication happens behind the scenes
46 // and presents an interface using CASA objects like Records. Applications
47 // that want to use this DBus communication layer need to know about the
48 // following for each method call:
49 // * The object name. Each object that registers with CASA's DBus server must
50 // do so with a unique name. Outside objects then can send messages to that
51 // object by using its name. Names have some restrictions; see
52 // dbusRegisterSelf(). You can check if a certain name is currently
53 // registered using dbusNameIsRegistered().
54 // * The method name. Each object keeps track of what methods it supports, and
55 // this process happens dynamically on a per-message basis.
56 // * Whether to call asynchronously or not. See dbusXmlCallNoRet().
57 // * The method parameters. The parameters are set using name/value pairs in a
58 // Record. Like the method name, this is on a dynamic, per-object basis.
59 // IMPORTANT: not all types are currently supported; see QtDBusXML
60 // documentation to see what is currently implemented.
61 // * The method return value. IMPORTANT: not all types are currently
62 // supported; see QtDBusXML documentation to see what is currently
63 // implemented.
64 //
65 // If a class wants to receive DBus communications, it must take the following
66 // steps:
67 // 1) Subclass QtDBusXmlApp.
68 // 2) Implement the dbusRunXmlMethod() method, which is called when a DBus method
69 // call is received. For a discussion of the parameters to this method, see
70 // above.
71 // 3) Call dbusRegisterSelf() with a unique name.
72 //
73 // Classes that wish to send but not receive messages do NOT need to subclass
74 // QtDBusXmlApp and can just use the public static methods to call methods on
75 // DBus-registered objects. For a discussion of the parameters to these
76 // static methods, see above.
77 class QtDBusXmlApp : public QtDBusApp {
78 
79  //# Friend class declarations.
80  friend class QtDBusXmlAppAdaptor;
81 
82 public:
83 
84  // Calls the given method on the object with the given name that is
85  // registered with CASA's DBus server, using the given parameters. The
86  // given from name is sent to the remote object, but not used otherwise. If
87  // isAsync is true, then the remote method runs asynchronously, which means
88  // that control returns immediately after sending the message. This method
89  // does NOT give a return value, even if the remote method does. Returns
90  // true for success, false for failure. Will fail if the given object
91  // name is not registered with CASA's DBus server.
92  static bool dbusXmlCallNoRet(const casacore::String& fromName,
93  const casacore::String& objectName, const casacore::String& methodName,
94  const casacore::Record& parameters, bool isAsync = false);
95 
96  // Like dbusXmlCallNoRet(), except that if the remote method has a
97  // return value of the given type, then the value is set accordingly. If
98  // there is no return value or it is a different type, the value is not
99  // set. If retValueSet is given, it will be set to true if the return
100  // value was set and false otherwise.
101  // <group>
102  static bool dbusXmlCall(const casacore::String& fromName,
103  const casacore::String& objectName, const casacore::String& methodName,
104  const casacore::Record& parameters, bool& retValue,
105  bool* retValueSet = NULL);
106  static bool dbusXmlCall(const casacore::String& fromName,
107  const casacore::String& objectName, const casacore::String& methodName,
108  const casacore::Record& parameters, int& retValue,
109  bool* retValueSet = NULL);
110  static bool dbusXmlCall(const casacore::String& fromName,
111  const casacore::String& objectName, const casacore::String& methodName,
112  const casacore::Record& parameters, casacore::uInt& retValue,
113  bool* retValueSet = NULL);
114  static bool dbusXmlCall(const casacore::String& fromName,
115  const casacore::String& objectName, const casacore::String& methodName,
116  const casacore::Record& parameters, double& retValue,
117  bool* retValueSet = NULL);
118  static bool dbusXmlCall(const casacore::String& fromName,
119  const casacore::String& objectName, const casacore::String& methodName,
120  const casacore::Record& parameters, casacore::String& retValue,
121  bool* retValueSet = NULL);
122  static bool dbusXmlCall(const casacore::String& fromName,
123  const casacore::String& objectName, const casacore::String& methodName,
124  const casacore::Record& parameters, casacore::Record& retValue,
125  bool* retValueSet = NULL);
126  static bool dbusXmlCall(const casacore::String& fromName,
127  const casacore::String& objectName, const casacore::String& methodName,
128  const casacore::Record& parameters, casacore::Array<bool>& retValue,
129  bool* retValueSet = NULL);
130  static bool dbusXmlCall(const casacore::String& fromName,
131  const casacore::String& objectName, const casacore::String& methodName,
132  const casacore::Record& parameters, casacore::Array<int>& retValue,
133  bool* retValueSet = NULL);
134  // </group>
135 
136 protected:
137  // Constructor.
138  QtDBusXmlApp();
139 
140  // Destructor. Unregisters from the CASA DBus server if needed.
141  virtual ~QtDBusXmlApp();
142 
143 
144  // ABSTRACT METHODS //
145 
146  // Runs the method with the specified name using the given parameters and
147  // placing the return value, if any, into the given retValue record. NOTE:
148  // when defining the return value, the name doesn't matter because the
149  // first entry is used. The caller name, and whether this is an
150  // asynchronous call or not, are also provided but do not need to be used.
151  // Note, however, that asynchronous method calls will NOT use a return
152  // value even if one is set.
153  virtual void dbusRunXmlMethod(const casacore::String& methodName,
154  const casacore::Record& parameters, casacore::Record& retValue,
155  const casacore::String& callerName, bool isAsync) = 0;
156 
157 
158  // VIRTUAL METHODS //
159 
160  // Method that can be overridden if the subclass wants to be informed
161  // whenever ANY dbus message is received, even if this object is not the
162  // intended recipient. Note that most applications won't need to do this
163  // (and probably shouldn't) since dbusRunXmlMethod() will be called with the
164  // appropriate parameters if this object is the intended recipient.
165  virtual void dbusXmlReceived(const QtDBusXML& xml) { (void)xml; }
166 
167 
168  // IMPLEMENTED METHODS //
169 
170  // Registers this object with CASA's DBus server, if it is not already,
171  // with the given name and returns whether or not the registration
172  // succeeded. If the name is blank, then the last set name is used, UNLESS
173  // this is the first time registering in which case the registration will
174  // fail. The registration name MUST contain only numbers, letters, and
175  // underscores, and MUST be unique for the DBus server; trying to register
176  // with a name that is already in use will result in registration failure.
177  // (Note, however, that the name needs ONLY to be unique within the CASA
178  // DBus application names rather than all system-wide DBus application
179  // names.) Trying to register when already registered (see
180  // dbusSelfIsRegistered()) will result in registration failure; to change
181  // names, you must unregister and then reregister with the new name.
182  bool dbusRegisterSelf(const casacore::String& name = "");
183 
184  // Unregisters this object with CASA's DBus server, if it is registered.
185  void dbusUnregisterSelf();
186 
187  // Returns true if this object is currently registered with CASA's DBus
188  // server, false otherwise.
189  bool dbusSelfIsRegistered() const;
190 
191  // Returns the name that this object is registered with with CASA's DBus
192  // server, or an empty casacore::String if this application is not currently
193  // registered.
195 
196  // Calls the static version of the method with this application's name.
197  // <group>
198  bool dbusXmlCallNoRet(const casacore::String& objectName,
199  const casacore::String& methodName, const casacore::Record& parameters,
200  bool isAsync = false) {
201  return dbusXmlCallNoRet(dbusSelfRegisteredName(), objectName,
202  methodName, parameters, isAsync); }
203  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
204  const casacore::Record& parameters, bool& retValue,
205  bool* retValueSet = NULL) {
206  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
207  parameters, retValue, retValueSet); }
208  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
209  const casacore::Record& parameters, int& retValue,
210  bool* retValueSet = NULL) {
211  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
212  parameters, retValue, retValueSet); }
213  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
214  const casacore::Record& parameters, casacore::uInt& retValue,
215  bool* retValueSet = NULL) {
216  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
217  parameters, retValue, retValueSet); }
218  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
219  const casacore::Record& parameters, double& retValue,
220  bool* retValueSet = NULL) {
221  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
222  parameters, retValue, retValueSet); }
223  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
224  const casacore::Record& parameters, casacore::String& retValue,
225  bool* retValueSet = NULL) {
226  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
227  parameters, retValue, retValueSet); }
228  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
229  const casacore::Record& parameters, casacore::Record& retValue,
230  bool* retValueSet = NULL) {
231  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
232  parameters, retValue, retValueSet); }
233  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
234  const casacore::Record& parameters, casacore::Array<bool>& retValue,
235  bool* retValueSet = NULL) {
236  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
237  parameters, retValue, retValueSet); }
238  bool dbusXmlCall(const casacore::String& objectName, const casacore::String& methodName,
239  const casacore::Record& parameters, casacore::Array<int>& retValue,
240  bool* retValueSet = NULL) {
241  return dbusXmlCall(dbusSelfRegisteredName(), objectName, methodName,
242  parameters, retValue, retValueSet); }
243  // </group>
244 
245 private:
246 
247  // Flag for whether the application is currently registered or not.
249 
250  // Name that the application is registered with.
251  QString dbusName_;
252 
253  // DBus adaptor.
255 
256 
257  // Method for when one of the slots in the adaptor is activated. First
258  // sends to dbusXmlReceived(), then to dbusRunXmlMethod().
259  void dbusSlot(QtDBusXML& xml);
260 
261 
262  // Private Static Methods //
263 
264  // Helper method for calling remote methods.
265  static bool dbusXmlCall(const casacore::String& from, const casacore::String& to,
266  const casacore::String& methodName, bool methodIsAsync,
267  const casacore::Record& parameters, casacore::Record* retValue);
268 };
269 
270 
271 // Subclass of QDBusAbstractAdaptor for use with CASA's QtDBusXmlApp class. This
272 // class is a very thin layer on top of QtDBusXmlApp.
273 class QtDBusXmlAppAdaptor : public QDBusAbstractAdaptor {
274  Q_OBJECT
275 
276  // Interface name definition.
277  // <group>
278  #define CASA_DBUS_XML_INTERFACE "edu.nrao.casa.QtDBusXmlApp"
279  Q_CLASSINFO("D-Bus Interface", "edu.nrao.casa.QtDBusXmlApp")
280  // </group>
281 
282  //# Friend class declarations.
283  friend class QtDBusXmlApp;
284 
285 public slots:
286  // Slot for receiving messages. If its name is changed,
287  // QtDBusXmlApp::DBUS_MESSAGE_SLOT must be updated.
288  QString xmlSlot(const QString& xml);
289 
290 private:
291  // Constructor which takes the application.
292  QtDBusXmlAppAdaptor(QtDBusXmlApp& app);
293 
294  // Destructor.
296 
297 
298  // Application.
299  QtDBusXmlApp& itsApp_;
300 
301  // Dummy object.
302  QObject* itsObject_;
303 };
304 
305 }
306 
307 #endif /* QTDBUSAPP_QO_H_ */
QObject * itsObject_
Dummy object.
virtual void dbusXmlReceived(const QtDBusXML &xml)
VIRTUAL METHODS //.
QtDBusXmlApp & itsApp_
Application.
#define private
Definition: miriad.h:68
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, casacore::Array< int > &retValue, bool *retValueSet=NULL)
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, casacore::String &retValue, bool *retValueSet=NULL)
Subclass of QDomDocument that represents an XML scheme that is to be used with CASA&#39;s Qt dbus communi...
Definition: QtDBusXML.h:55
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, casacore::uInt &retValue, bool *retValueSet=NULL)
casacore::String dbusSelfRegisteredName() const
Returns the name that this object is registered with with CASA&#39;s DBus server, or an empty casacore::S...
QtDBusXmlApp()
Constructor.
virtual ~QtDBusXmlApp()
Destructor.
void dbusUnregisterSelf()
Unregisters this object with CASA&#39;s DBus server, if it is registered.
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
QString dbusName_
Name that the application is registered with.
bool dbusRegisterSelf(const casacore::String &name="")
IMPLEMENTED METHODS //.
QtDBusXmlAppAdaptor * dbusAdaptor_
DBus adaptor.
static bool dbusXmlCall(const casacore::String &fromName, const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, bool &retValue, bool *retValueSet=NULL)
Like dbusXmlCallNoRet(), except that if the remote method has a return value of the given type...
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, casacore::Record &retValue, bool *retValueSet=NULL)
QString xmlSlot(const QString &xml)
Slot for receiving messages.
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, int &retValue, bool *retValueSet=NULL)
Abstract parent of classes that want to register themselves with CASA&#39;s DBus server.
A hierarchical collection of named fields of various types.
Definition: Record.h:180
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, bool &retValue, bool *retValueSet=NULL)
bool dbusXmlCallNoRet(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, bool isAsync=false)
Calls the static version of the method with this application&#39;s name.
bool dbusSelfIsRegistered() const
Returns true if this object is currently registered with CASA&#39;s DBus server, false otherwise...
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, double &retValue, bool *retValueSet=NULL)
Subclass of QDBusAbstractAdaptor for use with CASA&#39;s QtDBusXmlApp class.
bool dbusRegistered_
Flag for whether the application is currently registered or not.
void dbusSlot(QtDBusXML &xml)
Method for when one of the slots in the adaptor is activated.
String: the storage and methods of handling collections of characters.
Definition: String.h:223
static bool dbusXmlCallNoRet(const casacore::String &fromName, const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, bool isAsync=false)
Calls the given method on the object with the given name that is registered with CASA&#39;s DBus server...
bool dbusXmlCall(const casacore::String &objectName, const casacore::String &methodName, const casacore::Record &parameters, casacore::Array< bool > &retValue, bool *retValueSet=NULL)
virtual void dbusRunXmlMethod(const casacore::String &methodName, const casacore::Record &parameters, casacore::Record &retValue, const casacore::String &callerName, bool isAsync)=0
ABSTRACT METHODS //.
unsigned int uInt
Definition: aipstype.h:51