Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #ifndef SIMPLECHATSERVER_H_
00008 #define SIMPLECHATSERVER_H_
00009
00010 #include <boost/noncopyable.hpp>
00011
00012 #include <Wt/WSignal>
00013 #include <Wt/WString>
00014
00015 namespace Wt {
00016 class WServer;
00017 }
00018
00019 #include <set>
00020 #include <map>
00021 #include <boost/thread.hpp>
00022
00027
00030 class ChatEvent
00031 {
00032 public:
00035 enum Type { Login, Logout, Rename, Message };
00036
00039 Type type() const { return type_; }
00040
00043 const Wt::WString& user() const { return user_; }
00044
00047 const Wt::WString& message() const { return message_; }
00048
00051 const Wt::WString& data() const { return data_; }
00052
00055 const Wt::WString formattedHTML(const Wt::WString& user) const;
00056
00057 private:
00058 Type type_;
00059 Wt::WString user_;
00060 Wt::WString data_;
00061 Wt::WString message_;
00062
00063
00064
00065
00066 ChatEvent(const Wt::WString& user, const Wt::WString& message)
00067 : type_(Message), user_(user), message_(message)
00068 { }
00069
00070 ChatEvent(Type type, const Wt::WString& user,
00071 const Wt::WString& data = Wt::WString::Empty)
00072 : type_(type), user_(user), data_(data)
00073 { }
00074
00075 friend class SimpleChatServer;
00076 };
00077
00078 typedef boost::function<void (const ChatEvent&)> ChatEventCallback;
00079
00082 class SimpleChatServer : boost::noncopyable
00083 {
00084 public:
00085
00086
00087
00088 class Client
00089 {
00090 };
00091
00094 SimpleChatServer(Wt::WServer& server);
00095
00104 bool connect(Client *client, const ChatEventCallback& handleEvent);
00105
00111 bool disconnect(Client *client);
00112
00117 bool login(const Wt::WString& user);
00118
00121 void logout(const Wt::WString& user);
00122
00125 bool changeName(const Wt::WString& user, const Wt::WString& newUser);
00126
00129 Wt::WString suggestGuest();
00130
00133 void sendMessage(const Wt::WString& user, const Wt::WString& message);
00134
00137 typedef std::set<Wt::WString> UserSet;
00138
00141 UserSet users();
00142
00143 private:
00144 struct ClientInfo {
00145 std::string sessionId;
00146 ChatEventCallback eventCallback;
00147 };
00148
00149 typedef std::map<Client *, ClientInfo> ClientMap;
00150
00151 Wt::WServer& server_;
00152 boost::recursive_mutex mutex_;
00153 ClientMap clients_;
00154 UserSet users_;
00155
00156 void postChatEvent(const ChatEvent& event);
00157 };
00158
00161 #endif // SIMPLECHATSERVER_H_