source: nanovis/trunk/ResponseQueue.h @ 5064

Last change on this file since 5064 was 3613, checked in by ldelgass, 12 years ago

Include namespace in header guard defines

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