source: trunk/packages/vizservers/nanovis/ResponseQueue.h @ 3605

Last change on this file since 3605 was 3605, checked in by ldelgass, 6 years ago

Add writer thread to nanovis (set USE_THREADS in Makefile), more refactoring.

  • 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
8#include <pthread.h>
9#include <semaphore.h>
10#include <cstdlib>
11#include <cstring>
12#include <list>
13
14#ifndef NV_RESPONSE_QUEUE_H
15#define NV_RESPONSE_QUEUE_H
16
17namespace nv {
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 non-error message. */
34        ERROR,          /**< Error message. */
35        OK              /**< Ack for client when no render required */
36    };
37
38    Response(ResponseType type) :
39        _mesg(NULL),
40        _length(0),
41        _type(type)
42    {
43    }
44
45    virtual ~Response()
46    {
47        if ((_length > 0) && (_mesg != NULL) && (_allocType == DYNAMIC)) {
48            free(_mesg);
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
94private:
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 */
110class ResponseQueue
111{
112public:
113    ResponseQueue();
114
115    virtual ~ResponseQueue();
116
117    /// Add a response to the end of the queue
118    void enqueue(Response *response);
119
120    /// Remove a response from the front of the queue
121    Response *dequeue();
122
123private:
124    pthread_mutex_t _idle;
125    sem_t _ready; /**< Semaphore indicating that a response has been queued. */
126    std::list<Response *> _list;
127};
128
129}
130
131#endif
Note: See TracBrowser for help on using the repository browser.