Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2c78d216dbd1528e8a3d8d1a6ee2fe5c620de52b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.message.timer;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;

import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.ote.core.environment.EnvironmentTask;
import org.eclipse.osee.ote.core.environment.TestEnvironment;
import org.eclipse.osee.ote.core.environment.TimerControl;
import org.eclipse.osee.ote.core.environment.interfaces.ICancelTimer;
import org.eclipse.osee.ote.core.environment.interfaces.IScriptControl;
import org.eclipse.osee.ote.core.environment.interfaces.ITimeout;
import org.eclipse.osee.ote.message.MessageSystemTestEnvironment;

/**
 * We use a frequency resolution of 300hz.
 * 
 * @author Ryan D. Brooks
 * @author Andrew M. Finkbeiner
 */
public class SimulatedTime extends TimerControl {

   private static final class Task {
      private final EnvironmentTask task;
      private final TestEnvironment env;

      public Task(EnvironmentTask task, TestEnvironment env) {
         super();
         this.task = task;
         this.env = env;
      }

      public void doTask(int cycleCount) {
         try {
            task.baseRunOneCycle(cycleCount);
         } catch (Throwable ex) {
            OseeLog.log(MessageSystemTestEnvironment.class, Level.SEVERE,
                  "Aborting the test script because an Environment Task is failing", ex);
            env.getRunManager().abort(ex, false);
         }
      }
   }
   private final Collection<CycleCountDown> cycleCounters;
   private final Collection<CycleCountDown> scriptCycleCounters;
   private final IScriptControl scriptControl;
   private int cycleCount;
   private final CopyOnWriteArrayList<Task> tasks = new CopyOnWriteArrayList<>();

   private final long sysTime;

   /**
    * @param scriptControl -
    */
   public SimulatedTime(IScriptControl scriptControl) throws IOException {
      super(3);
      this.scriptControl = scriptControl;
      cycleCounters = new HashSet<>(32);
      scriptCycleCounters = new HashSet<>(32);
      cycleCount = 0;
      sysTime = System.currentTimeMillis();
   }

   @Override
   public long getEnvTime() {
      return (long) (cycleCount * 1000.0 / EnvironmentTask.cycleResolution);
   }

   @Override
   public ICancelTimer setTimerFor(ITimeout objToNotify, int milliseconds) {
      CycleCountDown cycleCountDown = new CycleCountDown(scriptControl, objToNotify,
            (int) Math.rint(milliseconds / (1000.0 / EnvironmentTask.cycleResolution)) - 1);
      synchronized (cycleCounters) {
         if(getRunManager().isCurrentThreadScript()){
            scriptCycleCounters.add(cycleCountDown);
         } else {
            cycleCounters.add(cycleCountDown);
         }
      }

      unlockScriptControl();

      return cycleCountDown;
   }

   protected void unlockScriptControl() {
      try {
         scriptControl.unlock();
      } catch (IllegalMonitorStateException ex) {
         OseeLog.log(MessageSystemTestEnvironment.class, Level.SEVERE, ex);
      }
   }

   @Override
   public void addTask(EnvironmentTask envTask, TestEnvironment environment) {
      for (Task task : tasks) {
         if (task.task == envTask) {
            return;
         }
      }

      tasks.add(new Task(envTask, environment));
   }

   @Override
   public void removeTask(EnvironmentTask task) {
      Task itemToRemove = null;
      for (Task t : tasks) {
         if (t.task == task) {
            itemToRemove = t;
            break;
         }
      }
      if (itemToRemove != null) {
         OseeLog.log(MessageSystemTestEnvironment.class, Level.FINE, "removing environment task " + task.toString());
         tasks.remove(itemToRemove);
      }
   }

   @Override
   public void step() {

      for (Task t : tasks) {
         t.doTask(cycleCount);
      }
      incrementCycleCount();
   }

   @Override
   public int getCycleCount() {
      return cycleCount;
   }

   public Collection<CycleCountDown> getCycleCounters() {
      return scriptCycleCounters;
   }

   @Override
   public void incrementCycleCount() {
      cycleCount++;
   }

   @Override
   public void setCycleCount(int cycle) {
      cycleCount = cycle;
   }

   @Override
   public void cancelAllTasks() {
      for (Task t : tasks) {
         t.task.cancel();
      }
      tasks.clear();
   }

   public void removeOccurredCycleCounters() {
      synchronized (cycleCounters) {
         Iterator<CycleCountDown> iter = cycleCounters.iterator();
         while (iter.hasNext()) {
            CycleCountDown counter = iter.next();
            if (counter.cycleOccurred()) {
               iter.remove();
            }
         }
         iter = scriptCycleCounters.iterator();
         while (iter.hasNext()) {
            CycleCountDown counter = iter.next();
            if (counter.cycleOccurred()) {
               iter.remove();
            }
         }
      }
   }

   @Override
   public void dispose() {
      cycleCounters.clear();
      scriptCycleCounters.clear();
      tasks.clear();
   }

   @Override
   public long getTimeOfDay() {
      return sysTime + getEnvTime();
   }

   @Override
   public boolean isRealtime() {
      return false;
   }
}

Back to the top