PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/VahidN/interlace
C# | 122 lines | 89 code | 31 blank | 2 comment | 7 complexity | 6a4dfed400276b9254972596b87426a4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Interlace.Erlang;
  6. using Interlace.PropertyLists;
  7. using ObviousCode.Interlace.Chatroom.Library;
  8. using ObviousCode.Interlace.ChatroomServer;
  9. using ObviousCode.Interlace.ChatroomServer.Plugins;
  10. using Interlace.ReactorCore;
  11. namespace ObviousCode.Interlace.ChatroomServer.Protocols
  12. {
  13. public class ChatroomServerProtocol : TermProtocol
  14. {
  15. ChatroomServerProtocolFactory _parent;
  16. Dictionary<string, IChatroomServerPlugin> _plugins;
  17. string _username;
  18. string _key;
  19. public ChatroomServerProtocol(ChatroomServerProtocolFactory parent, List<IChatroomServerPlugin> plugins)
  20. {
  21. _parent = parent;
  22. LoadPlugins(plugins);
  23. _username = null;
  24. _key = null;
  25. }
  26. protected override void ConnectionLost(CloseReason reason)
  27. {
  28. Console.WriteLine("{0}Connection Lost {1}", string.IsNullOrEmpty(_username) ? "Lurker " : "", string.IsNullOrEmpty(_username) ? _username : "");
  29. //TODO: Log event
  30. //LOST CONNECTION CALL BACK
  31. if (!string.IsNullOrEmpty(_username))
  32. {
  33. Parent.ReportDisconnection(this);
  34. }
  35. }
  36. protected override void TermReceived(object term)
  37. {
  38. Atom atom = term as Atom;
  39. if (atom == null || atom.Value == null) return;
  40. PropertyDictionary message = PropertyDictionary.FromString(atom.Value);
  41. if (!message.HasStringFor(ChatroomKeys.MessageType)) return;
  42. if (!_plugins.ContainsKey(message.StringFor(ChatroomKeys.MessageType))) return;
  43. _plugins[message.StringFor(ChatroomKeys.MessageType)].HandleRequest(this, message);
  44. }
  45. public ChatroomServerProtocolFactory Parent
  46. {
  47. get { return _parent; }
  48. }
  49. private void LoadPlugins(List<IChatroomServerPlugin> pluginList)
  50. {
  51. _plugins = new Dictionary<string, IChatroomServerPlugin>();
  52. foreach (IChatroomServerPlugin plugin in pluginList)
  53. {
  54. _plugins[plugin.Key] = plugin;
  55. }
  56. }
  57. internal void SendMalformedRequestResponse()
  58. {
  59. PropertyDictionary response = PropertyDictionary.EmptyDictionary();
  60. response.SetValueFor(ChatroomKeys.Message, ChatroomKeys.MalformedRequest);
  61. SendMessage(response);
  62. }
  63. protected override void ConnectionMade()
  64. {
  65. Console.WriteLine("Connection made ...");
  66. }
  67. public string Username
  68. {
  69. get { return _username; }
  70. set { _username = value; }
  71. }
  72. public string Key
  73. {
  74. get { return _key; }
  75. set { _key = value; }
  76. }
  77. internal void SendWelcomeMessage()
  78. {
  79. PropertyDictionary welcome = PropertyDictionary.EmptyDictionary();
  80. welcome.SetValueFor(ChatroomKeys.MessageType, ChatroomKeys.Message);
  81. welcome.SetValueFor(ChatroomKeys.Message, Parent.WelcomeMessage);
  82. SendMessage(welcome);
  83. welcome.SetValueFor(ChatroomKeys.Message, " ");
  84. SendMessage(welcome);
  85. }
  86. internal void SendMessage(PropertyDictionary message)
  87. {
  88. SendTerm(Atom.From(message.PersistToString()));
  89. }
  90. }
  91. }