fuse_imagelist_content.cpp

00001
00002 /***************************************************************************
00003  *  fuse_imagelist_content.cpp - FUSE image list content encapsulation
00004  *
00005  *  Created: Tue Nov 20 15:00:50 2007
00006  *  Copyright  2005-2007  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 <fvutils/net/fuse_imagelist_content.h>
00025 #include <netcomm/utils/dynamic_buffer.h>
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstdlib>
00029 #include <cstring>
00030 #include <netinet/in.h>
00031
00032 using namespace fawkes;
00033 
00034 /** @class FuseImageListContent <fvutils/net/fuse_imagelist_content.h>
00035  * FUSE image list content.
00036  * This content provides means to send an arbitrary length list of image
00037  * information chunks.
00038  * @author Tim Niemueller
00039  * @ingroup FUSE
00040  * @ingroup FireVision
00041  */
00042 
00043 /** Constructor.
00044  * Creates an empty list.
00045  */
00046 FuseImageListContent::FuseImageListContent()
00047 {
00048   __list = new DynamicBuffer(&(__imagelist_msg.image_list));
00049
00050   _payload_size = 0;
00051   _payload      = NULL;
00052 }
00053
00054 
00055 /** Parsing constructor.
00056  * Can be used with the FuseMessage::msgc() method to get correctly parsed output.
00057  * @param type message type, must be FUSE_MT_IMAGE_LIST
00058  * @param payload payload
00059  * @param payload_size size of payload
00060  * @exception TypeMismatchException thrown if the type is not FUSE_MT_IMAGE_LIST
00061  */
00062 FuseImageListContent::FuseImageListContent(uint32_t type, void *payload, size_t payload_size)
00063 {
00064   if ( type != FUSE_MT_IMAGE_LIST ) {
00065     throw TypeMismatchException("Type %u not equal to expected type FUSE_MT_IMAGE_LIST (%u)",
00066                                 type, FUSE_MT_IMAGE_LIST);
00067   }
00068   FUSE_imagelist_message_t *tmsg = (FUSE_imagelist_message_t *)payload;
00069   void *list_payload = (void *)((size_t)payload + sizeof(FUSE_imagelist_message_t));
00070   __list = new DynamicBuffer(&(tmsg->image_list), list_payload,
00071                              payload_size - sizeof(FUSE_imagelist_message_t));
00072 }
00073
00074 
00075 /** Destructor. */
00076 FuseImageListContent::~FuseImageListContent()
00077 {
00078   delete __list;
00079 }
00080
00081 
00082 /** Add image info.
00083  * This can only be called on contents that have been newly created, it is
00084  * a bug to call this method on contents read from the network.
00085  * @param image_id image ID
00086  * @param colorspace colorspace
00087  * @param pixel_width width of image in pixels
00088  * @param pixel_height height of image in pixels
00089  */
00090 void
00091 FuseImageListContent::add_imageinfo(const char *image_id, colorspace_t colorspace,
00092                                     unsigned int pixel_width, unsigned int pixel_height)
00093 {
00094   FUSE_imageinfo_t imageinfo;
00095   memset(&imageinfo, 0, sizeof(imageinfo));
00096
00097   strncpy(imageinfo.image_id, image_id, IMAGE_ID_MAX_LENGTH);
00098   imageinfo.colorspace = htons(colorspace);
00099   imageinfo.width = htonl(pixel_width);
00100   imageinfo.height = htonl(pixel_height);
00101   imageinfo.buffer_size = htonl(colorspace_buffer_size(colorspace, pixel_width, pixel_height));
00102
00103   __list->append(&imageinfo, sizeof(imageinfo));
00104 }
00105
00106 
00107 /** Reset iterator. */
00108 void
00109 FuseImageListContent::reset_iterator()
00110 {
00111   __list->reset_iterator();
00112 }
00113
00114 
00115 /** Check if another image info is available.
00116  * @return true if another image info is available, false otherwise
00117  */
00118 bool
00119 FuseImageListContent::has_next()
00120 {
00121   return __list->has_next();
00122 }
00123
00124 
00125 /** Get next image info.
00126  * @return next image info
00127  * @exception TypeMismatchException thrown if the content contained invalid data
00128  * @exception OutOfBoundsException thrown if no more data is available
00129  */
00130 FUSE_imageinfo_t *
00131 FuseImageListContent::next()
00132 {
00133   size_t size;
00134   void *tmp = __list->next(&size);
00135   if ( size != sizeof(FUSE_imageinfo_t) ) {
00136     throw TypeMismatchException("Image list content contains element that is of an "
00137                                 "unexpected size");
00138   }
00139
00140   return (FUSE_imageinfo_t *)tmp;
00141 }
00142
00143
00144 void
00145 FuseImageListContent::serialize()
00146 {
00147   _payload_size = sizeof(FUSE_imagelist_message_t) + __list->buffer_size();
00148   _payload = malloc(_payload_size);
00149
00150   copy_payload(0, &__imagelist_msg, sizeof(FUSE_imagelist_message_t));
00151   copy_payload(sizeof(FUSE_imagelist_message_t), __list->buffer(), __list->buffer_size());
00152 }