コード例 #1
0
 /// <summary>
 /// Eliminare un file significa settarlo come non Valido. Un file non valido
 /// resta nella lista dei file dell'utente. Viene eliminato definitivamente
 /// il più vecchio file tra quelli invalidi solo quando l'utente eccede il numero
 /// di file che possiede.
 /// </summary>
 /// <param name="file">Il file da eliminare</param>
 public void eliminaFile(FileUtente file)
 {
     if (file.Valido)
     {
         file.Valido = false;
     }
 }
コード例 #2
0
 /// <summary>
 /// Restituisce un file dell'utente
 /// </summary>
 /// <param name="index">Indica un file tra quelli dell'utente. Non è l'id.</param>
 /// <returns></returns>
 public FileUtente this[int index]
 {
     get
     {
         if (__file_list[index] == null)
         {
             __file_list[index] = new FileUtente(__nome_utente, __list_ids_files[index]);
         }
         return(__file_list[index]);
     }
 }
コード例 #3
0
        public FileUtente nuovoFile(string nome_file, string path_relativo, DateTime t_creazione = new DateTime())
        {
            //Se non c'è spazio, cerco un capro espiatorio da buttare per far posto a quello nuovo,
            //Altrimenti lancio un'eccezione
            string[][] parameters = new string[1][];
            if (this.__list_ids_files.Count >= this.__max_file)
            {
                int id_da_sacrificare = -1;
                parameters[0] = new string[2] {
                    "@nome_utente", __nome_utente
                };
                this.ExecuteQuery(Properties.SQLquery.sqlCercaFileDaDistruggere, parameters);
                foreach (int i in this.GetResults())
                {
                    id_da_sacrificare = Int32.Parse(this.ResultGetValue("id").ToString());
                    break;
                }

                if (id_da_sacrificare >= 0)
                {
                    for (int i = 0; i < this.Length; i++)
                    {
                        if (this[i].Id == id_da_sacrificare)
                        {
                            this[i].Distruggi();
                        }
                    }
                }
                else
                {
                    this.l.log("Non c'è più posto per l'utente " + __nome_utente, Level.INFO);
                    throw new DatabaseException("Non è più possibile inserire nuovi file. Limite superato.", DatabaseErrorCode.LimiteFileSuperato);
                }
            }

            if (t_creazione == DateTime.MinValue)
            {
                t_creazione = DateTime.Now;
            }

            parameters    = new string[4][];
            parameters[0] = new string[2] {
                "@t_creazione", t_creazione.ToString("u")
            };
            parameters[1] = new string[2] {
                "@path_relativo_c", path_relativo
            };
            parameters[2] = new string[2] {
                "@nome_file_c", nome_file
            };
            parameters[3] = new string[2] {
                "@nome_utente", this.__nome_utente
            };
            DB_Table db = new DB_Table();
            Log      l  = Log.getLog();

            db.ExecuteQuery(Properties.SQLquery.sqlNuovoFile, parameters);
            long id = db.getLastInsertedId();

            FileUtente file = new FileUtente(this.__nome_utente, (int)id);

            this.__list_ids_files.Add(file.Id);
            this.__file_list[this.__list_ids_files.Count - 1] = file;
            return(file);
        }
