Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 13e00b2b55f1bbeb1b874eab75dd97d263601517 (plain) (blame)
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
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
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
142
143
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.core.internal.channelmanager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IStreams;
import org.eclipse.tcf.te.runtime.interfaces.IDisposable;
import org.eclipse.tcf.te.tcf.core.activator.CoreBundleActivator;
import org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager;
import org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager.IStreamsListener;
import org.eclipse.tcf.te.tcf.core.interfaces.tracing.ITraceIds;

/**
 * Channel manager stream listener proxy implementation.
 */
final class StreamListenerProxy implements IStreams.StreamsListener, IChannelManager.IStreamsListenerProxy {
	// The channel
	private final IChannel channel;
	// The stream type the proxy is registered for
	private final String streamType;
	// The list of proxied stream listeners
	/* default */ ListenerList listeners = new ListenerList();
	// The list of delayed stream created events
	private final List<StreamListenerProxy.StreamCreatedEvent> delayedCreatedEvents = new ArrayList<StreamListenerProxy.StreamCreatedEvent>();

	/**
	 * Immutable stream created event.
	 */
	private final static class StreamCreatedEvent {
		/**
		 * The stream type.
		 */
		public final String streamType;
		/**
		 * The stream id.
		 */
		public final String streamId;
		/**
		 * The context id.
		 */
		public final String contextId;

		// As the class is immutable, we do not need to build the toString
		// value again and again. Build it once in the constructor and reuse it later.
		private final String toString;

		/**
		 * Constructor.
		 *
		 * @param streamType The stream type.
		 * @param streamId The stream id.
		 * @param contextId The context id.
		 */
		public StreamCreatedEvent(String streamType, String streamId, String contextId) {
			this.streamType = streamType;
			this.streamId = streamId;
			this.contextId = contextId;

			toString = toString();
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			return obj instanceof StreamListenerProxy.StreamCreatedEvent
					&& toString().equals(((StreamListenerProxy.StreamCreatedEvent)obj).toString());
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			return toString().hashCode();
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			if (toString != null) return toString;

			StringBuilder builder = new StringBuilder(getClass().getSimpleName());
			builder.append(": streamType = "); //$NON-NLS-1$
			builder.append(streamType);
			builder.append("; streamId = "); //$NON-NLS-1$
			builder.append(streamId);
			builder.append("; contextId = "); //$NON-NLS-1$
			builder.append(contextId);

			return builder.toString();
		}
	}

	/**
     * Constructor
     *
     * @param The channel. Must not be <code>null</code>.
     */
    public StreamListenerProxy(final IChannel channel, final String streamType) {
    	Assert.isNotNull(channel);
    	Assert.isNotNull(streamType);

    	this.channel = channel;
    	this.channel.addChannelListener(new IChannel.IChannelListener() {
			@Override
			public void onChannelOpened() {}

			@Override
			public void onChannelClosed(Throwable error) {
				// Channel is closed, remove ourself
				channel.removeChannelListener(this);
				// Dispose all registered streams listener
				Object[] candidates = listeners.getListeners();
				listeners.clear();
				for (Object listener : candidates) {
					if (listener instanceof IDisposable) {
						((IDisposable)listener).dispose();
					}
				}
			}

			@Override
			public void congestionLevel(int level) {
			}
		});

    	// Remember the stream type
    	this.streamType = streamType;
    }

    /**
     * Returns the stream type the proxy is registered for.
     *
     * @return The stream type.
     */
    public String getStreamType() {
    	return streamType;
    }

    /**
     * Adds the given streams listener to the list of proxied listeners.
     *
     * @param listener The streams listener. Must not be <code>null</code>.
     */
    public void addListener(IStreamsListener listener) {
    	Assert.isNotNull(listener);
    	listener.setProxy(this);
    	listeners.add(listener);
    }

    /**
     * Removes the given streams listener from the list of proxied listeners.
     *
     * @param listener The streams listener. Must not be <code>null</code>.
     */
    public void removeListener(IStreamsListener listener) {
    	Assert.isNotNull(listener);
    	listener.setProxy(null);
    	listeners.remove(listener);
    }

    /**
     * Returns if the proxied listeners list is empty or not.
     *
     * @return <code>True</code> if the list is empty, <code>false</code> otherwise.
     */
    public boolean isEmpty() {
    	return listeners.isEmpty();
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager.IStreamsListenerProxy#processDelayedCreatedEvents()
     */
    @Override
    public void processDelayedCreatedEvents() {
		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY)) {
			CoreBundleActivator.getTraceHandler().trace("StreamListenerProxy: processDelayedCreatedEvents()", //$NON-NLS-1$
			                                            0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY,
			                                            IStatus.INFO, getClass());
		}

