/usr/lib/Wt/examples/simplechat/SimpleChatServer.h is in witty-examples 3.3.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | // This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef SIMPLECHATSERVER_H_
#define SIMPLECHATSERVER_H_
#include <boost/noncopyable.hpp>
#include <Wt/WSignal>
#include <Wt/WString>
namespace Wt {
class WServer;
}
#include <set>
#include <map>
#include <boost/thread.hpp>
/**
* @addtogroup chatexample
*/
/*@{*/
/*! \brief Encapsulate a chat event.
*/
class ChatEvent
{
public:
/*! \brief Enumeration for the event type.
*/
enum Type { Login, Logout, Rename, Message };
/*! \brief Get the event type.
*/
Type type() const { return type_; }
/*! \brief Get the user who caused the event.
*/
const Wt::WString& user() const { return user_; }
/*! \brief Get the message of the event.
*/
const Wt::WString& message() const { return message_; }
/*! \brief Get the extra data for this event.
*/
const Wt::WString& data() const { return data_; }
/*! \brief Get the message formatted as HTML, rendered for the given user.
*
* The \p format indicates how the message should be formatted.
*/
const Wt::WString formattedHTML(const Wt::WString& user,
Wt::TextFormat format) const;
private:
Type type_;
Wt::WString user_;
Wt::WString data_;
Wt::WString message_;
/*
* Both user and html will be formatted as html
*/
ChatEvent(const Wt::WString& user, const Wt::WString& message)
: type_(Message), user_(user), message_(message)
{ }
ChatEvent(Type type, const Wt::WString& user,
const Wt::WString& data = Wt::WString::Empty)
: type_(type), user_(user), data_(data)
{ }
friend class SimpleChatServer;
};
typedef boost::function<void (const ChatEvent&)> ChatEventCallback;
/*! \brief A simple chat server
*/
class SimpleChatServer : boost::noncopyable
{
public:
/*
* A reference to a client.
*/
class Client
{
};
/*! \brief Create a new chat server.
*/
SimpleChatServer(Wt::WServer& server);
/*! \brief Connects to the chat server.
*
* The passed callback method is posted to when a new chat event is
* received.
*
* Returns whether the client has been connected (or false if the client
* was already connected).
*/
bool connect(Client *client, const ChatEventCallback& handleEvent);
/*! \brief Disconnect from the chat server.
*
* Returns whether the client has been disconnected (or false if the client
* was not connected).
*/
bool disconnect(Client *client);
/*! \brief Try to login with given user name.
*
* Returns false if the login was not successful.
*/
bool login(const Wt::WString& user);
/*! \brief Logout from the server.
*/
void logout(const Wt::WString& user);
/*! \brief Changes the name.
*/
bool changeName(const Wt::WString& user, const Wt::WString& newUser);
/*! \brief Get a suggestion for a guest user name.
*/
Wt::WString suggestGuest();
/*! \brief Send a message on behalve of a user.
*/
void sendMessage(const Wt::WString& user, const Wt::WString& message);
/*! \brief Typedef for a collection of user names.
*/
typedef std::set<Wt::WString> UserSet;
/*! \brief Get the users currently logged in.
*/
UserSet users();
private:
struct ClientInfo {
std::string sessionId;
ChatEventCallback eventCallback;
};
typedef std::map<Client *, ClientInfo> ClientMap;
Wt::WServer& server_;
boost::recursive_mutex mutex_;
ClientMap clients_;
UserSet users_;
void postChatEvent(const ChatEvent& event);
};
/*@}*/
#endif // SIMPLECHATSERVER_H_
|