Public Types | Public Member Functions | Private Attributes

SimpleChatServer Class Reference
[Chat example]

A simple chat server. More...

#include <SimpleChatServer.h>

Inherits Wt::WObject.

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.

  : chatEvent_(this)
{ }


Member Function Documentation

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.

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

{
  boost::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 60 of file SimpleChatServer.C.

{
  boost::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 86 of file SimpleChatServer.C.

{
  boost::mutex::scoped_lock lock(mutex_);

  chatEvent_.emit(ChatEvent(user, message));
}

WString SimpleChatServer::suggestGuest (  ) 

Get a suggestion for a guest user name.

Definition at line 73 of file SimpleChatServer.C.

{
  boost::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 93 of file SimpleChatServer.C.

{
  return users_;
}


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 Mon Nov 29 2010 08:03:14 for Wt by doxygen 1.7.1