/usr/lib/Wt/examples/simplechat/SimpleChatServer.h is in witty-examples 3.1.10-1ubuntu2.
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 | // 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.
*/
const Wt::WString formattedHTML(const Wt::WString& user) 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:
/*! \brief Create a new chat server.
*/
SimpleChatServer(Wt::WServer& server);
/*! \brief Try to login with given user name.
*
* Returns false if the login was not successfull. The passed callback method
* is posted to when a new chat event is received.
*/
bool login(const Wt::WString& user, const ChatEventCallback& handleEvent);
/*! \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 UserInfo {
std::string sessionId;
ChatEventCallback eventCallback;
};
typedef std::map<Wt::WString, UserInfo> UserMap;
Wt::WServer& server_;
boost::recursive_mutex mutex_;
UserMap users_;
void postChatEvent(const ChatEvent& event);
};
/*@}*/
#endif // SIMPLECHATSERVER_H_
|