Line | |
---|
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 | class Response |
---|
20 | { |
---|
21 | public: |
---|
22 | enum AllocationType { |
---|
23 | STATIC, |
---|
24 | DYNAMIC, |
---|
25 | VOLATILE |
---|
26 | }; |
---|
27 | enum ResponseType { |
---|
28 | IMAGE, /**< Image to be displayed. */ |
---|
29 | LEGEND, /**< Legend to be displayed. */ |
---|
30 | DATA /**< Any other type of message. */ |
---|
31 | }; |
---|
32 | |
---|
33 | Response(ResponseType type) : |
---|
34 | _mesg(NULL), |
---|
35 | _length(0), |
---|
36 | _type(type) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | virtual ~Response() |
---|
41 | { |
---|
42 | if (_length > 0) { |
---|
43 | if (_allocType == DYNAMIC) { |
---|
44 | free(_mesg); |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | ResponseType type() |
---|
50 | { |
---|
51 | return _type; |
---|
52 | } |
---|
53 | |
---|
54 | unsigned char *message() |
---|
55 | { |
---|
56 | return _mesg; |
---|
57 | } |
---|
58 | |
---|
59 | size_t length() |
---|
60 | { |
---|
61 | return _length; |
---|
62 | } |
---|
63 | |
---|
64 | void setMessage(unsigned char *mesg, size_t length, AllocationType type) |
---|
65 | { |
---|
66 | if (type == VOLATILE) { |
---|
67 | _length = length; |
---|
68 | _mesg = (unsigned char *)malloc(length); |
---|
69 | memcpy(_mesg, mesg, length); |
---|
70 | _allocType = DYNAMIC; |
---|
71 | } else { |
---|
72 | _length = length; |
---|
73 | _mesg = mesg; |
---|
74 | _allocType = type; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | private: |
---|
79 | /** |
---|
80 | * (Malloc-ed by caller, freed by destructor) |
---|
81 | * Contains the message/bytes to be sent to the client. */ |
---|
82 | unsigned char *_mesg; |
---|
83 | size_t _length; /**< # of bytes in the above message. */ |
---|
84 | ResponseType _type; |
---|
85 | AllocationType _allocType; |
---|
86 | }; |
---|
87 | |
---|
88 | class ResponseQueue |
---|
89 | { |
---|
90 | public: |
---|
91 | ResponseQueue(void *clientData); |
---|
92 | |
---|
93 | virtual ~ResponseQueue(); |
---|
94 | |
---|
95 | void *clientData() |
---|
96 | { |
---|
97 | return _clientData; |
---|
98 | } |
---|
99 | |
---|
100 | void enqueue(Response *response); |
---|
101 | |
---|
102 | Response *dequeue(); |
---|
103 | |
---|
104 | private: |
---|
105 | pthread_mutex_t _idle; |
---|
106 | sem_t _ready; /**< Semaphore indicating that a response has been queued. */ |
---|
107 | std::list<Response *> _list; |
---|
108 | void *_clientData; |
---|
109 | }; |
---|
110 | |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.