using SwanCSharp;
using SwanCSharp.Sockets;
using System;
using System.Collections.Generic;
using System.Text;

namespace ServerSocket
{
    class Program
    {
        private static SocketServer mobjServer;

        static void Main(string[] args)
        {
            mobjServer = new SocketServer("192.168.2.2", 5900);
            mobjServer.ReceiveData += new SocketServer.ReceiveDataEventHandler(mobjServer_ReceiveData);
            mobjServer.OpenClientConnection += new SocketServer.OpenClientConnectionEventHandler(mobjServer_OpenClientConnection);
        }

        private static void mobjServer_OpenClientConnection(string pstrIP, Int32 pintTCPPort)
        {
            Console.WriteLine("Connection accepted with IP: " + pstrIP + " - TCP Port: " + pintTCPPort.ToString());
        }

        private static void mobjServer_ReceiveData(string pstrCommand, string pstrData, string pstrIP, Int32 pintTCPPort, object pobjChannel)
        {
            string lstrReceivedData = "";
            if (pstrCommand.Length > 0)
            {
                switch (pstrCommand)
                {
                    case "01":
                        lstrReceivedData = pstrData;
                        /*
                        Include here the desired source code for
                        the command "01" (write data in databases,
                        data processing and return them to the client, etc.)
                        */
                        Console.WriteLine("Command '01' received from IP: " + pstrIP + " - TCP Port: " + pintTCPPort.ToString());
                        if (Validations.HexadecimalInString("FF"))
                        {
                            mobjServer.SendResponse("FF", "Command '01' executed successfully", pobjChannel);
                        }
                        break;
                    case "02":
                        lstrReceivedData = pstrData;
                        /*
                        Include here the desired source code for
                        the command "02" (write data in databases,
                        data processing and return them to the client, etc.)
                        */
                        Console.WriteLine("Command '02' received IP: " + pstrIP + " - TCP Port: " + pintTCPPort.ToString());
                        mobjServer.SendResponse("FF", "Command '02' executed successfully", pobjChannel);
                        break;
                    default:
                        Console.WriteLine("The command '" + pstrCommand + "' from IP: " + pstrIP + " - TCP Port: " + pintTCPPort.ToString() + " does not exist");
                        mobjServer.SendResponse("F0", "Error: The command does not exist", pobjChannel);
                        break;
                }
            }
        }
    }
}
