View Javadoc
1   /**
2    * Copyright (C) 2010-14 pvmanager developers. See COPYRIGHT.TXT
3    * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4    */
5   package org.epics.pvmanager.sim;
6   
7   import java.util.concurrent.Executors;
8   import java.util.concurrent.ScheduledExecutorService;
9   import java.util.logging.Logger;
10  import org.epics.pvmanager.ChannelHandler;
11  import org.epics.pvmanager.DataSource;
12  import org.epics.pvmanager.vtype.DataTypeSupport;
13  import static org.epics.pvmanager.util.Executors.*;
14  
15  /**
16   * Data source to produce simulated signals that can be using during development
17   * and testing.
18   *
19   * @author carcassi
20   */
21  public final class SimulationDataSource extends DataSource {
22  
23      static {
24          // Install type support for the types it generates.
25          DataTypeSupport.install();
26      }
27  
28      public SimulationDataSource() {
29          super(false);
30      }
31  
32      /**
33       * Data source instance.
34       *
35       * @return the data source instance
36       */
37      public static DataSource simulatedData() {
38          return SimulationDataSource.instance;
39      }
40  
41      private static final Logger log = Logger.getLogger(SimulationDataSource.class.getName());
42      static final SimulationDataSource instance = new SimulationDataSource();
43  
44      /**
45       * ExecutorService on which all simulated data is generated.
46       */
47      private static ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(namedPool("PVMgr Simulator "));
48  
49      @Override
50      @SuppressWarnings("unchecked")
51      protected ChannelHandler createChannel(String channelName) {
52          if (channelName.startsWith("const(")) {
53              return new ConstantChannelHandler(channelName);
54          }
55          if (channelName.startsWith("delayedConnectionChannel(")) {
56              return new DelayedConnectionChannelHandler(channelName, exec);
57          }
58          if (channelName.startsWith("intermittentChannel(")) {
59              return new IntermittentChannelHandler(channelName, exec);
60          }
61          
62          SimFunction<?> simFunction = (SimFunction<?>) NameParser.createFunction(channelName);
63          return new SimulationChannelHandler(channelName, simFunction, exec);
64      }
65  
66  }