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.
Wt::Signal< ChatEvent > & chatEvent ()
 Signal that will convey chat events.
UserSet users ()
 Get the users currently logged in.

Private Attributes

Wt::Signal< ChatEventchatEvent_
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   : chatEvent_(this)
00044 { }


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 46 of file SimpleChatServer.C.

00047 {
00048   boost::mutex::scoped_lock lock(mutex_);
00049   
00050   if (users_.find(user) == users_.end()) {
00051     users_.insert(user);
00052 
00053     chatEvent_.emit(ChatEvent(ChatEvent::Login, user));
00054 
00055     return true;
00056   } else
00057     return false;
00058 }

void SimpleChatServer::logout ( const Wt::WString user  ) 

Logout from the server.

Definition at line 60 of file SimpleChatServer.C.

00061 {
00062   boost::mutex::scoped_lock lock(mutex_);
00063   
00064   UserSet::iterator i = users_.find(user);
00065 
00066   if (i != users_.end()) {
00067     users_.erase(i);
00068 
00069     chatEvent_.emit(ChatEvent(ChatEvent::Logout, user));
00070   }
00071 }

WString SimpleChatServer::suggestGuest (  ) 

Get a suggestion for a guest user name.

Definition at line 73 of file SimpleChatServer.C.

00074 {
00075   boost::mutex::scoped_lock lock(mutex_);
00076 
00077   for (int i = 1;; ++i) {
00078     std::string s = "guest " + boost::lexical_cast<std::string>(i);
00079     WString ss = s;
00080 
00081     if (users_.find(ss) == users_.end())
00082       return ss;
00083   }
00084 }

void SimpleChatServer::sendMessage ( const Wt::WString user,
const Wt::WString message 
)

Send a message on behalve of a user.

Definition at line 86 of file SimpleChatServer.C.

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

Wt::Signal<ChatEvent>& SimpleChatServer::chatEvent (  )  [inline]

Signal that will convey chat events.

Every client should connect to this signal, and process events.

Definition at line 97 of file SimpleChatServer.h.

00097 { return chatEvent_; }

SimpleChatServer::UserSet SimpleChatServer::users (  ) 

Get the users currently logged in.

Definition at line 93 of file SimpleChatServer.C.

00094 {
00095   return users_;
00096 }


Member Data Documentation

Definition at line 108 of file SimpleChatServer.h.

boost::mutex SimpleChatServer::mutex_ [private]

Definition at line 109 of file SimpleChatServer.h.

Definition at line 111 of file SimpleChatServer.h.


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

Generated on Fri Mar 26 17:12:12 2010 for Wt by doxygen 1.5.6