source: source/ariba/utility/transport/tcpip/protlib/fqueue.cpp@ 7038

Last change on this file since 7038 was 5284, checked in by mies, 15 years ago

+ added new transport modules and adapted ariba to them
+ exchange endpoint descriptors an link establishment
+ clean up of base communication
+ link establishment with in the presence of multiple endpoints
+ local discovery for ipv6, ipv4 and bluetooth mac addresses

File size: 5.4 KB
Line 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file fqueue.cpp
3/// wrapper class for fastqueue
4/// ----------------------------------------------------------
5/// $Id: fqueue.cpp 2549 2007-04-02 22:17:37Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/src/fqueue.cpp $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29
30#include "fqueue.h"
31#include "stdio.h"
32#include "logfile.h"
33
34#include <string>
35
36namespace protlib {
37
38/** @addtogroup fastqueue Fast Queue
39 * @{
40 */
41
42 using namespace log;
43
44/**
45 * Constructor.
46 *
47 * Initialize a FastQueue with a queue name and enable/disable expedited
48 * data.
49 *
50 * @param qname the queue's name, or NULL
51 * @param exp if true, expedited data support is enabled
52 */
53FastQueue::FastQueue(const char *qname, bool exp)
54 : queue_name((qname == 0) ? "" : (char*)qname), shutdownflag(false)
55{
56 if ((queue = create_queue(qname)) == NULL)
57 {
58 Log(ERROR_LOG, LOG_ALERT, "FastQueue", "Could not create queue " << queue_name);
59 throw FQError();
60 } else queue_enable_expedited(queue,exp);
61}
62
63
64/**
65 * Add a message to the queue.
66 *
67 * If exp is true and the queue allows expedited data, the message will
68 * pass all normal messages in the queue and thus will be delivered earlier.
69 * If there are other expedited messages in the queue already, it will be
70 * added after the already existing expedited messages.
71 *
72 * This method may fail (and return false) if the queue is in shutdown mode,
73 * there is a problem aquiring locks, or some other threading problem.
74 *
75 * In case the queue is deleted before this message has been removed, this
76 * message is deleted using the delete operator. Because of this, the same
77 * message may only appear once in a queue.
78 *
79 * @param element a pointer to the message to add
80 * @param exp true if this is expedited data
81 *
82 * @return true if the element was enqueued successfully
83 */
84bool FastQueue::enqueue(message *element, bool exp)
85{
86 if (shutdownflag) return false;
87 if (enqueue_element_expedited_signal(queue, (void*)element, exp) < 0)
88 {
89 Log(ERROR_LOG, LOG_ALERT, "FastQueue", "Could not enqueue element in queue " << queue_name);
90 return false;
91 }
92 return true;
93}
94
95
96/**
97 * Wait for a message for a given time.
98 *
99 * If no message arrives in the given time period, NULL is returned.
100 *
101 * @param msec the time to wait in milliseconds
102 *
103 * @return the message, or NULL
104 */
105message *FastQueue::dequeue_timedwait(const long int msec)
106{
107 struct timespec tspec = {0,0};
108 tspec.tv_sec = msec/1000;
109 tspec.tv_nsec = (msec%1000)*1000000;
110 return (message*)dequeue_element_timedwait(queue, &tspec);
111}
112
113
114/**
115 * Destructor.
116 *
117 * Destroys the queue. All messages which are still in the queue are deleted
118 * using the delete operator.
119 */
120FastQueue::~FastQueue()
121{
122 if (queue)
123 {
124 cleanup();
125 if ((destroy_queue(queue)) < 0)
126 {
127 Log(ERROR_LOG, LOG_ALERT, "FastQueue", "Could not destroy queue " << queue_name);
128 }
129 }
130 DLog("FastQueue", "~FastQueue() - done for queue " << queue_name);
131}
132
133/**
134 * Test if the queue is empty.
135 *
136 * @return true if the queue is empty
137 */
138bool FastQueue::is_empty() const
139{
140 if (queue_nr_of_elements(queue)==0)
141 return true;
142 else
143 return false;
144}
145
146
147/**
148 * Return the number of messages in the queue.
149 *
150 * @return the number of enqueued messages
151 */
152unsigned long FastQueue::size() const
153{
154 return queue_nr_of_elements(queue);
155}
156
157
158/**
159 * Test if expedited message support is enabled.
160 *
161 * @return true if expedited message support is enabled
162 */
163bool FastQueue::is_expedited_enabled() const
164{
165 if (queue_is_expedited_enabled(queue))
166 return true;
167 else
168 return false;
169}
170
171/**
172 * Enable or disable expedited messages.
173 *
174 * This also returns the previous value of this flag.
175 *
176 * @return true, if expedited messages were previously enabled, false otherwise
177 */
178bool FastQueue::enable_expedited(bool exp)
179{
180 if (queue_enable_expedited(queue,exp))
181 return true;
182 else
183 return false;
184}
185
186
187/**
188 * Disable enqueueing of new messages.
189 *
190 * A queue in shutdown mode does not accept messages any more.
191 */
192void FastQueue::shutdown() { shutdownflag = true; }
193
194
195/**
196 * Put queue into shutdown mode and delete all stored messages..
197 *
198 * @return the number of messages that were in the queue
199 */
200unsigned long FastQueue::cleanup()
201{
202 unsigned long count = 0;
203 message* m = NULL;
204 shutdownflag = true;
205 while (!is_empty())
206 if ((m = dequeue(false))) {
207 delete m;
208 m = NULL;
209 count++;
210 }
211 return count;
212}
213
214//@}
215
216} // end namespace protlib
Note: See TracBrowser for help on using the repository browser.