From bdfafa290c8498c1e8663adfc1b7e2bf43b95fc7 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 21 May 2020 21:20:06 -0400 Subject: [PATCH] Basic task lookup --- .github/workflows/auth_test.yml | 4 +++ authorizer/Server.cs | 50 +++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.github/workflows/auth_test.yml b/.github/workflows/auth_test.yml index dcd592b..5410eb9 100644 --- a/.github/workflows/auth_test.yml +++ b/.github/workflows/auth_test.yml @@ -20,6 +20,10 @@ jobs: working-directory: authorizer run: | dotnet add package StackExchange.Redis --version 2.1.39 --source https://www.myget.org/F/stackoverflow/api/v3/index.json + + # AWS SDK + dotnet add package AWSSDK.Core --version 3.5.0-beta + dotnet add package AWSSDK.ECS --version 3.5.0-beta - name: Build working-directory: authorizer run: | diff --git a/authorizer/Server.cs b/authorizer/Server.cs index 39f6883..82e4ab1 100644 --- a/authorizer/Server.cs +++ b/authorizer/Server.cs @@ -30,6 +30,22 @@ class AuthServer server = new TcpListener(address, port); ecs = new AmazonECSClient(); } + + private Amazon.ECS.Model.Task GetTask(string task_arn) + { + // Builds ECS Request + DescribeTasksRequest r = new DescribeTasksRequest(); + List tasks = new List(); + tasks.Add(task_arn); + r.Tasks = tasks; + + //Send Describe Tasks Request + var t = ecs.DescribeTasksAsync(r); + t.RunSynchronously(); + + //Return result + return t.Result.Tasks[0]; + } private void ServerLoop() { while(running) @@ -49,29 +65,33 @@ class AuthServer Byte[] bytes = new byte[256]; String data = null; + long num_of_tasks = redis.conn.ListLength("tasks"); - writer.Write("Hey there bud!"); - writer.Flush(); - Console.WriteLine("HERE"); + if(num_of_tasks > 0) + { + + var task = GetTask(redis.conn.ListGetByIndex("tasks",0)); + int port = task.Containers[0].NetworkBindings[0].HostPort; + string hostname = task.Containers[0].NetworkBindings[0].BindIP; - byte[] sendBytes = System.Text.Encoding.ASCII.GetBytes("GET / HTTP/1.1"); - stream.Write(sendBytes, 0, sendBytes.Length); - client.Client.Send(sendBytes); - - - //BinaryWriter writer = new BinaryWriter(stream); - //writer.Write("TEST"); - - + writer.Write("server:" + hostname + ":" + port.ToString()); + writer.Flush(); + Console.WriteLine("Routed client to " + hostname + ":" + port.ToString()); + } + else + { + string msg = "ERROR: No valid game server found!"; + Console.WriteLine(msg); + writer.Write(msg); + writer.Flush(); + } + //Read any client response int i; - while((i = stream.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Recieved: {0}", data); - - } client.Close();