// 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.Collections.Generic; using System.Linq; namespace Chat.Framework.Protocol { public abstract class Message { public string ClientID { get; set; } } public abstract class RequestMessage : Message { } public abstract class ResponseMessage : Message { public bool Status { get; set; } } } namespace Chat.Framework.Provider { using Chat.Framework.Protocol; using Chat.Framework.Core; public interface ISerializer { bool Accept(string strmsg); RequestMessage Parse(string strmsg, string id); bool Accept(ResponseMessage objmsg); string Format(ResponseMessage objmsg); } public interface IProcessor { bool Accept(RequestMessage reqmsg); Tuple Process(ClientState prevstate, RequestMessage reqmsg, Server cntx); } public interface IProvider { ISerializer GetSerializer(); IProcessor GetProcessor(); } public class SerializerBase : ISerializer { private string msgtyp; private Type reqtyp; private Type resptyp; public SerializerBase(string msgtyp, Type reqtyp, Type resptyp) { this.msgtyp = msgtyp; this.reqtyp = reqtyp; this.resptyp = resptyp; } public virtual bool Accept(string strmsg) { return strmsg.StartsWith(string.Format("<{0}-request>", msgtyp)) && strmsg.EndsWith(string.Format("", msgtyp)); } public virtual RequestMessage Parse(string strmsg, string id) { RequestMessage objmsg = (RequestMessage)Activator.CreateInstance(reqtyp); objmsg.ClientID = id; return objmsg; } public virtual bool Accept(ResponseMessage objmsg) { return objmsg.GetType() == resptyp; } public virtual string Format(ResponseMessage objmsg) { return string.Format("<{0}-response>{1}", msgtyp, objmsg.Status); } } public class ProcessorBase : IProcessor { private Type reqtyp; private Type resptyp; public ProcessorBase(Type reqtyp, Type resptyp) { this.reqtyp = reqtyp; this.resptyp = resptyp; } public virtual bool Accept(RequestMessage reqmsg) { return reqmsg.GetType() == reqtyp; } public virtual Tuple Process(ClientState prevstate, RequestMessage reqmsg, Server cntx) { ResponseMessage objmsg = (ResponseMessage)Activator.CreateInstance(resptyp); objmsg.Status = true; return Tuple.Create(prevstate, objmsg); } } } namespace Chat.Framework.Core { using Chat.Framework.Protocol; using Chat.Framework.Provider; public class ChatException : Exception { public ChatException(string descrip) : base(descrip) { } } public class UnknownMessageChatException : ChatException { public UnknownMessageChatException() : base("Unknown message type") { } } public class IllegalStateChatException : ChatException { public IllegalStateChatException() : base("Illegal state for message type") { } } public static class ProviderRepository { private static List repo = new List(); private static bool dirty = false; private static List ser = new List(); private static List proc = new List(); public static void RegisterProvideer(IProvider provider) { lock(repo) { repo.Add(provider); dirty = true; } } public static List GetSerializers() { lock(repo) { if(dirty) { ser = repo.Select(f => f.GetSerializer()).ToList(); } return ser; } } public static List GetProcessors() { lock(repo) { if(dirty) { proc = repo.Select(f => f.GetProcessor()).ToList(); } return proc; } } } public class Server { private Dictionary clients = new Dictionary(); public void AddClient(string id) { clients.Add(id, new Client(id, this)); } public void RemoveClient(string id) { clients.Remove(id); } public string Process(string id, string reqmsg) { return clients[id].Process(reqmsg); } public void Send(string id, string onetext) { clients[id].Send(onetext); } public List Read(string id) { return clients[id].Read(); } } public enum ClientState { SIGNEDIN, NOT_SIGNEDIN } public class Client { private string id; private Server cntx; private ClientState state; private List text; public Client(string id, Server cntx) { this.id = id; this.cntx = cntx; state = ClientState.NOT_SIGNEDIN; text = new List(); } public string Process(string strmsg) { foreach(ISerializer s in ProviderRepository.GetSerializers()) { if(s.Accept(strmsg)) { return s.Format(Process(s.Parse(strmsg, id))); } } throw new UnknownMessageChatException(); } public ResponseMessage Process(RequestMessage reqmsg) { foreach(IProcessor p in ProviderRepository.GetProcessors()) { if(p.Accept(reqmsg)) { Tuple res = p.Process(state, reqmsg, cntx); state = res.Item1; return res.Item2; } } throw new UnknownMessageChatException(); } public void Send(string onetext) { text.Add(onetext); } public List Read() { List res = text; text = new List(); return res; } } }