SmartphoneCellularCommunicationEnergyComponent.java 10.1 KB
Newer Older
Nils Richerzhagen's avatar
Nils Richerzhagen committed
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;
Nils Richerzhagen's avatar
Nils Richerzhagen committed
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;
Nils Richerzhagen's avatar
Nils Richerzhagen committed
30
31
32
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tudarmstadt.maki.simonstrator.api.Message;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
33
import de.tudarmstadt.maki.simonstrator.api.Time;
Nils Richerzhagen's avatar
Nils Richerzhagen committed
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.Monitor.Level;

public class SmartphoneCellularCommunicationEnergyComponent implements
EnergyCommunicationComponent {


	private PhyType phy;

	private EnergyEventListener energyModel;

	/**
	 * The different states of this energy component.
	 */
	private final EnergyState IDLE, TX, RX, RAMP_TX, RAMP_RX, TAIL_TX, TAIL_RX;

	private final long RAMP_RX_DURATION = 1994 * Simulator.MILLISECOND_UNIT;

	private final long TAIL_RX_DURATION = 3132 * Simulator.MILLISECOND_UNIT;

	private final long RAMP_TX_DURATION = 3096 * Simulator.MILLISECOND_UNIT;

	private final long TAIL_TX_DURATION = 4796 * Simulator.MILLISECOND_UNIT;

	private int tailCounter;

	/**
	 * 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 SmartphoneCellularCommunicationEnergyComponent(PhyType phy) {
		this.phy = phy;
		IDLE = new DefaultEnergyState("IDLE", 0);
		TX = new DefaultEnergyState("TX", 1.105 * 1000000);
		RAMP_TX = new DefaultEnergyState("RAMP_TX", 0.749 * 1000000);
		TAIL_TX = new DefaultEnergyState("TAIL_TX", 0.563 * 1000000);
		RX = new DefaultEnergyState("RX", 0.786 * 1000000);
		RAMP_RX = new DefaultEnergyState("RAMP_RX", 0.505 * 1000000);
		TAIL_RX = new DefaultEnergyState("TAIL_RX", 0.483 * 1000000);
		tailCounter = 0;

		currentState = IDLE;
		lastStateChange = 0;
	}

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

	@Override
92
	public boolean turnOff() {
Nils Richerzhagen's avatar
Nils Richerzhagen committed
93
94
95
96
		if (!currentState.equals(IDLE)) {
			doStateChange(IDLE);
		}
		tailCounter++;
97
		return true;
Nils Richerzhagen's avatar
Nils Richerzhagen committed
98
99
100
101
102
103
104
105
106
	}

	@Override
	public boolean turnOn() {
		return true;
	}

	@Override
	public boolean isOn() {
107
		if (energyModel.componentCanBeActivated(this)) {
Nils Richerzhagen's avatar
Nils Richerzhagen committed
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
			doFakeStateChange();
			return true;
		}
		return false;
	}

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

	@Override
	public void eventOccurred(Object content, int type) {
		if (type == tailCounter) {
			assert currentState.equals(TAIL_RX) || currentState.equals(TAIL_TX) : "Assert1 SmartphoneCellularCommunicationEnergy";

			long timeInTailState = Simulator.getCurrentTime() - lastStateChange;

			if((TAIL_RX_DURATION-timeInTailState)!=0 && currentState.equals(TAIL_RX))
				Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.WARN, TAIL_RX_DURATION +" vs. " + timeInTailState);

			if((TAIL_TX_DURATION-timeInTailState)!=0 && currentState.equals(TAIL_TX))
				Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.WARN,TAIL_TX_DURATION +" vs. " + timeInTailState);

			assert timeInTailState==TAIL_TX_DURATION || currentState.equals(TAIL_RX) : "Assert2 SmartphoneCellularCommunicationEnergy";
//			assert timeInTailState==TAIL_TX_DURATION || timeInTailState==TAIL_RX_DURATION : "Assert2 SmartphoneCellularCommunicationEnergy";

			Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.DEBUG, Simulator.getFormattedTime(Simulator.getCurrentTime())
					+ " "
					+ ((EnergyModel) energyModel).getHost().getHostId()
					+ " consumed "
					+ (currentState.getEnergyConsumption() * (timeInTailState / (double) Simulator.SECOND_UNIT))
					+ " uJ in State " + currentState.getName()
					+ " after spending "
					+ (timeInTailState / (double) Simulator.SECOND_UNIT)
					+ " sec there.");
144
145
						
			energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeInTailState));
Nils Richerzhagen's avatar
Nils Richerzhagen committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
			tailCounter++;
			currentState = IDLE;
			lastStateChange = Simulator.getCurrentTime();
		}
	}

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

	@Override
	public void send(long duration, Message msg, boolean isBroadcast) {
		if (currentState.equals(IDLE)) {
			/*
			 * Consume the energy for being in the ramp state. There is not
			 * explicit ramp state, because the node immediately sends the
			 * message. However, the energy for the ramp state before sending is
			 * consumed.
			 */
166
			energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(RAMP_TX, RAMP_TX_DURATION));
