본문 바로가기

카테고리 없음

2022 연습 b-3

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TEST
{
    public class HttpResult
    {
        public string result { get; set; }
    }

    public class param
    {
        public string uri { get; set; }
        public int index { get; set; }
        //public string text { get; set; }
    }

    class Program
    {
        static string output = null;

        public static async void GetCallAPI()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    //var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
                    var response = await client.GetAsync("http://127.0.0.1:80/queueIn");
                    if (response != null)
                    {
                        var jsonString = await response.Content.ReadAsStringAsync();
                        Console.WriteLine("{0}", jsonString);
                        //return JsonConvert.DeserializeObject<object>(jsonString);
                        JObject Jobject = JObject.Parse(jsonString);
                        int queueCount = (int)Jobject["inputQueueCou"];
                        JArray array = (JArray)Jobject["inputQueueUR"];
                        output = (string)Jobject["outputQURI"];

                        for (int i = 0; i < array.Count; i++)
                        {
                            Thread myThread = new Thread(reqQueue);
                            string uri = array[i].ToString();
                            param p = new param();
                            p.uri = uri;
                            p.index = i;
                            myThread.Start(p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception1" + ex.ToString());
            }
        }

        private static void reqQueue(object _obj)
        {
            try
            {
                param pa = (param)_obj;
                Worker w = new Worker(pa.index);

                while (true)
                {
                    WebClient client = new WebClient();
                    string resp = client.DownloadString(pa.uri);
                    JObject Jobject = JObject.Parse(resp);
                    int timestamp = (int)Jobject["timestamp"];
                    string value = (string)Jobject["value"];

                    Console.WriteLine("{0} {1} {2}", pa.uri, timestamp, value);

                    string resultStr = w.Run(timestamp, value);
                    if (resultStr != null)
                    {
                        Console.WriteLine("######### {0}", resultStr);
                        JObject result = new JObject(
                            new JProperty("result", resultStr)
                            );
                        
                        string re = "{\"result\":\"" + value + "\"}";
   
                        WebClient webClient = new WebClient();
                        webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                        webClient.Encoding = UTF8Encoding.UTF8;
                        string responseJSON = webClient.UploadString(output, result.ToString());
                    }
                }
            }
            catch (Exception eee) { }
        }

        static void Main(string[] args)
        {
            Task T = new Task(GetCallAPI);
            T.Start();
            Console.ReadLine();
        }
    }
}

 

            List<string> temp = new List<string>();

            for (int i = 0; i < store.Count; i++)
            {
                string[] list = store[i].Split('#');
                long time = long.Parse(list[0]);
                //Console.WriteLine("{0}", store[i]);
                if (timestamp - time > 3000)
                {
                    //Console.WriteLine("remove {0}", store[i]);
                    //store.RemoveAt(i);
                    temp.Add(store[i]);
                }
            }
            for (int j = 0; j < temp.Count; j++)
            {
                for (int i = 0; i < store.Count; i++)
                {
                    if (store[i] == temp[j])
                    {
                        //Console.WriteLine("####### remove {0}", store[i]);
                        store.Remove(store[i]);
                        break;
                    }
                }
            }
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TEST
{
    public class HttpResult
    {
        public string result { get; set; }
    }

    public class param
    {
        public string uri { get; set; }
        public int index { get; set; }
        //public string text { get; set; }
    }

    class Program
    {
        static string output = null;

        public static async void GetCallAPI()
        {
            string jsonString = null;
            while (true)
            {
                try
                {
                    HttpClient client = new HttpClient();
                    
                    //var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
                    var response = await client.GetAsync("http://127.0.0.1:8/qInfo");
                    if (response != null)
                    {
                        jsonString = await response.Content.ReadAsStringAsync();
                        Console.WriteLine("{0}", jsonString);
                        //return JsonConvert.DeserializeObject<object>(jsonString);
                        break;
                    }
                    
                }
                catch (Exception ex)
                {
                    //Console.WriteLine("Exception1" + ex.ToString());
                }
            }
            JObject Jobject = JObject.Parse(jsonString);
            int queuent = (int)Jobject["inputQuCount"];
            JArray array = (JArray)Jobject["inputQeURIs"];
            output = (string)Jobject["outputQeURI"];

            for (int i = 0; i < array.Count; i++)
            {
                Thread myThread = new Thread(reqQueue);
                string uri = array[i].ToString();
                param p = new param();
                p.uri = uri;
                p.index = i;
                myThread.Start(p);
            }
        }

        private static void reqQueue(object _obj)
        {
            try
            {
                param pa = (param)_obj;
                Worker w = new Worker(pa.index);

                while (true)
                {
                    WebClient client = new WebClient();
                    string resp = client.DownloadString(pa.uri);
                    JObject Jobject = JObject.Parse(resp);
                    int timestamp = (int)Jobject["timestamp"];
                    string value = (string)Jobject["value"];

                    Console.WriteLine("{0} {1} {2}", pa.uri, timestamp, value);

                    string resultStr = w.Run(timestamp, value);
                    if (resultStr != null)
                    {
                        Console.WriteLine("######### {0}", resultStr);
                        JObject result = new JObject(
                            new JProperty("result", resultStr)
                            );

                        string re = "{\"result\":\"" + value + "\"}";

                        WebClient webClient = new WebClient();
                        webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                        webClient.Encoding = UTF8Encoding.UTF8;
                        string responseJSON = webClient.UploadString(output, result.ToString());
                    }
                }
            }
            catch (Exception eee) { }
        }

        static void Main(string[] args)
        {
            Task T = new Task(GetCallAPI);
            T.Start();
            Console.ReadLine();
        }
    }
}

#if false
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TEST
{
    class Param
    {
       // public HttpListenerContext ctx { get; set; }

        public string url;
        public string outUrl;
        public Worker w;

    }
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Detail
    {
        [JsonProperty("timestamp ")]
        public int timestamp { get; set; }
        public string value { get; set; }
    }

    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Root
    {
        public int inputQuCount { get; set; }

        [JsonProperty("inputQURIs ")]
        public List<string> inputQURIs { get; set; }

        [JsonProperty("outputQURI ")]
        public string outputQURI { get; set; }
    }
    public class ResultObject
    {
        public string result { get; set; }
    }
    class Program
    {
        
        static void Main(string[] args)
        {

            Task.Run(() => requestURL().Wait());

            while (true) {
                Thread.Sleep(100);
            }
        }

        static async Task requestURL()
        {
            List<string> qInfo = new List<string>();
            List<Worker> w = new List<Worker>();
            try
            {
                string responseString = string.Empty;
                HttpClient httpClient = new HttpClient();
                HttpStatusCode statusCode = HttpStatusCode.OK;

                while (true)
                {
                    try
                    {
                        HttpResponseMessage resp = await httpClient.GetAsync("http://127.0.0.1:8080/qunfo");
                        statusCode = resp.StatusCode;

                        responseString = await resp.Content.ReadAsStringAsync();

                        if (responseString != string.Empty)
                            break;
                    }
                    catch(Exception ex)
                    {

                    }
                    Thread.Sleep(100);
                }
                Console.WriteLine("responseString " + responseString);
                responseString = responseString.Replace("\r\n", "");

                JObject json = JObject.Parse(responseString);
                
                Root root = JsonConvert.DeserializeObject<Root>(responseString);
                root.inputQueueURIs = new List<string>();

                JArray arr = (JArray)json["inputQURIs"];
                root.outputQueueURI = (string)json["outputQURI"];
                for (int i=0; i<root.inputQCount; i++)
                {
                    w.Add(new Worker(i));
                    root.inputQURIs.Add((string)arr[i]);
                    qInfo.Add((string)arr[i]);
                }

                for(int i=0; i<w.Count; i++)
                {
                    Param param = new Param();
                    param.url = qInfo[i];
                    param.outUrl = root.outputQURI;
                    param.w = w[i];

                    Thread myThread = new Thread(requURL);
                    myThread.Start(param);
                }
            }
            catch (WebException ex)
            {

            }
        }

        static void requURL(object _param)
        {
            Task.Run(() => requestURL(_param)).Wait();
        }
        static async Task requestURL(object _param)
        {
            Param param = (Param)_param;

            //HttpClient client = new HttpClient();
            string resStr = string.Empty;
            while (true)
            {
                try
                {
                    Console.WriteLine("Req: "+param.url);

                    WebClient client = new WebClient();
                    resStr = client.DownloadString(param.url);
                    //HttpResponseMessage response = await client.GetAsync();
                    //statusCode = response.StatusCode;

                    //resStr = await response.Content.ReadAsStringAsync();
                    //if (resStr != string.Empty)
                    //    break;

                    Detail d = JsonConvert.DeserializeObject<Detail>(resStr);

                    string resultStr = param.w.Run(d.timestamp, d.value);
                    if (resultStr != null )
                    {
                        //if (!resultStr.Contains("Matched AD2")&& !resultStr.Contains("Matched AD3") && !resultStr.Contains("Matched AD6"))
                        {
                            ResultObject resultObject = new ResultObject
                            {
                                result = resultStr
                            };

                            string jsonString = JsonConvert.SerializeObject(resultObject);

                            /*HttpClient outClient = new HttpClient();
                            HttpContent httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
                            HttpResponseMessage outResp = await outClient.PostAsync(param.outUrl, httpContent);*/
                            WebClient webClient = new WebClient();
                            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                            webClient.Encoding = UTF8Encoding.UTF8;
                            string responseJSON = webClient.UploadString(param.outUrl, jsonString);
                        }
                    }
                    else
                    {
                        Console.WriteLine("empty");
                    }
                }
                catch (Exception ex) { }
            }
        }
    }
}
#endif
반응형