diff --git a/authorizer/Program.cs b/authorizer/Program.cs index a0d9c52..195bfcd 100644 --- a/authorizer/Program.cs +++ b/authorizer/Program.cs @@ -6,19 +6,9 @@ namespace authorizer class Program { static AuthServer server; - static Redis redis; static void Main(string[] args) { - redis = new Redis(Environment.GetEnvironmentVariable("REDIS_HOSTNAME")); - - redis.SetTest(); - - Thread.Sleep(100); - - redis.GetTest(); - - - /* server = new AuthServer(); + server = new AuthServer(); server.Start(); @@ -28,7 +18,7 @@ namespace authorizer input = Console.ReadLine(); } while(input != "stop"); - server.Stop(); */ + server.Stop(); } } } diff --git a/authorizer/Redis.cs b/authorizer/Redis.cs index d358c85..8ba1c7e 100644 --- a/authorizer/Redis.cs +++ b/authorizer/Redis.cs @@ -4,7 +4,7 @@ using StackExchange.Redis; class Redis { private ConnectionMultiplexer muxer; - private IDatabase conn; + public IDatabase conn; private string hostname; private int port; public Redis(string host = "127.0.0.1", int p = 6379) @@ -25,18 +25,6 @@ class Redis Console.WriteLine("Connected to redis server!"); } - public void SetTest() - { - string test_val = "Potato"; - conn.StringSet("test_val", test_val); - Console.WriteLine("Set value to: " + test_val); - } - - public void GetTest() - { - Console.WriteLine("Value is: " + conn.StringGet("test_val")); - } - ~Redis() { muxer.Close(); diff --git a/authorizer/Server.cs b/authorizer/Server.cs index feecd77..39f6883 100644 --- a/authorizer/Server.cs +++ b/authorizer/Server.cs @@ -1,44 +1,86 @@ using System; using System.Threading; +using System.Threading.Tasks; +using System.IO; using System.Net; using System.Net.Sockets; + +using Amazon.ECS; +using Amazon.ECS.Model; +using System.Collections.Generic; class AuthServer { - private int port; - private IPAddress address; - private TcpListener server; - private Thread thread; private bool running = false; + private int port; + private Thread thread; + private IPAddress address; + + // Core objects + private TcpListener server; + private Redis redis; + private AmazonECSClient ecs; + + public AuthServer(string addr = "0.0.0.0", int p = 7778) { port = p; address = IPAddress.Parse(addr); + redis = new Redis(Environment.GetEnvironmentVariable("REDIS_HOSTNAME")); server = new TcpListener(address, port); + ecs = new AmazonECSClient(); } private void ServerLoop() { while(running) { - Console.WriteLine("Waiting for a connection..."); - - TcpClient client = server.AcceptTcpClient(); - Console.WriteLine("Connected!"); - - Byte[] bytes = new byte[256]; - String data = null; - - NetworkStream stream = client.GetStream(); - - int i; - - while((i = stream.Read(bytes, 0, bytes.Length)) != 0) + try { - data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); - Console.WriteLine("Recieved: {0}", data); + //Wait for connection + TcpClient client = server.AcceptTcpClient(); + //Get remote address + IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint; + Console.WriteLine(endPoint.Address.ToString() + " connected!"); + + //Create streams + NetworkStream stream = client.GetStream(); + StreamWriter writer = new StreamWriter(stream); + + Byte[] bytes = new byte[256]; + String data = null; + + + writer.Write("Hey there bud!"); + writer.Flush(); + Console.WriteLine("HERE"); + + 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"); + + + + 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(); + Console.WriteLine("Client disconnected"); + } + catch(Exception e) + { + Console.WriteLine("Fatal exception: " + e.ToString()); } - - client.Close(); } } @@ -49,6 +91,7 @@ class AuthServer ThreadStart entrypoint = new ThreadStart(ServerLoop); thread = new Thread(entrypoint); thread.Start(); + Console.WriteLine("Waiting for a connection..."); } public void Stop() { @@ -56,6 +99,7 @@ class AuthServer } public void ForceStop() { - thread.Join(); + running = false; + thread.Abort(); } } \ No newline at end of file diff --git a/authorizer/authorizer.csproj b/authorizer/authorizer.csproj index a814ae8..678dd2b 100644 --- a/authorizer/authorizer.csproj +++ b/authorizer/authorizer.csproj @@ -6,6 +6,8 @@ + + diff --git a/client/project.godot b/client/project.godot index 555412c..ff2196c 100644 --- a/client/project.godot +++ b/client/project.godot @@ -24,6 +24,7 @@ config/icon="res://icon.png" MusicManager="*res://nodes/MusicManager.tscn" NetworkManager="*res://nodes/NetworkManager.tscn" ImportantEntities="*res://scripts/singletons/ImportantEntities.gd" +Authorizer="*res://scripts/network/Authorizer.gd" [input] diff --git a/client/scripts/network/Authorizer.gd b/client/scripts/network/Authorizer.gd new file mode 100644 index 0000000..d93c190 --- /dev/null +++ b/client/scripts/network/Authorizer.gd @@ -0,0 +1,61 @@ +extends Node + +signal auth_connected +signal auth_disconnected + +var client : StreamPeerTCP = null +var server_hostname : String = "127.0.0.1" +var server_port = 7778 + +func _ready(): + client = StreamPeerTCP.new() + client.set_no_delay(true) + set_process(false) + +func auth_connect(host=server_hostname, port=server_port): + + # Connect if not connected + if !client.is_connected_to_host(): + server_hostname = host + server_port = port + + # Connect Socket & Create Stream + client.connect_to_host(server_hostname, port) + + # Start listening + set_process(true) + + # Validate intial connection + if client.is_connected_to_host(): + client.put_string("Hey there daddy!") + emit_signal("auth_connected") + return true + else: + # Timeout implemented in `process` loop + print("Waiting for host connection...") + return false + else: + print("Client is already connected to server!") + return false + +func auth_disconnect(): + client.disconnect_from_host() + set_process(false) # Disable listening loop + print_debug("Disconnected from host.") + emit_signal("auth_disconnected") + +var count = 0 +func _process(delta): + + if client.get_available_bytes() > 0: + print(client.get_available_bytes()) + + print(client.get_string(client.get_available_bytes())) + + + # Await for client connection + if client.get_status()==1: + count= count+delta + if count>1: # if it took more than 1s to connect, error + print_debug("Failed connect, disconnecting...") + auth_disconnect() #interrupts connection to nothing diff --git a/client/scripts/systems/LoginManager.gd b/client/scripts/systems/LoginManager.gd index 49872b3..5d6e50b 100644 --- a/client/scripts/systems/LoginManager.gd +++ b/client/scripts/systems/LoginManager.gd @@ -1,17 +1,31 @@ extends Node +var auth func _ready(): $"/root/MusicManager".play_music("wizards") $Button.connect("button_down", self, "_on_button_press") $"/root/NetworkManager".connect("error_occured", self, "_on_error") $"/root/NetworkManager".connect("logged_in", self, "_on_login") + auth = $"/root/Authorizer" + auth.connect("auth_connected", self, "_on_auth_connection") + auth.connect("auth_disconnected", self, "_on_auth_disconnection") + func _on_error(): $ErrorDialog/ErrorLabel.text = $"/root/NetworkManager".error_info $ErrorDialog.popup_centered() func _on_button_press(): + auth.auth_connect() + +func _on_auth_connection(): + $Button.disabled = true + +func _on_auth_disconnection(): + $Button.disabled = false + +func _on_button_press_OLD(): if($"/root/NetworkManager".connected): $"/root/NetworkManager".disconnect_from_server() else: diff --git a/infrastructure/cloudformation/dt/top.yaml b/infrastructure/cloudformation/dt/top.yaml index ea0d51b..739e1f4 100644 --- a/infrastructure/cloudformation/dt/top.yaml +++ b/infrastructure/cloudformation/dt/top.yaml @@ -101,7 +101,7 @@ Resources: TemplateURL: !Sub 'https://s3.${AWS::Region}.amazonaws.com/sumu-stacks/dt/${release}/cloudformation/dt/cloudwatch.yaml' Parameters: environment: !Ref environment - Cluster: !GetAtt EcsCluster.Outputs.Cluster + Cluster: !GetAtt EcsCluster.Outputs.ClusterArn LambdaArn: !GetAtt LambdaFunctions.Outputs.TaskListManager #--------- diff --git a/infrastructure/lambda/task_queue_manager/lambda_function.py b/infrastructure/lambda/task_queue_manager/lambda_function.py index ddaa514..7be45f6 100644 --- a/infrastructure/lambda/task_queue_manager/lambda_function.py +++ b/infrastructure/lambda/task_queue_manager/lambda_function.py @@ -1,4 +1,15 @@ -import json +import redis +import json, os def lambda_handler(event, context): - print(json.dumps(event)) \ No newline at end of file + r = redis.Redis(host=os.environ['REDIS_HOST'], port=6379, db=0) + + if event["detail"]["group"] == "service:" + os.environ["ECS_SERVICE"]: + desired = event["detail"]["desiredStatus"] + last = event["detail"]["lastStatus"] + if desired == "RUNNING" and desired == last: + print("Added task: " + event["detail"]["taskArn"]) + r.lpush("tasks", event["detail"]["taskArn"]) + elif desired == "STOPPED" or last == "STOPPED": + r.lrem("tasks", event["detail"]["taskArn"], 1) + print("Removed task: " + event["detail"]["taskArn"], 1) \ No newline at end of file diff --git a/infrastructure/lambda/task_queue_manager/requirements.txt b/infrastructure/lambda/task_queue_manager/requirements.txt new file mode 100644 index 0000000..74b362f --- /dev/null +++ b/infrastructure/lambda/task_queue_manager/requirements.txt @@ -0,0 +1 @@ +redis \ No newline at end of file