コード例 #4
0
            /// <summary>
            /// Comando per la creazione di un nuovo file sul server. Se l'utente ha troppi file, il più
            /// vecchio tra quelli eliminati viene distrutto. Se non ci sono file eliminati da distruggere
            /// viene generato un errore.
            /// </summary>
            /// <param name="dati">
            ///     [0]: nome_file
            ///     [1]: path_relativo
            ///     [2]: timestamp creazione (in ticks)
            ///     [3]: sha256 del contenuto
            ///     [4]: dimensione
            /// </param>
            /// <returns></returns>
            public override IEnumerable <string> esegui(List <string> dati)
            {
                Log           l  = Log.getLog();
                StringBuilder sb = new StringBuilder();

                if (user == null)
                {
                    yield return(sb.Append(CommandErrorCode.UtenteNonLoggato.ToString("D")).Append(" Utente non loggato.").ToString());

                    yield break;
                }
                if (dati.Count < 7)
                {
                    yield return(sb.Append(CommandErrorCode.DatiIncompleti.ToString("D"))
                                 .Append(" I dati inviati non sono sufficienti").ToString());

                    yield break;
                }

                string   nome_file     = dati[0];
                string   path_relativo = dati[1];
                DateTime timestamp     = new DateTime();
                DateTime t_modifica    = new DateTime();
                int      dim           = -1;

                sb.Clear();
                try
                {
                    timestamp  = new DateTime(Int64.Parse(dati[2]));
                    t_modifica = new DateTime(Int64.Parse(dati[3]));
                    dim        = Int32.Parse(dati[5]);
                }catch (Exception e)
                {
                    sb.Append(CommandErrorCode.FormatoDatiErrato.ToString("D")).Append(" Dimensione o timestamp non corretti");
                    l.log("Utente: " + user.Nome + " " + e.Message, Level.INFO);
                }
                // if exception occurred...
                if (dim == -1 || timestamp == DateTime.MinValue || t_modifica == DateTime.MinValue)
                {
                    yield return(sb.ToString());

                    yield break;
                }
                sb.Clear();
                string     sha256 = dati[4];
                FileUtente nuovo  = null;

                try
                {
                    nuovo = user.FileList.nuovoFile(nome_file, path_relativo, timestamp);
                }
                catch (DatabaseException e)
                {
                    switch (e.ErrorCode)
                    {
                    case DatabaseErrorCode.LimiteFileSuperato:
                        sb.Append(CommandErrorCode.LimiteFileSuperato.ToString("D")).Append(" L'utente ha superato il limite di file creabili.");
                        break;

                    case DatabaseErrorCode.FileEsistente:
                        sb.Append(CommandErrorCode.FileEsistente.ToString("D")).Append(" Un file con quel nome esiste gia'.");
                        break;

                    default:
                        l.log("Server Error!! " + e.Message, Level.ERR);
                        sb.Append(CommandErrorCode.Default.ToString("D")).Append(" Errore del server durante la creazione del file.");
                        break;
                    }
                    l.log("Errore nella creazione del file." + e.Message, Level.ERR);
                    throw;
                }
                if (nuovo == null)
                {
                    yield return(sb.ToString());

                    yield break;
                }
                Snapshot snap;

                sb.Clear();
                try
                {
                    snap = nuovo.Snapshots.Nuovo(t_modifica, dim, sha256);
                }
                catch (Exception e)
                {
                    l.log("Errore... " + e.Message, Level.ERR);
                    sb.Append(CommandErrorCode.Unknown.ToString("D")).Append(" Un errore sconosciuto è accaduto nel server.");
                    snap = null;
                }
                if (snap == null)
                {
                    yield return(sb.ToString());

                    yield break;
                }
                string token = CollegamentoDati.getNewToken();

                yield return(sb.Append(CommandErrorCode.OKIntermedio.ToString("D")).Append(" Stream dati pronto").ToString());

                yield return(token);

                yield return("");

                NetworkStream stream_dati = CollegamentoDati.getCollegamentoDati(token);

                byte[] buffer = new byte[1024];
                int    letti  = 0;

                try
                {
                    do
                    {
                        letti = stream_dati.Read(buffer, 0, 1024);
                        snap.scriviBytes(buffer, letti);
                    } while (letti != 0);
                    snap.completaScrittura();
                }
                catch (Exception e)
                {
                    l.log("Errore nella scrittura del file" + e.Message, Level.ERR);
                    throw;
                }
                yield return(CommandErrorCode.OK.ToString("D") + " Trasferimento completato con successo");
            }
コード例 #5
0
 /// <summary>
 /// Restituisce un file dell'utente
 /// </summary>
 /// <param name="index">Indica un file tra quelli dell'utente. Non è l'id.</param>
 /// <returns></returns>
 public FileUtente this[int index]
 {
     get
     {
         if(__file_list[index] == null)
         {
             __file_list[index] = new FileUtente(__nome_utente, __list_ids_files[index]);
         }
         return __file_list[index];
     }
 }
コード例 #6
0
        public FileUtente nuovoFile(string nome_file, string path_relativo,DateTime t_creazione= new DateTime())
        {
            //Se non c'è spazio, cerco un capro espiatorio da buttare per far posto a quello nuovo,
            //Altrimenti lancio un'eccezione
            string[][] parameters = new string[1][];
            if (this.__list_ids_files.Count >= this.__max_file)
            {
                int id_da_sacrificare=-1;
                parameters[0] = new string[2] { "@nome_utente", __nome_utente};
                this.ExecuteQuery(Properties.SQLquery.sqlCercaFileDaDistruggere, parameters);
                foreach(int i in this.GetResults())
                {
                    id_da_sacrificare = Int32.Parse(this.ResultGetValue("id").ToString());
                    break;
                }

                if (id_da_sacrificare >= 0)
                {
                    for(int i = 0; i < this.Length; i++)
                    {
                        if(this[i].Id == id_da_sacrificare)
                        {
                            this[i].Distruggi();
                        }
                    }
                }
                else
                {
                    this.l.log("Non c'è più posto per l'utente " + __nome_utente, Level.INFO);
                    throw new DatabaseException("Non è più possibile inserire nuovi file. Limite superato.", DatabaseErrorCode.LimiteFileSuperato);
                }
            }

            if(t_creazione == DateTime.MinValue)
            {
                t_creazione = DateTime.Now;
            }

            parameters = new string[4][];
            parameters[0] = new string[2] { "@t_creazione", t_creazione.ToString("u") };
            parameters[1] = new string[2] { "@path_relativo_c", path_relativo };
            parameters[2] = new string[2] { "@nome_file_c", nome_file };
            parameters[3] = new string[2] { "@nome_utente", this.__nome_utente };
            DB_Table db = new DB_Table();
            Log l = Log.getLog();
            db.ExecuteQuery(Properties.SQLquery.sqlNuovoFile, parameters);
            long id = db.getLastInsertedId();

            FileUtente file = new FileUtente(this.__nome_utente, (int)id);
            this.__list_ids_files.Add(file.Id);
            this.__file_list[this.__list_ids_files.Count - 1] = file;
            return file;
        }
コード例 #7
0
 /// <summary>
 /// Eliminare un file significa settarlo come non Valido. Un file non valido
 /// resta nella lista dei file dell'utente. Viene eliminato definitivamente 
 /// il più vecchio file tra quelli invalidi solo quando l'utente eccede il numero
 /// di file che possiede.
 /// </summary>
 /// <param name="file">Il file da eliminare</param>
 public void eliminaFile(FileUtente file)
 {
     if (file.Valido)
     {
         file.Valido = false;
     }
 }