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
32using namespace fawkes;
33
34namespace 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 */
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 */
63FuseImageListContent::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,
75}
76
77/** Destructor. */
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 */
91void
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. */
110void
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 */
119bool
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 */
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
143void
145{
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
Dynamically growing buffer.
size_t buffer_size()
Get buffer size.
void append(const void *data, size_t data_size)
Append data.
void reset_iterator()
Reset iterator.
bool has_next()
Check if another element is available.
void * next(size_t *size)
Get next buffer.
void * buffer()
Get pointer to buffer.
virtual void serialize()
Serialize message content.
bool has_next()
Check if another image info is available.
void add_imageinfo(const char *image_id, colorspace_t colorspace, unsigned int pixel_width, unsigned int pixel_height)
Add image info.
FUSE_imageinfo_t * next()
Get next image info.
void copy_payload(size_t offset, void *buf, size_t len)
Copy payload into payload buffer to a specified offset.
virtual void * payload() const
Return pointer to payload.
virtual size_t payload_size() const
Return payload size.
void * _payload
Pointer to payload.
Fawkes library namespace.
Image info message.
Definition: fuse.h:168
uint32_t colorspace
color space
Definition: fuse.h:170
uint32_t height
height in pixels
Definition: fuse.h:173
uint32_t width
width in pixels
Definition: fuse.h:172
char image_id[IMAGE_ID_MAX_LENGTH]
image ID
Definition: fuse.h:169
uint32_t buffer_size
size of following image buffer in bytes
Definition: fuse.h:174
Image list message.
Definition: fuse.h:189
fawkes::dynamic_list_t image_list
DynamicBuffer holding a list of FUSE_imageinfo_t.
Definition: fuse.h:190