Advertisement
Guest User

Untitled

a guest
Jun 13th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.83 KB | None | 0 0
  1. public class Cloud {
  2.     // attributi funzionali
  3.    
  4.     // buffer circolare contenente valori di temperatura
  5.     private double          bufferTemp[];
  6.     // buffer circolare contenente valori di luminosità
  7.     private double          bufferLum[];
  8.     // puntatori logici di inserimento / rimozione del bufferTemp
  9.     private int             inTemp, outTemp;
  10.     // puntatori logici di inserimento / rimozione del bufferLum
  11.     private int             inLum, outLum;
  12.    
  13.     // attributi di sincronizzazione
  14.    
  15.     // a guardia di : bufferTemp[], inTemp , outTemp
  16.     private ReentrantLock   lckTemp;
  17.     // a guardia di : bufferLum[],  inLum,  outLum
  18.     private ReentrantLock   lckLum;
  19.     // semafori contatore per bloccare gli utenti nei due buffer
  20.     private Semaphore       notEmptyTemp;
  21.     private Semaphore       notEmptyLum;
  22.     // semafori contatore per bloccare i sensori nei due buffer
  23.     private Semaphore       notFullTemp;
  24.     private Semaphore       notFullLum;
  25.    
  26.    
  27.     // costruttore
  28.    
  29.     public Cloud(){
  30.         // inizializzo i due buffer che contengono
  31.         // entrambi 30 elementi
  32.         this.bufferTemp     = new double[30];
  33.         this.bufferLum      = new double[30];
  34.         // inizializzo tutti i puntatori logici a 0
  35.         this.inTemp         = 0;
  36.         this.outTemp        = 0;
  37.         this.inLum          = 0;
  38.         this.outLum         = 0;
  39.        
  40.         this.lckTemp        = new ReentrantLock();
  41.         this.lckLum         = new ReentrantLock();
  42.        
  43.         // inizializzo notEmpty a 0 così da bloccare subito gli utenti
  44.         // nei due buffer, per poi svegliarli in ordine FIFO
  45.         this.notEmptyTemp   = new Semaphore(0, true);
  46.         this.notEmptyLum    = new Semaphore(0, true);
  47.        
  48.         // inizializzo notFull a 30 così i sensori possono iniziare
  49.         // a scrivere nei buffer circolare fin da subito.
  50.         // Verranno svegliati in ordine FIFO
  51.         this.notFullTemp    = new Semaphore(30, true);
  52.         this.notFullLum     = new Semaphore(30, true);
  53.     } // end costruttore
  54.    
  55.     // metodo pubblico invocato da User per ottenere la
  56.     // media di 4 dati nel bufferTemp e consumarli
  57.     public double readAverageTemp(User u){
  58.        
  59.         // array contenente i 4 valori letti da User
  60.         double valoriLetti[] = new double[4];
  61.         // attributo contenente la media dei 4 valori letti
  62.         double media         = 0;
  63.        
  64.         try{
  65.             // acquisisco 4 permessi su notEmptyTemp per assicurarmi che ci
  66.             // siano almeno 4 elementi da leggere nel bufferTemp
  67.             this.notEmptyTemp.acquire(4);  
  68.            
  69.             // ci sono 4 elementi da leggere
  70.            
  71.             // INIZIO SESSIONE CRITICA
  72.            
  73.             this.lckTemp.lock();
  74.             // sono in mutua esclusione : posso gestire
  75.             // bufferTemp e outTemp
  76.            
  77.             // leggo 4 valori di temperatura
  78.             System.out.println("["+u.getName() +"] --> Sto leggendo 4 valori di temperatura nel cloud ... "
  79.                                  + "Leggo i valori nel bufferTemp in posizione : " + this.outTemp + " - "
  80.                                  +((this.outTemp+1)% this.bufferTemp.length)+" - "
  81.                                  +((this.outTemp+2)% this.bufferTemp.length)+" - "
  82.                                  +((this.outTemp+3)% this.bufferTemp.length));
  83.            
  84.             valoriLetti[0] = this.bufferTemp[this.outTemp];
  85.             valoriLetti[1] = this.bufferTemp[(this.outTemp + 1) % this.bufferTemp.length];
  86.             valoriLetti[2] = this.bufferTemp[(this.outTemp + 2) % this.bufferTemp.length];
  87.             valoriLetti[3] = this.bufferTemp[(this.outTemp + 3) % this.bufferTemp.length];
  88.            
  89.             // calcolo la media dei 4 valori letti
  90.             for(int i = 0; i < valoriLetti.length; i++){
  91.                 media += valoriLetti[i];
  92.             }
  93.             media /= 4;
  94.            
  95.             // aggiorno il puntatore logico outTemp
  96.             this.outTemp   = (this.outTemp + 4) % this.bufferTemp.length;
  97.            
  98.             // segnalo ai sensori che ci sono 4 nuovi spazi liberi nel bufferTemp
  99.             this.notFullTemp.release(4);
  100.         } catch(InterruptedException e){
  101.             System.out.println(e);  
  102.         } finally{
  103.             // FINE SEZIONE CRITICA
  104.             this.lckTemp.unlock();
  105.         }
  106.        
  107.         return media;
  108.     } // end metodo readAverageTemp
  109.    
  110.    
  111.     // metodo pubblico invocato da User per ottenere la
  112.     // media di 4 dati nel bufferLum e consumarli
  113.     public double readAverageLight(User u){
  114.        
  115.         // array contenente i 4 valori letti da User
  116.         double valoriLetti[] = new double[4];
  117.         // attributo contenente la media dei 4 valori letti
  118.         double media         = 0;
  119.        
  120.         try{
  121.             // acquisisco 4 permessi su notEmptyLum per assicurarmi che ci
  122.             // siamo almeno 4 elementi da leggere nel bufferLum
  123.             this.notEmptyLum.acquire(4);
  124.            
  125.             // ci sono 4 elementi da leggere
  126.            
  127.             // INIZIO SEZIONE CRITICA
  128.            
  129.             this.lckLum.lock();
  130.            
  131.             // sono in mutua esclusione : posso gestire
  132.             // bufferLum e outLum
  133.            
  134.             // leggo 4 valori di luminosità
  135.             System.out.println("["+u.getName() +"] --> Sto leggendo 4 valori di luminosità nel cloud ... "
  136.                                  + "Leggo i valori nel bufferLum in posizione : " + this.outLum + " - "
  137.                                  +((this.outLum+1)% this.bufferLum.length)+" - "
  138.                                  +((this.outLum+2)% this.bufferLum.length)+" - "
  139.                                  +((this.outLum+3)% this.bufferLum.length));
  140.            
  141.             valoriLetti[0] = this.bufferLum[this.outLum];
  142.             valoriLetti[1] = this.bufferLum[(this.outLum + 1) % this.bufferLum.length];
  143.             valoriLetti[2] = this.bufferLum[(this.outLum + 2) % this.bufferLum.length];
  144.             valoriLetti[3] = this.bufferLum[(this.outLum + 3) % this.bufferLum.length];
  145.            
  146.             // calcolo la media dei 4 valori letti
  147.             for(int i = 0; i < valoriLetti.length; i++){
  148.                 media += valoriLetti[i];
  149.             }
  150.             media /= 4;
  151.            
  152.             // aggiorno il puntatore logico outLum
  153.             this.outLum   = (this.outLum + 4) % this.bufferLum.length;
  154.            
  155.             // segnalo ai sensori che ci sono 4 nuovi spazi liberi nel bufferLum
  156.             this.notFullLum.release(4);
  157.         } catch(InterruptedException e){
  158.             System.out.println(e);
  159.         } finally{
  160.             // FINE SEZIONE CRITICA
  161.             this.lckLum.unlock();
  162.         }
  163.        
  164.         return media;
  165.     } // end metodo readAverageLight
  166.    
  167.     // metodo pubblico invocato dai sensori per
  168.     // inserire i dati nei buffer circolari
  169.     public void writeData(Sensor s, double parametriLetti[]) throws InterruptedException {
  170.        
  171.         // scrivo la temperatura nel buffer delle temperature
  172.         try{
  173.             // acquisisco un permesso su notFullTemp per assicurarmi che
  174.             // ci sia un posto libero nel quale scrivere nel bufferTemp
  175.             this.notFullTemp.acquire();
  176.            
  177.             // c'è un posto libero nel quale scrivere
  178.            
  179.             // INIZIO SEZIONE CRITICA
  180.    
  181.             this.lckTemp.lock();
  182.    
  183.             // sono in mutua esclusione : posso gestire
  184.             // bufferTemp e inTemp
  185.            
  186.             // scrivo la temperatura che ho appena misurato  nel bufferTemp.
  187.             // la temperatura si trova in posizione 0 nell'array parametriLetti
  188.             System.out.println("["+s.getName() +"] --> Sto scrivendo la temperatura letta nel cloud... "
  189.                                     + "Scrivo nel bufferTemp in posizione "+ this.inTemp);
  190.             this.bufferTemp[this.inTemp] = parametriLetti[0];
  191.            
  192.             // aggiorno il puntatore logico inTemp
  193.             this.inTemp                  = (this.inTemp + 1) % this.bufferTemp.length;
  194.      
  195.             // avverto gli utenti che c'è un nuovo elemento nel
  196.             // bufferTemp
  197.             this.notEmptyTemp.release();
  198.            
  199.         } finally{
  200.             // FINE SEZIONE CRITICA
  201.             this.lckTemp.unlock();
  202.         }
  203.        
  204.         // scrivo la luminosità nel buffer delle luminosità
  205.        
  206.         try{
  207.             // acquisisco un permesso su notFullLum per assicurarmi che
  208.             // ci sia un posto libero nel quale scrivere nel bufferLum
  209.             this.notFullLum.acquire();
  210.            
  211.             // c'è un posto libero nel quale scrivere
  212.            
  213.             // INIZIO LA SEZIONE CRITICA
  214.            
  215.             this.lckLum.lock();
  216.            
  217.             // sono in mutua esclusione : posso gestire
  218.             // bufferLum e inLum
  219.            
  220.             // scrivo la luminosità che ho appena misurato  nel bufferLum.
  221.             // la luminosità si trova in posizione 1 nell'array parametriLetti
  222.             System.out.println("["+s.getName() +"] --> Sto scrivendo la luminosità letta nel cloud... "
  223.                                     + "Scrivo nel bufferLum in posizione "+ this.inLum);
  224.             this.bufferLum[this.inLum]   = parametriLetti[1];
  225.            
  226.             // aggiorno il puntatori logico inLum
  227.             this.inLum                   = (this.inLum + 1) % this.bufferLum.length;
  228.            
  229.             // avverto gli utenti che c'è un nuovo elemento nel
  230.             // bufferLum
  231.             this.notEmptyLum.release();
  232.         }  finally {
  233.             // FINE SEZIONE CRITICA
  234.             this.lckLum.unlock();
  235.         }
  236.     } // end metodo writeData
  237.        
  238.        
  239. } // end class Cloud
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement