첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

REST 기술을 사용해 Point-to-Point 방식으로 데이터를 송수신할 수 있는 기능을 클래스 라이브러리로 만들었습니다.

HTTP 프로토콜을 지원합니다.

 

 

▶ 사용법

 

Server

using System;
using System.Threading;

using DSCORE.NETWORK.MESSAGE;
using DSCORE.NETWORK.MESSAGE.REST;

namespace TEST.NETWORK.MESSAGE.REST.SERVER
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 로그 헬퍼
        /// </summary>
        private static ILogHelper _logHelper = new ConsoleLogHelper();

        /// <summary>
        /// 송신기
        /// </summary>
        private static ISender _sender = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 수신기 메시지 수신시 처리하기 - receiver_MessageReceived(sender, e)

        /// <summary>
        /// 수신기 메시지 수신시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            RESTReceiver receiver = sender as RESTReceiver;

            if(e.Message.SendType == SendType.ASYNC)
            {
                try
                {
                    if(e.Message is MessageRequest)
                    {
                        #region 메시지 요청을 처리한다.

                        MessageRequest request = e.Message as MessageRequest;

                        //////////////////////////////////////////////////
                        _logHelper.WriteLog("ASYNC MESSAGE REQUEST RECEIVED : {0}", request.Content);

                        string resultContent = null;

                        for(int i = 1; i <= 10; i++)
                        {
                            _logHelper.WriteLog("ASYNC MESSAGE REQUEST {0} PROCESSED {1} JOB...", request.Content, i);

                            System.Threading.Thread.Sleep(1000);

                            if(request.Canceled)
                            {
                                _logHelper.WriteLog("ASYNC MESSAGE REQUEST {0} CANCELED...", request.Content);

                                break;
                            }
                        }

                        resultContent = request.Content;
                        //////////////////////////////////////////////////

                        if(!request.Canceled)
                        {
                            MessagePoint remotePoint = new MessagePoint(receiver.LocalPoint.TransportMode,
                                request.SendAddress, request.SendPort);

                            MessageResult messageResult = new MessageResult(request);

                            messageResult.Content = resultContent;

                            try
                            {
                                _sender.SendMessageResultAsync(remotePoint, messageResult);

                                _logHelper.WriteLog("ASYNC MESSAGE RESULT SENT : {0}", request.Content);
                            }
                            catch(Exception exception)
                            {
                                _logHelper.WriteLog     ("ERROR : SEND ASYNC MESSAGE RESULT"                 );
                                _logHelper.WriteMessage ("--------------------------------------------------");
                                _logHelper.WriteMessage (request.Content                                     );
                                _logHelper.WriteMessage ("--------------------------------------------------");
                                _logHelper.WriteErrorLog(exception                                           );
                                _logHelper.WriteMessage ("--------------------------------------------------");
                            }
                        }

                        #endregion
                    }
                }
                finally
                {
                    receiver.ReceiveMessageDictionary.RemoveMessage(e.Message.ID);
                }
            }
            else if(e.Message.SendType == SendType.SYNC)
            {
                if(e.Message is MessageRequest)
                {
                    #region 메시지 요청을 처리한다.

                    MessageRequest messageRequest = e.Message as MessageRequest;

                    _logHelper.WriteLog("SYNC MESSAGE REQUEST RECEIVED : {0}", messageRequest.Content);

                    e.ReplyContent = "SUCCESS";

                    #endregion
                }
            }
        }

        #endregion

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            ThreadPool.SetMinThreads(500 , 500 );
            ThreadPool.SetMaxThreads(5000, 5000);

            #region 수신기를 설정한다.

            IReceiver receiver = new RESTReceiver();

            receiver.SetEndpoint(TransportMode.TCP, "127.0.0.1", 8000, 50);

            receiver.MessageReceived += receiver_MessageReceived;

            #endregion
            #region 송신기를 설정한다.

            _sender = new RESTSender();

            _sender.SetEndpoint(TransportMode.TCP, "127.0.0.1", 8010);

            #endregion

            receiver.Start();

            _logHelper.WriteLog("PRESS ANY KEY TO EXIT PROGRAM...");

            Console.ReadKey(true);

            receiver.Stop();

            _logHelper.WriteLog("RECEIVER STOPPED...");
        }

        #endregion
    }
}

 

