Esempio n. 1
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. 2
0
 /// <summary>
 /// This method sends the response of the request command
 /// </summary>
 /// <param name="ResponseCommand"></param>
 public void sendResponseCommand(Command ResponseCommand)
 {
     CommandFactory cmdFactory = new CommandFactory();
     //To be sure that cmd is really marked..
     ResponseCommand = cmdFactory.markAllSpecialCharacters(ResponseCommand);
     this.server.Server.Send(Encoding.ASCII.GetBytes(ResponseCommand.toString()));
 }
Esempio n. 3
0
        private Command login(Command cmd)
        {
            CommandFactory cmdFactory = new CommandFactory();

            return(cmdFactory.createCommand(cmd.Id, SERVERNAME, STATUS, "none", "login success", "none"));
        }
Esempio n. 4
0
        private Command thumbnail(Command cmd)
        {
            CommandFactory cmdFactory = new CommandFactory();

            return(cmdFactory.createCommand(cmd.Id, SERVERNAME, SEND_CLIENT_THUMBNAIL, cmd.Filename, "will coming soon", "none"));
        }
Esempio n. 5
0
        public Command createErrorCommand(Command cmd)
        {
            CommandFactory cmdFactory = new CommandFactory();

            return(cmdFactory.createCommand(cmd.Id, SERVERNAME, STATUS, "none", "error", "none"));
        }
Esempio n. 6
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);
            }
        }