ilist_content.cpp

00001 
00002 /***************************************************************************
00003  *  net_ilist_content.cpp - BlackBoard network: interface list content
00004  *
00005  *  Created: Mon Mar 03 12:04:51 2008
00006  *  Copyright  2006-2008  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 #include <blackboard/net/ilist_content.h>
00025 
00026 #include <netcomm/utils/dynamic_buffer.h>
00027 #include <netcomm/fawkes/component_ids.h>
00028 #include <core/exceptions/software.h>
00029 #include <cstdlib>
00030 #include <cstring>
00031 
00032 namespace fawkes {
00033 
00034 /** @class BlackBoardInterfaceListContent <blackboard/net/ilist_content.h>
00035  * BlackBoard interface list content.
00036  * A complex dynamic message with an arbitrary number of interfaces. Uses
00037  * DynamicBuffer for the internal list of plugins and thus the buffer is
00038  * limited to 4 GB in total.
00039  *
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor. */
00044 BlackBoardInterfaceListContent::BlackBoardInterfaceListContent()
00045 {
00046   interface_list = new DynamicBuffer(&(msg.interface_list));
00047 }
00048 
00049 
00050 /** Message content constructor.
00051  * This constructor is meant to be used with FawkesNetworkMessage::msgc().
00052  * @param component_id component ID
00053  * @param msg_id message ID
00054  * @param payload message payload
00055  * @param payload_size total payload size
00056  */
00057 BlackBoardInterfaceListContent::BlackBoardInterfaceListContent(unsigned int component_id,
00058                                                                unsigned int msg_id,
00059                                                                void *payload,
00060                                                                size_t payload_size)
00061 {
00062   if ( component_id != FAWKES_CID_BLACKBOARD ) {
00063     throw TypeMismatchException("BlackBoardInterfaceListContent: invalid component ID");
00064   }
00065   bb_ilist_msg_t *tmsg = (bb_ilist_msg_t *)payload;
00066   void *ilist_payload = (void *)((size_t)payload + sizeof(msg));
00067   interface_list = new DynamicBuffer(&(tmsg->interface_list), ilist_payload,
00068                                      payload_size - sizeof(msg));
00069 }
00070 
00071 
00072 /** Destructor. */
00073 BlackBoardInterfaceListContent::~BlackBoardInterfaceListContent()
00074 {
00075   delete interface_list;
00076   if (_payload != NULL) {
00077     free(_payload);
00078     _payload = NULL;
00079     _payload_size = 0;
00080   }
00081 }
00082 
00083 
00084 
00085 /** Append interface info.
00086  * @param type type of interface
00087  * @param id ID of interface instance
00088  * @param hash version hash of interface instance/type
00089  * @param serial instance serial
00090  * @param has_writer true if a writer exists, false otherwise
00091  * @param num_readers number of readers
00092  */
00093 void
00094 BlackBoardInterfaceListContent::append_interface(const char *type, const char *id,
00095                                                  const unsigned char *hash,
00096                                                  unsigned int serial,
00097                                                  bool has_writer, unsigned int num_readers)
00098 {
00099   bb_iinfo_msg_t info;
00100   memset(&info, 0, sizeof(info));
00101   strncpy(info.type, type, __INTERFACE_TYPE_SIZE);
00102   strncpy(info.id, id, __INTERFACE_ID_SIZE);
00103   memcpy(info.hash, hash, __INTERFACE_HASH_SIZE);
00104   interface_list->append(&info, sizeof(info));
00105   info.serial      = serial;
00106   info.has_writer  = has_writer ? 1 : 0;
00107   info.num_readers = num_readers;
00108 }
00109 
00110 
00111 /** Append interface info.
00112  * @param iinfo interface info
00113  */
00114 void
00115 BlackBoardInterfaceListContent::append_interface(InterfaceInfo &iinfo)
00116 {
00117   bb_iinfo_msg_t info;
00118   memset(&info, 0, sizeof(info));
00119   strncpy(info.type, iinfo.type(), __INTERFACE_TYPE_SIZE);
00120   strncpy(info.id, iinfo.id(), __INTERFACE_ID_SIZE);
00121   memcpy(info.hash, iinfo.hash(), __INTERFACE_HASH_SIZE);
00122   info.serial      = iinfo.serial();
00123   info.has_writer  = iinfo.has_writer() ? 1 : 0;
00124   info.num_readers = iinfo.num_readers();
00125   interface_list->append(&info, sizeof(info));
00126 }
00127 
00128 
00129 void
00130 BlackBoardInterfaceListContent::serialize()
00131 {
00132   _payload_size = sizeof(msg) + interface_list->buffer_size();
00133   _payload = malloc(_payload_size);
00134   copy_payload(0, &msg, sizeof(msg));
00135   copy_payload(sizeof(msg), interface_list->buffer(), interface_list->buffer_size());
00136 }
00137 
00138 
00139 /** Reset iterator.
00140  * For incoming messages only.
00141  */
00142 void
00143 BlackBoardInterfaceListContent::reset_iterator()
00144 {
00145   interface_list->reset_iterator();
00146 }
00147 
00148 
00149 /** Check if more list elements are available.
00150  * For incoming messages only.
00151  * @return true if there are more elements available, false otherwise.
00152  */
00153 bool
00154 BlackBoardInterfaceListContent::has_next()
00155 {
00156   return interface_list->has_next();
00157 }
00158 
00159 
00160 /** Get next plugin from list.
00161  * @param size upon return contains the size of the returned data element.
00162  * @return next config entitiy from the list. The value is only of the type of
00163  * the header. Check the message type and the size and cast the message to the correct
00164  * entity.
00165  */
00166 bb_iinfo_msg_t *
00167 BlackBoardInterfaceListContent::next(size_t *size)
00168 {
00169   void *tmp = interface_list->next(size);
00170   return (bb_iinfo_msg_t *)tmp;
00171 }
00172 
00173 } // end namespace fawkes

Generated on Tue Feb 22 13:31:25 2011 for Fawkes API by  doxygen 1.4.7