// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Xml; namespace Chat.Module.Login { using Chat.Framework.Protocol; using Chat.Framework.Provider; using Chat.Framework.Core; public class LoginRequestMessage : RequestMessage { public string Username { get; set; } public string Password { get; set; } } public class LoginResponseMessage : ResponseMessage { } public class LoginSerializer : SerializerBase { public LoginSerializer() : base("login", typeof(LoginRequestMessage), typeof(LoginResponseMessage)) { } public override RequestMessage Parse(string strmsg, string id) { LoginRequestMessage objmsg = new LoginRequestMessage(); objmsg.ClientID = id; XmlDocument doc = new XmlDocument(); doc.LoadXml(strmsg); objmsg.Username = doc.SelectSingleNode("//login-request/username/text()").Value; objmsg.Password = doc.SelectSingleNode("//login-request/password/text()").Value; return objmsg; } } public class LoginProcessor : ProcessorBase { public LoginProcessor() : base(typeof(LoginRequestMessage), typeof(LoginResponseMessage)) { } public override Tuple Process(ClientState prevstate, RequestMessage reqmsg, Server cntx) { if(prevstate == ClientState.NOT_SIGNEDIN) { ClientState nextstate; LoginResponseMessage respmsg = new LoginResponseMessage(); if(((LoginRequestMessage)reqmsg).Username == "test" && ((LoginRequestMessage)reqmsg).Password == "hemmeligt") { nextstate = ClientState.SIGNEDIN; respmsg.Status = true; } else { nextstate = prevstate; respmsg.Status = false; } return Tuple.Create(nextstate, (ResponseMessage)respmsg); } else { throw new IllegalStateChatException(); } } } public class LoginProvider : IProvider { public ISerializer GetSerializer() { return new LoginSerializer(); } public IProcessor GetProcessor() { return new LoginProcessor(); } } }