Package examples.async

Source Code of examples.async.StreamingTSDataPlugin

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2004, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id: StreamingTSDataPlugin.java,v 1.9 2007/09/04 10:58:24 ogor Exp $
*
* Changes
* -------
* 11 jan. 2005 : Initial public release (CC);
*
*/
package examples.async;

import java.io.IOException;
import java.util.AbstractList;
import java.util.Collection;
import jsynoptic.base.Plugin;
import simtools.data.DataException;
import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.data.DataSourceCollection;
import simtools.data.DataSourcePool;
import simtools.data.DuplicateIdException;
import simtools.data.EndNotificationListener;
import simtools.data.UnsupportedOperation;
import simtools.data.async.StreamingTSDataSource;
import simtools.data.async.StreamingTSDataSourceCollection;
import simtools.data.async.TimeStampedDataSourceCollection.InvalidFormatException;

/**
* A test/demo for streaming time stamped data sources
*
* @author Claude Cazenave
*/
public class StreamingTSDataPlugin extends Plugin {
    static SourceCollection collection = null;

    /**
     * When this plugin is loaded, it adds a times stamped source collection to
     * the application
     */
    public StreamingTSDataPlugin() {
        if (collection == null) {
            try {
                collection = new SourceCollection();
                DataSourcePool.global.addDataSourceCollection(collection);
            } catch (IOException e) {
            } catch (InvalidFormatException e) {
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see jsynoptic.base.Plugin#getDataSourceIcons()
     */
    public Object[][] getDataSourceIcons() {
        Object[][] ret = new Object[2][2];
        ret[0][0] = StreamingTSDataSource.class;
        ret[0][1] = jsynoptic.ui.SourceTree.resources.getIcon("dynamicSourceIcon");
        ret[1][0] = SourceCollection.class;
        ret[1][1] = jsynoptic.ui.SourceTree.resources.getIcon("dynamicSourceCollectionIcon");
        return ret;
    }

    /**
     * Enter your plugin information here. This will appear in the about box.
     * Warning : uses HTML...
     *
     * @see jsynoptic.base.Plugin#about()
     */
    public String about() {
        return "Time stamped sources, an example plugin provided by the JSynoptic team";
    }

    public static class SourceCollection extends StreamingTSDataSourceCollection implements EndNotificationListener {
        public static final String ID_MARKER = "TestStreamingTS:";

        static int cptThread = 0;

        CollectionContainer cc;

        StreamingTSDataSource a = null;

        StreamingTSDataSource b = null;

        /**
         * @throws IOException
         * @throws InvalidFormatException
         */
        public SourceCollection() throws IOException, InvalidFormatException {
            super();
            cc = new CollectionContainer();
            Thread t = new Thread("StreamingTSDataPlugin" + (cptThread++)) {
                public void run() {
                    int cpt = 0;
                    while (true) {
                        try {
                            Thread.sleep(10);
                            long t = System.currentTimeMillis();
                            add("A", t, Math.sin(cpt / 100.));
                            if ((cpt % 4) == 0) {
                                add("B", t, Math.cos(cpt / 10.));
                            }
                            if (cpt == 0) {
                                cc.path1.addDataSource((DataSource) SourceCollection.this.get(0));
                                cc.path2.addDataSource((DataSource) SourceCollection.this.get(1 * 2));
                            }
                            cpt++;
                        } catch (InterruptedException ie) {
                        }
                    }
                }
            };
            t.start();
            Thread t2 = new Thread("AsyncData" + (cptThread++)) {
                public void run() {
                    // first get the data from the data pool
                    while (true) {
                        try {
                            if (a == null) {
                                DataSource d = DataSourcePool.global.getDataSourceWithId("A");
                                if (d instanceof StreamingTSDataSource) {
                                    a = (StreamingTSDataSource) d;
                                    a.addEndNotificationListener(SourceCollection.this);
                                }
                            }
                            if (b == null) {
                                DataSource d = DataSourcePool.global.getDataSourceWithId("B");
                                if (d instanceof StreamingTSDataSource) {
                                    b = (StreamingTSDataSource) d;
                                }
                            }
                            if ((a != null) && (b != null)) {
                                break;
                            }
                            Thread.sleep(10);
                        } catch (DuplicateIdException e) {
                            // unforesen in the test case
                            e.printStackTrace();
                            return;
                        } catch (InterruptedException e) {
                        }
                    }
                }
            };
            t2.start();
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.data.DataSourceCollection#getCollectionContainers()
         */
        public Collection getCollectionContainers() {
            return cc;
        }

        /* (non-Javadoc)
         * @see simtools.data.async.StreamingTSDataSourceCollection#getInformation()
         */
        public DataInfo getInformation() {
            DataInfo info = new DataInfo("Test", ID_MARKER + "Test");
            return info;
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.data.DataSourceCollection#isCompound()
         */
        public boolean isCompound() {
            return true;
        }

        class CollectionContainer extends AbstractList {
            DataSourceCollection.Container path1 = new DataSourceCollection.Container("path1");

            DataSourceCollection.Container path2 = new DataSourceCollection.Container("path2");

            DataSourceCollection.Container path2s = new DataSourceCollection.Container("subpath");

            DataSourceCollection.Container path3 = new DataSourceCollection.Container("path3");

            CollectionContainer() {
                path2.addContainer(path2s);
            }

            /*
             * (non-Javadoc)
             *
             * @see java.util.AbstractList#get(int)
             */
            public Object get(int index) {
                switch (index) {
                case 0:
                    return path1;
                case 1:
                    return path2;
                case 2:
                    return path3;
                }
                return null;
            }

            /*
             * (non-Javadoc)
             *
             * @see java.util.AbstractCollection#size()
             */
            public int size() {
                return 3;
            }
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.data.EndNotificationListener#notificationEnd(java.lang.Object)
         */
        public void notificationEnd(Object referer) {
            if ((referer == a) && (b != null)) {
                boolean first = false;
                if (SourceCollection.this.size() == (2 * 2)) {
                    first = true;
                }
                try {
                    // take last value of b and add a
                    long ai = a.getLastIndex();
                    long bi = b.getLastIndex();
                    double av = a.getDoubleValue(ai);
                    double bv = b.getDoubleValue(bi);
                    double at = a.getTime().getDoubleValue(ai);
                    add("C", at, av + bv);
                    // the same with interpolation on b
                    if (bi > 0) {
                        double bvp = b.getDoubleValue(bi - 1);
                        double bt = b.getTime().getDoubleValue(bi);
                        double btp = b.getTime().getDoubleValue(bi - 1);
                        double db = (bv - bvp) / (bt - btp);
                        add("Csmooth", at, av + bv + db * (at - btp));
                    } else {
                        add("Csmooth", at, av + bv);
                    }
                    if (first) {
                        DataSource c = (DataSource) SourceCollection.this.get(2 * 2);
                        cc.path2s.addDataSource(c);
                        DataSource cs = (DataSource) SourceCollection.this.get(3 * 2);
                        cc.path2s.addDataSource(cs);
                        DataSourcePool.global.DataSourceCollectionDataSourceAdded(this, c);
                    }
                } catch (UnsupportedOperation e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (DataException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
TOP

Related Classes of examples.async.StreamingTSDataPlugin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.