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;
//static List<Worker> wo = null;
//static readonly HttpClient httpClient = new HttpClient();
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:8080/queueInfo");
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["inputQueueCount"];
JArray array = (JArray)Jobject["inputQueueURIs"];
output = (string)Jobject["outputQueueURI"];
for (int i = 0; i < array.Count; i++)
{
//Worker www = new Worker(i);
//wo.Add(www);
Thread myThread = new Thread(reqQueue);
string uri = array[i].ToString();
param p = new param();
p.uri = uri;
p.index = i;
//p.value = ;
myThread.Start(p);
/*Worker w = new Worker(i);
while (true)
{
string uri = array[i].ToString();
try
{
//Console.WriteLine("{0}", url);
HttpClient httpClient = new HttpClient();
var res = await httpClient.GetAsync(uri);
if (response != null)
{
var jsonStr = await response.Content.ReadAsStringAsync();
JObject Jobje = JObject.Parse(jsonString);
int timestamp = (int)Jobject["timestamp"];
string value = (string)Jobject["value"];
Console.WriteLine("{0} {1} {2}", uri, timestamp, value);
string resultStr = w.Run(timestamp, value);
if (resultStr != null)
{
JObject result = new JObject(
new JProperty("result", value)
);
// var parameters = new Dictionary<string, string>();
//parameters.Add("result", value);
//var encodedContent = new FormUrlEncodedContent(result);
HttpClient httpPut = new HttpClient();
//httpPut.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string re = "{\"result\":\"" + value + "\"}";
//var payload = JsonSerializer.Serialize(re);
var content = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
await httpPut.PostAsync(output, content);
}
}
else
{
//Console.WriteLine($" -- response.ReasonPhrase ==> {response.ReasonPhrase}");
}
}
catch (Exception ex2)
{
Console.WriteLine("Exception2" + ex2.ToString());
}
}*/
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception1"+ ex.ToString());
}
//return null;
}
static async void Testttt(object p)
{
Console.WriteLine("Testttt");
param pa = (param)p;
Worker w = new Worker(pa.index);
Console.WriteLine("Test"+pa.index.ToString());
while (true)
{
try
{
//Console.WriteLine("{0}", url);
HttpClient httpClient = new HttpClient();
var response = await httpClient.GetAsync(pa.uri);
if (response != null)
{
var jsonString = await response.Content.ReadAsStringAsync();
JObject Jobject = JObject.Parse(jsonString);
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)
{
JObject result = new JObject(
new JProperty("result", value)
);
// var parameters = new Dictionary<string, string>();
//parameters.Add("result", value);
//var encodedContent = new FormUrlEncodedContent(result);
HttpClient httpPut = new HttpClient();
//httpPut.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string re = "{\"result\":\"" + value + "\"}";
//var payload = JsonSerializer.Serialize(re);
var content = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
await httpPut.PostAsync(output, content);
}
}
else
{
//Console.WriteLine($" -- response.ReasonPhrase ==> {response.ReasonPhrase}");
}
}
catch (Exception ex2)
{
Console.WriteLine("Exception2" + ex2.ToString());
}
}
}
private static void reqQueue(object _obj)
{
param pa = (param)_obj;
Worker w = new Worker(pa.index);
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)
{
JObject result = new JObject(
new JProperty("result", value)
);
// var parameters = new Dictionary<string, string>();
//parameters.Add("result", value);
//var encodedContent = new FormUrlEncodedContent(result);
HttpClient httpPut = new HttpClient();
//httpPut.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string re = "{\"result\":\"" + value + "\"}";
//var payload = JsonSerializer.Serialize(re);
var content = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
// await httpPut.PostAsync(output, content);
}
}
private static void requestQueue(object _obj)
{
//HttpClient client = new HttpClient();
//client.GetAsync((string)obj);
Console.WriteLine("requestQueue");
try
{
//Testttt(_obj);
Task tt = new Task(Testttt, _obj);
Console.WriteLine("requestQueue1");
tt.Start();
Console.WriteLine("requestQueue2");
}
catch(Exception e)
{
Console.WriteLine("Exception3" + e.ToString());
}
//Task.Factory.StartNew(Test, obj);
//Test(obj).GetAwaiter().GetResult();
}
static void Main(string[] args)
{
//wo = new List<Worker>();
Task T = new Task(GetCallAPI);
T.Start();
Console.ReadLine();
/*
Worker worker0 = new Worker(0);
Worker worker1 = new Worker(1);
long timestamp = 0;
string num = null;
string val = null;
string line = null;
string result = null;
while (true)
{
line = Console.ReadLine();
string[] tok = line.Split(' ');
timestamp = long.Parse(tok[0]);
num = tok[1];
val = tok[2];
if (num == "0")
{
result = worker0.Run(timestamp, val);
}
else if (num == "1")
{
result = worker1.Run(timestamp, val);
}
if (result != null) Console.WriteLine("{0}", result);
//.RemoveExpiredStoreItems(long timestamp, store)
}*/
}
}
}
카테고리 없음
task
반응형