Wt examples  3.2.2
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
SimpleChatServer Class Reference

A simple chat server. More...

#include <SimpleChatServer.h>

Classes

class  Client
struct  ClientInfo

Public Types

typedef std::set< Wt::WStringUserSet
 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::WServerserver_
boost::recursive_mutex mutex_
ClientMap clients_
UserSet users_

Detailed Description

A simple chat server.

Definition at line 85 of file SimpleChatServer.h.

Member Typedef Documentation

typedef std::map<Client *, ClientInfo> SimpleChatServer::ClientMap
private

Definition at line 152 of file SimpleChatServer.h.

Typedef for a collection of user names.

Definition at line 140 of file SimpleChatServer.h.

Constructor & Destructor Documentation

SimpleChatServer::SimpleChatServer ( Wt::WServer server)

Create a new chat server.

Definition at line 57 of file SimpleChatServer.C.

: server_(server)
{ }

Member Function Documentation

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);
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.

{
boost::recursive_mutex::scoped_lock lock(mutex_);
if (clients_.count(client) == 0) {
ClientInfo clientInfo;
clientInfo.sessionId = WApplication::instance()->sessionId();
clientInfo.eventCallback = handleEvent;
clients_[client] = clientInfo;
return true;
} else
return false;
}
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.

{
boost::recursive_mutex::scoped_lock lock(mutex_);
return clients_.erase(client) == 1;
}
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);
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);
}
}
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 ( )

Get a suggestion for a guest user name.

Definition at line 136 of file SimpleChatServer.C.

{
boost::recursive_mutex::scoped_lock lock(mutex_);
for (int i = 1;; ++i) {
std::string s = "guest " + boost::lexical_cast<std::string>(i);
WString ss = s;
if (users_.find(ss) == users_.end())
return ss;
}
}
SimpleChatServer::UserSet SimpleChatServer::users ( )

Get the users currently logged in.

Definition at line 180 of file SimpleChatServer.C.

{
boost::recursive_mutex::scoped_lock lock(mutex_);
UserSet result = users_;
return result;
}

Member Data Documentation

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.


The documentation for this class was generated from the following files:

Generated on Tue Oct 30 2012 for the C++ Web Toolkit (Wt) by doxygen 1.8.1.2