|
Wt examples
3.3.0
|
A simple chat server. More...
#include <SimpleChatServer.h>
Classes | |
| class | Client |
| struct | ClientInfo |
Public Types | |
| typedef std::set< Wt::WString > | UserSet |
| Typedef for a collection of user names. | |
Public Member Functions | |
| SimpleChatServer (Wt::WServer &server) | |
| Create a new chat server. | |
| bool | connect (Client *client, const ChatEventCallback &handleEvent) |
| Connects to the chat server. | |
| bool | disconnect (Client *client) |
| Disconnect from the 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. | |
| bool | changeName (const Wt::WString &user, const Wt::WString &newUser) |
| Changes the name. | |
| 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. | |
Private Types | |
| typedef std::map< Client *, ClientInfo > | ClientMap |
Private Member Functions | |
| void | postChatEvent (const ChatEvent &event) |
Private Attributes | |
| Wt::WServer & | server_ |
| boost::recursive_mutex | mutex_ |
| ClientMap | clients_ |
| UserSet | users_ |
A simple chat server.
Definition at line 85 of file SimpleChatServer.h.
typedef std::map<Client *, ClientInfo> SimpleChatServer::ClientMap [private] |
Definition at line 152 of file SimpleChatServer.h.
| typedef std::set<Wt::WString> SimpleChatServer::UserSet |
Typedef for a collection of user names.
Definition at line 140 of file SimpleChatServer.h.
| SimpleChatServer::SimpleChatServer | ( | Wt::WServer & | server | ) |
| bool SimpleChatServer::changeName | ( | const Wt::WString & | user, |
| const Wt::WString & | newUser | ||
| ) |
Changes the name.
Definition at line 113 of file SimpleChatServer.C.
{
if (user == newUser)
return true;
boost::recursive_mutex::scoped_lock lock(mutex_);
UserSet::iterator i = users_.find(user);
if (i != users_.end()) {
if (users_.count(newUser) == 0) {
users_.erase(i);
users_.insert(newUser);
postChatEvent(ChatEvent(ChatEvent::Rename, user, newUser));
return true;
} else
return false;
} else
return false;
}
| bool SimpleChatServer::connect | ( | Client * | client, |
| const ChatEventCallback & | handleEvent | ||
| ) |
Connects to the chat server.
The passed callback method is posted to when a new chat event is received.
Returns whether the client has been connected (or false if the client was already connected).
Definition at line 61 of file SimpleChatServer.C.
| bool SimpleChatServer::disconnect | ( | Client * | client | ) |
Disconnect from the chat server.
Returns whether the client has been disconnected (or false if the client was not connected).
Definition at line 79 of file SimpleChatServer.C.
| bool SimpleChatServer::login | ( | const Wt::WString & | user | ) |
Try to login with given user name.
Returns false if the login was not successful.
Definition at line 86 of file SimpleChatServer.C.
{
boost::recursive_mutex::scoped_lock lock(mutex_);
if (users_.find(user) == users_.end()) {
users_.insert(user);
postChatEvent(ChatEvent(ChatEvent::Login, user));
return true;
} else
return false;
}
| void SimpleChatServer::logout | ( | const Wt::WString & | user | ) |
Logout from the server.
Definition at line 100 of file SimpleChatServer.C.
{
boost::recursive_mutex::scoped_lock lock(mutex_);
UserSet::iterator i = users_.find(user);
if (i != users_.end()) {
users_.erase(i);
postChatEvent(ChatEvent(ChatEvent::Logout, user));
}
}
| void SimpleChatServer::postChatEvent | ( | const ChatEvent & | event | ) | [private] |
Definition at line 154 of file SimpleChatServer.C.
{
boost::recursive_mutex::scoped_lock lock(mutex_);
WApplication *app = WApplication::instance();
for (ClientMap::const_iterator i = clients_.begin(); i != clients_.end();
++i) {
/*
* If the user corresponds to the current application, we directly
* call the call back method. This avoids an unnecessary delay for
* the update to the user causing the event.
*
* For other uses, we post it to their session. By posting the
* event, we avoid dead-lock scenarios, race conditions, and
* delivering the event to a session that is just about to be
* terminated.
*/
if (app && app->sessionId() == i->second.sessionId)
i->second.eventCallback(event);
else
server_.post(i->second.sessionId,
boost::bind(i->second.eventCallback, event));
}
}
| void SimpleChatServer::sendMessage | ( | const Wt::WString & | user, |
| const Wt::WString & | message | ||
| ) |
Send a message on behalve of a user.
Definition at line 149 of file SimpleChatServer.C.
{
postChatEvent(ChatEvent(user, message));
}
| WString SimpleChatServer::suggestGuest | ( | ) |
| SimpleChatServer::UserSet SimpleChatServer::users | ( | ) |
Get the users currently logged in.
Definition at line 180 of file SimpleChatServer.C.
ClientMap SimpleChatServer::clients_ [private] |
Definition at line 156 of file SimpleChatServer.h.
boost::recursive_mutex SimpleChatServer::mutex_ [private] |
Definition at line 155 of file SimpleChatServer.h.
Wt::WServer& SimpleChatServer::server_ [private] |
Definition at line 154 of file SimpleChatServer.h.
UserSet SimpleChatServer::users_ [private] |
Definition at line 157 of file SimpleChatServer.h.
1.7.5.1