message.h

00001
00002 /***************************************************************************
00003  *  message.h - BlackBoard message
00004  *
00005  *  Created: Sun Oct 08 00:08:10 2006
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #ifndef __INTERFACE_MESSAGE_H_
00025 #define __INTERFACE_MESSAGE_H_
00026 
00027 #include <interface/field_iterator.h>
00028 #include <interface/types.h>
00029 #include <core/utils/refcount.h>
00030
00031 #define __INTERFACE_MESSAGE_TYPE_SIZE 32
00032 
00033 namespace fawkes {
00034
00035 class Mutex;
00036 class Interface;
00037 class InterfaceFieldIterator;
00038
00039 class Message : public RefCount
00040 {
00041  friend class Interface;
00042  public:
00043   Message(const char *type);
00044   Message(const Message *mesg);
00045   Message(const Message &mesg);
00046   virtual ~Message();
00047
00048   Message &         operator=  (const Message & m);
00049
00050   unsigned int      id() const;
00051   void              set_id(unsigned int message_id);
00052   void              mark_enqueued();
00053   bool              enqueued() const;
00054
00055   unsigned int      sender_id() const;
00056   const char *      sender_thread_name() const;
00057   Interface *       interface() const;
00058   const char *      type() const;
00059
00060   InterfaceFieldIterator     fields();
00061   InterfaceFieldIterator     fields_end();
00062
00063   unsigned int      num_fields() const;
00064
00065   const void *      datachunk() const;
00066   unsigned int      datasize() const;
00067
00068   unsigned int      hops() const;
00069   void              set_hops(unsigned int hops);
00070
00071   void              set_from_chunk(const void *chunk);
00072
00073   unsigned int      recipient() const;
00074
00075   virtual Message * clone() const;
00076 
00077   /** Check if message has desired type.
00078    * @return true, if message has desired type, false otherwise
00079    */
00080   template <class MessageType>
00081     bool           is_of_type();
00082
00083  private:
00084   void              set_interface(Interface *iface);
00085
00086   unsigned int  __message_id;
00087   unsigned int  __hops;
00088   bool          __enqueued;
00089
00090   unsigned int  recipient_interface_mem_serial;
00091   unsigned int  sender_interface_instance_serial;
00092
00093   char          *_type;
00094   char          *_sender_thread_name;
00095   unsigned int   _sender_id;
00096
00097   Interface     *_transmit_via_iface;
00098
00099   interface_fieldinfo_t  *__fieldinfo_list;
00100
00101   unsigned int __num_fields;
00102
00103  protected:
00104   void add_fieldinfo(interface_fieldtype_t type, const char *name,
00105                      size_t length, void *value);
00106
00107   void         *data_ptr;
00108   unsigned int  data_size;
00109 };
00110
00111 template <class MessageType>
00112 bool
00113 Message::is_of_type()
00114 {
00115   return (dynamic_cast<MessageType *>(this) != 0);
00116 }
00117
00118
00119 } // end namespace fawkes
00120
00121 #endif