FixedAssignmentStrategy.java 5.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * 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/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement.modular.transition;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.common.SimHostComponent;
32
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
33
34
35
36
37
38
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.modular.ModularMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modular.attraction.AttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.Randoms;

/**
39
40
41
 * A {@link TransitionStrategy} for a case in which nodes are affiliated to an
 * {@link AttractionPoint} only in the beginning. No further transition will
 * take place.
42
43
44
45
46
 * 
 * @author Nils Richerzhagen
 * @version 1.0, 04.08.2014
 */
public class FixedAssignmentStrategy implements TransitionStrategy {
47

48
49
	private Random rnd;

50
	private List<SimLocationActuator> comps = new LinkedList<SimLocationActuator>();
51
52
53

	private List<AttractionPoint> aPoints = new LinkedList<AttractionPoint>();

54
	private Map<SimLocationActuator, AttractionPoint> assignments = new LinkedHashMap<SimLocationActuator, AttractionPoint>();
55

56
	private Map<SimLocationActuator, SimHost> mappingMSHost = new LinkedHashMap<SimLocationActuator, SimHost>();
57

58
	private Map<SimHost, SimLocationActuator> mappingHostMS = new LinkedHashMap<SimHost, SimLocationActuator>();
59
60
61
62
63
64
65
66

	private Map<String, AttractionPoint> mappingGroupIdAP = new LinkedHashMap<String, AttractionPoint>();

	private Map<AttractionPoint, String> mappingAPGroupId = new LinkedHashMap<AttractionPoint, String>();

	public FixedAssignmentStrategy() {
		rnd = Randoms.getRandom(FixedAssignmentStrategy.class);
	}
67

68
	@Override
69
70
	public Map<SimLocationActuator, AttractionPoint> getAssignments() {
		return new HashMap<SimLocationActuator, AttractionPoint>(assignments);
71
72
73
74
75
76
77
78
	}

	@Override
	public void setAttractionPoints(List<AttractionPoint> attractionPoints) {
		aPoints.addAll(attractionPoints);
	}

	@Override
79
	public void addComponent(SimLocationActuator ms) {
80
		comps.add(ms);
81
82
		mappingHost(ms);

83
84
85
86
87
88
89
90
		// No assignments been done before.
		if (assignments.isEmpty()) {
			AttractionPoint aPoint = aPoints.iterator().next();
			assignments.put(ms, aPoint);
			mappingGroupId(ms, aPoint);
			setStartPosition(ms, aPoint.getRealPosition());
		}
		// GroupId is not mapped.
91
92
		else if (!mappingGroupIdAP.containsKey(
				mappingMSHost.get(ms).getProperties().getGroupID())) {
93
94
95
96
97
98
99
100
101
102
			for (AttractionPoint actAP : aPoints) {
				if (!mappingAPGroupId.containsKey(actAP)) {
					assignments.put(ms, actAP);
					mappingGroupId(ms, actAP);
					setStartPosition(ms, actAP.getRealPosition());
					break;
				}
			}
		}
		// GroupId is already mapped.
103
104
105
106
		else if (mappingGroupIdAP.containsKey(
				mappingMSHost.get(ms).getProperties().getGroupID())) {
			AttractionPoint aPoint = mappingGroupIdAP
					.get(mappingMSHost.get(ms).getProperties().getGroupID());
107
108
109
110
111
112
113
114
			assignments.put(ms, aPoint);
			setStartPosition(ms, aPoint.getRealPosition());
		} else {
			throw new Error("Should not happen.");
		}

	}

115
	private void mappingHost(SimLocationActuator ms) {
116
117
118
119
120
121
122
123
124
		SimHostComponent comp = (SimHostComponent) ms;
		SimHost host = comp.getHost();

		assert host != null;

		mappingHostMS.put(host, ms);
		mappingMSHost.put(ms, host);
	}

125
	private void mappingGroupId(SimLocationActuator ms, AttractionPoint AP) {
126
127
128
129
130
		SimHostComponent comp = (SimHostComponent) ms;
		SimHost host = comp.getHost();

		assert host != null;

131
132
133
134
		mappingAPGroupId.put(AP,
				mappingMSHost.get(ms).getProperties().getGroupID());
		mappingGroupIdAP.put(mappingMSHost.get(ms).getProperties().getGroupID(),
				AP);
135
136
	}

137
	private void setStartPosition(SimLocationActuator ms,
138
139
140
			PositionVector aPointReferencePosition) {
		double minJitter = 50.0;
		double maxJitter = 100.0;
141
142
143
144
145

		double xJitter = (rnd.nextDouble() * (maxJitter - minJitter))
				+ minJitter;
		double yJitter = (rnd.nextDouble() * (maxJitter - minJitter))
				+ minJitter;
146
		PositionVector jitterVector = new PositionVector(xJitter, yJitter);
147
148
		PositionVector newPos = aPointReferencePosition.plus(jitterVector);
		ms.updateCurrentLocation(newPos.getLongitude(), newPos.getLatitude());
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
	}

	/**
	 * Used by the MobilityModel (M1) of the {@link ModularMovementModel} to get
	 * the groupId of the affiliated nodes to that {@link AttractionPoint}. Once
	 * the groupId is known nodes can be set <b>offline</b> or <b>online</b>.
	 * 
	 * @param attractionPoint
	 * @return
	 */
	public String getGroupIdOfAttractionPoint(AttractionPoint attractionPoint) {
		return mappingAPGroupId.get(attractionPoint);
	}

}