/*******************************************************************************
 * Copyright 2016, 2018 vanilladb.org contributors
 *
 * 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.vanilladb.bench.rte;

import java.util.concurrent.atomic.AtomicInteger;

import org.vanilladb.bench.BenchTransactionType;
import org.vanilladb.bench.StatisticMgr;
import org.vanilladb.bench.TxnResultSet;
import org.vanilladb.bench.remote.SutConnection;

public abstract class RemoteTerminalEmulator<T extends BenchTransactionType> extends Thread {

	private static AtomicInteger rteCount = new AtomicInteger(0);

	private volatile boolean stopBenchmark;
	private volatile boolean isWarmingUp = true;
	private SutConnection conn;
	private StatisticMgr statMgr;
	private long sleepTime;
	
	public RemoteTerminalEmulator(SutConnection conn, StatisticMgr statMgr,
			long sleepTime) {
		this.conn = conn;
		this.statMgr = statMgr;
		this.sleepTime = sleepTime;
		
		// Set the thread name
		setName("RTE-" + rteCount.getAndIncrement());
	}

	@Override
	public void run() {
		while (!stopBenchmark) {
			TxnResultSet rs = executeTxnCycle(conn);
			if (!isWarmingUp)
				statMgr.processTxnResult(rs);
			
			// Sleep for a while
			if (sleepTime > 0) {
				try {
					Thread.sleep(sleepTime);
				} catch (InterruptedException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
	}

	public void startRecordStatistic() {
		statMgr.setRecordStartTime();
		isWarmingUp = false;
	}

	public void stopBenchmark() {
		stopBenchmark = true;
	}

	protected abstract T getNextTxType();
	
	protected abstract TransactionExecutor<T> getTxExeutor(T type);

	private TxnResultSet executeTxnCycle(SutConnection conn) {
		T txType = getNextTxType();
		TransactionExecutor<T> executor = getTxExeutor(txType);
		return executor.execute(conn);
	}
}
@@ -1189,17 +1189,42 @@
 ger(0);%0A
+%09%0A%09protected int rteId;%0A%09
 %0A
-
 %09private
@@ -1593,24 +1593,15 @@
 e%0A%09%09
-setName(%22RTE-%22 +
+rteId =
  rte
@@ -1623,16 +1623,42 @@
 rement()
+;%0A%09%09setName(%22RTE-%22 + rteId
 );%0A%09%7D%0A%0A%09
@@ -1849,180 +1849,16 @@
 %0A%09%09%09
-if (sleepTime %3E 0) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09Thread.sleep(sleepTime);%0A%09%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09throw new RuntimeException(e);%0A%09%09%09%09%7D%0A%09%09%09%7D
+sleep();
 %0A%09%09%7D
@@ -2124,16 +2124,218 @@
  type);%0A
+%09%0A%09protected void sleep() %7B%09%09%0A%09%09if (sleepTime %3E 0) %7B%0A%09%09%09try %7B%0A%09%09%09%09Thread.sleep(sleepTime);%0A%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%09throw new RuntimeException(e);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09
 %0A%09privat

Refactor RTE's code