Exemplo n.º 1
0
        public int clientEntreation(Command requestCommand)
        {
            if (requestCommand.Typ == KEEP_ALIVE_TYP)
            {
                if (this.LastAlive == NOT_BORN)
                {
                    this.LastAlive = GetCurrentUnixTimestampMillis();
                }
                else
                {
                    long currentTime = GetCurrentUnixTimestampMillis();
                    if (Math.Abs(currentTime - this.LastAlive) < LIVING_TIME)
                    {
                        this.LastAlive = currentTime;
                    }
                }
                Logger.print("SURVIVED");
                return 1;

            }
            else
            {
                long currentTime = GetCurrentUnixTimestampMillis();
                if (Math.Abs(currentTime - this.LastAlive) > LIVING_TIME)
                {
                    this.LastAlive = NOT_BORN;
                    this.SmartAuthenticator.Login = false;
                }
            }
            return 0;
        }
Exemplo n.º 2
0
 public static Command createCommand(int id, string username, int typ, string filename, string parameter, byte[] data)
 {
     Command cmd = new Command();
     cmd.Id = id;
     cmd.Username = username;
     cmd.Typ = typ;
     cmd.Filename = filename;
     cmd.Parameter = parameter;
     cmd.Data = data;
     cmd = markAllSpecialCharacters(cmd);
     return cmd;
 }
