~ubuntu-branches/ubuntu/natty/synergy/natty

« back to all changes in this revision

Viewing changes to lib/server/CConfig.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Lutz
  • Date: 2003-10-31 19:36:30 UTC
  • Revision ID: james.westby@ubuntu.com-20031031193630-knbv79x5az7qh49y
Tags: upstream-1.0.14
ImportĀ upstreamĀ versionĀ 1.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * synergy -- mouse and keyboard sharing utility
 
3
 * Copyright (C) 2002 Chris Schoeneman
 
4
 * 
 
5
 * This package is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * found in the file COPYING that should have accompanied this file.
 
8
 * 
 
9
 * This package is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 */
 
14
 
 
15
#ifndef CCONFIG_H
 
16
#define CCONFIG_H
 
17
 
 
18
#include "OptionTypes.h"
 
19
#include "ProtocolTypes.h"
 
20
#include "CNetworkAddress.h"
 
21
#include "CStringUtil.h"
 
22
#include "XBase.h"
 
23
#include "stdmap.h"
 
24
#include "stdset.h"
 
25
#include <iosfwd>
 
26
 
 
27
class CConfig;
 
28
 
 
29
namespace std {
 
30
template <>
 
31
struct iterator_traits<CConfig> {
 
32
        typedef CString                                         value_type;
 
33
        typedef ptrdiff_t                                       difference_type;
 
34
        typedef bidirectional_iterator_tag      iterator_category;
 
35
        typedef CString*                                        pointer;
 
36
        typedef CString&                                        reference;
 
37
};
 
38
};
 
39
 
 
40
//! Server configuration
 
41
/*!
 
42
This class holds server configuration information.  That includes
 
43
the names of screens and their aliases, the links between them,
 
44
and network addresses.
 
45
 
 
46
Note that case is preserved in screen names but is ignored when
 
47
comparing names.  Screen names and their aliases share a
 
48
namespace and must be unique.
 
49
*/
 
