PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/source/samples/ObviousCode.Interlace.Chatroom/ObviousCode.Interlace.ChatRoomServer/Protocols/ChatroomServerProtocolFactory.cs

https://bitbucket.org/VahidN/interlace
C# | 172 lines | 131 code | 41 blank | 0 comment | 6 complexity | d75d765bb88937bd8ab9cfbcca6867ee MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Interlace.ReactorCore;
  6. using Interlace.PropertyLists;
  7. using Interlace.Erlang;
  8. using ObviousCode.Interlace.ChatroomServer.Plugins;
  9. using ObviousCode.Interlace.Chatroom.Library;
  10. using ObviousCode.Interlace.ChatroomServer;
  11. namespace ObviousCode.Interlace.ChatroomServer.Protocols
  12. {
  13. public class ChatroomServerProtocolFactory : IProtocolFactory
  14. {
  15. ChatroomSettings _settings;
  16. List<IChatroomServerPlugin> _plugins;
  17. ClientCache _clients;
  18. public ChatroomServerProtocolFactory(ChatroomSettings settings, params IChatroomServerPlugin[] plugins)
  19. {
  20. _settings = settings;
  21. LoadDefaultPlugins();
  22. _plugins.AddRange(plugins);
  23. _clients = new ClientCache();
  24. }
  25. private void LoadDefaultPlugins()
  26. {
  27. ChatroomServerBroadcastPlugin broadcaster = new ChatroomServerBroadcastPlugin();
  28. ChatroomServerLoginRequestPlugin login = new ChatroomServerLoginRequestPlugin();
  29. ChatroomServerLogoutRequestPlugin logout = new ChatroomServerLogoutRequestPlugin();
  30. _plugins = new List<IChatroomServerPlugin>();
  31. _plugins.Add(broadcaster);
  32. _plugins.Add(login);
  33. _plugins.Add(logout);
  34. }
  35. public Protocol BuildProtocol()
  36. {
  37. ChatroomServerProtocol newInstance = new ChatroomServerProtocol(this, _plugins);
  38. return newInstance;
  39. }
  40. internal void Broadcast(PropertyDictionary request)
  41. {
  42. foreach (ChatroomServerProtocol instance in _clients)
  43. {
  44. instance.SendMessage(request);
  45. }
  46. }
  47. internal void BroadcastLoggedInUsers()
  48. {
  49. PropertyDictionary dictionary = PropertyDictionary.EmptyDictionary();
  50. dictionary.SetValueFor(ChatroomKeys.MessageType, ChatroomKeys.ChatterListUpdate);
  51. dictionary.SetValueFor(ChatroomKeys.Message, BuildUserList());
  52. Broadcast(dictionary);
  53. }
  54. internal void BroadcastServerMessage(string message, params string[] args)
  55. {
  56. PropertyDictionary dictionary = PropertyDictionary.EmptyDictionary();
  57. dictionary.SetValueFor(ChatroomKeys.MessageType, ChatroomKeys.Message);
  58. dictionary.SetValueFor(ChatroomKeys.Message, string.Format(message, args));
  59. Broadcast(dictionary);
  60. }
  61. private bool CanAddClient
  62. {
  63. get
  64. {
  65. return _clients.Count < _settings.MaximumClients;
  66. }
  67. }
  68. internal string ResolveSenderName(PropertyDictionary request)
  69. {
  70. if (request.HasValueFor(ChatroomKeys.SenderId))
  71. {
  72. ChatroomServerProtocol protocol = _clients[request.StringFor(ChatroomKeys.SenderId)];
  73. return protocol == null ? null : protocol.Username;
  74. }
  75. return null;
  76. }
  77. private bool UsernameIsAvailable(string username)
  78. {
  79. foreach (ChatroomServerProtocol instance in _clients)
  80. {
  81. if (instance.Username.ToUpperInvariant() == username.Trim().ToUpperInvariant()) return false;
  82. }
  83. return true;
  84. }
  85. public void StartedConnecting()
  86. {
  87. Console.WriteLine("ChatRoom Server connection commenced ...");
  88. }
  89. internal object RequestLogin(ChatroomServerProtocol protocol, PropertyDictionary request)
  90. {
  91. if (!CanAddClient) return ChatroomKeys.LoginFail_TooManyClients;
  92. if (!UsernameIsAvailable(request.StringFor(ChatroomKeys.SenderName))) return ChatroomKeys.LoginFail_UserNameInUse;
  93. protocol.Username = request.StringFor(ChatroomKeys.SenderName);
  94. protocol.Key = request.StringFor(ChatroomKeys.SenderId);
  95. _clients.AddClient(protocol);
  96. BroadcastLoggedInUsers();
  97. return ChatroomKeys.LoginSuccess;
  98. }
  99. private PropertyArray BuildUserList()
  100. {
  101. PropertyArray users = PropertyArray.EmptyArray();
  102. foreach (ChatroomServerProtocol user in _clients)
  103. {
  104. users.AppendValue(user.Username);
  105. }
  106. return users;
  107. }
  108. public void ConnectionFailed(Exception ex)
  109. {
  110. Console.WriteLine("Server Protocol Factory reports Connection Failed");
  111. }
  112. internal void ReportDisconnection(ChatroomServerProtocol disconnectingProtocol)
  113. {
  114. _clients.RemoveClient(disconnectingProtocol);
  115. BroadcastServerMessage("{0} has left.", disconnectingProtocol.Username);
  116. BroadcastLoggedInUsers();
  117. }
  118. internal string WelcomeMessage
  119. {
  120. get
  121. {
  122. return _settings.WelcomeMessage;
  123. }
  124. }
  125. internal void NotifyOnLogout(ChatroomServerProtocol protocol)
  126. {
  127. Console.WriteLine("{0} logged out gracefully.", protocol.Username);
  128. ReportDisconnection(protocol);
  129. }
  130. }
  131. }