Wt examples: /home/koen/project/wt/public-git/wt/examples/simplechat/SimpleChatServer.h Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/simplechat/SimpleChatServer.h
Go to the documentation of this file.

    00001 // This may look like C code, but it's really -*- C++ -*-
    00002 /*
    00003  * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
    00004  *
    00005  * See the LICENSE file for terms of use.
    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
    00057   const Wt::WString formattedHTML(const Wt::WString& user,
    00058                                   Wt::TextFormat format) const;
    00059
    00060 private:
    00061   Type type_;
    00062   Wt::WString user_;
    00063   Wt::WString data_;
    00064   Wt::WString message_;
    00065
    00066   /*
    00067    * Both user and html will be formatted as html
    00068    */
    00069   ChatEvent(const Wt::WString& user, const Wt::WString& message)
    00070     : type_(Message), user_(user), message_(message)
    00071   { }
    00072
    00073   ChatEvent(Type type, const Wt::WString& user,
    00074             const Wt::WString& data = Wt::WString::Empty)
    00075     : type_(type), user_(user), data_(data)
    00076   { }
    00077
    00078   friend class SimpleChatServer;
    00079 };
    00080
    00081 typedef boost::function<void (const ChatEvent&)> ChatEventCallback;
    00082
    00085 class SimpleChatServer : boost::noncopyable
    00086 {
    00087 public:
    00088   /*
    00089    * A reference to a client.
    00090    */
    00091   class Client
    00092   {
    00093   };
    00094
    00097   SimpleChatServer(Wt::WServer& server);
    00098
    00107   bool connect(Client *client, const ChatEventCallback& handleEvent);
    00108
    00114   bool disconnect(Client *client);
    00115
    00120   bool login(const Wt::WString& user);
    00121
    00124   void logout(const Wt::WString& user);
    00125
    00128   bool changeName(const Wt::WString& user, const Wt::WString& newUser);
    00129
    00132   Wt::WString suggestGuest();
    00133
    00136   void sendMessage(const Wt::WString& user, const Wt::WString& message);
    00137
    00140   typedef std::set<Wt::WString> UserSet;
    00141
    00144   UserSet users();
    00145
    00146 private:
    00147   struct ClientInfo {
    00148     std::string sessionId;
    00149     ChatEventCallback eventCallback;
    00150   };
    00151
    00152   typedef std::map<Client *, ClientInfo> ClientMap;
    00153
    00154   Wt::WServer& server_;
    00155   boost::recursive_mutex mutex_;
    00156   ClientMap clients_;
    00157   UserSet users_;
    00158
    00159   void postChatEvent(const ChatEvent& event);
    00160 };
    00161
    00164 #endif // SIMPLECHATSERVER_H_