00001 // This may look like C code, but it's really -*- C++ -*- 00002 /* 00003 * Copyright (C) 2007 Koen Deforche 00004 * 00005 * See the LICENSE file for terms of use. 00006 */ 00007 #ifndef SIMPLECHATSERVER_H_ 00008 #define SIMPLECHATSERVER_H_ 00009 00010 #include <Wt/WObject> 00011 #include <Wt/WSignal> 00012 #include <Wt/WString> 00013 00014 #include <set> 00015 #include <boost/thread.hpp> 00016 00021 00024 class ChatEvent 00025 { 00026 public: 00029 enum Type { Login, Logout, Message }; 00030 00033 Type type() const { return type_; } 00034 00037 const Wt::WString& user() const { return user_; } 00038 00041 const Wt::WString& message() const { return message_; } 00042 00045 const Wt::WString formattedHTML(const Wt::WString& user) const; 00046 00047 private: 00048 Type type_; 00049 Wt::WString user_; 00050 Wt::WString message_; 00051 00052 /* 00053 * Both user and html will be formatted as html 00054 */ 00055 ChatEvent(const Wt::WString& user, const Wt::WString& message) 00056 : type_(Message), user_(user), message_(message) 00057 { } 00058 00059 ChatEvent(Type type, const Wt::WString& user) 00060 : type_(type), user_(user) 00061 { } 00062 00063 friend class SimpleChatServer; 00064 }; 00065 00068 class SimpleChatServer : public Wt::WObject 00069 { 00070 public: 00073 SimpleChatServer(); 00074 00079 bool login(const Wt::WString& user); 00080 00083 void logout(const Wt::WString& user); 00084 00087 Wt::WString suggestGuest(); 00088 00091 void sendMessage(const Wt::WString& user, const Wt::WString& message); 00092 00097 Wt::Signal<ChatEvent>& chatEvent() { return chatEvent_; } 00098 00101 typedef std::set<Wt::WString> UserSet; 00102 00105 UserSet users(); 00106 00107 private: 00108 Wt::Signal<ChatEvent> chatEvent_; 00109 boost::mutex mutex_; 00110 00111 UserSet users_; 00112 }; 00113 00116 #endif // SIMPLECHATSERVER_H_