示例#1
0
        //Add session check.
        private bool AlgorithmServerPicker(Message <string, string> message, out ServerChatter serverOut)
        {
            if (Algorithm != null)
            {
                ServerChatter server = Algorithm.GetServerArrayPosition(servers);
                if (server == null)
                {
                    Console.WriteLine("No server found. Are you sure servers are listening?");
                    message.Body = "503: No Server Found.";
                    serverOut    = server;
                    return(false);
                }
                Console.WriteLine("Chosen Server: " + server.Id);
                Console.WriteLine("Connecting to new Server.");

                //server.AddClient(message);
                serverOut = server;
                return(true);
            }
            else
            {
                Console.WriteLine("No algorithm selected.");
                message.Body = "500: Internal Server Error.";
                serverOut    = null;
                return(false);
            }
        }
示例#2
0
        public void AddServer(string ip, int port, Action <ServerChatter> AddServerCallback)
        {
            foreach (ServerChatter sc in servers.Values)
            {
                if (sc.Port == port)
                {
                    Console.WriteLine("Port is already in use.");
                    return;
                }
            }

            ServerChatter server = new ServerChatter(ip, port, SendMessageToClient);

            servers.Add(server.Id, server);
            AddServerCallback(server);

            Console.WriteLine($"Added server with port: {port}");
        }
示例#3
0
 //TODO: Fix this affinity shit to work with sessions or cookies.
 private bool ServerAffinityExists(Message <string, string> message, out ServerChatter server)
 {
     if (Sessions != null)
     {
         if (message.Headers.ContainsKey("Id"))
         {
             if (Sessions.GetServerIdForClient(message, out string serverId))
             {
                 Console.WriteLine("Session exists.");
                 if (servers.TryGetValue(serverId, out ServerChatter sessionServer))
                 {
                     server = sessionServer;
                     return(true);
                 }
                 else
                 {
                     Console.WriteLine("Server doesn't exist where it used to.");
                     server = null;
                     return(true);
                 }
             }
             else
             {
                 if (AlgorithmServerPicker(message, out ServerChatter randomServer))
                 {
                     Console.WriteLine("Adding client to session.");
                     Sessions.AddSession(message, randomServer.Id);
                     server = randomServer;
                     return(true);
                 }
                 else
                 {
                     Console.WriteLine("Couldn't set server affinity.");
                     server = null;
                     return(false);
                 }
             }
         }
     }
     Console.WriteLine("No server affinity set.");
     server = null;
     return(false);
 }