Wt examples 3.1.10
/build/buildd/witty-3.1.10/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 
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    * Both user and html will be formatted as html
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:
00087   SimpleChatServer(Wt::WServer& server);
00088 
00094   bool login(const Wt::WString& user, const ChatEventCallback& handleEvent);
00095 
00098   void logout(const Wt::WString& user);
00099 
00102   bool changeName(const Wt::WString& user, const Wt::WString& newUser);
00103 
00106   Wt::WString suggestGuest();
00107 
00110   void sendMessage(const Wt::WString& user, const Wt::WString& message);
00111 
00114   typedef std::set<Wt::WString> UserSet;
00115 
00118   UserSet users();
00119 
00120 private:
00121   struct UserInfo {
00122     std::string sessionId;
00123     ChatEventCallback eventCallback;
00124   };
00125 
00126   typedef std::map<Wt::WString, UserInfo> UserMap;
00127 
00128   Wt::WServer& server_;
00129   boost::recursive_mutex mutex_;
00130   UserMap users_;
00131 
00132   void postChatEvent(const ChatEvent& event);
00133 };
00134 
00137 #endif // SIMPLECHATSERVER_H_

Generated on Wed Jul 27 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.4