Esempio n. 1
0
 public Executor(Guardian guardian)
 {
     this.guardian = guardian;
 }
Esempio n. 2
0
 /// <summary>
 /// Use this method to run the server
 /// </summary>
 public void run()
 {
     //This Factory creates commands
     CommandFactory MyCommandFactory = new CommandFactory();
     //This Guardian protects the current user
     Guardian MyGuardian = new Guardian();
     //This Executor executes commands
     Executor MyExecutor = new Executor(MyGuardian);
     while (true)
     {
         //This Command will be the response command
         Command ResponseCommand;
         //Wait for a client
         TcpClient client = server.AcceptTcpClient();
         //Get RequestCommand as String
         String RequestCommandStr = getRequestCommandStr(client);
         //Extract RequestCommandStr
         Command CurrentCommand = MyCommandFactory.extractCommandFromStr(RequestCommandStr);
         //Wrong command?
         if(CurrentCommand == null)
         {
             continue;
         }
         //Is no user under protection/login?
         if (!MyGuardian.isGuarding() && CurrentCommand.Typ ==9)
         {
             //If Password correct.. Login
             if (CurrentCommand.Parameter == MyGuardian.ServerPassword)
             {
                 CurrentCommand.Id = MyGuardian.generateGuardingId();
                 //Activate keep alive
                 MyGuardian.keepClientAlive();
             }
             else
             {
                 //TODO: Wrong PASSWORD!!
             }
         }
         //No user is logged in, but no incoming login
         else if(!MyGuardian.isGuarding() && CurrentCommand.Typ != 9)
         {
             //Continue if no login
             continue;
         }
         //Is this the protected user & all alright?
         if(MyGuardian.getGuardingId()==CurrentCommand.Id && MyGuardian.isClientAlive())
         {
             ResponseCommand = MyExecutor.execute(CurrentCommand);
             //Generate new ID for security reasons
             ResponseCommand.Id = generateNewId(CurrentCommand.Id);
             //Let the new ID know by the guardian
             MyGuardian.setGuardingId(ResponseCommand.Id);
         }
         //Client is dead and sent command to late
         else if(MyGuardian.getGuardingId() == CurrentCommand.Id && !MyGuardian.isClientAlive())
         {
             CurrentCommand.Typ = 8; //Simulate User logout
             //Kill Client..
             ResponseCommand = MyExecutor.execute(CurrentCommand);
         }
         //Other user is connecting while main user is dead
         else if(!MyGuardian.isClientAlive())
         {
             //Reset rest
             MyGuardian.resetProtection();
             //Generate new command
             CurrentCommand.Id = MyGuardian.generateGuardingId();
             //Activate keep alive
             MyGuardian.keepClientAlive();
             //Create Response with login successed
             ResponseCommand = MyExecutor.execute(CurrentCommand);
         }
         else
         {
             //No access, because other user is using the server
             ResponseCommand = MyExecutor.createErrorCommand(CurrentCommand);
         }
         //Send response
         sendResponseCommand(ResponseCommand);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Use this method to run the server
        /// </summary>
        public void run()
        {
            //This Factory creates commands
            CommandFactory MyCommandFactory = new CommandFactory();
            //This Guardian protects the current user
            Guardian MyGuardian = new Guardian();
            //This Executor executes commands
            Executor MyExecutor = new Executor(MyGuardian);

            while (true)
            {
                //This Command will be the response command
                Command ResponseCommand;
                //Wait for a client
                TcpClient client = server.AcceptTcpClient();
                //Get RequestCommand as String
                String RequestCommandStr = getRequestCommandStr(client);
                //Extract RequestCommandStr
                Command CurrentCommand = MyCommandFactory.extractCommandFromStr(RequestCommandStr);
                //Wrong command?
                if (CurrentCommand == null)
                {
                    continue;
                }
                //Is no user under protection/login?
                if (!MyGuardian.isGuarding() && CurrentCommand.Typ == 9)
                {
                    //If Password correct.. Login
                    if (CurrentCommand.Parameter == MyGuardian.ServerPassword)
                    {
                        CurrentCommand.Id = MyGuardian.generateGuardingId();
                        //Activate keep alive
                        MyGuardian.keepClientAlive();
                    }
                    else
                    {
                        //TODO: Wrong PASSWORD!!
                    }
                }
                //No user is logged in, but no incoming login
                else if (!MyGuardian.isGuarding() && CurrentCommand.Typ != 9)
                {
                    //Continue if no login
                    continue;
                }
                //Is this the protected user & all alright?
                if (MyGuardian.getGuardingId() == CurrentCommand.Id && MyGuardian.isClientAlive())
                {
                    ResponseCommand = MyExecutor.execute(CurrentCommand);
                    //Generate new ID for security reasons
                    ResponseCommand.Id = generateNewId(CurrentCommand.Id);
                    //Let the new ID know by the guardian
                    MyGuardian.setGuardingId(ResponseCommand.Id);
                }
                //Client is dead and sent command to late
                else if (MyGuardian.getGuardingId() == CurrentCommand.Id && !MyGuardian.isClientAlive())
                {
                    CurrentCommand.Typ = 8; //Simulate User logout
                    //Kill Client..
                    ResponseCommand = MyExecutor.execute(CurrentCommand);
                }
                //Other user is connecting while main user is dead
                else if (!MyGuardian.isClientAlive())
                {
                    //Reset rest
                    MyGuardian.resetProtection();
                    //Generate new command
                    CurrentCommand.Id = MyGuardian.generateGuardingId();
                    //Activate keep alive
                    MyGuardian.keepClientAlive();
                    //Create Response with login successed
                    ResponseCommand = MyExecutor.execute(CurrentCommand);
                }
                else
                {
                    //No access, because other user is using the server
                    ResponseCommand = MyExecutor.createErrorCommand(CurrentCommand);
                }
                //Send response
                sendResponseCommand(ResponseCommand);
            }
        }