Beispiel #1
0
        public void start()
        {
            try
            {
                LogItem = new LogItem(Socket.RemoteEndPoint.ToString());

                //make a byte array and receive data from the client
                Byte[] bReceive = new Byte[1024];
                int    i        = Socket.Receive(bReceive, bReceive.Length, SocketFlags.None);

                //Remove \0 bytes
                List <byte> received = new List <byte>(bReceive);

                received.RemoveAll((byte b) => { return(b == '\0'); });


                //Convert Byte to String
                string sBuffer = Encoding.ASCII.GetString(received.ToArray());

                string[] sBufferArray = sBuffer.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

                string[] request = sBufferArray[0].Split(' ');

                for (int j = 1; j < sBufferArray.Length; j++)
                {
                    if (!(request[0] == "POST" && sBufferArray.Length - 1 == j))
                    {
                        string[] parts = sBufferArray[j].Split(new string[] { ": " }, 2, StringSplitOptions.None);
                        Headers.Add(parts[0], parts[1]);
                    }
                }
                LogItem.Url = Socket.LocalEndPoint + request[1];

                Session = ServerInstance.findSession(this, out newSession);


                switch (request[0])
                {
                case "POST":
                    POST(sBufferArray);
                    break;

                case "GET":
                    GET(sBufferArray);
                    break;

                default:
                    sendError(400, "Bad Request");
                    break;
                }
            }
            catch
            { }
            finally
            {
                close();
            }
        }
Beispiel #2
0
        private static void Main()
        {
            Console.Title = "Server";

            var instance = ServerInstance.Initialize().GetAwaiter().GetResult();

            Console.WriteLine("Start workinkg server . . . ");
            Console.WriteLine("Waiting messages . . . ");

            var projectionContext = new ProjectionProvider();

            var customerProducer = new CustomerProducer(projectionContext, instance);

            while (true)
            {
                var line = Console.ReadLine();

                if (line == null)
                {
                    continue;
                }

                if (line == "exit")
                {
                    break;
                }

                if (line.Contains("check-out"))
                {
                    var commandItems = line.Split(' ');

                    var orderId = Guid.Parse(commandItems[1]);

                    instance.Send(new CheckOutOrderCommand
                    {
                        OrderId = orderId
                    }).GetAwaiter().GetResult();
                }
                else if (line == "order-processor-restore")
                {
                    instance.Send(new RestoreOrderProcessorCommand()).GetAwaiter().GetResult();
                }
                else if (line == "propagate-customers")
                {
                    customerProducer.PropagateCustomersFromStoreAsync().GetAwaiter().GetResult();
                }
            }

            instance.Stop().GetAwaiter().GetResult();
        }
Beispiel #3
0
        static async Task MainAsync()
        {
            Console.Title = "Legends of Equestria Server";
            ServerInstance instance = new ServerInstance();

            if (await instance.Startup())
            {
                while (instance.IsRunning)
                {
                    instance.DoCommand(Console.ReadLine());
                }
                instance.Stop();
                Thread.Sleep(500);
            }
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            if (args is null || args.Length <= 0)
            {
                System.Console.WriteLine(@"""
					Error: please give the correct amount of parameters.
					Server.exe <server_id> <server_url> <max_faults> <min_delay> <max_delay>"""
                                         );
                return;
            }
            // id, url, fults, delaymin, delaymax
            var serverId  = args[0];
            var serverUrl = args[1];
            var maxFaults = int.Parse(args[2]);
            var minDelay  = int.Parse(args[3]);
            var maxDelay  = int.Parse(args[4]);

            string[] splitUrl    = Regex.Split(serverUrl, "[:/]");
            string   serviceName = splitUrl[splitUrl.Length - 1];
            int      port        = int.Parse(splitUrl[splitUrl.Length - 2]);

            // ----------------------------------


            Console.WriteLine("Registering remote object, Port|" + port + "| Remote object name|" + serviceName);
            TcpChannel channel = new TcpChannel(port);

            ChannelServices.RegisterChannel(channel, true);

            ServerInstance server = new ServerInstance(serverId, minDelay, maxDelay, maxFaults);

            RemotingServices.Marshal(server, serviceName, typeof(ServerInstance));

            //RemotingConfiguration.RegisterWellKnownServiceType(
            //	typeof(ServerInstance),
            //	serviceName,
            //	WellKnownObjectMode.Singleton);

            // ----------------------------------

            System.Console.WriteLine("Server started successfully.");
            System.Console.WriteLine("Press <Enter> to exit...");
            System.Console.ReadLine();
        }