50
class CConfig {
 
51
public:
 
52
        typedef std::map<OptionID, OptionValue> CScreenOptions;
 
53
 
 
54
private:
 
55
        class CCell {
 
56
        public:
 
57
                CString                 m_neighbor[kLastDirection - kFirstDirection + 1];
 
58
                CScreenOptions  m_options;
 
59
        };
 
60
        typedef std::map<CString, CCell, CStringUtil::CaselessCmp> CCellMap;
 
61
        typedef std::map<CString, CString, CStringUtil::CaselessCmp> CNameMap;
 
62
 
 
63
public:
 
64
        typedef CCellMap::const_iterator internal_const_iterator;
 
65
        typedef CNameMap::const_iterator all_const_iterator;
 
66
        class const_iterator : std::iterator_traits<CConfig> {
 
67
        public:
 
68
                explicit const_iterator() : m_i() { }
 
69
                explicit const_iterator(const internal_const_iterator& i) : m_i(i) { }
 
70
 
 
71
                const_iterator& operator=(const const_iterator& i) {
 
72
                        m_i = i.m_i;
 
73
                        return *this;
 
74
                }
 
75
                CString                 operator*() { return m_i->first; }
 
76
                const CString*  operator->() { return &(m_i->first); }
 
77
                const_iterator& operator++() { ++m_i;  return *this; }
 
78
                const_iterator  operator++(int) { return const_iterator(m_i++); }
 
79
                const_iterator& operator--() { --m_i;  return *this; }
 
80
                const_iterator  operator--(int) { return const_iterator(m_i--); }
 
81
                bool                    operator==(const const_iterator& i) const {
 
82
                        return (m_i == i.m_i);
 
83
                }
 
84
                bool                    operator!=(const const_iterator& i) const {
 
85
                        return (m_i != i.m_i);
 
86
                }
 
87
 
 
88
        private:
 
89
                internal_const_iterator m_i;
 
90
        };
 
91
 
 
92
        CConfig();
 
93
        virtual ~CConfig();
 
94
 
 
95
        //! @name manipulators
 
96
        //@{
 
97
 
 
98
        //! Add screen
 
99
        /*!
 
100
        Adds a screen, returning true iff successful.  If a screen or
 
101
        alias with the given name exists then it fails.
 
102
        */
 
103
        bool                            addScreen(const CString& name);
 
104
 
 
105
        //! Rename screen
 
106
        /*!
 
107
        Renames a screen.  All references to the name are updated.
 
108
        Returns true iff successful.
 
109
        */
 
110
        bool                            renameScreen(const CString& oldName,
 
111
                                                        const CString& newName);
 
112
 
 
113
        //! Remove screen
 
114
        /*!
 
115
        Removes a screen.  This also removes aliases for the screen and
 
116
        disconnects any connections to the screen.  \c name may be an
 
117
        alias.
 
118
        */
 
119
        void                            removeScreen(const CString& name);
 
120
 
 
121
        //! Remove all screens
 
122
        /*!
 
123
        Removes all screens, aliases, and connections.
 
124
        */
 
125
        void                            removeAllScreens();
 
126
 
 
127
        //! Add alias
 
128
        /*!
 
129
        Adds an alias for a screen name.  An alias can be used
 
130
        any place the canonical screen name can (except addScreen()).
 
131
        Returns false if the alias name already exists or the canonical
 
132
        name is unknown, otherwise returns true.
 
133
        */
 
134
        bool                            addAlias(const CString& canonical,
 
135
                                                        const CString& alias);
 
136
 
 
137
        //! Remove alias
 
138
        /*!
 
139
        Removes an alias for a screen name.  It returns false if the
 
140
        alias is unknown or a canonical name, otherwise returns true.
 
141
        */
 
142
        bool                            removeAlias(const CString& alias);
 
143
 
 
144
        //! Remove all aliases
 
145
        /*!
 
146
        This removes all aliases but not the screens.
 
147
        */
 
148
        void                            removeAllAliases();
 
149
 
 
150
        //! Connect screens
 
151
        /*!
 
152
        Establishes a one-way connection between opposite edges of two
 
153
        screens.  The user will be able to jump from the \c srcSide of
 
154
        screen \c srcName to the opposite side of screen \c dstName
 
155
        when both screens are connected to the server and the user
 
156
        isn't locked to a screen.  Returns false if \c srcName is
 
157
        unknown.
 
158
        */
 
159
        bool                            connect(const CString& srcName,
 
160
                                                        EDirection srcSide,
 
161
                                                        const CString& dstName);
 
162
 
 
163
        //! Disconnect screens
 
164
        /*!
 
165
        Removes a connection created by connect().  Returns false if
 
166
        \c srcName is unknown.
 
167
        */
 
168
        bool                            disconnect(const CString& srcName,
 
169
                                                        EDirection srcSide);
 
170
 
 
171
        //! Set server address
 
172
        /*!
 
173
        Set the synergy listen addresses.  There is no default address so
 
174
        this must be called to run a server using this configuration.
 
175
        */
 
176
        void                            setSynergyAddress(const CNetworkAddress&);
 
177
 
 
178
        //! Set HTTP server address
 
179
        /*!
 
180
        Set the HTTP listen addresses.  There is no default address so
 
181
        this must be called to run an HTTP server using this configuration.
 
182
        */
 
183
        void                            setHTTPAddress(const CNetworkAddress&);
 
184
 
 
185
        //! Add a screen option
 
186
        /*!
 
187
        Adds an option and its value to the named screen.  Replaces the
 
188
        existing option's value if there is one.  Returns true iff \c name
 
189
        is a known screen.
 
190
        */
 
191
        bool                            addOption(const CString& name,
 
192
                                                        OptionID option, OptionValue value);
 
193
 
 
194
        //! Remove a screen option
 
195
        /*!
 
196
        Removes an option and its value from the named screen.  Does
 
197
        nothing if the option doesn't exist on the screen.  Returns true
 
198
        iff \c name is a known screen.
 
199
        */
 
200
        bool                            removeOption(const CString& name, OptionID option);
 
201
 
 
202
        //! Remove a screen options
 
203
        /*!
 
204
        Removes all options and values from the named screen.  Returns true
 
205
        iff \c name is a known screen.
 
206
        */
 
207
        bool                            removeOptions(const CString& name);
 
208
 
 
209
        //@}
 
210
        //! @name accessors
 
211
        //@{
 
212
 
 
213
        //! Test screen name validity
 
214
        /*!
 
215
        Returns true iff \c name is a valid screen name.
 
216
        */
 
217
        bool                            isValidScreenName(const CString& name) const;
 
218
 
 
219
        //! Get beginning (canonical) screen name iterator
 
220
        const_iterator          begin() const;
 
221
        //! Get ending (canonical) screen name iterator
 
222
        const_iterator          end() const;
 
223
 
 
224
        //! Get beginning screen name iterator
 
225
        all_const_iterator      beginAll() const;
 
226
        //! Get ending screen name iterator
 
227
        all_const_iterator      endAll() const;
 
228
 
 
229
        //! Test for screen name
 
230
        /*!
 
231
        Returns true iff \c name names a screen.
 
232
        */
 
233
        bool                            isScreen(const CString& name) const;
 
234
 
 
235
        //! Test for canonical screen name
 
236
        /*!
 
237
        Returns true iff \c name is the canonical name of a screen.
 
238
        */
 
239
        bool                            isCanonicalName(const CString& name) const;
 
240
 
 
241
        //! Get canonical name
 
242
        /*!
 
243
        Returns the canonical name of a screen or the empty string if
 
244
        the name is unknown.  Returns the canonical name if one is given.
 
245
        */
 
246
        CString                         getCanonicalName(const CString& name) const;
 
247
 
 
248
        //! Get neighbor
 
249
        /*!
 
250
        Returns the canonical screen name of the neighbor in the given
 
251
        direction (set through connect()).  Returns the empty string
 
252
        if there is no neighbor in that direction.
 
253
        */
 
254
        CString                         getNeighbor(const CString&, EDirection) const;
 
255
 
 
256
        //! Get the server address
 
257
        const CNetworkAddress&  getSynergyAddress() const;
 
258
        //! Get the HTTP server address
 
259
        const CNetworkAddress&  getHTTPAddress() const;
 
260
 
 
261
        //! Get the screen options
 
262
        /*!
 
263
        Returns all the added options for the named screen.  Returns NULL
 
264
        if the screen is unknown and an empty collection if there are no
 
265
        options.
 
266
        */
 
267
        const CScreenOptions* getOptions(const CString& name) const;
 
268
 
 
269
        //! Compare configurations
 
270
        bool                            operator==(const CConfig&) const;
 
271
        //! Compare configurations
 
272
        bool                            operator!=(const CConfig&) const;
 
273
 
 
274
        //! Read configuration
 
275
        /*!
 
276
        Reads a configuration from a stream.  Throws XConfigRead on error.
 
277
        */
 
278
        friend std::istream&    operator>>(std::istream&, CConfig&);
 
279
 
 
280
        //! Write configuration
 
281
        /*!
 
282
        Writes a configuration to a stream.
 
283
        */
 
284
        friend std::ostream&    operator<<(std::ostream&, const CConfig&);
 
285
 
 
286
        //! Get direction name
 
287
        /*!
 
288
        Returns the name of a direction (for debugging).
 
289
        */
 
290
        static const char*      dirName(EDirection);
 
291
 
 
292
        //@}
 
293
 
 
294
private:
 
295
        static bool                     readLine(std::istream&, CString&);
 
296
        static OptionValue      parseBoolean(const CString&);
 
297
        static OptionValue      parseInt(const CString&);
 
298
        static OptionValue      parseModifierKey(const CString&);
 
299
        static const char*      getOptionName(OptionID);
 
300
        static CString          getOptionValue(OptionID, OptionValue);
 
301
        void                            readSection(std::istream&);
 
302
        void                            readSectionOptions(std::istream&);
 
303
        void                            readSectionScreens(std::istream&);
 
304
        void                            readSectionLinks(std::istream&);
 
305
        void                            readSectionAliases(std::istream&);
 
306
 
 
307
private:
 
308
        CCellMap                        m_map;
 
309
        CNameMap                        m_nameToCanonicalName;
 
310
        CNetworkAddress         m_synergyAddress;
 
311
        CNetworkAddress         m_httpAddress;
 
312
        CScreenOptions          m_globalOptions;
 
313
};
 
314
 
 
315
//! Configuration stream read exception
 
316
/*!
 
317
Thrown when a configuration stream cannot be parsed.
 
318
*/
 
319
class XConfigRead : public XBase {
 
320
public:
 
321
        XConfigRead(const CString&);
 
322
        ~XConfigRead();
 
323
 
 
324
protected:
 
325
        // XBase overrides
 
326
        virtual CString         getWhat() const throw();
 
327
 
 
328
private:
 
329
        CString                         m_error;
 
330
};
 
331
 
 
332
#endif