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

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1