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 <fvutils/net/fuse_imagelist_content.h>
25 #include <netcomm/utils/dynamic_buffer.h>
26 #include <core/exceptions/software.h>
27 
28 #include <cstdlib>
29 #include <cstring>
30 #include <netinet/in.h>
31 
32 using namespace fawkes;
33 
34 namespace firevision {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 /** @class FuseImageListContent <fvutils/net/fuse_imagelist_content.h>
40  * FUSE image list content.
41  * This content provides means to send an arbitrary length list of image
42  * information chunks.
43  * @author Tim Niemueller
44  * @ingroup FUSE
45  * @ingroup FireVision
46  */
47 
48 /** Constructor.
49  * Creates an empty list.
50  */
51 FuseImageListContent::FuseImageListContent()
52 {
53  __list = new DynamicBuffer(&(__imagelist_msg.image_list));
54 
55  _payload_size = 0;
56  _payload = NULL;
57 }
58 
59 
60 /** Parsing constructor.
61  * Can be used with the FuseMessage::msgc() method to get correctly parsed output.
62  * @param type message type, must be FUSE_MT_IMAGE_LIST
63  * @param payload payload
64  * @param payload_size size of payload
65  * @exception TypeMismatchException thrown if the type is not FUSE_MT_IMAGE_LIST
66  */
67 FuseImageListContent::FuseImageListContent(uint32_t type, void *payload, size_t payload_size)
68 {
69  if ( type != FUSE_MT_IMAGE_LIST ) {
70  throw TypeMismatchException("Type %u not equal to expected type FUSE_MT_IMAGE_LIST (%u)",
71  type, FUSE_MT_IMAGE_LIST);
72  }
74  void *list_payload = (void *)((size_t)payload + sizeof(FUSE_imagelist_message_t));
75  __list = new DynamicBuffer(&(tmsg->image_list), list_payload,
76  payload_size - sizeof(FUSE_imagelist_message_t));
77 }
78 
79 
80 /** Destructor. */
81 FuseImageListContent::~FuseImageListContent()
82 {
83  delete __list;
84 }
85 
86 
87 /** Add image info.
88  * This can only be called on contents that have been newly created, it is
89  * a bug to call this method on contents read from the network.
90  * @param image_id image ID
91  * @param colorspace colorspace
92  * @param pixel_width width of image in pixels
93  * @param pixel_height height of image in pixels
94  */
95 void
96 FuseImageListContent::add_imageinfo(const char *image_id, colorspace_t colorspace,
97  unsigned int pixel_width, unsigned int pixel_height)
98 {
99  FUSE_imageinfo_t imageinfo;
100  memset(&imageinfo, 0, sizeof(imageinfo));
101 
102  strncpy(imageinfo.image_id, image_id, IMAGE_ID_MAX_LENGTH);
103  imageinfo.colorspace = htons(colorspace);
104  imageinfo.width = htonl(pixel_width);
105  imageinfo.height = htonl(pixel_height);
106  imageinfo.buffer_size = htonl(colorspace_buffer_size(colorspace, pixel_width, pixel_height));
107 
108  __list->append(&imageinfo, sizeof(imageinfo));
109 }
110 
111 
112 /** Reset iterator. */
113 void
114 FuseImageListContent::reset_iterator()
115 {
116  __list->reset_iterator();
117 }
118 
119 
120 /** Check if another image info is available.
121  * @return true if another image info is available, false otherwise
122  */
123 bool
124 FuseImageListContent::has_next()
125 {
126  return __list->has_next();
127 }
128 
129 
130 /** Get next image info.
131  * @return next image info
132  * @exception TypeMismatchException thrown if the content contained invalid data
133  * @exception OutOfBoundsException thrown if no more data is available
134  */
136 FuseImageListContent::next()
137 {
138  size_t size;
139  void *tmp = __list->next(&size);
140  if ( size != sizeof(FUSE_imageinfo_t) ) {
141  throw TypeMismatchException("Image list content contains element that is of an "
142  "unexpected size");
143  }
144 
145  return (FUSE_imageinfo_t *)tmp;
146 }
147 
148 
149 void
150 FuseImageListContent::serialize()
151 {
152  _payload_size = sizeof(FUSE_imagelist_message_t) + __list->buffer_size();
153  _payload = malloc(_payload_size);
154 
155  copy_payload(0, &__imagelist_msg, sizeof(FUSE_imagelist_message_t));
156  copy_payload(sizeof(FUSE_imagelist_message_t), __list->buffer(), __list->buffer_size());
157 }
158 
159 } // end namespace firevision
Image info message.
Definition: fuse.h:165
Fawkes library namespace.
uint32_t width
width in pixels
Definition: fuse.h:169
uint32_t colorspace
color space
Definition: fuse.h:167
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
Definition: fuse.h:166
fawkes::dynamic_list_t image_list
DynamicBuffer holding a list of FUSE_imageinfo_t.
Definition: fuse.h:185
Dynamically growing buffer.
uint32_t height
height in pixels
Definition: fuse.h:170
uint32_t buffer_size
size of following image buffer in bytes
Definition: fuse.h:171
Image list message.
Definition: fuse.h:184