All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robokind.api.messaging.DefaultMessageBlockingReceiver Maven / Gradle / Ivy

There is a newer version: 0.9.5
Show newest version
/*
 * Copyright 2011 Hanson Robokind LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.robokind.api.messaging;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.jflux.api.core.Adapter;
import org.jflux.api.core.Listener;

/**
 * Default implementation of the MessageBlockingReceiver.
 * Blocks until a Record is received.  Uses an Adapter to adapt Records to 
 * Messages and notifies Listeners.
 * 
 * @param  type of message to return
 * @param  type of record to receive
 * @author Matthew Stevenson 
 */
public class DefaultMessageBlockingReceiver 
        implements MessageBlockingReceiver {
    private final static Logger theLogger = 
            Logger.getLogger(DefaultMessageBlockingReceiver.class.getName());
    private RecordBlockingReceiver myRecordReceiver;
    private Adapter myAdapter;
    private List> myListeners;
    private long myTimeout;
    
    /**
     * Creates an empty DefaultMessageBlockingReceiver.
     */
    public DefaultMessageBlockingReceiver(){
        myTimeout = MessageBlockingReceiver.DEFAULT_TIMEOUT_LENGTH;
        myListeners = new ArrayList>();
    }
    /**
     * Creates an empty DefaultMessageBlockingReceiver.
     * @param timeout timeout length for receiving messages
     */
    public DefaultMessageBlockingReceiver(long timeout){
        myTimeout = timeout;
        myListeners = new ArrayList>();
    }
    
    /**
     * Sets the RecordAsyncReceiver used to receive Records.
     * @param service theRecordReceiver to set
     */
    public void setRecordReceiver(RecordBlockingReceiver receiver){
        myRecordReceiver = receiver;
    }
    /**
     * Sets the Adapter used to convert Records to Messages.
     * @param adapter  the Adapter to set
     */
    public void setAdapter(Adapter adapter){
        myAdapter = adapter;
    }

    @Override
    public void setTimeout(long timeout) {
        if(timeout < 0){
            throw new IllegalArgumentException("timeout must be positive.");
        }
        myTimeout = timeout;
    }

    @Override
    public long getTimeout() {
        return myTimeout;
    }

    @Override
    public void start(){
        if(myRecordReceiver == null){
            theLogger.warning("No Record PollingService, unable to receive.");
            return;
        }else if(myAdapter == null){
            theLogger.warning("No Record Adapter, unable to send receive.");
            return;
        }
    }

    @Override
    public void stop() {}

    @Override
    public Msg getValue(){
        if(myRecordReceiver == null || myAdapter == null){
            throw new NullPointerException();
        }
        Rec rec = myRecordReceiver.fetchRecord(myTimeout);
        if(rec == null){
            return null;
        }
        Msg msg = myAdapter.adapt(rec);
        if(msg != null){
            fireMessageEvent(msg);
        }
        return msg;
    }
    /**
     * Notifies listeners of a Message
     * @param message 
     */
    protected void fireMessageEvent(Msg message){
        for(Listener listener : myListeners){
            listener.handleEvent(message);
        }
    }

    @Override
    public int clearMessages() {
        return myRecordReceiver.clearRecords();
    }

    @Override
    public void addMessageListener(Listener listener) {
        if(listener == null){
            return;
        }
        if(!myListeners.contains(listener)){
            myListeners.add(listener);
        }
    }

    @Override
    public void removeMessageListener(Listener listener) {
        if(listener == null){
            return;
        }
        myListeners.remove(listener);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy