summaryrefslogtreecommitdiffstats
path: root/src/jabber_whiteboard/message-aggregator.h
blob: a73ee9876dfcffd63ab0807977dee6aa6038fba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Aggregates individual serialized XML::Events into larger packages 
 * for more efficient delivery
 *
 * Authors:
 * David Yip <yipdw@rose-hulman.edu>
 *
 * Copyright (c) 2005 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#ifndef __WHITEBOARD_MESSAGE_AGGREGATOR_H__
#define __WHITEBOARD_MESSAGE_AGGREGATOR_H__

#include <glibmm.h>

namespace Inkscape {

namespace Whiteboard {

/**
 * Aggregates individual serialized XML::Events into larger messages for increased
 * efficiency.
 *
 * \see Inkscape::Whiteboard::Serializer
 */
class MessageAggregator {
public:
	// TODO: This should be user-configurable; perhaps an option in Inkscape Preferences...
	/// Maximum size of aggregates in kilobytes; ULONG_MAX = no limit.
	static unsigned int const MAX_SIZE = 16384;

	MessageAggregator() { }
    virtual ~MessageAggregator() { }

	/**
	 * Return the instance of this class.
	 *
	 * \return MessageAggregator instance.
	 */
	static MessageAggregator& instance()
	{
		static MessageAggregator singleton;
		return singleton;
	}

	/**
	 * Adds one message to the aggregate 
	 * using a user-provided buffer.  Returns true if more messages can be
	 * added to the buffer; false otherwise.
	 *
	 * \param msg The message to add to the aggregate.
	 * \param buf The aggregate buffer.
	 * \return Whether or not more messages can be added to the buffer.
	 */
	bool addOne(Glib::ustring const& msg, Glib::ustring& buf);

	/**
	 * Adds one message to the aggregate using the internal buffer.  
	 * Note that since this class is designed to be a singleton class, usage of the internal
	 * buffer is not thread-safe.  Use the above method if this matters to you
	 * (it currently shouldn't matter, but in future...)
	 *
	 * Also note that usage of the internal buffer means that you will have to manually
	 * clear the internal buffer; use reset() for that.
	 *
	 * \param msg The message to add to the aggregate.
	 * \return Whether or not more messages can be added to the buffer.
	 */
	bool addOne(Glib::ustring const& msg);

	/**
	 * Return the aggregate message.
	 *
	 * Because this method returns a reference to a string, it is not safe to assume
         * that its contents will remain untouched across two calls to this MessageAggregator.
         * If you require that guarantee, make a copy.
	 *
	 * \return A reference to the aggregate message.
	 */
	Glib::ustring const& getAggregate()
	{
		return this->_buf;
	}

	/**
	 * Return the aggregate message.
	 *
	 * \return The aggregate message.
	 */
	Glib::ustring const getAggregateCopy()
	{
		return this->_buf;
	}

	/**
	 * Return the aggregate message and clear the internal buffer.
	 *
	 * \return The aggregate message.
	 */
	Glib::ustring const detachAggregate()
	{
		Glib::ustring ret = this->_buf;
		this->_buf.clear();
		return ret;
	}

	/**
	 * Clear the internal buffer.
	 */
	void reset()
	{
		this->_buf.clear();
	}

private:
	Glib::ustring _buf;
};

}

}

#endif

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :