source: trunk/packages/vizservers/vtkvis/ResponseQueue.h @ 2657

Last change on this file since 2657 was 2657, checked in by gah, 13 years ago
  • Property svn:eol-style set to native
File size: 3.0 KB
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
16namespace Rappture {
17namespace VtkVis {
18
19/**
20 * \brief Holds data for a response to be sent to client
21 */
22class Response
23{
24public:
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) && (_mesg != NULL) && (_allocType == DYNAMIC)) {
46            free(_mesg);
47        }
48    }
49
50    /// Get the ResponseType
51    ResponseType type()
52    {
53        return _type;
54    }
55
56    /// Get the Response data
57    unsigned char *message()
58    {
59        return _mesg;
60    }
61
62    /// Get the number of bytes in the Response data
63    size_t length()
64    {
65        return _length;
66    }
67
68    /// Set the message/data making up the Response
69    /**
70     * If the AllocationType is DYNAMIC, the message data will be free()d
71     * by the destructor.  If the AllocationType is VOLATILE, a copy of
72     * the message data will be made.
73     *
74     * \param[in] mesg The Response data, can be a command and/or binary data
75     * \param[in] length The number of bytes in mesg
76     * \param[in] type Specify how the memory was allocated for mesg
77     */
78    void setMessage(unsigned char *mesg, size_t length, AllocationType type)
79    {
80        if (type == VOLATILE) {
81            _length = length;
82            _mesg = (unsigned char *)malloc(length);
83            memcpy(_mesg, mesg, length);
84            _allocType = DYNAMIC;
85        } else {
86            _length = length;
87            _mesg = mesg;
88            _allocType = type;
89        }
90    }
91
92private:
93    /**
94     * (Malloc-ed by caller, freed by destructor)
95     * Contains the message/bytes to be sent to the client. */
96    unsigned char *_mesg;
97    size_t _length;       /**< # of bytes in the above message. */
98    ResponseType _type;
99    AllocationType _allocType;
100};
101
102/**
103 * \brief Queue to hold pending Responses to be sent to the client
104 *
105 * A semaphore and mutex are used to control access to the
106 * queue by a reader and writer thread
107 */
108class ResponseQueue
109{
110public:
111    ResponseQueue(void *clientData);
112
113    virtual ~ResponseQueue();
114
115    /// A place to store a data pointer.  Not used internally.
116    /* XXX: This probably doesn't belong here */
117    void *clientData()
118    {
119        return _clientData;
120    }
121
122    /// Add a response to the end of the queue
123    void enqueue(Response *response);
124
125    /// Remove a response from the front of the queue
126    Response *dequeue();
127
128private:
129    pthread_mutex_t _idle;
130    sem_t _ready; /**< Semaphore indicating that a response has been queued. */
131    std::list<Response *> _list;
132    void *_clientData;
133};
134
135}
136}
137
138#endif
Note: See TracBrowser for help on using the repository browser.