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 org.epics.pvmanager.MultiplexedChannelHandler;
8   import org.epics.pvmanager.ChannelWriteCallback;
9   import java.util.List;
10  import java.util.concurrent.ScheduledExecutorService;
11  import java.util.concurrent.ScheduledFuture;
12  import java.util.concurrent.TimeUnit;
13  import java.util.logging.Level;
14  import java.util.logging.Logger;
15  import org.epics.pvmanager.*;
16  import org.epics.util.time.TimeInterval;
17  import org.epics.util.time.Timestamp;
18  
19  /**
20   *
21   * @author carcassi
22   */
23  class SimulationChannelHandler<T> extends MultiplexedChannelHandler<Simulation<T>, T> {
24  
25      private final Simulation<T> simulation;
26      private final ScheduledExecutorService exec;
27      private final Runnable task = new Runnable() {
28  
29          @Override
30          public void run() {
31              // Protect the timer thread for possible problems.
32              try {
33                  if (simulation.lastTime == null) {
34                      simulation.lastTime = Timestamp.now();
35                  }
36                  List<T> newValues = simulation.createValues(TimeInterval.between(simulation.lastTime, Timestamp.now()));
37  
38                  for (T newValue : newValues) {
39                      processMessage(newValue);
40                  }
41              } catch (Exception ex) {
42                  log.log(Level.WARNING, "Data simulation problem", ex);
43              }
44          }
45      };
46      private static final Logger log = Logger.getLogger(SimulationChannelHandler.class.getName());
47      private ScheduledFuture<?> taskFuture;
48  
49      public SimulationChannelHandler(String channelName, Simulation<T> simulation, ScheduledExecutorService exec) {
50          super(channelName);
51          this.simulation = simulation;
52          this.exec = exec;
53      }
54  
55      @Override
56      public void connect() {
57          simulation.lastTime = Timestamp.now();
58          if (simulation instanceof SimFunction) {
59              simulation.lastTime = simulation.lastTime.minus(((SimFunction<?>) simulation).getTimeBetweenSamples());
60          }
61          taskFuture = exec.scheduleWithFixedDelay(task, 0, 10, TimeUnit.MILLISECONDS);
62          processConnection(simulation);
63      }
64  
65      @Override
66      public void disconnect() {
67          taskFuture.cancel(false);
68          taskFuture = null;
69          processConnection(null);
70      }
71  
72      @Override
73      public void write(Object newValue, ChannelWriteCallback callback) {
74          throw new UnsupportedOperationException("Can't write to simulation channel.");
75      }
76  
77      @Override
78      public boolean isConnected(Simulation<T> sim) {
79          return taskFuture != null;
80      }
81  }