Og saa moduler for 4 message types.
Login.cs
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<ClientState,ResponseMessage> 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();
}
}
}
Send.cs
using System;
using System.Xml;
namespace Chat.Module.Send
{
using Chat.Framework.Protocol;
using Chat.Framework.Provider;
using Chat.Framework.Core;
public class SendRequestMessage : RequestMessage
{
public string Receiver { get; set; }
public string Text { get; set; }
}
public class SendResponseMessage : ResponseMessage
{
}
public class SendSerializer : SerializerBase
{
public SendSerializer() : base("send", typeof(SendRequestMessage), typeof(SendResponseMessage))
{
}
public override RequestMessage Parse(string strmsg, string id)
{
SendRequestMessage objmsg = new SendRequestMessage();
objmsg.ClientID = id;
XmlDocument doc = new XmlDocument();
doc.LoadXml(strmsg);
objmsg.Receiver = doc.SelectSingleNode("//send-request/receiver/text()").Value;
objmsg.Text = doc.SelectSingleNode("//send-request/text/text()").Value;
return objmsg;
}
}
public class SendProcessor : ProcessorBase
{
public SendProcessor() : base(typeof(SendRequestMessage), typeof(SendResponseMessage))
{
}
public override Tuple<ClientState,ResponseMessage> Process(ClientState prevstate, RequestMessage reqmsg, Server cntx)
{
if(prevstate == ClientState.SIGNEDIN)
{
SendRequestMessage reqmsg2 = (SendRequestMessage)reqmsg;
cntx.Send(reqmsg2.Receiver, reqmsg2.Text);
ClientState nextstate = prevstate;
SendResponseMessage respmsg = new SendResponseMessage();
respmsg.Status = true;
return Tuple.Create(nextstate, (ResponseMessage)respmsg);
}
else
{
throw new IllegalStateChatException();
}
}
}
public class SendProvider : IProvider
{
public ISerializer GetSerializer()
{
return new SendSerializer();
}
public IProcessor GetProcessor()
{
return new SendProcessor();
}
}
}
Read.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Chat.Module.Read
{
using Chat.Framework.Protocol;
using Chat.Framework.Provider;
using Chat.Framework.Core;
public class ReadRequestMessage : RequestMessage
{
}
public class ReadResponseMessage : ResponseMessage
{
public List<string> Text { get; set; }
}
public class ReadSerializer : SerializerBase
{
public ReadSerializer() : base("read", typeof(ReadRequestMessage), typeof(ReadResponseMessage))
{
}
public override string Format(ResponseMessage objmsg)
{
StringBuilder sb = new StringBuilder();
sb.Append("<read-response>");
sb.Append(string.Format("<status>{0}</status>", objmsg.Status));
foreach(string onetext in ((ReadResponseMessage)objmsg).Text)
{
sb.Append(string.Format("<text>{0}</text>", onetext));
}
sb.Append("</read-response>");
return sb.ToString();
}
}
public class ReadProcessor : ProcessorBase
{
public ReadProcessor() : base(typeof(ReadRequestMessage), typeof(ReadResponseMessage))
{
}
public override Tuple<ClientState,ResponseMessage> Process(ClientState prevstate, RequestMessage reqmsg, Server cntx)
{
if(prevstate == ClientState.SIGNEDIN)
{
ClientState nextstate = prevstate;
ReadResponseMessage respmsg = new ReadResponseMessage();
respmsg.Status = true;
respmsg.Text = cntx.Read(reqmsg.ClientID);
return Tuple.Create(nextstate, (ResponseMessage)respmsg);
}
else
{
throw new IllegalStateChatException();
}
}
}
public class ReadProvider : IProvider
{
public ISerializer GetSerializer()
{
return new ReadSerializer();
}
public IProcessor GetProcessor()
{
return new ReadProcessor();
}
}
}
Logout.cs
using System;
namespace Chat.Module.Logout
{
using Chat.Framework.Protocol;
using Chat.Framework.Provider;
using Chat.Framework.Core;
public class LogoutRequestMessage : RequestMessage
{
}
public class LogoutResponseMessage : ResponseMessage
{
}
public class LogoutSerializer : SerializerBase
{
public LogoutSerializer() : base("logout", typeof(LogoutRequestMessage), typeof(LogoutResponseMessage))
{
}
}
public class LogoutProcessor : ProcessorBase
{
public LogoutProcessor() : base(typeof(LogoutRequestMessage), typeof(LogoutResponseMessage))
{
}
public override Tuple<ClientState,ResponseMessage> Process(ClientState prevstate, RequestMessage reqmsg, Server cntx)
{
if(prevstate == ClientState.SIGNEDIN)
{
ClientState nextstate = ClientState.NOT_SIGNEDIN;
LogoutResponseMessage respmsg = new LogoutResponseMessage();
respmsg.Status = true;
return Tuple.Create(nextstate, (ResponseMessage)respmsg);
}
else
{
throw new IllegalStateChatException();
}
}
}
public class LogoutProvider : IProvider
{
public ISerializer GetSerializer()
{
return new LogoutSerializer();
}
public IProcessor GetProcessor()
{
return new LogoutProcessor();
}
}
}