Wt examples  3.7.1
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>

Inheritance diagram for SimpleChatServer:
Inheritance graph
[legend]

Classes

class  Client
 
struct  ClientInfo
 

Public Types

typedef std::set< Wt::WStringUserSet
 Typedef for a collection of user names. More...
 

Public Member Functions

 SimpleChatServer (Wt::WServer &server)
 Create a new chat server. More...
 
bool connect (Client *client, const ChatEventCallback &handleEvent)
 Connects to the chat server. More...
 
bool disconnect (Client *client)
 Disconnect from the chat server. More...
 
bool login (const Wt::WString &user)
 Try to login with given user name. More...
 
void logout (const Wt::WString &user)
 Logout from the server. More...
 
bool changeName (const Wt::WString &user, const Wt::WString &newUser)
 Changes the name. More...
 
Wt::WString suggestGuest ()
 Get a suggestion for a guest user name. More...
 
void sendMessage (const Wt::WString &user, const Wt::WString &message)
 Send a message on behalve of a user. More...
 
UserSet users ()
 Get the users currently logged in. More...
 

Private Types

typedef std::map< Client *, ClientInfoClientMap
 

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

◆ ClientMap

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

Definition at line 152 of file SimpleChatServer.h.

◆ UserSet

Typedef for a collection of user names.

Definition at line 140 of file SimpleChatServer.h.

Constructor & Destructor Documentation

◆ SimpleChatServer()

SimpleChatServer::SimpleChatServer ( Wt::WServer server)

Create a new chat server.

Definition at line 57 of file SimpleChatServer.C.

58  : server_(server)
59 { }
Wt::WServer & server_

Member Function Documentation

◆ changeName()

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

Changes the name.

Definition at line 113 of file SimpleChatServer.C.

114 {
115  if (user == newUser)
116  return true;
117 
118  boost::recursive_mutex::scoped_lock lock(mutex_);
119 
120  UserSet::iterator i = users_.find(user);
121 
122  if (i != users_.end()) {
123  if (users_.count(newUser) == 0) {
124  users_.erase(i);
125  users_.insert(newUser);
126 
127  postChatEvent(ChatEvent(ChatEvent::Rename, user, newUser));
128 
129  return true;
130  } else
131  return false;
132  } else
133  return false;
134 }
boost::recursive_mutex mutex_
Encapsulate a chat event.
void postChatEvent(const ChatEvent &event)

◆ connect()

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.

63 {
64  boost::recursive_mutex::scoped_lock lock(mutex_);
65 
66  if (clients_.count(client) == 0) {
67  ClientInfo clientInfo;
68 
69  clientInfo.sessionId = WApplication::instance()->sessionId();
70  clientInfo.eventCallback = handleEvent;
71 
72  clients_[client] = clientInfo;
73 
74  return true;
75  } else
76  return false;
77 }
boost::recursive_mutex mutex_

◆ disconnect()

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.

80 {
81  boost::recursive_mutex::scoped_lock lock(mutex_);
82 
83  return clients_.erase(client) == 1;
84 }
boost::recursive_mutex mutex_

◆ login()

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.

87 {
88  boost::recursive_mutex::scoped_lock lock(mutex_);
89 
90  if (users_.find(user) == users_.end()) {
91  users_.insert(user);
92 
94 
95  return true;
96  } else
97  return false;
98 }
boost::recursive_mutex mutex_
Encapsulate a chat event.
void postChatEvent(const ChatEvent &event)

◆ logout()

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

Logout from the server.

Definition at line 100 of file SimpleChatServer.C.

101 {
102  boost::recursive_mutex::scoped_lock lock(mutex_);
103 
104  UserSet::iterator i = users_.find(user);
105 
106  if (i != users_.end()) {
107  users_.erase(i);
108 
110  }
111 }
boost::recursive_mutex mutex_
Encapsulate a chat event.
void postChatEvent(const ChatEvent &event)

◆ postChatEvent()

void SimpleChatServer::postChatEvent ( const ChatEvent event)
private

Definition at line 154 of file SimpleChatServer.C.

155 {
156  boost::recursive_mutex::scoped_lock lock(mutex_);
157 
158  WApplication *app = WApplication::instance();
159 
160  for (ClientMap::const_iterator i = clients_.begin(); i != clients_.end();
161  ++i) {
162  /*
163  * If the user corresponds to the current application, we directly
164  * call the call back method. This avoids an unnecessary delay for
165  * the update to the user causing the event.
166  *
167  * For other uses, we post it to their session. By posting the
168  * event, we avoid dead-lock scenarios, race conditions, and
169  * delivering the event to a session that is just about to be
170  * terminated.
171  */
172  if (app && app->sessionId() == i->second.sessionId)
173  i->second.eventCallback(event);
174  else
175  server_.post(i->second.sessionId,
176  boost::bind(i->second.eventCallback, event));
177  }
178 }
boost::recursive_mutex mutex_
WT_API void post(const std::string &sessionId, const std::function< void()> &function, const std::function< void()> &fallBackFunction=std::function< void()>())
Wt::WServer & server_
std::string sessionId() const

◆ sendMessage()

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.

150 {
151  postChatEvent(ChatEvent(user, message));
152 }
Encapsulate a chat event.
void postChatEvent(const ChatEvent &event)

◆ suggestGuest()

WString SimpleChatServer::suggestGuest ( )

Get a suggestion for a guest user name.

Definition at line 136 of file SimpleChatServer.C.

137 {
138  boost::recursive_mutex::scoped_lock lock(mutex_);
139 
140  for (int i = 1;; ++i) {
141  std::string s = "guest " + boost::lexical_cast<std::string>(i);
142  WString ss = s;
143 
144  if (users_.find(ss) == users_.end())
145  return ss;
146  }
147 }
boost::recursive_mutex mutex_

◆ users()

SimpleChatServer::UserSet SimpleChatServer::users ( )

Get the users currently logged in.

Definition at line 180 of file SimpleChatServer.C.

181 {
182  boost::recursive_mutex::scoped_lock lock(mutex_);
183 
184  UserSet result = users_;
185 
186  return result;
187 }
boost::recursive_mutex mutex_
std::set< Wt::WString > UserSet
Typedef for a collection of user names.

Member Data Documentation

◆ clients_

ClientMap SimpleChatServer::clients_
private

Definition at line 156 of file SimpleChatServer.h.

◆ mutex_

boost::recursive_mutex SimpleChatServer::mutex_
private

Definition at line 155 of file SimpleChatServer.h.

◆ server_

Wt::WServer& SimpleChatServer::server_
private

Definition at line 154 of file SimpleChatServer.h.

◆ users_

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 Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13