Fawkes API  Fawkes Development Version
fuse_imagelist_content.cpp
1 
2 /***************************************************************************
3  * fuse_imagelist_content.cpp - FUSE image list content encapsulation
4  *
5  * Created: Tue Nov 20 15:00:50 2007
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <core/exceptions/software.h>
25 #include <fvutils/net/fuse_imagelist_content.h>
26 #include <netcomm/utils/dynamic_buffer.h>
27 #include <netinet/in.h>
28 
29 #include <cstdlib>
30 #include <cstring>
31 
32 using namespace fawkes;
33 
34 namespace firevision {
35 
36 /** @class FuseImageListContent <fvutils/net/fuse_imagelist_content.h>
37  * FUSE image list content.
38  * This content provides means to send an arbitrary length list of image
39  * information chunks.
40  * @author Tim Niemueller
41  * @ingroup FUSE
42  * @ingroup FireVision
43  */
44 
45 /** Constructor.
46  * Creates an empty list.
47  */
48 FuseImageListContent::FuseImageListContent()
49 {
50  list_ = new DynamicBuffer(&(imagelist_msg_.image_list));
51 
52  _payload_size = 0;
53  _payload = NULL;
54 }
55 
56 /** Parsing constructor.
57  * Can be used with the FuseMessage::msgc() method to get correctly parsed output.
58  * @param type message type, must be FUSE_MT_IMAGE_LIST
59  * @param payload payload
60  * @param payload_size size of payload
61  * @exception TypeMismatchException thrown if the type is not FUSE_MT_IMAGE_LIST
62  */
63 FuseImageListContent::FuseImageListContent(uint32_t type, void *payload, size_t payload_size)
64 {
65  if (type != FUSE_MT_IMAGE_LIST) {
66  throw TypeMismatchException("Type %u not equal to expected type FUSE_MT_IMAGE_LIST (%u)",
67  type,
68  FUSE_MT_IMAGE_LIST);
69  }
71  void *list_payload = (void *)((size_t)payload + sizeof(FUSE_imagelist_message_t));
72  list_ = new DynamicBuffer(&(tmsg->image_list),
73  list_payload,
74  payload_size - sizeof(FUSE_imagelist_message_t));
75 }
76 
77 /** Destructor. */
78 FuseImageListContent::~FuseImageListContent()
79 {
80  delete list_;
81 }
82 
83 /** Add image info.
84  * This can only be called on contents that have been newly created, it is
85  * a bug to call this method on contents read from the network.
86  * @param image_id image ID
87  * @param colorspace colorspace
88  * @param pixel_width width of image in pixels
89  * @param pixel_height height of image in pixels
90  */
91 void
92 FuseImageListContent::add_imageinfo(const char * image_id,
93  colorspace_t colorspace,
94  unsigned int pixel_width,
95  unsigned int pixel_height)
96 {
97  FUSE_imageinfo_t imageinfo;
98  memset(&imageinfo, 0, sizeof(imageinfo));
99 
100  strncpy(imageinfo.image_id, image_id, IMAGE_ID_MAX_LENGTH - 1);
101  imageinfo.colorspace = htons(colorspace);
102  imageinfo.width = htonl(pixel_width);
103  imageinfo.height = htonl(pixel_height);
104  imageinfo.buffer_size = htonl(colorspace_buffer_size(colorspace, pixel_width, pixel_height));
105 
106  list_->append(&imageinfo, sizeof(imageinfo));
107 }
108 
109 /** Reset iterator. */
110 void
111 FuseImageListContent::reset_iterator()
112 {
113  list_->reset_iterator();
114 }
115 
116 /** Check if another image info is available.
117  * @return true if another image info is available, false otherwise
118  */
119 bool
120 FuseImageListContent::has_next()
121 {
122  return list_->has_next();
123 }
124 
125 /** Get next image info.
126  * @return next image info
127  * @exception TypeMismatchException thrown if the content contained invalid data
128  * @exception OutOfBoundsException thrown if no more data is available
129  */
131 FuseImageListContent::next()
132 {
133  size_t size;
134  void * tmp = list_->next(&size);
135  if (size != sizeof(FUSE_imageinfo_t)) {
136  throw TypeMismatchException("Image list content contains element that is of an "
137  "unexpected size");
138  }
139 
140  return (FUSE_imageinfo_t *)tmp;
141 }
142 
143 void
144 FuseImageListContent::serialize()
145 {
146  _payload_size = sizeof(FUSE_imagelist_message_t) + list_->buffer_size();
147  _payload = malloc(_payload_size);
148 
149  copy_payload(0, &imagelist_msg_, sizeof(FUSE_imagelist_message_t));
150  copy_payload(sizeof(FUSE_imagelist_message_t), list_->buffer(), list_->buffer_size());
151 }
152 
153 } // end namespace firevision
Image info message.
Definition: fuse.h:167
Fawkes library namespace.
uint32_t width
width in pixels
Definition: fuse.h:172
uint32_t colorspace
color space
Definition: fuse.h:170
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
Definition: fuse.h:169
fawkes::dynamic_list_t image_list
DynamicBuffer holding a list of FUSE_imageinfo_t.
Definition: fuse.h:190
Dynamically growing buffer.
uint32_t height
height in pixels
Definition: fuse.h:173
uint32_t buffer_size
size of following image buffer in bytes
Definition: fuse.h:174
Image list message.
Definition: fuse.h:188