001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.server.controllers;
020
021import java.rmi.RemoteException;
022import java.util.ArrayList;
023import java.util.concurrent.Semaphore;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import pt.ua.dicoogle.rGUI.interfaces.controllers.IPendingMessages;
029import pt.ua.dicoogle.rGUI.interfaces.signals.IPendingMessagesSignal;
030
031/**
032 * Controller to manage the pending messages to the administrator
033 *
034 * @author Samuel Campos <samuelcampos@ua.pt>
035 */
036@Deprecated
037public class PendingMessages implements IPendingMessages {
038
039    private IPendingMessagesSignal signal;
040
041    // list of files that are already indexed but the administrator as to decide if they will be re-indexed or not
042    private ArrayList<String> filesAlreadyIndexed;
043
044    /**
045     * I thought of using synchronized methods to take care of concurrency 
046     * in filesAlreadyIndexed ArrayList but this causes deadlock.
047     * I use Semaphore semList instead.
048     */
049    private static Semaphore semList = new Semaphore(1, true);
050    
051    private static PendingMessages instance = null;
052    private static Semaphore sem = new Semaphore(1, true);
053
054    public static synchronized PendingMessages getInstance()
055    {
056        try {
057            sem.acquire();
058            if (instance == null) {
059                instance = new PendingMessages();
060            }
061            sem.release();
062        } catch (InterruptedException ex) {
063            LoggerFactory.getLogger(PendingMessages.class).error(ex.getMessage(), ex);
064        }
065        return instance;
066    }
067
068    private PendingMessages(){
069        filesAlreadyIndexed = new ArrayList<String>();
070    }
071
072
073    /**
074     * Add one file to the list of files already indexed
075     * 
076     * @param absolutePath
077     */
078    public void addFileAlreadyIndexed(String absolutePath){
079
080        try {
081            
082            semList.acquire();
083            filesAlreadyIndexed.add(absolutePath);
084            semList.release();
085            
086            try {
087                if (signal != null) {
088                    signal.sendPendingMessagesSignal(0);
089                }
090            } catch (RemoteException ex) {
091                LoggerFactory.getLogger(PendingMessages.class).error(ex.getMessage(), ex);
092            }
093            
094        } catch (InterruptedException ex) {
095            LoggerFactory.getLogger(PendingMessages.class).error(ex.getMessage(), ex);
096        }
097        
098    }
099
100    /**
101     *
102     * @return the list of the files that are already indexed
103     * @throws RemoteException
104     */
105    @Override
106    public ArrayList<String> getFilesAlreadyIndexed() throws RemoteException {
107        try {
108            semList.acquire();
109
110            ArrayList<String> temp = filesAlreadyIndexed;
111            filesAlreadyIndexed = new ArrayList<String>();
112
113            semList.release();
114
115            return temp;
116        } catch (InterruptedException ex) {
117            LoggerFactory.getLogger(PendingMessages.class).error(ex.getMessage(), ex);
118            return null;
119        }
120    }
121
122    /**
123     * Register the remote signal object to send pending messages
124     *
125     * @param signalBack
126     * @throws RemoteException
127     */
128    @Override
129    public void RegisterSignalBack(IPendingMessagesSignal signalBack) throws RemoteException {
130        if (signalBack != null){
131            signal = signalBack;
132
133            if(filesAlreadyIndexed.size() > 0)
134                signal.sendPendingMessagesSignal(0);
135        }
136    }
137
138    public void resetSignalBack(){
139        signal = null;
140    }
141
142}