mercredi 29 juin 2016

How to Mock TCPClient by implementing the interface

I am new to computer science and have few knowledge in network. I got a task in my internship.It is to mock a TCPClient by implement Interface. The idea is that since we don't need to connect to the real server, we just need a mock TCPClient to receive data, save it and send it out. From what I understand, is it just to mock the sendmessage function in TCPClient??

public interface ITCPClient
{
    /// <summary>
    /// Event handler when TCP client is connected 
    /// </summary>
    event ConnectionEventHandler Connected;

    /// <summary>
    /// Event handler for TCP Client on receiving data
    /// </summary>
    event DataReceivedEventHandler DataReceived;

    /// <summary>
    /// Event handler when TCP client is disconnect
    /// </summary>
    event ConnectionEventHandler Disconnected;

    /// <summary>
    /// Reports error when an error occurs
    /// </summary>
    event ErrorEventHandler OnError;

    /// <summary>
    /// Set whether to use HostByteOrder or NetworkByteorder with the socket - gateway expects network byte order
    /// </summary>
    bool ByteOrder { get; set; }

    /// <summary>
    /// Returns true if the client is running and connected to the server
    /// </summary>
    bool IsRunning { get; }

    /// <summary>
    /// Add the message to the sendqueue. The message will be processed by the ProcessMessage function waiting for the message.
    /// </summary>
    /// <param name="Message"></param>
    void SendMessage(string Message);

    /// <summary>
    /// Add the message to the sendqueue. The message willbe processed by the ProcessMessage function waiting for the message.
    /// </summary>
    /// <param name="Message"></param>
    void SendMessageHiPriority(string Message);

    /// <summary>
    /// Starts the client and spawn the thread for processing the message
    /// </summary>
    void Start();

    /// <summary>
    /// Disconnects and stops listening for messages 
    /// </summary>
    void StopProcessing();

}

public class MockTCPClient : ITCPClient
{
    #region Private Fields

    private string msg;

    #endregion Private Fields


    #region Constructor

    public MockTCPClient()
    {
        //nothing 
    }


    #endregion Constructor

    #region event

    public event ConnectionEventHandler Connected;
    public event ConnectionEventHandler Disconnected;
    public event ErrorEventHandler OnError;
    public event DataReceivedEventHandler DataReceived;

    #endregion event 

    #region property
    //question 
    public string ReceivedMessage
    {
        get
        {
            return msg;
        }

        set
        {
            msg = value;
        }

    }

    #endregion property

    //question??
    private void OnDataReceived(object sender, string e)
    {
        DataReceivedEventHandler dataReceived = DataReceived;
        if (dataReceived != null)
            dataReceived(this, e);
    }


    #region ITCPClient Members
    #region properties

    public bool ByteOrder { get; set; }


    public bool IsRunning { get; }

    #endregion properties

    public void SendMessage(string Message)
    {
        msg = Message;
    }

    public void SendMessageHiPriority(string Message)
    {    
    }

    public void Start()
    {
    }


    public void StopProcessing()
    {
    }


    #endregion ITCPClient Members

}

Aucun commentaire:

Enregistrer un commentaire