REC RPC library
rec_rpc_common.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011, REC Robotics Equipment Corporation GmbH, Planegg, Germany
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 - Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 - Neither the name of the REC Robotics Equipment Corporation GmbH nor the names of
14  its contributors may be used to endorse or promote products derived from this software
15  without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
68 #ifndef _REC_RPC_COMMON_H_
69 #define _REC_RPC_COMMON_H_
70 
73 
74 #include "rec/rpc/defines.h"
75 
76 #include <QHostAddress>
77 
78 namespace rec
79 {
80  namespace rpc
81  {
88 
95 
102 
109 
116 
126  REC_RPC_EXPORT void getLibraryVersion( int* major, int* minor, int* patch, QString* suffix, int* date );
127 
134 
138  const int defaultPort = 9280;
139 
148  {
150  virtual serialization::SerializablePtr createData() const = 0;
151 
159  virtual void listen( const serialization::Serializable& data, const rec::rpc::ClientInfo& client, rec::rpc::ErrorCode errorCode ) const = 0;
160  };
161 
163  typedef QSharedPointer< TopicListenerBase > TopicListenerBasePtr;
164 
165  namespace detail
166  {
167  template< typename T >
168  inline QSharedPointer< T > createSerializable()
169  {
170  return QSharedPointer< T >( new T );
171  }
172 
173  template< typename T, typename P >
174  inline QSharedPointer< T > createSerializable( const P& param )
175  {
176  return QSharedPointer< T >( new T( param ) );
177  }
178 
179  template<>
180  inline rec::rpc::serialization::SerializablePtr createSerializable()
181  {
183  }
184 
185  template< typename Parent_t, typename Data_t >
186  struct TopicListener : public rec::rpc::TopicListenerBase
187  {
188  typedef void( Parent_t::*Listen_f )( const Data_t&, const rec::rpc::ClientInfo&, rec::rpc::ErrorCode );
189 
190  TopicListener( Parent_t* parent, Listen_f listener ) : _parent( parent ), _listener( listener ) { }
191  rec::rpc::serialization::SerializablePtr createData() const { return createSerializable< Data_t >(); }
192 
193  void listen( const rec::rpc::serialization::Serializable& data, const rec::rpc::ClientInfo& client, rec::rpc::ErrorCode errorCode ) const
194  {
195  if ( typeid( data ) == typeid( Data_t ) )
196  {
197  ( _parent->*_listener )( static_cast< const Data_t& >( data ), client, errorCode );
198  }
199  else
200  {
201  Data_t d;
202  ( _parent->*_listener )( d, client, rec::rpc::WrongDataFormat );
203  }
204  }
205 
206  Parent_t* _parent;
207  Listen_f _listener;
208  };
209  }
211  }
212 }
213 
222 #define DECLARE_TOPICLISTENER( TOPICNAME ) \
223  public: \
224  void set_##TOPICNAME##_enabled( bool enable ); \
225  bool is_##TOPICNAME##_enabled() const; \
226  private: \
227  rec::rpc::TopicListenerBasePtr createTopic##TOPICNAME##Listener(); \
228  void topic##TOPICNAME( const topic##TOPICNAME##Data& data, const rec::rpc::ClientInfo& client, rec::rpc::ErrorCode errorCode );
229 
242 #define BEGIN_TOPICLISTENER_DEFINITION( CLASSNAME, TOPICNAME ) \
243  void CLASSNAME::set_##TOPICNAME##_enabled( bool enable ) \
244  { \
245  if ( enable ) \
246  { \
247  REGISTER_TOPICLISTENER( TOPICNAME ); \
248  } \
249  else \
250  { \
251  UNREGISTER_TOPICLISTENER( TOPICNAME ); \
252  } \
253  } \
254  bool CLASSNAME::is_##TOPICNAME##_enabled() const \
255  { \
256  return IS_TOPICLISTENER_REGISTERED( TOPICNAME ); \
257  } \
258  inline rec::rpc::TopicListenerBasePtr CLASSNAME::createTopic##TOPICNAME##Listener() \
259  { \
260  return rec::rpc::TopicListenerBasePtr( new rec::rpc::detail::TopicListener< CLASSNAME, topic##TOPICNAME##Data >( this, &CLASSNAME::topic##TOPICNAME ) ); \
261  } \
262  void CLASSNAME::topic##TOPICNAME( const topic##TOPICNAME##Data& data, const rec::rpc::ClientInfo& client, rec::rpc::ErrorCode errorCode ) \
263  {
264 
268 #define END_TOPICLISTENER_DEFINITION }
269 
282 #define REGISTER_TOPICLISTENER( TOPICNAME ) registerTopicListener( #TOPICNAME, createTopic##TOPICNAME##Listener() );
283 
293 #define UNREGISTER_TOPICLISTENER( TOPICNAME ) unregisterTopicListener( #TOPICNAME );
294 
302 #define IS_TOPICLISTENER_REGISTERED( TOPICNAME ) isTopicListenerRegistered( #TOPICNAME );
303 
313 #define DECLARE_TOPICINFOCHANGED( TOPICNAME ) \
314  public: \
315  void set_##TOPICNAME##_info_enabled( bool enable ); \
316  bool is_##TOPICNAME##_info_enabled() const; \
317  private: \
318  rec::rpc::TopicListenerBasePtr createTopic##TOPICNAME##InfoChanged(); \
319  void topic##TOPICNAME##_infoChanged_raw( const rec::rpc::serialization::TopicInfo& data, const rec::rpc::ClientInfo& client, rec::rpc::ErrorCode errorCode ) \
320  { \
321  topic##TOPICNAME##_infoChanged( data, errorCode ); \
322  } \
323  void topic##TOPICNAME##_infoChanged( const rec::rpc::ClientInfoSet& info, rec::rpc::ErrorCode errorCode );
324 
335 #define BEGIN_TOPICINFOCHANGED_DEFINITION( CLASSNAME, TOPICNAME ) \
336  void CLASSNAME::set_##TOPICNAME##_info_enabled( bool enable ) \
337  { \
338  if ( enable ) \
339  { \
340  REGISTER_TOPICINFOCHANGED( TOPICNAME ); \
341  } \
342  else \
343  { \
344  UNREGISTER_TOPICINFOCHANGED( TOPICNAME ); \
345  } \
346  } \
347  bool CLASSNAME::is_##TOPICNAME##_info_enabled() const \
348  { \
349  return IS_TOPICINFOCHANGED_REGISTERED( TOPICNAME ); \
350  } \
351  inline rec::rpc::TopicListenerBasePtr CLASSNAME::createTopic##TOPICNAME##InfoChanged() \
352  { \
353  return rec::rpc::TopicListenerBasePtr( new rec::rpc::detail::TopicListener< CLASSNAME, rec::rpc::serialization::TopicInfo >( this, &CLASSNAME::topic##TOPICNAME##_infoChanged_raw ) ); \
354  } \
355  void CLASSNAME::topic##TOPICNAME##_infoChanged( const rec::rpc::ClientInfoSet& info, rec::rpc::ErrorCode errorCode ) \
356  {
357 
361 #define END_TOPICINFOCHANGED_DEFINITION }
362 
374 #define REGISTER_TOPICINFOCHANGED( TOPICNAME ) registerTopicListener( #TOPICNAME "__info", createTopic##TOPICNAME##InfoChanged() );
375 
385 #define UNREGISTER_TOPICINFOCHANGED( TOPICNAME ) unregisterTopicListener( #TOPICNAME "__info" );
386 
394 #define IS_TOPICINFOCHANGED_REGISTERED( TOPICNAME ) isTopicListenerRegistered( #TOPICNAME "__info" );
395 
405 #define PREPARE_TOPIC( TOPICNAME ) \
406  const char* topicName = #TOPICNAME; \
407  topic##TOPICNAME##DataPtr dataPtr = rec::rpc::detail::createSerializable< topic##TOPICNAME##Data >(); \
408  topic##TOPICNAME##Data& data = *dataPtr;
409 
418 #define PUBLISH_TOPIC publishTopic( topicName, dataPtr );
419 
432 #define PUBLISH_TOPIC_SIMPLE( TOPICNAME, DATA ) publishTopic( #TOPICNAME, rec::rpc::detail::createSerializable< topic##TOPICNAME##Data >( DATA ) );
433 
434 #define PUBLISH_TOPIC_SIMPLE_EMPTY( TOPICNAME ) publishTopic( #TOPICNAME, rec::rpc::detail::createSerializable< rec::rpc::serialization::Serializable >() );
435 
436 #endif // _REC_RPC_COMMON_H_
ErrorCode
Pre-defined error codes.
RPC client info.
REC_RPC_EXPORT int getLibraryMajorVersion()
Get the library&#39;s major version.
virtual void listen(const serialization::Serializable &data, const rec::rpc::ClientInfo &client, rec::rpc::ErrorCode errorCode) const =0
const int defaultPort
The TCP port which will be used by default if no other one is specified.
REC_RPC_EXPORT QString getLibraryVersionString()
Get the library&#39;s version as string.
#define REC_RPC_EXPORT
Definition: defines.h:49
REC_RPC_EXPORT QString getLibraryVersionSuffix()
Get the library&#39;s version suffix.
REC_RPC_EXPORT int getLibraryDate()
Get the library&#39;s version date.
topic listener wrapper interface
REC_RPC_EXPORT void getLibraryVersion(int *major, int *minor, int *patch, QString *suffix, int *date)
Get the library&#39;s version.
REC_RPC_EXPORT int getLibraryMinorVersion()
Get the library&#39;s minor version.
REC_RPC_EXPORT int getLibraryPatchVersion()
Get the library&#39;s patch version.
virtual serialization::SerializablePtr createData() const =0