package org.md2k.datakit.datarouter;

import org.md2k.datakitapi.datatype.DataType;
import org.md2k.datakitapi.status.StatusCodes;

import java.util.ArrayList;
import java.util.List;
/**
 * Copyright (c) 2015, The University of Memphis, MD2K Center
 * - Syed Monowar Hossain <monowar.hossain@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

public class Publisher {
    private static final String TAG = Publisher.class.getSimpleName();
    int ds_id;
    boolean active;
    private List<MessageSubscriber> messageSubscribers;
    private DatabaseSubscriber databaseSubscriber;
    Publisher(int ds_id){
        this.ds_id=ds_id;
        databaseSubscriber=null;
        active=false;
        messageSubscribers =new ArrayList<>();
    }
    public void close(){
        messageSubscribers.clear();
        messageSubscribers=null;
    }
    public void setDatabaseSubscriber(DatabaseSubscriber databaseSubscriber){
        this.databaseSubscriber=databaseSubscriber;
    }
    public void setActive(boolean active){
        this.active=active;
    }
    public boolean isActive(){
        return active;
    }
    public void receivedData(DataType dataType) {
        notifyAllObservers(dataType);
    }
    boolean isExists(MessageSubscriber subscriber){
        return get(subscriber) != -1;
    }
    int get(MessageSubscriber subscriber){
        for(int i=0;i<messageSubscribers.size();i++)
            if(messageSubscribers.get(i).reply.equals(subscriber.reply))
                return i;
        return -1;
    }

    public int add(MessageSubscriber subscriber){
        if(isExists(subscriber)) return StatusCodes.ALREADY_SUBSCRIBED;
        messageSubscribers.add(subscriber);
        return StatusCodes.SUCCESS;
    }
    public int remove(MessageSubscriber subscriber){
        if(!isExists(subscriber)) return StatusCodes.DATASOURCE_NOT_EXIST;
        messageSubscribers.remove(get(subscriber));
        return StatusCodes.SUCCESS;
    }
    public void notifyAllObservers(DataType dataType){
        if(databaseSubscriber!=null) databaseSubscriber.update(ds_id,dataType);
        for (MessageSubscriber subscriber : messageSubscribers) {
            if(!subscriber.update(ds_id,dataType))
                messageSubscribers.remove(subscriber);
        }
    }
}

@@ -31,16 +31,44 @@
 outer;%0A%0A
+import android.os.Message;%0A%0A
 import o
@@ -105,16 +105,16 @@
 taType;%0A
-
 import o
@@ -151,16 +151,54 @@
 usCodes;
+%0Aimport org.md2k.utilities.Report.Log;
 %0A%0Aimport
@@ -219,16 +219,43 @@
 ayList;%0A
+import java.util.Iterator;%0A
 import j
@@ -1899,16 +1899,17 @@
 ribers;%0A
+%0A
     priv
@@ -3053,32 +3053,108 @@
 dd(subscriber);%0A
+        Log.d(TAG,%22add()... id=%22+ds_id+%22 size=%22+messageSubscribers.size());%0A
         return S
@@ -3460,87 +3460,470 @@
 e)%7B%0A
-        if(databaseSubscriber!=null) databaseSubscriber.update(ds_id,dataType);
+%0A        if(messageSubscribers.size()%3E0)%0A            Log.d(TAG, %22id=%22+ds_id+%22 subscriber=%22 + messageSubscribers.size());%0A        if(databaseSubscriber!=null) databaseSubscriber.update(ds_id,dataType);%0A        for (Iterator%3CMessageSubscriber%3E iterator = messageSubscribers.iterator(); iterator.hasNext();) %7B%0A            MessageSubscriber subscriber = iterator.next();%0A            if(!subscriber.update(ds_id,dataType))%0A                iterator.remove();%0A        %7D
 %0A   

remove subscriber if not responding