1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2011, Purdue Research Foundation |
---|
4 | * |
---|
5 | * Author: George A. Howlett <gah@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <pthread.h> |
---|
9 | #include <semaphore.h> |
---|
10 | #include <cstdlib> |
---|
11 | #include <list> |
---|
12 | |
---|
13 | #ifndef _RESPONSE_QUEUE_H |
---|
14 | #define _RESPONSE_QUEUE_H |
---|
15 | |
---|
16 | namespace Rappture { |
---|
17 | namespace VtkVis { |
---|
18 | |
---|
19 | /** |
---|
20 | * \brief Holds data for a response to be sent to client |
---|
21 | */ |
---|
22 | class Response |
---|
23 | { |
---|
24 | public: |
---|
25 | enum AllocationType { |
---|
26 | STATIC, |
---|
27 | DYNAMIC, |
---|
28 | VOLATILE |
---|
29 | }; |
---|
30 | enum ResponseType { |
---|
31 | IMAGE, /**< Image to be displayed. */ |
---|
32 | LEGEND, /**< Legend to be displayed. */ |
---|
33 | DATA /**< Any other type of message. */ |
---|
34 | }; |
---|
35 | |
---|
36 | Response(ResponseType type) : |
---|
37 | _mesg(NULL), |
---|
38 | _length(0), |
---|
39 | _type(type) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | virtual ~Response() |
---|
44 | { |
---|
45 | if (_length > 0) { |
---|
46 | if (_allocType == DYNAMIC) { |
---|
47 | free(_mesg); |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /// Get the ResponseType |
---|
53 | ResponseType type() |
---|
54 | { |
---|
55 | return _type; |
---|
56 | } |
---|
57 | |
---|
58 | /// Get the Response data |
---|
59 | unsigned char *message() |
---|
60 | { |
---|
61 | return _mesg; |
---|
62 | } |
---|
63 | |
---|
64 | /// Get the number of bytes in the Response data |
---|
65 | size_t length() |
---|
66 | { |
---|
67 | return _length; |
---|
68 | } |
---|
69 | |
---|
70 | /// Set the message/data making up the Response |
---|
71 | /** |
---|
72 | * If the AllocationType is DYNAMIC, the message data will be free()d |
---|
73 | * by the destructor. If the AllocationType is VOLATILE, a copy of |
---|
74 | * the message data will be made. |
---|
75 | * |
---|
76 | * \param[in] mesg The Response data, can be a command and/or binary data |
---|
77 | * \param[in] length The number of bytes in mesg |
---|
78 | * \param[in] type Specify how the memory was allocated for mesg |
---|
79 | */ |
---|
80 | void setMessage(unsigned char *mesg, size_t length, AllocationType type) |
---|
81 | { |
---|
82 | if (type == VOLATILE) { |
---|
83 | _length = length; |
---|
84 | _mesg = (unsigned char *)malloc(length); |
---|
85 | memcpy(_mesg, mesg, length); |
---|
86 | _allocType = DYNAMIC; |
---|
87 | } else { |
---|
88 | _length = length; |
---|
89 | _mesg = mesg; |
---|
90 | _allocType = type; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | private: |
---|
95 | /** |
---|
96 | * (Malloc-ed by caller, freed by destructor) |
---|
97 | * Contains the message/bytes to be sent to the client. */ |
---|
98 | unsigned char *_mesg; |
---|
99 | size_t _length; /**< # of bytes in the above message. */ |
---|
100 | ResponseType _type; |
---|
101 | AllocationType _allocType; |
---|
102 | }; |
---|
103 | |
---|
104 | /** |
---|
105 | * \brief Queue to hold pending Responses to be sent to the client |
---|
106 | * |
---|
107 | * A semaphore and mutex are used to control access to the |
---|
108 | * queue by a reader and writer thread |
---|
109 | */ |
---|
110 | class ResponseQueue |
---|
111 | { |
---|
112 | public: |
---|
113 | ResponseQueue(void *clientData); |
---|
114 | |
---|
115 | virtual ~ResponseQueue(); |
---|
116 | |
---|
117 | /// A place to store a data pointer. Not used internally. |
---|
118 | /* XXX: This probably doesn't belong here */ |
---|
119 | void *clientData() |
---|
120 | { |
---|
121 | return _clientData; |
---|
122 | } |
---|
123 | |
---|
124 | /// Add a response to the end of the queue |
---|
125 | void enqueue(Response *response); |
---|
126 | |
---|
127 | /// Remove a response from the front of the queue |
---|
128 | Response *dequeue(); |
---|
129 | |
---|
130 | private: |
---|
131 | pthread_mutex_t _idle; |
---|
132 | sem_t _ready; /**< Semaphore indicating that a response has been queued. */ |
---|
133 | std::list<Response *> _list; |
---|
134 | void *_clientData; |
---|
135 | }; |
---|
136 | |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | #endif |
---|