SmartphoneCommunicationEnergyComponent.java 5.73 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
26
27
28

import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyCommunicationComponent;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
import de.tud.kom.p2psim.api.energy.EnergyModel;
import de.tud.kom.p2psim.api.energy.EnergyState;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
29
import de.tud.kom.p2psim.impl.energy.DefaultEnergyState;
30
31
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tudarmstadt.maki.simonstrator.api.Message;
32
33
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
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
import de.tudarmstadt.maki.simonstrator.api.Time;

/**
 * An energy component, which models the power consumption for the Wi-Fi ad ho
 * communication over a smartphone. The model and values are based on the Master
 * Thesis from Fabian Kaup.
 * 
 * @author stingl
 */
public class SmartphoneCommunicationEnergyComponent implements
		EnergyCommunicationComponent {
	
	private PhyType phy;
	
	private EnergyEventListener energyModel;
	
	/**
	 * The different states of this energy component.
	 */
	private final EnergyState IDLE, SEND, RECEIVE, OFF;
	
	/**
	 * Represents the state, this energy component is currently in.
	 */
	private EnergyState currentState;

	/**
	 * Represents the time, when the energy component entered the current energy
	 * state.
	 */
	private long lastStateChange;

	public SmartphoneCommunicationEnergyComponent(PhyType phy) {
		this.phy = phy;
		
		/*
		 * For a better comparison, e.g., with Feeney, the corresponding values
		 * for the power consumption are represented in µJW. IDLE consists of
		 * the WI-FI-IDLE-State (0.314W) and the Device-Lock (0.039W) and is
		 * always consumed. For that reason, the IDLE-State is always calculated
		 * over the whole online-time of a node, i.e., if a node goes offline or
		 * a simulation is finished.
		 */
		IDLE = new DefaultEnergyState("IDLE", (0.314 + 0.039) * 1000000);
		SEND = new DefaultEnergyState("SEND", 0.687 * 1000000);
		RECEIVE = new DefaultEnergyState("RECEIVE", 0.352 * 1000000);
		OFF = new DefaultEnergyState("OFF", 0);

		this.currentState = IDLE;
		this.lastStateChange = Simulator.getCurrentTime();
	}

	@Override
	public ComponentType getType() {
		return ComponentType.COMMUNICATION;
	}

	@Override
92
	public boolean turnOff() {
93
94
95
		if (!currentState.equals(OFF)) {
			doStateChange(OFF);
		}
96
		return true;
97
98
99
100
	}

	@Override
	public boolean turnOn() {
101
		if (energyModel.componentCanBeActivated(this)) {
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
			if (!currentState.equals(IDLE)) {
				doStateChange(IDLE);
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean isOn() {
		return !currentState.equals(OFF);
	}

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

	@Override
	public void eventOccurred(Object content, int type) {
		// not required
	}

	@Override
	public PhyType getPhyType() {
		return phy;
	}

	@Override
	public void send(long duration, Message msg, boolean isBroadcast) {
		assert isOn();
133
134
135
136
		Monitor.log(
				SmartphoneCommunicationEnergyComponent.class,
				Level.DEBUG,
				Time.getFormattedTime()
137
138
139
140
141
142
				+ " "
				+ ((EnergyModel) energyModel).getHost().getHostId()
				+ " consumed "
				+ (SEND.getEnergyConsumption() * (duration/ (double) Simulator.SECOND_UNIT))
				+ " uJ in State " + SEND.getName() + " after spending "
				+ (duration / (double) Simulator.SECOND_UNIT) + " sec there.");
143
144
145
		
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(SEND, duration));	
		
146
147
148
149
150
151
	}

	@Override
	public void receive(long duration, Message msg, boolean isBroadcast,
			boolean isIntendedReceiver) {
		assert isOn();
152
153
154
155
156
157
158
159
160
161
162
163
		Monitor.log(
				SmartphoneCommunicationEnergyComponent.class,
				Level.DEBUG,
				Time.getFormattedTime()
						+ " "
						+ ((EnergyModel) energyModel).getHost().getHostId()
						+ " consumed "
						+ (RECEIVE.getEnergyConsumption() * (duration / (double) Simulator.SECOND_UNIT))
						+ " uJ in State " + RECEIVE.getName()
						+ " after spending "
						+ (duration / (double) Simulator.SECOND_UNIT)
						+ " sec there.");
164
165
166
		
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(RECEIVE, duration));
	
167
168
169
170
171
172
173
174
	}

	public void doFakeStateChange() {
		doStateChange(currentState);
	}

	private void doStateChange(EnergyState newState){
		long timeSpentInState = Simulator.getCurrentTime() - lastStateChange;
175
176
177
178
179
180
181
182
183
184
185
186
		Monitor.log(
				SmartphoneCommunicationEnergyComponent.class,
				Level.DEBUG,
				Time.getFormattedTime()
						+ " "
						+ ((EnergyModel) energyModel).getHost().getHostId()
						+ " consumed "
						+ (currentState.getEnergyConsumption() * (timeSpentInState / (double) Simulator.SECOND_UNIT))
						+ " uJ in State " + currentState.getName()
						+ " after spending "
						+ (timeSpentInState / (double) Simulator.SECOND_UNIT)
						+ " sec there.");
187
188
		
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));
189
190
191
192
193
		currentState = newState;
		lastStateChange = Simulator.getCurrentTime();
	}

}