#include <SimpleChatServer.h>
Public Types | |
typedef std::set< Wt::WString > | UserSet |
Typedef for a collection of user names. | |
Public Member Functions | |
SimpleChatServer () | |
Create a new chat server. | |
bool | login (const Wt::WString &user) |
Try to login with given user name. | |
void | logout (const Wt::WString &user) |
Logout from the server. | |
Wt::WString | suggestGuest () |
Get a suggestion for a guest user name. | |
void | sendMessage (const Wt::WString &user, const Wt::WString &message) |
Send a message on behalve of a user. | |
UserSet | users () |
Get the users currently logged in. | |
Public Attributes | |
Wt::Signal< ChatEvent > | chatEvent |
Signal that will convey chat events. | |
Private Attributes | |
boost::mutex | mutex_ |
UserSet | users_ |
Definition at line 68 of file SimpleChatServer.h.
typedef std::set<Wt::WString> SimpleChatServer::UserSet |
SimpleChatServer::SimpleChatServer | ( | ) |
bool SimpleChatServer::login | ( | const Wt::WString & | user | ) |
Try to login with given user name.
Returns false if the login was not successfull.
Definition at line 45 of file SimpleChatServer.C.
00046 { 00047 boost::mutex::scoped_lock lock(mutex_); 00048 00049 if (users_.find(user) == users_.end()) { 00050 users_.insert(user); 00051 00052 chatEvent.emit(ChatEvent(ChatEvent::Login, user)); 00053 00054 return true; 00055 } else 00056 return false; 00057 }
void SimpleChatServer::logout | ( | const Wt::WString & | user | ) |
Logout from the server.
Definition at line 59 of file SimpleChatServer.C.
00060 { 00061 boost::mutex::scoped_lock lock(mutex_); 00062 00063 UserSet::iterator i = users_.find(user); 00064 00065 if (i != users_.end()) { 00066 users_.erase(i); 00067 00068 chatEvent.emit(ChatEvent(ChatEvent::Logout, user)); 00069 } 00070 }
WString SimpleChatServer::suggestGuest | ( | ) |
Get a suggestion for a guest user name.
Definition at line 72 of file SimpleChatServer.C.
00073 { 00074 boost::mutex::scoped_lock lock(mutex_); 00075 00076 for (int i = 1;; ++i) { 00077 std::string s = "guest " + boost::lexical_cast<std::string>(i); 00078 WString ss = s; 00079 00080 if (users_.find(ss) == users_.end()) 00081 return ss; 00082 } 00083 }
void SimpleChatServer::sendMessage | ( | const Wt::WString & | user, | |
const Wt::WString & | message | |||
) |
SimpleChatServer::UserSet SimpleChatServer::users | ( | ) |
Get the users currently logged in.
Definition at line 92 of file SimpleChatServer.C.
00093 { 00094 return users_; 00095 }
Signal that will convey chat events.
Every client should connect to this signal, and process events.
Definition at line 97 of file SimpleChatServer.h.
boost::mutex SimpleChatServer::mutex_ [private] |
Definition at line 108 of file SimpleChatServer.h.
UserSet SimpleChatServer::users_ [private] |
Definition at line 110 of file SimpleChatServer.h.