SimpleChatServer Class Reference
[Chat example]

A simple chat server. More...

#include <SimpleChatServer.h>

Inheritance diagram for SimpleChatServer:

Inheritance graph
[legend]

List of all members.

Public Types

typedef std::set< Wt::WStringUserSet
 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< ChatEventchatEvent
 Signal that will convey chat events.

Private Attributes

boost::mutex mutex_
UserSet users_


Detailed Description

A simple chat server.

Definition at line 68 of file SimpleChatServer.h.


Member Typedef Documentation

Typedef for a collection of user names.

Definition at line 101 of file SimpleChatServer.h.


Constructor & Destructor Documentation

SimpleChatServer::SimpleChatServer (  ) 

Create a new chat server.

Definition at line 42 of file SimpleChatServer.C.

00043 { }


Member Function Documentation

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 
)

Send a message on behalve of a user.

Definition at line 85 of file SimpleChatServer.C.

00086 {
00087   boost::mutex::scoped_lock lock(mutex_);
00088 
00089   chatEvent.emit(ChatEvent(user, message));
00090 }

SimpleChatServer::UserSet SimpleChatServer::users (  ) 

Get the users currently logged in.

Definition at line 92 of file SimpleChatServer.C.

00093 {
00094   return users_;
00095 }


Member Data Documentation

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.

Definition at line 110 of file SimpleChatServer.h.


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

Generated on Mon Jan 26 14:14:23 2009 for Wt by doxygen 1.5.6