1 | |
---|
2 | #include <pthread.h> |
---|
3 | #include <semaphore.h> |
---|
4 | #include <list> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <cstdlib> |
---|
7 | |
---|
8 | #ifndef _RESPONSE_QUEUE_H |
---|
9 | #define _RESPONSE_QUEUE_H |
---|
10 | |
---|
11 | |
---|
12 | class Response { |
---|
13 | public: |
---|
14 | typedef enum AllocTypes { |
---|
15 | STATIC, DYNAMIC, VOLATILE |
---|
16 | } AllocationType; |
---|
17 | typedef enum ResponseTypes { |
---|
18 | IMAGE, /* Image to be displayed. */ |
---|
19 | LEGEND, /* Legend to be displayed. */ |
---|
20 | DATA /* Any other type of message. */ |
---|
21 | } ResponseType; |
---|
22 | Response(ResponseType type) { |
---|
23 | _length = 0; |
---|
24 | _type = type; |
---|
25 | _mesg = NULL; |
---|
26 | } |
---|
27 | ResponseType Type(void) { |
---|
28 | return _type; |
---|
29 | } |
---|
30 | unsigned char *Message(void) { |
---|
31 | return _mesg; |
---|
32 | } |
---|
33 | size_t Length(void) { |
---|
34 | return _length; |
---|
35 | } |
---|
36 | ~Response(void) { |
---|
37 | if (_length > 0) { |
---|
38 | if (_allocType == DYNAMIC) { |
---|
39 | free(_mesg); |
---|
40 | } |
---|
41 | } |
---|
42 | } |
---|
43 | void SetMessage(unsigned char *mesg, size_t length, AllocationType type) { |
---|
44 | if (type == VOLATILE) { |
---|
45 | _length = length; |
---|
46 | _mesg = (unsigned char *)malloc(length); |
---|
47 | memcpy(_mesg, mesg, length); |
---|
48 | _allocType = DYNAMIC; |
---|
49 | } else { |
---|
50 | _length = length; |
---|
51 | _mesg = mesg; |
---|
52 | _allocType = type; |
---|
53 | } |
---|
54 | } |
---|
55 | private: |
---|
56 | unsigned char *_mesg; /* (Malloc-ed by caller, freed by |
---|
57 | * destructor) Contains the |
---|
58 | * message/bytes to be sent to the |
---|
59 | * client. */ |
---|
60 | size_t _length; /* # of bytes in the above message. */ |
---|
61 | ResponseType _type; |
---|
62 | AllocationType _allocType; |
---|
63 | }; |
---|
64 | |
---|
65 | class ResponseQueue { |
---|
66 | pthread_mutex_t _idle; |
---|
67 | sem_t _ready; /* Semaphore indicating that a |
---|
68 | * response has been queued. */ |
---|
69 | std::list<Response *> _list; |
---|
70 | void *_clientData; |
---|
71 | |
---|
72 | public: |
---|
73 | ResponseQueue(void *clientData); |
---|
74 | ~ResponseQueue(void); |
---|
75 | void *ClientData(void) { |
---|
76 | return _clientData; |
---|
77 | } |
---|
78 | void Enqueue(Response *responsePtr); |
---|
79 | Response *Dequeue(); |
---|
80 | }; |
---|
81 | |
---|
82 | #endif |
---|