Beispiel #5
0
        public Dictionary <string, string> getAgregatedClientsSubset()
        {
            Dictionary <string, string> result = getMyClientsSubset();

            foreach (string serverURL in otherServers.Values)
            {
                ServerInstance s = (ServerInstance)Activator.GetObject(
                    typeof(ServerInstance),
                    serverURL
                    );
                var clientDict = s.getMyClientsSubset();
                foreach (var clientKV in clientDict)
                {
                    result.Add(clientKV.Key, clientKV.Value);
                }
            }

            return(result);
        }
Beispiel #6
0
        private bool RB_Broadcast(Executable executable, bool starter)
        {
            int replies = 0;

            //Broadcast to everyone
            foreach (string serverURL in otherServers.Values)
            {
                Thread thread = new Thread(() => {
                    try{
                        ServerInstance s = (ServerInstance)Activator.GetObject(
                            typeof(ServerInstance),
                            serverURL
                            );
                        Object output = s.RB_Deliver(executable);
                        replies++;
                        System.Console.WriteLine("Replies: " + replies);
                    }
                    catch (Exception e)
                    {
                    }
                });
                thread.Start();
            }
            if (starter && !(leader == serverId))
            {
                long   unixTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                double timer    = 0;
                while (replies < maxFaults)
                {
                    if (timer > 2)
                    {
                        return(false);
                    }
                    System.Threading.Thread.Sleep(50);
                    timer += 0.05;
                }
                System.Console.WriteLine("Waited for: " + (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - unixTime) + " milliseconds.");
            }
            return(true);
        }
Beispiel #7
0
        private bool LE_Broadcast(Executable executable, int to_sn)
        {
            alive.Add(remoteSLE(executable));
            //Broadcast to everyone
            int replies = 0;

            foreach (string serverURL in otherServers.Values)
            {
                Thread thread = new Thread(() =>
                {
                    try
                    {
                        ServerInstance s = (ServerInstance)Activator.GetObject(
                            typeof(ServerInstance),
                            serverURL
                            );
                        alive.Add(s.remoteSLE(executable));
                        replies++;
                    } catch (Exception e)
                    {
                    }
                });
                thread.Start();
            }
            double timer = 0;

            while (replies < maxFaults)
            {
                if (timer >= 2)
                {
                    return(false);
                }
                System.Threading.Thread.Sleep(50);
                timer += 0.05;
            }
            return(true);
        }
Beispiel #8
0
        private void TO_Broadcast(Executable executable, int to_sn)
        {
            this.Change_TO_SN(executable, to_sn);

            //Broadcast to everyone
            foreach (string serverURL in otherServers.Values)
            {
                Thread thread = new Thread(() =>
                {
                    try
                    {
                        ServerInstance s = (ServerInstance)Activator.GetObject(
                            typeof(ServerInstance),
                            serverURL
                            );
                        s.Change_TO_SN(executable, to_sn);
                    }
                    catch (Exception e)
                    {
                    }
                });
                thread.Start();
            }
        }
Beispiel #9
0
 private bool stopLeaderElection()
 {
     stopLE(alive[0]);
     //Broadcast to everyone
     foreach (string serverURL in otherServers.Values)
     {
         Thread thread = new Thread(() =>
         {
             try
             {
                 ServerInstance s = (ServerInstance)Activator.GetObject(
                     typeof(ServerInstance),
                     serverURL
                     );
                 s.stopLE(alive[0]);
             }
             catch (Exception e)
             {
             }
         });
         thread.Start();
     }
     return(true);
 }