OneStateEnergyComponent.java 2.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

21
package de.tud.kom.p2psim.impl.energy.components;
22
23
24
25

import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyComponent;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
26
import de.tud.kom.p2psim.impl.energy.DefaultEnergyState;
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
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.Time;

/**
 * This class provides an energy Component with only one state. Ok there are two
 * states (offline), but it gives only one state, which is running all the time.<br>
 * 
 * The component creates an energy component, which consume every second the
 * given power. It is starting with consuming at the creation of this component!
 * 
 * The component can be stopped and startet with the turnOff and turnOn methods
 * of this class.
 * 
 * @author Christoph Muenker
 * @version 1.0, 07.03.2013
 */
public class OneStateEnergyComponent implements EnergyComponent {

	private DefaultEnergyState state = null;

	private ComponentType componentType = null;

	private EnergyEventListener energyModel;

	boolean on = true;

	/**
	 * Creates the OneStateEnergyComponent with the given power consumption.
	 * 
	 * @param energyConsumption
	 *            The basic energy consumption in uWatt.
	 */
	public OneStateEnergyComponent(double energyConsumptionUwatt,
			ComponentType componentType) {
		this.componentType = componentType;
		state = new DefaultEnergyState(componentType.toString(),
				energyConsumptionUwatt);
		Event.scheduleWithDelay(Time.SECOND, this, this, 0);
	}

	@Override
	public void eventOccurred(Object content, int type) {
		if (isOn()) {
			energyModel.switchedState(this, state, state, Time.SECOND);
			if (energyModel.turnOn(this)) {
				Event.scheduleWithDelay(Time.SECOND, this, null, 0);
			} else {
				this.turnOff();
			}
		}
	}

	@Override
	public ComponentType getType() {
		return componentType;
	}

	@Override
	public void turnOff() {
		this.on = false;
	}

	@Override
	public boolean turnOn() {
		this.on = true;
		Event.scheduleWithDelay(Time.SECOND, this, this, 0);
		return true;
	}

	@Override
	public boolean isOn() {
		return on;
	}

	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;
	}

}