using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace ssgrestserver { abstract class ARequestObject { protected string TAG = "DB Connection Abstarct Class"; //private const string SSG_REST_SERVER_REAL = "http://inf.shinsegaetvshopping.com/"; private const string SSG_REST_SERVER_REAL = "http://202.3.21.200/"; private const string SSG_REST_SERVER_REAL_New = "https://m.shinsegaetvshopping.com/"; private const string SSG_REST_SERVER_TEST = "http://sinf-dev.shinsegaetvshopping.com/"; protected List mNeedKeyList = new List(); protected Dictionary mAccessableDictionary = null; internal ARequestObject() { mRequestParameterKVPairList = new Dictionary(); } /// /// 접근할 테이블명 /// protected string TABLE_NAME = ""; protected bool mIsSendTypeGet = false; protected Dictionary mRequestParameterKVPairList = null; protected DataCenter mDataCenter = DataCenter.getInstance(); protected string m채널이름 = ""; protected readonly string[] 송출가능목록 = { "상품", "경품", "텍스트", "채팅", "상품평", "QNA", "공지", "채팅판", "동영상", "콜데이터", "매진" }; /// /// 테스트중 /// public bool mIsRealURL = true; internal static string create(string receiveValue) { string returnValue = ""; try { //인스턴스에서 쓸 값 초기화. ARequestObject bufInstance = null; ///요청종류/씬포멧명/적용범위 ?Key=Value&Key=Value... ///방송용/코스피지수?Key=Value&Key=Value... 1타입 //////방송용/9?적용범위=코스피지수&종류=당일 2타입 //URL에서 Request부분(?)과 address부분을 분리 string[] bufURLSplit = receiveValue.Split('?'); //리퀘스트타입은 Get으로 넘어오는 Address데이터의 마지막부분 -> /RequestType string[] bufRequestArray = bufURLSplit[0].Split('/'); string 사용처 = bufRequestArray[2]; string 씬이름 = bufRequestArray[3]; ///씬에따라 구분. ///사용처는 나중에 User Key로 사용할예정 현재는 그냥 쓰자. 테스트용임 switch (사용처) { #region "방송용데이터" case "새데이터조회": switch (씬이름) { case "상품수량": bufInstance = new 새상품수량(); break; case "경품프로모션": bufInstance = new 새경품프로모션(); break; case "채팅리스트": bufInstance = new 채팅리스트(); break; case "채팅답변등록": bufInstance = new 채팅답변등록(); break; case "방송채택": bufInstance = new 방송채택(); break; case "방송편성": bufInstance = new 방송편성(); break; case "상품평조회": bufInstance = new 상품평조회(); break; case "상품QnA": bufInstance = new 상품QnA(); break; case "공지CRUD": bufInstance = new 공지CRUD(); break; case "콜데이터조회": bufInstance = new 콜데이터조회(); break; case "방송VVIP": bufInstance = new 방송VVIP(); break; case "매진조회": bufInstance = new 매진조회(); break; default: returnValue = "알수없는 분류 - " + 사용처 + "----" + 씬이름; break; } break; #endregion case "데이터입력": switch (씬이름) { case "상품수량": bufInstance = new 입력상품수량(); break; case "경품프로모션": bufInstance = new 입력경품프로모션(); break; default: returnValue = "알수없는 분류 - " + 사용처 + "----" + 씬이름; break; } break; case "입력데이터조회": switch (씬이름) { case "상품수량": bufInstance = new 기존상품수량(); break; case "경품프로모션": bufInstance = new 기존경품프로모션(); break; default: returnValue = "알수없는 분류 - " + 사용처 + "----" + 씬이름; break; } break; case "자막상황": switch (씬이름) { case "조회": bufInstance = new 자막조회(); break; case "입력": bufInstance = new 자막입력(); break; default: returnValue = "알수없는 분류 - " + 사용처 + "----" + 씬이름; break; } break; default: break; case "텍스트": switch (씬이름) { case "조회": bufInstance = new 기존텍스트(); break; case "입력": bufInstance = new 입력텍스트(); break; } break; case "씬이름": switch (씬이름) { case "조회": bufInstance = new 조회씬이름(); break; case "입력": bufInstance = new 입력씬이름(); break; } break; case "QNA": switch (씬이름) { case "조회": bufInstance = new 기존QNA(); break; case "입력": bufInstance = new 입력QNA(); break; } break; case "공지": switch (씬이름) { case "조회": bufInstance = new 기존공지(); break; case "입력": bufInstance = new 입력공지(); break; } break; case "상품평": switch (씬이름) { case "조회": bufInstance = new 기존상품평(); break; case "입력": bufInstance = new 입력상품평(); break; } break; case "채팅": switch (씬이름) { case "조회": bufInstance = new 기존채팅(); break; case "입력": bufInstance = new 입력채팅(); break; } break; case "채팅판": switch (씬이름) { case "조회": bufInstance = new 기존채팅판(); break; case "입력": bufInstance = new 입력채팅판(); break; } break; case "동영상": switch (씬이름) { case "조회": bufInstance = new 기존동영상(); break; case "입력": bufInstance = new 입력동영상(); break; } break; case "콜데이터": switch (씬이름) { case "조회": bufInstance = new 기존콜데이터(); break; case "입력": bufInstance = new 입력콜데이터(); break; } break; case "매진": switch (씬이름) { case "조회": bufInstance = new 기존매진(); break; case "입력": bufInstance = new 입력매진(); break; } break; case "test": switch (씬이름) { case "test": bufInstance = new 테스트(); break; } break; } bufInstance.mNeedKeyList = new List(); if (bufRequestArray[1].Trim() == "") { throw new Exception("잘못된 채널정보"); } bufInstance.m채널이름 = bufRequestArray[1]; if (bufInstance != null) { //URL이 충분히 왔을때만 작업 if (bufURLSplit.Length > 1) { bufInstance.mRequestParameterKVPairList = bufInstance.parseParameter(bufURLSplit[1]); } //필수키가 있는 리퀘스트에 한해서 체크한다. string checkString = ""; if (bufInstance is INeedKeyRequest) { ((INeedKeyRequest)bufInstance).setNeedKeys(); checkString = bufInstance.checkNeedUpdateKey(); } if (checkString == "") { returnValue = bufInstance.startUpdateWork(); } else { returnValue = checkString; } } return returnValue; } catch (DuplicateNameException dne) { return "같은 종목을 여러번 요청하였습니다. " + dne.ToString(); } } protected Dictionary parseParameter(string recvParametersInURL) { Dictionary rtnValue = new Dictionary(); string[] bufGetDataArray = recvParametersInURL.Split('&'); foreach (string item in bufGetDataArray) { string[] bufKVDataArray = item.Split('='); rtnValue.Add(bufKVDataArray[0], bufKVDataArray[1]); } return rtnValue; } protected abstract string startUpdateWork(); protected string httpRequest(string requestURL) { string rtnValue = ""; try { bool isMasterGoodsInfo = requestURL.Contains("masterGoodsInfo"); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create((mIsRealURL ? SSG_REST_SERVER_REAL : SSG_REST_SERVER_TEST) + requestURL); if (isMasterGoodsInfo) { httpWebRequest = (HttpWebRequest)WebRequest.Create((SSG_REST_SERVER_REAL_New) + requestURL); } ; // 인코딩 UTF-8 byte[] sendData = UTF8Encoding.UTF8.GetBytes(""); if (mIsSendTypeGet || isMasterGoodsInfo) { httpWebRequest.Method = "GET"; } else { httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = sendData.Length; Stream requestStream = httpWebRequest.GetRequestStream(); requestStream.Write(sendData, 0, sendData.Length); requestStream.Close(); } HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")); rtnValue = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); return rtnValue; } catch (Exception ex) { return ex.ToString(); } } private string checkNeedUpdateKey() { foreach (string item in mNeedKeyList) { if (!mRequestParameterKVPairList.ContainsKey(item)) { return "필수키가 없음 - " + item; } } return ""; } protected void 조회시간추가() { Dictionary bufTargetDictionary = DataCenter.getInstance().getData(DataCenter.관리중인데이터들배열.코더상태); if (mRequestParameterKVPairList.ContainsKey("보낸사람")) { if (mRequestParameterKVPairList["보낸사람"] == "코더") { bufTargetDictionary[m채널이름]["코더조회시간"] = DateTime.Now; } } TimeSpan 시간차 = DateTime.Now - Convert.ToDateTime(bufTargetDictionary[m채널이름]["코더조회시간"]); if (시간차.TotalSeconds > 3) { bufTargetDictionary[m채널이름]["코더"] = "N"; } else { bufTargetDictionary[m채널이름]["코더"] = "Y"; } bufTargetDictionary[m채널이름]["현재서버시간"] = DateTime.Now; } protected void 코더확인객채생성여부() { if (!DataCenter.getInstance().getData(DataCenter.관리중인데이터들배열.코더상태).ContainsKey(m채널이름)) { JObject buf코더상태 = new JObject(); foreach (string item in 송출가능목록) { buf코더상태.Add(item, "N"); } buf코더상태.Add("코더", "N"); buf코더상태.Add("코더조회시간", new DateTime(1990, 1, 1)); buf코더상태.Add("현재서버시간", DateTime.Now); DataCenter.getInstance().getData(DataCenter.관리중인데이터들배열.코더상태).Add(m채널이름, buf코더상태); } } } }