		synchronized (delayedCreatedEvents) {
			// Make a snapshot of all delayed created events
			StreamListenerProxy.StreamCreatedEvent[] events = delayedCreatedEvents.toArray(new StreamListenerProxy.StreamCreatedEvent[delayedCreatedEvents.size()]);
			// Clear the events now, it will be refilled by calling the created method
			delayedCreatedEvents.clear();
			// Loop the delayed created events and recall the created method to process them
			for (StreamListenerProxy.StreamCreatedEvent event : events) {
				created(event.streamType, event.streamId, event.contextId);
			}
		}
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.services.IStreams.StreamsListener#created(java.lang.String, java.lang.String, java.lang.String)
	 */
    @Override
    public void created(String stream_type, String stream_id, String context_id) {
    	Assert.isNotNull(stream_type);
    	Assert.isNotNull(stream_id);

		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY)) {
			CoreBundleActivator.getTraceHandler().trace("StreamListenerProxy: created(" + stream_type + ", " + stream_id + ", " + context_id + ")", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			                                            0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY,
			                                            IStatus.INFO, getClass());
		}

		// If the context_id is null, disconnect from the stream right away. We do not support
		// old TCF agents not sending the context id in the created event.
		if (context_id == null) {
			IStreams service = channel.getRemoteService(IStreams.class);
			if (service != null) {
				service.disconnect(stream_id, new IStreams.DoneDisconnect() {
					@Override
					public void doneDisconnect(IToken token, Exception error) {
						if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY)) {
							CoreBundleActivator.getTraceHandler().trace("StreamListenerProxy: disconnect -> context id must be not null.", //$NON-NLS-1$
																		0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY, IStatus.INFO, getClass());
						}
					}
				});
			}

			return;
		}

		boolean delayed = false;
		boolean disconnect = true;

		// Loop all listeners
    	for (Object l : listeners.getListeners()) {
    		IStreamsListener listener = (IStreamsListener)l;

    		// If the listener has no context set yet, the listener cannot decide if
    		// the event is consumed or not. In this case, the event must be delayed.
    		if (!listener.hasContext()) {
    			delayed |= true;
    			continue;
    		}

    		// Does the listener consume the event?
    		boolean consume = listener.isCreatedConsumed(stream_type, stream_id, context_id);
    		if (consume) listener.created(stream_type, stream_id, context_id);
    		// If the created event is consumed by one listener, it cannot be disconnected anymore
    		disconnect &= !consume;
    	}

    	if (delayed) {
			// Context not set yet --> add to the delayed list
			StreamListenerProxy.StreamCreatedEvent event = new StreamCreatedEvent(stream_type, stream_id, context_id);
			synchronized (delayedCreatedEvents) {
				if (!delayedCreatedEvents.contains(event)) {
					delayedCreatedEvents.add(event);

					if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY)) {
						CoreBundleActivator.getTraceHandler().trace("StreamListenerProxy: delayed -> at least one listener does not have a context set", //$NON-NLS-1$
																	0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY, IStatus.INFO, getClass());
					}
				}
			}
			return;
    	}

    	if (disconnect) {
			IStreams service = channel.getRemoteService(IStreams.class);
			if (service != null) {
				service.disconnect(stream_id, new IStreams.DoneDisconnect() {
					@Override
					public void doneDisconnect(IToken token, Exception error) {
						if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY)) {
							CoreBundleActivator.getTraceHandler().trace("StreamListenerProxy: disconnect -> not interested in context id", //$NON-NLS-1$
																		0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY, IStatus.INFO, getClass());
						}
					}
				});
			}
    	}
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.services.IStreams.StreamsListener#disposed(java.lang.String, java.lang.String)
	 */
    @Override
    public void disposed(String stream_type, String stream_id) {
    	Assert.isNotNull(stream_type);
    	Assert.isNotNull(stream_id);

    	if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY)) {
			CoreBundleActivator.getTraceHandler().trace("StreamListenerProxy: disposed(" + stream_type + ", " + stream_id + ")", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			                                            0, ITraceIds.TRACE_STREAMS_LISTENER_PROXY,
			                                            IStatus.INFO, getClass());
		}

		// If the delayed created events list is not empty, we have
		// to check if one of the delayed create events got disposed
		synchronized (delayedCreatedEvents) {
			Iterator<StreamListenerProxy.StreamCreatedEvent> iterator = delayedCreatedEvents.iterator();
			while (iterator.hasNext()) {
				StreamListenerProxy.StreamCreatedEvent event = iterator.next();
				if (stream_type.equals(event.streamType) && stream_id.equals(event.streamId)) {
					// Remove the create event from the list
					iterator.remove();
				}
			}
		}

    	for (Object l : listeners.getListeners()) {
    		((IStreamsListener)l).disposed(stream_type, stream_id);
    	}
    }
}

Back to the top