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-sdk-ext.
005 *
006 * Dicoogle/dicoogle-sdk-ext 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-sdk-ext 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.sdk.p2p.Messages.Handlers;
020
021//import pt.ua.dicoogle.p2p.jgroups.sockets.MulticastSocketHandler;
022import pt.ua.dicoogle.sdk.NetworkPluginAdapter;
023import pt.ua.dicoogle.sdk.datastructs.SearchResult;
024import pt.ua.dicoogle.sdk.observables.ListObservable;
025import pt.ua.dicoogle.sdk.p2p.Messages.MessageI;
026import pt.ua.dicoogle.sdk.p2p.Messages.MessageType;
027
028/**
029 * Class that chooses which message handler will process the message received.
030 * @author Carlos Ferreira
031 * @author Pedro Bento
032 */
033public class MainMessageHandler implements MessageHandler
034{
035    private NetworkPluginAdapter NPA;
036    private ListObservable<SearchResult> results = null;
037
038    public MainMessageHandler(NetworkPluginAdapter NPA)
039    {
040        this.NPA = NPA;
041        results = NPA.getSearchResults();
042    }
043
044    public void handleMessage(MessageI message, String address)
045    {
046        MessageHandler handler = null;
047        if (message.getType().equals(MessageType.QUERY))
048        {
049            if (address.equals(this.NPA.getLocalAddress()))
050            {
051                return;
052            }
053            handler = new QueryHandler(1000, this.NPA);
054        }
055        if (message.getType().equals(MessageType.QUERY_RESP))
056        {
057            handler = new QueryResponseHandler(this.NPA, this.results);
058        }
059
060        if (message.getType().equals(MessageType.FILE_REQ))
061        {
062            handler = new FileRequestHandler(this.NPA);
063        }
064        if (message.getType().equals(MessageType.FILE_RESP))
065        {
066            handler = new FileResponseHandler("received", this.NPA);
067        }
068        handler.handleMessage(message, address);
069    }
070}