Wt examples  3.3.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>
13 #include <Wt/WString>
14 
15 namespace Wt {
16  class WServer;
17 }
18 
19 #include <set>
20 #include <map>
21 #include <boost/thread.hpp>
22 
27 
30 class ChatEvent
31 {
32 public:
36 
39  Type type() const { return type_; }
40 
43  const Wt::WString& user() const { return user_; }
44 
47  const Wt::WString& message() const { return message_; }
48 
51  const Wt::WString& data() const { return data_; }
52 
58  Wt::TextFormat format) const;
59 
60 private:
65 
66  /*
67  * Both user and html will be formatted as html
68  */
70  : type_(Message), user_(user), message_(message)
71  { }
72 
75  : type_(type), user_(user), data_(data)
76  { }
77 
78  friend class SimpleChatServer;
79 };
80 
81 typedef boost::function<void (const ChatEvent&)> ChatEventCallback;
82 
85 class SimpleChatServer : boost::noncopyable
86 {
87 public:
88  /*
89  * A reference to a client.
90  */
91  class Client
92  {
93  };
94 
98 
107  bool connect(Client *client, const ChatEventCallback& handleEvent);
108 
114  bool disconnect(Client *client);
115 
120  bool login(const Wt::WString& user);
121 
124  void logout(const Wt::WString& user);
125 
128  bool changeName(const Wt::WString& user, const Wt::WString& newUser);
129 
133 
136  void sendMessage(const Wt::WString& user, const Wt::WString& message);
137 
140  typedef std::set<Wt::WString> UserSet;
141 
144  UserSet users();
145 
146 private:
147  struct ClientInfo {
148  std::string sessionId;
150  };
151 
152  typedef std::map<Client *, ClientInfo> ClientMap;
153 
155  boost::recursive_mutex mutex_;
158 
159  void postChatEvent(const ChatEvent& event);
160 };
161 
164 #endif // SIMPLECHATSERVER_H_

Generated on Fri May 31 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.3.1