Public Types | Public Member Functions | Private Attributes

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

Private Attributes

Wt::Signal< ChatEventchatEvent_
boost::recursive_mutex mutex_
UserSet users_

Detailed Description

A simple chat server.

Definition at line 74 of file SimpleChatServer.h.


Member Typedef Documentation

Typedef for a collection of user names.

Definition at line 111 of file SimpleChatServer.h.


Constructor & Destructor Documentation

SimpleChatServer::SimpleChatServer (  )

Create a new chat server.

Definition at line 48 of file SimpleChatServer.C.

  : chatEvent_(this)
{ }

Member Function Documentation

bool SimpleChatServer::changeName ( const Wt::WString user,
const Wt::WString newUser 
)

Changes the name.

Definition at line 84 of file SimpleChatServer.C.

{
  if (user == newUser)
    return true;

  SyncLock<boost::recursive_mutex::scoped_lock> lock(mutex_);
  
  UserSet::iterator i = users_.find(user);

  if (i != users_.end()) {
    if (users_.find(newUser) == users_.end()) {
      users_.erase(i);
      users_.insert(newUser);

      chatEvent_.emit(ChatEvent(ChatEvent::Rename, user, newUser));

      return true;
    } else
      return false;
  } else
    return false;
}
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 107 of file SimpleChatServer.h.

{ return chatEvent_; }
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 52 of file SimpleChatServer.C.

{
  // In every application path that holds a lock to a mutex while also
  // trying to update another application (as is in this method the
  // case during chatEvent_.emit()) we need to use Wt::SyncLock to
  // avoid dead-locks.

  SyncLock<boost::recursive_mutex::scoped_lock> lock(mutex_);
  
  if (users_.find(user) == users_.end()) {
    users_.insert(user);

    chatEvent_.emit(ChatEvent(ChatEvent::Login, user));

    return true;
  } else
    return false;
}
void SimpleChatServer::logout ( const Wt::WString user )

Logout from the server.

Definition at line 71 of file SimpleChatServer.C.

{
  SyncLock<boost::recursive_mutex::scoped_lock> lock(mutex_);
  
  UserSet::iterator i = users_.find(user);

  if (i != users_.end()) {
    users_.erase(i);

    chatEvent_.emit(ChatEvent(ChatEvent::Logout, user));
  }
}
void SimpleChatServer::sendMessage ( const Wt::WString user,
const Wt::WString message 
)

Send a message on behalve of a user.

Definition at line 120 of file SimpleChatServer.C.

WString SimpleChatServer::suggestGuest (  )

Get a suggestion for a guest user name.

Definition at line 107 of file SimpleChatServer.C.

{
  SyncLock<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 127 of file SimpleChatServer.C.


Member Data Documentation

Definition at line 118 of file SimpleChatServer.h.

boost::recursive_mutex SimpleChatServer::mutex_ [private]

Definition at line 119 of file SimpleChatServer.h.

Definition at line 121 of file SimpleChatServer.h.


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

Generated on Fri Feb 4 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.2