SimpleChatServer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <boost/noncopyable.hpp>
  10. #include <Wt/WSignal>
  11. #include <Wt/WString>
  12. namespace Wt {
  13. class WServer;
  14. }
  15. #include <set>
  16. #include <map>
  17. #include <boost/thread.hpp>
  18. /**
  19. * @addtogroup chatexample
  20. */
  21. /*@{*/
  22. /*! \brief Encapsulate a chat event.
  23. */
  24. class ChatEvent
  25. {
  26. public:
  27. /*! \brief Enumeration for the event type.
  28. */
  29. enum Type { Login, Logout, Rename, Message };
  30. /*! \brief Get the event type.
  31. */
  32. Type type() const { return type_; }
  33. /*! \brief Get the user who caused the event.
  34. */
  35. const Wt::WString& user() const { return user_; }
  36. /*! \brief Get the message of the event.
  37. */
  38. const Wt::WString& message() const { return message_; }
  39. /*! \brief Get the extra data for this event.
  40. */
  41. const Wt::WString& data() const { return data_; }
  42. /*! \brief Get the message formatted as HTML, rendered for the given user.
  43. *
  44. * The \p format indicates how the message should be formatted.
  45. */
  46. const Wt::WString formattedHTML(const Wt::WString& user,
  47. Wt::TextFormat format) const;
  48. private:
  49. Type type_;
  50. Wt::WString user_;
  51. Wt::WString data_;
  52. Wt::WString message_;
  53. /*
  54. * Both user and html will be formatted as html
  55. */
  56. ChatEvent(const Wt::WString& user, const Wt::WString& message)
  57. : type_(Message), user_(user), message_(message)
  58. { }
  59. ChatEvent(Type type, const Wt::WString& user,
  60. const Wt::WString& data = Wt::WString::Empty)
  61. : type_(type), user_(user), data_(data)
  62. { }
  63. friend class SimpleChatServer;
  64. };
  65. typedef boost::function<void (const ChatEvent&)> ChatEventCallback;
  66. /*! \brief A simple chat server
  67. */
  68. class SimpleChatServer : boost::noncopyable
  69. {
  70. public:
  71. /*
  72. * A reference to a client.
  73. */
  74. class Client
  75. {
  76. };
  77. /*! \brief Create a new chat server.
  78. */
  79. SimpleChatServer(Wt::WServer& server);
  80. /*! \brief Connects to the chat server.
  81. *
  82. * The passed callback method is posted to when a new chat event is
  83. * received.
  84. *
  85. * Returns whether the client has been connected (or false if the client
  86. * was already connected).
  87. */
  88. bool connect(Client *client, const ChatEventCallback& handleEvent);
  89. /*! \brief Disconnect from the chat server.
  90. *
  91. * Returns whether the client has been disconnected (or false if the client
  92. * was not connected).
  93. */
  94. bool disconnect(Client *client);
  95. /*! \brief Try to login with given user name.
  96. *
  97. * Returns false if the login was not successful.
  98. */
  99. bool login(const Wt::WString& user);
  100. /*! \brief Logout from the server.
  101. */
  102. void logout(const Wt::WString& user);
  103. /*! \brief Changes the name.
  104. */
  105. bool changeName(const Wt::WString& user, const Wt::WString& newUser);
  106. /*! \brief Get a suggestion for a guest user name.
  107. */
  108. Wt::WString suggestGuest();
  109. /*! \brief Send a message on behalve of a user.
  110. */
  111. void sendMessage(const Wt::WString& user, const Wt::WString& message);
  112. /*! \brief Typedef for a collection of user names.
  113. */
  114. typedef std::set<Wt::WString> UserSet;
  115. /*! \brief Get the users currently logged in.
  116. */
  117. UserSet users();
  118. private:
  119. struct ClientInfo {
  120. std::string sessionId;
  121. ChatEventCallback eventCallback;
  122. };
  123. typedef std::map<Client *, ClientInfo> ClientMap;
  124. Wt::WServer& server_;
  125. boost::recursive_mutex mutex_;
  126. ClientMap clients_;
  127. UserSet users_;
  128. void postChatEvent(const ChatEvent& event);
  129. };
  130. /*@}*/
  131. #endif // SIMPLECHATSERVER_H_