Client

using System;
using System.Threading;

using DSCORE.NETWORK.MESSAGE;
using DSCORE.NETWORK.MESSAGE.REST;

namespace TEST.NETWORK.MESSAGE.REST.CLIENT
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 로그 헬퍼
        /// </summary>
        private static ILogHelper _logHelper = new ConsoleLogHelper();

        /// <summary>
        /// 지역 주소
        /// </summary>
        private const string LOCAL_ADDRESS = "127.0.0.1";

        /// <summary>
        /// 지역 포트
        /// </summary>
        private const int LOCAL_PORT = 9000;

        /// <summary>
        /// 서버 주소
        /// </summary>
        private const string SERVER_ADDRESS = "127.0.0.1";

        /// <summary>
        /// 서버 포트
        /// </summary>
        private const int SERVER_PORT = 8000;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 수신기 메시지 수신시 처리하기 - receiver_MessageReceived(sender, e)

        /// <summary>
        /// 수신기 메시지 수신시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            try
            {
                if(e.Message.SendType == SendType.ASYNC)
                {
                    if(e.Message is MessageResult)
                    {
                        MessageResult messageResult = e.Message as MessageResult;

                        _logHelper.WriteLog("ASYNC MESSAGE RECEIVED : {0}", messageResult.Content);

                        Thread.Sleep(1000);

                        Environment.Exit(0);
                    }
                }
            }
            catch(Exception exception)
            {
                _logHelper.WriteLog(exception.Message);
            }
        }

        #endregion

        #region 프로그램 시작하기 - Main(argumentArray)

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        /// <param name="argumentArray">인자 배열</param>
        /// <returns>처리 결과</returns>
        private static int Main(string[] argumentArray)
        {
            string number = null;
            
            if(argumentArray.Length > 0)
            {
                number = argumentArray[0];
            }
            else
            {
                number = "100";
            }

            _logHelper.WriteLog("START MESSAGE TEST : NUMBER={0}", number);

            MessagePoint localPoint = new MessagePoint(TransportMode.TCP, LOCAL_ADDRESS, NetworkHelper.GetAvailablePort());

            #region 수신기를 설정한다.

            IReceiver receiver = new RESTReceiver();

            receiver.SetEndpoint(localPoint, 10);

            receiver.MessageReceived += receiver_MessageReceived;

            #endregion
            #region 송신기를 설정한다.

            ISender sender = new RESTSender();

            sender.SetEndpoint(localPoint);

            #endregion

            receiver.Start();

            #region 메시지 요청을 송신한다.

            MessagePoint remotePoint = new MessagePoint(TransportMode.TCP, SERVER_ADDRESS, SERVER_PORT);

            MessageRequest request = new MessageRequest();

            request.Group   = "AAA";
            request.Content = number;

            CallResult callResult = sender.SendMessageRequestAsync(remotePoint, request);

            if(callResult != null)
            {
                _logHelper.WriteLog("MESSAGE REQUEST SENT...");
            }
            else
            {
                _logHelper.WriteLog("ERROR OCCURRED WHEN MESSAGE REQUEST SENDING...");
            }

            #endregion

            _logHelper.WriteLog("PRESS ANY KEY TO EXIT PROGRAM...");

            Console.ReadKey(true);

            return 0;
        }

        #endregion
    }
}

 

▶ 개발 환경

  • 개발 도구 : Visual Studio 2017
  • 프로그래밍 언어 : C# (.NET Framework 4.5)
  • 사용 3자 컴포넌트 : (없음)

 

▶ 소스 코드 구성

 

▶ 구입 문의

소스 코드 및 구입 방법에 대해 icodebroker@naver.com 메일로 문의하시면 답변을 드리도록 하겠습니다.

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요