using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NLog;
using System.Diagnostics;   // Process

namespace TargetProject
{
    class Program
    {
        static Logger log = LogManager.GetLogger("target_logger");
        static void Main(string[] args)
        {
            string home_path = "C:\\Users\\hwajin\\source\\repos\\TargetProcess\\TargetProject\\";
            int chk_num = 0;
            bool is_num = int.TryParse(args[0], out chk_num);
            Console.WriteLine("is_num : " + is_num);

            if(is_num)
            {
                string port = args[0];
                Console.WriteLine("port : " + port);

                // 2. database connection
                DBConnect.DBConnection database_connect = new DBConnect.DBConnection();
                DBConnect.Struct.ConnectStateStc connect_result = database_connect.ConnectDB(home_path);

                // 2-1. connection exception
                if(!connect_result.is_connect)
                {
                    log.Error("database connection fail");
                    return;
                }

                log.Debug("database connection success");
                Console.WriteLine("database connection success");

                // process socket
                ProcessSocket.SocketProcess socket_process = new ProcessSocket.SocketProcess();
                String other_process_status = socket_process.TargetSocket(port, connect_result.connection);

                if(other_process_status == "exit")
                {
                    log.Debug("process exit");
                    Console.WriteLine("process exit");
                    Process process = Process.GetCurrentProcess();
                    process.Refresh();
                    process.Kill();
                }
            } else
            {
                Console.WriteLine("incorrect port");
                log.Error("incorrect port");
            }
        }
    }
}
