DefaultChurnGenerator.java 6.18 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
32
33
34
35
36
37
/*
 * Copyright (c) 2005-2011 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.churn;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import de.tud.kom.p2psim.api.churn.ChurnGenerator;
import de.tud.kom.p2psim.api.churn.ChurnModel;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.network.SimNetworkComponent;
import de.tud.kom.p2psim.api.scenario.Configurator;
import de.tud.kom.p2psim.api.scenario.HostBuilder;
import de.tud.kom.p2psim.impl.util.toolkits.CollectionHelpers;
import de.tud.kom.p2psim.impl.util.toolkits.Predicates;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
38
39
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
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
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
133
134
135
136
137
138
139
140
141
import de.tudarmstadt.maki.simonstrator.api.Time;

/**
 * Provides a random number generator for times. do sth
 * 
 * @author Sebastian Kaune
 */
public class DefaultChurnGenerator implements EventHandler, ChurnGenerator {

	HostBuilder hostBuilder;

	ChurnModel churnModel;

	List<SimHost> hosts = null;

	long endTime = Long.MAX_VALUE;

	boolean testMode = false;

	public final static int CHURN_START = 1;

	public final static int CHURN_STOP = 2;

	public final static int CHURN_EVENT = 3;

	public final static int CHURN_NOCHURN_HOSTS = 4;

	public void setChurnModel(ChurnModel model) {
		this.churnModel = model;
	}

	public void setStart(long time) {
		Event.scheduleWithDelay(time - Time.getCurrentTime(), this, null,
				CHURN_START);
	}

	public void setStop(long time) {
		this.endTime = time;
		Event.scheduleWithDelay(time - Time.getCurrentTime(), this, null,
				CHURN_STOP);
	}

	public void compose(Configurator config) {
		hostBuilder = (HostBuilder) config
				.getConfigurable(Configurator.HOST_BUILDER);
	}

	@Override
	public void eventOccurred(Object content, int type) {
		if (type == CHURN_EVENT) {
			ChurnEvent churnEvent = (ChurnEvent) content;
			SimNetworkComponent net = churnEvent.host.getNetworkComponent();
			if (churnEvent.goOnline) {
				for (SimNetInterface netI : net.getSimNetworkInterfaces()) {
					if (netI.isOffline()) {
						netI.goOnline();
					}
				}
			} else {
				for (SimNetInterface netI : net.getSimNetworkInterfaces()) {
					if (netI.isOnline()) {
						netI.goOffline();
					}
				}
			}
			scheduleChurnEvent(churnEvent.host);
		} else if (type == CHURN_START) {
			this.hosts = new ArrayList<SimHost>(this.filterHosts());
			this.churnModel.prepare(this.hosts);
			this.activate();
		} else if (type == CHURN_STOP) {
			if (hosts == null)
				throw new RuntimeException(
						"Tried to stop churn at "
								+ Time.getCurrentTime()
								+ ", but it never started. Maybe you should correct your churn start and stop.");
			for (SimHost host : this.hosts) {
				for (SimNetInterface netI : host.getNetworkComponent()
						.getSimNetworkInterfaces()) {
					if (netI.isOffline()) {
						netI.goOnline();
					}
				}
			}
		} else if (type == CHURN_NOCHURN_HOSTS) {
			// Send no-churn-hosts online immediately
			List<SimHost> nochurnhosts = (List<SimHost>) content;
			for (SimHost host : nochurnhosts) {
				for (SimNetInterface netI : host.getNetworkComponent()
						.getSimNetworkInterfaces()) {
					if (netI.isOffline()) {
						netI.goOnline();
					}
				}
			}
		}
	}

	void activate() {
		for (SimHost host : hosts) {
			scheduleChurnEvent(host);
		}
142
143
		Monitor.log(DefaultChurnGenerator.class, Level.INFO,
				"Scheduled %s churn events", hosts.size());
144
145
146
147
148
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
220
221
	}

	private List<SimHost> filterHosts() {
		List<SimHost> tmp = hostBuilder.getAllHosts();
		List<SimHost> filteredHosts = new LinkedList<SimHost>();

		CollectionHelpers.filter(tmp, filteredHosts,
				Predicates.IS_CHURN_AFFECTED);
		List<SimHost> noChurn = new LinkedList<SimHost>();
		noChurn.addAll(tmp);
		noChurn.removeAll(filteredHosts);
		Event.scheduleImmediately(this, noChurn, CHURN_NOCHURN_HOSTS);
		return filteredHosts;
	}

	private void scheduleChurnEvent(SimHost host) {
		SimNetworkComponent net = host.getNetworkComponent();
		long offset;
		boolean goOnline;
		long currentTime = Time.getCurrentTime();

		/*
		 * Sanity check: currently, we assume within this churn generator, that
		 * all NetInterfaces of a host behave identical, i.e., they are all
		 * either online or offline.
		 */
		boolean oneOffline = false;
		boolean oneOnline = false;
		for (SimNetInterface netI : net.getSimNetworkInterfaces()) {
			if (netI.isOffline()) {
				oneOffline = true;
			} else {
				oneOnline = true;
			}
		}
		if (oneOffline && oneOnline) {
			throw new AssertionError(
					"Unexpectedly, one NetInterface of the host is offline, while another one is online...");
		}

		if (oneOnline) {
			offset = this.churnModel.getNextDowntime(host);
			goOnline = false;
			// if (testMode)
			// this.testStub.onlineEvent(host, currentTime);
		} else {
			offset = this.churnModel.getNextUptime(host);
			goOnline = true;
			// if (testMode)
			// this.testStub.offlineEvent(host, currentTime);
		}

		long timepoint = currentTime + offset;

		if (currentTime < endTime && timepoint < endTime) {
			Event.scheduleWithDelay(offset, this,
					new ChurnEvent(host, goOnline), CHURN_EVENT);
		}
	}

	private class ChurnEvent {

		public final SimHost host;

		public final boolean goOnline;

		ChurnEvent(SimHost host, boolean goOnline) {
			this.host = host;
			this.goOnline = goOnline;
		}
	}

	// void setTestStub(ChurnTestStub testStub) {
	// this.testStub = testStub;
	// this.testMode = true;
	// }

}