Exemplo n.º 3
0
        private static Command extractHeader(string header)
        {
            char[] splitsCharacters = new char[] { ';', '{', '}' };
            Command nCommand = new Command();
            String[] commandParts = header.Split(splitsCharacters);
            if (commandParts == null)
            {
                return null;
            }

            for (int i = 0; i < commandParts.Length; i++)
            {
                try
                {
                    switch (i)
                    {
                        case 0:
                            //ID
                            nCommand.Id = Int32.Parse(commandParts[i + 1]);
                            break;
                        case 1:
                            //USERNAME
                            nCommand.Username = commandParts[i + 1];
                            nCommand.Username = unmarkSpecialCharacters(nCommand.Username);
                            break;
                        case 2:
                            //TYP
                            nCommand.Typ = Int32.Parse(commandParts[i + 1]);
                            break;
                        case 3:
                            //FILENAME
                            nCommand.Filename = commandParts[i + 1];
                            nCommand.Filename = unmarkSpecialCharacters(nCommand.Filename);
                            break;
                        case 4:
                            //PARAMETER
                            nCommand.Parameter = commandParts[i + 1];
                            nCommand.Parameter = unmarkSpecialCharacters(nCommand.Parameter);
                            break;

                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }

            return nCommand;
        }
Exemplo n.º 4
0
        public Command execute(Command cmd)
        {
            Command responseCommand;
            switch (cmd.Typ)
            {
                case GET_DATA_FROM_SERVER:
                    Logger.getDataFromServer();
                    responseCommand =  getDataFromServer(cmd);
                    break;
                case SAVE_DATA_ON_SERVER:
                    responseCommand = saveDataOnServer(cmd);
                    break;
                case DELETE_FILE_FROM_SERVER:
                    Logger.deleteFileFromServer(cmd);
                    responseCommand = deleteFileFromServer(cmd);
                    break;
                case GET_AVAIBLE_FILES:
                    Logger.getAvaibleFiles();
                    responseCommand = getAvaibleFiles(cmd);
                    break;
                case CLIENT_LOGOUT:
                    Logger.userLoggedOut();
                    return logout();
                case CLIENT_WANT_THUMBNAIL:
                    Logger.clientWantThumbnail(cmd);
                    responseCommand = thumbnail(cmd);
                    break;
                default:
                    Logger.undefinedError();
                    return createErrorCommand();

            }
            responseCommand.Id = SmartAuthenticator.generateNewId();
            Logger.generatedNewId(responseCommand.Id);
            return responseCommand;
        }
Exemplo n.º 5
0
 public bool kill(Command requestCommand)
 {
     if(requestCommand.Id==NOT_BORN)
     {
         return false;
     }
     long currentTime = GetCurrentUnixTimestampMillis();
     if (Math.Abs(currentTime - this.LastAlive) > LIVING_TIME)
     {
         this.LastAlive = NOT_BORN;
         return true;
     }
     return false;
 }
Exemplo n.º 6
0
 private Command thumbnail(Command cmd)
 {
     CommandFactory cmdFactory = new CommandFactory();
     Security SmartSecurity = new Security();
     XmlManager xmlManager = new XmlManager();
     List<string> allRootPaths = xmlManager.getAllChilds();
     //TODO: IS IT A REAL IMAGE????
     Image image = Image.FromFile(cmd.Filename);
     Image thumb = image.GetThumbnailImage(120, 120, () => false, IntPtr.Zero);
     ImageConverter converter = new ImageConverter();
     byte[] imgArray = (byte[])converter.ConvertTo(thumb, typeof(byte[]));
     return CommandFactory.createCommand(this.SmartAuthenticator.Id, SERVERNAME, SEND_CLIENT_THUMBNAIL, cmd.Filename, "none", imgArray);
 }
Exemplo n.º 7
0
 private Command saveDataOnServer(Command cmd)
 {
     XmlManager xmlManager = new XmlManager();
     List<string> allRootPaths = xmlManager.getAllChilds();
     string category = cmd.Parameter;
     string filename = cmd.Filename;
     FileManager MyFileManager = new FileManager();
     CommandFactory cmdFactory = new CommandFactory();
     //TODO: IS PATH FOR SAVING DATA ALLOWED? (NOT SOOO IMPORTANT)
     MyFileManager.saveFile(filename, cmd.Data);
     Logger.saveDataOnServer(cmd);
     return CommandFactory.createCommand(cmd.Id, SERVERNAME, STATUS, filename, "saved file", new byte[1]);
 }
Exemplo n.º 8
0
 private Command getDataFromServer(Command cmd)
 {
     XmlManager xmlManager = new XmlManager();
     Security SmartSecurity = new Security();
     string category = cmd.Parameter;
     string filename = cmd.Filename;
     //TODO: IS FILE PATH ALLOWED?
     FileManager MyFileManager = new FileManager();
     byte[] data = MyFileManager.loadFile(category, filename);
     Logger.getDataFromServer();
     return CommandFactory.createCommand(cmd.Id, SERVERNAME, SEND_DATA_TO_CLIENT, filename, category, data);
 }
Exemplo n.º 9
0
 private Command getAvaibleFiles(Command cmd)
 {
     XmlManager xmlManager = new XmlManager();
     FileManager fileManager = new FileManager();
     List<string> allFilePaths = fileManager.listAllFilesInCategoryFolders(xmlManager.getAllChilds());
     Logger.print(allFilePaths.Count+"");
     //allFilePaths = sortFilesByCreationTime(allFilePaths);
     return CommandFactory.createCommand(cmd.Id, SERVERNAME, SEND_AVAIBLE_FILES, "none", concatFiles(allFilePaths), new byte[1]);
 }
Exemplo n.º 10
0
 private Command deleteFileFromServer(Command cmd)
 {
     Security SmartSecurity = new Security();
     XmlManager xmlManager = new XmlManager();
     List<string> allRootPaths = xmlManager.getAllChilds();
     CommandFactory cmdFactory = new CommandFactory();
     //TODO: IS FILE PATH ALLOWED?
     //if (SmartSecurity.PathIsAllowed(allRootPaths,cmd.Filename))
     //{
     FileManager fileManager = new FileManager();
     fileManager.deleteFile(cmd.Filename);
     return CommandFactory.createCommand(SmartAuthenticator.Id,SERVERNAME,STATUS,cmd.Filename,"deleted file",new byte[1]);
     //}
     //return CommandFactory.createCommand(SmartAuthenticator.Id, SERVERNAME, STATUS, cmd.Filename, "not deleted file", new byte[1]);
 }
Exemplo n.º 11
0
 private static Command markAllSpecialCharacters(Command cmd)
 {
     cmd.Username = markSpecialCharacters(cmd.Username);
     cmd.Filename = markSpecialCharacters(cmd.Filename);
     cmd.Parameter = markSpecialCharacters(cmd.Parameter);
     return cmd;
 }
Exemplo n.º 12
0
 internal void send(Command responseCommand, Socket currentClient)
 {
     SenderAssistant senderAssistent = new SenderAssistant();
     senderAssistent.send(Crypto.Encrypt(responseCommand.toByteArr(),SmartTransferServer.SERVER_PW), currentClient);
     currentClient.Close();
 }