Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Cloud {
- // attributi funzionali
- // buffer circolare contenente valori di temperatura
- private double bufferTemp[];
- // buffer circolare contenente valori di luminosità
- private double bufferLum[];
- // puntatori logici di inserimento / rimozione del bufferTemp
- private int inTemp, outTemp;
- // puntatori logici di inserimento / rimozione del bufferLum
- private int inLum, outLum;
- // attributi di sincronizzazione
- // a guardia di : bufferTemp[], inTemp , outTemp
- private ReentrantLock lckTemp;
- // a guardia di : bufferLum[], inLum, outLum
- private ReentrantLock lckLum;
- // semafori contatore per bloccare gli utenti nei due buffer
- private Semaphore notEmptyTemp;
- private Semaphore notEmptyLum;
- // semafori contatore per bloccare i sensori nei due buffer
- private Semaphore notFullTemp;
- private Semaphore notFullLum;
- // costruttore
- public Cloud(){
- // inizializzo i due buffer che contengono
- // entrambi 30 elementi
- this.bufferTemp = new double[30];
- this.bufferLum = new double[30];
- // inizializzo tutti i puntatori logici a 0
- this.inTemp = 0;
- this.outTemp = 0;
- this.inLum = 0;
- this.outLum = 0;
- this.lckTemp = new ReentrantLock();
- this.lckLum = new ReentrantLock();
- // inizializzo notEmpty a 0 così da bloccare subito gli utenti
- // nei due buffer, per poi svegliarli in ordine FIFO
- this.notEmptyTemp = new Semaphore(0, true);
- this.notEmptyLum = new Semaphore(0, true);
- // inizializzo notFull a 30 così i sensori possono iniziare
- // a scrivere nei buffer circolare fin da subito.
- // Verranno svegliati in ordine FIFO
- this.notFullTemp = new Semaphore(30, true);
- this.notFullLum = new Semaphore(30, true);
- } // end costruttore
- // metodo pubblico invocato da User per ottenere la
- // media di 4 dati nel bufferTemp e consumarli
- public double readAverageTemp(User u){
- // array contenente i 4 valori letti da User
- double valoriLetti[] = new double[4];
- // attributo contenente la media dei 4 valori letti
- double media = 0;
- try{
- // acquisisco 4 permessi su notEmptyTemp per assicurarmi che ci
- // siano almeno 4 elementi da leggere nel bufferTemp
- this.notEmptyTemp.acquire(4);
- // ci sono 4 elementi da leggere
- // INIZIO SESSIONE CRITICA
- this.lckTemp.lock();
- // sono in mutua esclusione : posso gestire
- // bufferTemp e outTemp
- // leggo 4 valori di temperatura
- System.out.println("["+u.getName() +"] --> Sto leggendo 4 valori di temperatura nel cloud ... "
- + "Leggo i valori nel bufferTemp in posizione : " + this.outTemp + " - "
- +((this.outTemp+1)% this.bufferTemp.length)+" - "
- +((this.outTemp+2)% this.bufferTemp.length)+" - "
- +((this.outTemp+3)% this.bufferTemp.length));
- valoriLetti[0] = this.bufferTemp[this.outTemp];
- valoriLetti[1] = this.bufferTemp[(this.outTemp + 1) % this.bufferTemp.length];
- valoriLetti[2] = this.bufferTemp[(this.outTemp + 2) % this.bufferTemp.length];
- valoriLetti[3] = this.bufferTemp[(this.outTemp + 3) % this.bufferTemp.length];
- // calcolo la media dei 4 valori letti
- for(int i = 0; i < valoriLetti.length; i++){
- media += valoriLetti[i];
- }
- media /= 4;
- // aggiorno il puntatore logico outTemp
- this.outTemp = (this.outTemp + 4) % this.bufferTemp.length;
- // segnalo ai sensori che ci sono 4 nuovi spazi liberi nel bufferTemp
- this.notFullTemp.release(4);
- } catch(InterruptedException e){
- System.out.println(e);
- } finally{
- // FINE SEZIONE CRITICA
- this.lckTemp.unlock();
- }
- return media;
- } // end metodo readAverageTemp
- // metodo pubblico invocato da User per ottenere la
- // media di 4 dati nel bufferLum e consumarli
- public double readAverageLight(User u){
- // array contenente i 4 valori letti da User
- double valoriLetti[] = new double[4];
- // attributo contenente la media dei 4 valori letti
- double media = 0;
- try{
- // acquisisco 4 permessi su notEmptyLum per assicurarmi che ci
- // siamo almeno 4 elementi da leggere nel bufferLum
- this.notEmptyLum.acquire(4);
- // ci sono 4 elementi da leggere
- // INIZIO SEZIONE CRITICA
- this.lckLum.lock();
- // sono in mutua esclusione : posso gestire
- // bufferLum e outLum
- // leggo 4 valori di luminosità
- System.out.println("["+u.getName() +"] --> Sto leggendo 4 valori di luminosità nel cloud ... "
- + "Leggo i valori nel bufferLum in posizione : " + this.outLum + " - "
- +((this.outLum+1)% this.bufferLum.length)+" - "
- +((this.outLum+2)% this.bufferLum.length)+" - "
- +((this.outLum+3)% this.bufferLum.length));
- valoriLetti[0] = this.bufferLum[this.outLum];
- valoriLetti[1] = this.bufferLum[(this.outLum + 1) % this.bufferLum.length];
- valoriLetti[2] = this.bufferLum[(this.outLum + 2) % this.bufferLum.length];
- valoriLetti[3] = this.bufferLum[(this.outLum + 3) % this.bufferLum.length];
- // calcolo la media dei 4 valori letti
- for(int i = 0; i < valoriLetti.length; i++){
- media += valoriLetti[i];
- }
- media /= 4;
- // aggiorno il puntatore logico outLum
- this.outLum = (this.outLum + 4) % this.bufferLum.length;
- // segnalo ai sensori che ci sono 4 nuovi spazi liberi nel bufferLum
- this.notFullLum.release(4);
- } catch(InterruptedException e){
- System.out.println(e);
- } finally{
- // FINE SEZIONE CRITICA
- this.lckLum.unlock();
- }
- return media;
- } // end metodo readAverageLight
- // metodo pubblico invocato dai sensori per
- // inserire i dati nei buffer circolari
- public void writeData(Sensor s, double parametriLetti[]) throws InterruptedException {
- // scrivo la temperatura nel buffer delle temperature
- try{
- // acquisisco un permesso su notFullTemp per assicurarmi che
- // ci sia un posto libero nel quale scrivere nel bufferTemp
- this.notFullTemp.acquire();
- // c'è un posto libero nel quale scrivere
- // INIZIO SEZIONE CRITICA
- this.lckTemp.lock();
- // sono in mutua esclusione : posso gestire
- // bufferTemp e inTemp
- // scrivo la temperatura che ho appena misurato nel bufferTemp.
- // la temperatura si trova in posizione 0 nell'array parametriLetti
- System.out.println("["+s.getName() +"] --> Sto scrivendo la temperatura letta nel cloud... "
- + "Scrivo nel bufferTemp in posizione "+ this.inTemp);
- this.bufferTemp[this.inTemp] = parametriLetti[0];
- // aggiorno il puntatore logico inTemp
- this.inTemp = (this.inTemp + 1) % this.bufferTemp.length;
- // avverto gli utenti che c'è un nuovo elemento nel
- // bufferTemp
- this.notEmptyTemp.release();
- } finally{
- // FINE SEZIONE CRITICA
- this.lckTemp.unlock();
- }
- // scrivo la luminosità nel buffer delle luminosità
- try{
- // acquisisco un permesso su notFullLum per assicurarmi che
- // ci sia un posto libero nel quale scrivere nel bufferLum
- this.notFullLum.acquire();
- // c'è un posto libero nel quale scrivere
- // INIZIO LA SEZIONE CRITICA
- this.lckLum.lock();
- // sono in mutua esclusione : posso gestire
- // bufferLum e inLum
- // scrivo la luminosità che ho appena misurato nel bufferLum.
- // la luminosità si trova in posizione 1 nell'array parametriLetti
- System.out.println("["+s.getName() +"] --> Sto scrivendo la luminosità letta nel cloud... "
- + "Scrivo nel bufferLum in posizione "+ this.inLum);
- this.bufferLum[this.inLum] = parametriLetti[1];
- // aggiorno il puntatori logico inLum
- this.inLum = (this.inLum + 1) % this.bufferLum.length;
- // avverto gli utenti che c'è un nuovo elemento nel
- // bufferLum
- this.notEmptyLum.release();
- } finally {
- // FINE SEZIONE CRITICA
- this.lckLum.unlock();
- }
- } // end metodo writeData
- } // end class Cloud
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement