Wt examples: SimpleChatServer Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
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>`

## Classes
class | Client
struct | ClientInfo

## Public Types
typedef std::set< Wt::WString > | UserSet
| Typedef for a collection of user names.

## Public Member Functions
| SimpleChatServer (Wt::WServer &server)
| Create a new chat server.
| SimpleChatServer (const SimpleChatServer &)=delete
SimpleChatServer & | operator= (const SimpleChatServer &)=delete
bool | connect (Client *client, const ChatEventCallback &handleEvent)
| Connects to the chat server.
bool | disconnect (Client *client)
| Disconnect from the 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.
UserSet | users ()
| Get the users currently logged in.

## Private Types
typedef std::map< Client *, ClientInfo > | ClientMap

## Private Member Functions
void | postChatEvent (const ChatEvent &event)

## Private Attributes
Wt::WServer & | server_
std::recursive_mutex | mutex_
ClientMap | clients_
UserSet | users_

## Detailed Description

A simple chat server.

Definition at line 84 of file SimpleChatServer.h.

## Member Typedef Documentation

## ◆ ClientMap

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

Definition at line 154 of file SimpleChatServer.h.

## ◆ UserSet

std::set<Wt::WString> SimpleChatServer::UserSet

Typedef for a collection of user names.

Definition at line 142 of file SimpleChatServer.h.

## Constructor & Destructor Documentation

## ◆ SimpleChatServer() [1/2]

SimpleChatServer::SimpleChatServer | ( | Wt::WServer & | server | ) |

Create a new chat server.

Definition at line 55 of file SimpleChatServer.C.

56 : server_(server)
57{ }
SimpleChatServer::server_
Wt::WServer & server_
Definition SimpleChatServer.h:156

## ◆ SimpleChatServer() [2/2]

SimpleChatServer::SimpleChatServer | ( | const SimpleChatServer & |  | ) |
| delete

## Member Function Documentation

## ◆ changeName()

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

Changes the name.

Definition at line 111 of file SimpleChatServer.C.

112{
113 if (user == newUser)
114 return true;
115
116 std::unique_lock<std::recursive_mutex> lock(mutex_);
117
118 UserSet::iterator i = users_.find(user);
119
120 if (i != users_.end()) {
121 if (users_.count(newUser) == 0) {
122 users_.erase(i);
123 users_.insert(newUser);
124
125 postChatEvent(ChatEvent(ChatEvent::Rename, user, newUser));
126
127 return true;
128 } else
129 return false;
130 } else
131 return false;
132}
ChatEvent
Encapsulate a chat event.
Definition SimpleChatServer.h:30
ChatEvent::Rename
@ Rename
Definition SimpleChatServer.h:34
SimpleChatServer::postChatEvent
void postChatEvent(const ChatEvent &event)
Definition SimpleChatServer.C:152
SimpleChatServer::mutex_
std::recursive_mutex mutex_
Definition SimpleChatServer.h:157
SimpleChatServer::users_
UserSet users_
Definition SimpleChatServer.h:159

## ◆ 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 59 of file SimpleChatServer.C.

61{
62 std::unique_lock<std::recursive_mutex> lock(mutex_);
63
64 if (clients_.count(client) == 0) {
65 ClientInfo clientInfo;
66
67 clientInfo.sessionId = Wt::WApplication::instance()->sessionId();
68 clientInfo.eventCallback = handleEvent;
69
70 clients_[client] = clientInfo;
71
72 return true;
73 } else
74 return false;
75}
SimpleChatServer::clients_
ClientMap clients_
Definition SimpleChatServer.h:158

## ◆ 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 77 of file SimpleChatServer.C.

78{
79 std::unique_lock<std::recursive_mutex> lock(mutex_);
80
81 return clients_.erase(client) == 1;
82}

## ◆ 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 84 of file SimpleChatServer.C.

85{
86 std::unique_lock<std::recursive_mutex> lock(mutex_);
87
88 if (users_.find(user) == users_.end()) {
89 users_.insert(user);
90
91 postChatEvent(ChatEvent(ChatEvent::Login, user));
92
93 return true;
94 } else
95 return false;
96}
ChatEvent::Login
@ Login
Definition SimpleChatServer.h:34

## ◆ logout()

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

Logout from the server.

Definition at line 98 of file SimpleChatServer.C.

99{
100 std::unique_lock<std::recursive_mutex> lock(mutex_);
101
102 UserSet::iterator i = users_.find(user);
103
104 if (i != users_.end()) {
105 users_.erase(i);
106
107 postChatEvent(ChatEvent(ChatEvent::Logout, user));
108 }
109}
ChatEvent::Logout
@ Logout
Definition SimpleChatServer.h:34

## ◆ operator=()

SimpleChatServer & SimpleChatServer::operator= | ( | const SimpleChatServer & |  | ) |
| delete

## ◆ postChatEvent()

void SimpleChatServer::postChatEvent | ( | const ChatEvent & | event | ) |
| private

Definition at line 152 of file SimpleChatServer.C.

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

## ◆ sendMessage()

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

Send a message on behalve of a user.

Definition at line 147 of file SimpleChatServer.C.

148{
149 postChatEvent(ChatEvent(user, message));
150}

## ◆ suggestGuest()

Wt::WString SimpleChatServer::suggestGuest | ( |  | ) |

Get a suggestion for a guest user name.

Definition at line 134 of file SimpleChatServer.C.

135{
136 std::unique_lock<std::recursive_mutex> lock(mutex_);
137
138 for (int i = 1;; ++i) {
139 std::string s = "guest " + std::to_string(i);
140 Wt::WString ss = s;
141
142 if (users_.find(ss) == users_.end())
143 return ss;
144 }
145}

## ◆ users()

SimpleChatServer::UserSet SimpleChatServer::users | ( |  | ) |

Get the users currently logged in.

Definition at line 178 of file SimpleChatServer.C.

179{
180 std::unique_lock<std::recursive_mutex> lock(mutex_);
181
182 UserSet result = users_;
183
184 return result;
185}
SimpleChatServer::UserSet
std::set< Wt::WString > UserSet
Typedef for a collection of user names.
Definition SimpleChatServer.h:142

## Member Data Documentation

## ◆ clients_

ClientMap SimpleChatServer::clients_
| private

Definition at line 158 of file SimpleChatServer.h.

## ◆ mutex_

std::recursive_mutex SimpleChatServer::mutex_
| private

Definition at line 157 of file SimpleChatServer.h.

## ◆ server_

Wt::WServer& SimpleChatServer::server_
| private

Definition at line 156 of file SimpleChatServer.h.

## ◆ users_

UserSet SimpleChatServer::users_
| private

Definition at line 159 of file SimpleChatServer.h.

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

- simplechat/SimpleChatServer.h
- simplechat/SimpleChatServer.C