DefaultCachingComponent.java 9.04 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 Simonstrator.KOM.
 *
 * Simonstrator.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.vehicular.caching;
22
23
24
25
26
27

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

28
import de.tudarmstadt.maki.simonstrator.api.Host;
29
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
30
31
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;
32
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
33
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;
34
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent;
35
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
Tobias Meuser's avatar
Tobias Meuser committed
36
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
37
38
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
39
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
40
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.JamInformation;
41
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
42
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
43
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetwork;
44
45
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
46

47
48
public class DefaultCachingComponent
implements CachingComponent, ConnectivityListener {
49
	private Map<Class<? extends Object>, List<PointInformation>> _cache = new HashMap<>();
50
51
52

	private Map<Integer, Integer> _lastColorValues = new HashMap<>();

53
54
	private Host _host;

Tobias Meuser's avatar
Tobias Meuser committed
55
	private CacheInvalidationStrategy _invalidationStrategy;
56

Tobias Meuser's avatar
Tobias Meuser committed
57
	private CacheReplacementStrategy _replacementStrategy;
58

Tobias Meuser's avatar
Tobias Meuser committed
59
	private CacheDecisionStrategy _decisionStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
60

61
62
63
64
65
66
	private double[] informationRatios = new double[] {1, 0.75, 0.5, 0.25, 0};

	public DefaultCachingComponent(Host pHost,
			CacheInvalidationStrategy pInvalidationStrategy,
			CacheReplacementStrategy pReplacementStrategy,
			CacheDecisionStrategy pDecisionStrategy) {
67
68
		_host = pHost;

Tobias Meuser's avatar
Tobias Meuser committed
69
		if (_host != null) {
70
71
			_host.getNetworkComponent().getByName(NetInterfaceName.WIFI)
			.addConnectivityListener(this);
Tobias Meuser's avatar
Tobias Meuser committed
72
		}
Tobias Meuser's avatar
Tobias Meuser committed
73
74
75

		_invalidationStrategy = pInvalidationStrategy;
		_replacementStrategy = pReplacementStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
76
77
78
79
		_decisionStrategy = pDecisionStrategy;
	}

	@Override
80
81
	public <T extends PointInformation> List<T> getDecidedCacheEntries(
			Class<T> pCacheEntryClass) {
Tobias Meuser's avatar
Tobias Meuser committed
82
83
84
85
86
87
88
89
90
91
		List<T> cacheEntries = getCacheEntries(pCacheEntryClass);

		if (cacheEntries == null) {
			return null;
		}

		Map<Object, List<PointInformation>> similarCacheEntries = new HashMap<>();

		for (T t : cacheEntries) {
			Object position = getEdgeOrPosition(t);
92
			
Tobias Meuser's avatar
Tobias Meuser committed
93
94
95
96
97
98
99
			if (!similarCacheEntries.containsKey(position)) {
				similarCacheEntries.put(position, new ArrayList<>());
			}
			similarCacheEntries.get(position).add(t);
		}

		List<T> decidedInformation = new ArrayList<>();
100
101
102
103
		for (List<PointInformation> similarEntries : similarCacheEntries
				.values()) {
			PointInformation correctInformation = _decisionStrategy
					.decideOnCorrectInformation(similarEntries);
Tobias Meuser's avatar
Tobias Meuser committed
104
105
106
107
			decidedInformation.add((T) correctInformation);
		}

		return decidedInformation;
108
	}
109
110

	@Override
111
112
	public <T extends PointInformation> List<T> getCacheEntries(
			Class<T> pCacheEntryClass) {
113
		if (_cache.containsKey(pCacheEntryClass)) {
114
115
			List<? extends PointInformation> cacheEntries = _cache
					.get(pCacheEntryClass);
116
117
			List<T> results = new ArrayList<>();

118
119
120
121
122
123
			for (int i = 0; i < cacheEntries.size(); i++) {
				PointInformation object = cacheEntries.get(i);
				if (_invalidationStrategy.checkInformation(object)) {
					cacheEntries.remove(i--);
					continue;
				}
124
125
126
127
128
129
130
131
				results.add((T) object);
			}

			return results;
		}
		return null;
	}

132
	@Override
133
	public <T extends PointInformation> boolean containsEntry(T pCacheEntry) {
Tobias Meuser's avatar
Tobias Meuser committed
134
135
		if (_cache.containsKey(pCacheEntry.getClass())) {
			List<? extends Object> cacheEntries = _cache.get(pCacheEntry.getClass());
136
137
138
139
140
			return cacheEntries.contains(pCacheEntry);
		}
		return false;
	}

141
	@Override
142
	public <T extends PointInformation> void storeCacheEntry(T pCacheEntry) {
143
144
145
		if (!_cache.containsKey(pCacheEntry.getClass())) {
			_cache.put(pCacheEntry.getClass(), new ArrayList<>());
		}
146

147
		List<PointInformation> entries = _cache.get(pCacheEntry.getClass());
Tobias Meuser's avatar
Tobias Meuser committed
148
		entries.add(pCacheEntry);
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
	@Override
	public void initialize() {
		_cache.clear();
	}

	@Override
	public void shutdown() {
	}

	@Override
	public Host getHost() {
		return _host;
	}

	@Override
	public void wentOnline(Host pHost, NetInterface pNetInterface) {
		_cache.clear();
	}

	@Override
	public void wentOffline(Host pHost, NetInterface pNetInterface) {

	}

175
176
177
178
179
	@Override
	public void clear() {
		_cache.clear();
	}

Tobias Meuser's avatar
Tobias Meuser committed
180
181
	public Object getEdgeOrPosition(PointInformation information) {
		if (information.hasAttribute(AvailableInformationAttributes.EDGE)) {
182
183
			return information
					.getAttribute(AvailableInformationAttributes.EDGE);
184
185
		} else if (information instanceof RoadInformation) {
		    return ((RoadInformation) information).getEdge();
Tobias Meuser's avatar
Tobias Meuser committed
186
187
188
189
190
		} else {
			return information.getLocation();
		}
	}

191
192
193
194
	public CacheDecisionStrategy getDecisionStrategy() {
		return _decisionStrategy;
	}

195
196
	@Override
	public String getNodeDescription() {
Tobias Meuser's avatar
Tobias Meuser committed
197
		return "    " + getHost().getId();
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
	}

	@Override
	public int getNodeColorDimensions() {
		return 2;
	}

	@Override
	public String[] getNodeColorDimensionDescriptions() {
		return new String[] {"Route Information", "Number of jam information"};
	}

	@Override
	public String[] getNodeColorDescriptions(int pDimension) {
		if (pDimension == 0) {
			String[] labels = new String[informationRatios.length];
			for (int i = 0; i < informationRatios.length; i++) {
				labels[i] = informationRatios[i] * 100 + "%";
			}
			return labels;
		} else if (pDimension == 1) {
			String[] numbers = new String[10];
			for (int i = 0; i < numbers.length; i++) {
				numbers[i] = String.valueOf(i);
			}
			return numbers;
		}
		return new String[0];
	}

	@Override
	public int getNodeColor(int pDimension) {
		if (pDimension == 0) {
			List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
			try {
				VehicleInformationComponent vehicleInformationComponent = getHost().getComponent(VehicleInformationComponent.class);
				if (vehicleInformationComponent.isValid()) {
					RoadNetworkRoute currentRoute = vehicleInformationComponent.getCurrentRoute();
					if (currentRoute != null) {
						int count = 0;
						int active = 0;
						for (RoadNetworkEdge edge : currentRoute.getRoute()) {
							if (edge.isActive()) {
								if (decidedCacheEntries != null) {
									for (JamInformation jamInformation : decidedCacheEntries) {
										if (jamInformation.getEdge().equals(edge) && jamInformation.getValue()) {
											count++;
											break;
										}
									}
								}
								active++;
							}
						}
						if (active != 0) {
							double ratio = count / ((double) active);
							for (int i = 0; i < informationRatios.length; i++) {
								if (informationRatios[i] <= ratio) {
									_lastColorValues.put(pDimension, i);
									break;
								}
							}
						} else {
							_lastColorValues.put(pDimension, 0);
						}
					}
				}
			} catch (ComponentNotAvailableException e) {
			}
			if (_lastColorValues.containsKey(pDimension)) {
				return _lastColorValues.get(pDimension);
			}
			return 0;
		}
		if (pDimension == 1) {
			List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
			if (decidedCacheEntries != null) {
				_lastColorValues.put(pDimension, decidedCacheEntries.size());
			}
			if (_lastColorValues.containsKey(pDimension)) {
				return Math.min(9, _lastColorValues.get(pDimension));
			}
			return 0;
		}
		return 0;
	}

	@Override
	public boolean isActive() {
		return getHost().getNetworkComponent().getByName(NetInterfaceName.MOBILE).isUp()
				|| getHost().getNetworkComponent().getByName(NetInterfaceName.WIFI).isUp();
	}
290
}