Nils Richerzhagen's avatar
Nils Richerzhagen committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
		} else {
			assert currentState.equals(TAIL_RX) || currentState.equals(TAIL_TX);

			long timeInTailState = Simulator.getCurrentTime()
					- (lastStateChange + duration);
			Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.DEBUG, Simulator.getFormattedTime(Simulator.getCurrentTime())
					+ " "
					+ ((EnergyModel) energyModel).getHost().getHostId()
					+ " consumed "
					+ (currentState.getEnergyConsumption() * (timeInTailState / (double) Simulator.SECOND_UNIT))
					+ " uJ in State " + currentState.getName()
					+ " after spending "
					+ (timeInTailState / (double) Simulator.SECOND_UNIT)
					+ " sec there before starting a transmission.");
181
182
			
			energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeInTailState));			
Nils Richerzhagen's avatar
Nils Richerzhagen committed
183
184
185
186
187
188
			tailCounter++;
		}

		/*
		 * Consume the energy for the transmission of data.
		 */
189
190
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(TX, duration));
		
Nils Richerzhagen's avatar
Nils Richerzhagen committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
		Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.DEBUG, Simulator.getFormattedTime(Simulator.getCurrentTime())
				+ " "
				+ ((EnergyModel) energyModel).getHost().getHostId()
				+ " consumed "
				+ (TX.getEnergyConsumption() * (duration / (double) Simulator.SECOND_UNIT))
				+ " uJ in State " + TX.getName() + " after spending "
				+ (duration / (double) Simulator.SECOND_UNIT) + " sec there.");

		/*
		 * Jump into the tail state and trigger the event to consume the energy
		 * for the tail state after the given amount of time if no message must
		 * be sent or received in the meantime.
		 */
		currentState = TAIL_TX;
		lastStateChange = Simulator.getCurrentTime();
		Simulator.getScheduler().scheduleIn(TAIL_TX_DURATION, this,
				TAIL_TX, tailCounter);
	}

	@Override
	public void receive(long duration, Message msg, boolean isBroadcast,
			boolean isIntendedReceiver) {
		if(currentState.equals(IDLE)){
			/*
			 * Consume the energy for being in the ramp state. There is not
			 * explicit ramp state, because the node immediately receives the
			 * message. However, the energy for the ramp state before receiving
			 * is consumed.
			 */
220
221
			energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(RAMP_RX, RAMP_RX_DURATION));
			
Nils Richerzhagen's avatar
Nils Richerzhagen committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
		} else {
			assert currentState.equals(TAIL_RX) || currentState.equals(TAIL_TX);

			long timeInTailState = Simulator.getCurrentTime() + duration
					- lastStateChange;
			Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.DEBUG,Simulator.getCurrentTime()
					+ " "
					+ ((EnergyModel) energyModel).getHost().getHostId()
					+ " consumed "
					+ (currentState.getEnergyConsumption() * (timeInTailState / (double) Simulator.SECOND_UNIT))
					+ " uJ in State " + currentState.getName()
					+ " after spending "
					+ (timeInTailState / (double) Simulator.SECOND_UNIT)
					+ " sec there before receiving a message.");
236
237
238
			
			energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeInTailState));			
			
Nils Richerzhagen's avatar
Nils Richerzhagen committed
239
240
241
242
243
			tailCounter++;
		}
		/*
		 * Consume the energy for the receiption of data.
		 */
244
245
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(RX, duration));			
	
Nils Richerzhagen's avatar
Nils Richerzhagen committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
		Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.DEBUG,Simulator.getCurrentTime()
				+ " "
				+ ((EnergyModel) energyModel).getHost().getHostId()
				+ " consumed "
				+ (RX.getEnergyConsumption() * (duration / (double) Simulator.SECOND_UNIT))
				+ " uJ in State " + RX.getName() + " after spending "
				+ (duration / (double) Simulator.SECOND_UNIT) + " sec there.");

		currentState = TAIL_RX;
		lastStateChange = Simulator.getCurrentTime();
		Simulator.getScheduler().scheduleIn(TAIL_RX_DURATION, this, TAIL_RX,
				tailCounter);
	}

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

	private void doStateChange(EnergyState newState){
		long timeSpentInState = Simulator.getCurrentTime() - lastStateChange;
		Monitor.log(SmartphoneCellularCommunicationEnergyComponent.class, Level.DEBUG,Simulator.getFormattedTime(Simulator.getCurrentTime())
				+ " "
				+ ((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.");
275
276
277
		
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));			
		
Nils Richerzhagen's avatar
Nils Richerzhagen committed
278
279
280
281
282
283
		currentState = newState;
		if(!currentState.equals(TAIL_RX) && !currentState.equals(TAIL_TX))
			lastStateChange = Simulator.getCurrentTime();
	}

}