Wt examples  4.0.0
SimpleChatServer.h
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef SIMPLECHATSERVER_H_
8 #define SIMPLECHATSERVER_H_
9 
10 #include <boost/noncopyable.hpp>
11 
12 #include <Wt/WSignal.h>
13 #include <Wt/WString.h>
14 
15 namespace Wt {
16  class WServer;
17 }
18 
19 #include <set>
20 #include <map>
21 #include <thread>
22 #include <mutex>
23 
28 
31 class ChatEvent
32 {
33 public:
36  enum Type { Login, Logout, Rename, Message };
37 
40  Type type() const { return type_; }
41 
44  const Wt::WString& user() const { return user_; }
45 
48  const Wt::WString& message() const { return message_; }
49 
52  const Wt::WString& data() const { return data_; }
53 
58  const Wt::WString formattedHTML(const Wt::WString& user,
59  Wt::TextFormat format) const;
60 
61 private:
66 
67  /*
68  * Both user and html will be formatted as html
69  */
70  ChatEvent(const Wt::WString& user, const Wt::WString& message)
71  : type_(Message), user_(user), message_(message)
72  { }
73 
74  ChatEvent(Type type, const Wt::WString& user,
75  const Wt::WString& data = Wt::WString::Empty)
76  : type_(type), user_(user), data_(data)
77  { }
78 
79  friend class SimpleChatServer;
80 };
81 
82 typedef std::function<void (const ChatEvent&)> ChatEventCallback;
83 
86 class SimpleChatServer : boost::noncopyable
87 {
88 public:
89  /*
90  * A reference to a client.
91  */
92  class Client
93  {
94  };
95 
99 
108  bool connect(Client *client, const ChatEventCallback& handleEvent);
109 
115  bool disconnect(Client *client);
116 
121  bool login(const Wt::WString& user);
122 
125  void logout(const Wt::WString& user);
126 
129  bool changeName(const Wt::WString& user, const Wt::WString& newUser);
130 
133  Wt::WString suggestGuest();
134 
137  void sendMessage(const Wt::WString& user, const Wt::WString& message);
138 
141  typedef std::set<Wt::WString> UserSet;
142 
145  UserSet users();
146 
147 private:
148  struct ClientInfo {
149  std::string sessionId;
151  };
152 
153  typedef std::map<Client *, ClientInfo> ClientMap;
154 
156  std::recursive_mutex mutex_;
157  ClientMap clients_;
158  UserSet users_;
159 
160  void postChatEvent(const ChatEvent& event);
161 };
162 
165 #endif // SIMPLECHATSERVER_H_
std::function< void(const ChatEvent &)> ChatEventCallback
std::map< Client *, ClientInfo > ClientMap
Encapsulate a chat event.
std::set< Wt::WString > UserSet
Typedef for a collection of user names.
const Wt::WString & message() const
Get the message of the event.
Type
Enumeration for the event type.
ChatEvent(const Wt::WString &user, const Wt::WString &message)
std::recursive_mutex mutex_
Wt::WServer & server_
TextFormat
const Wt::WString & user() const
Get the user who caused the event.
static const WString Empty
Type type() const
Get the event type.
Wt::WString message_
Wt::WString user_
Wt::WString data_
A simple chat server.
ChatEvent(Type type, const Wt::WString &user, const Wt::WString &data=Wt::WString::Empty)
const Wt::WString & data() const
Get the extra data for this event.

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11