Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f037d4b4133b05f902bda2d0f622cfc367ee9d4 (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
/*******************************************************************************
 * Copyright (c) 2014, 2015 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 2.0 which accompanies this distribution, and is
 * available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.tcf.te.tcf.core.internal.channelmanager.steps;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.runtime.stepper.StepperAttributeUtil;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext;
import org.eclipse.tcf.te.runtime.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.core.channelmanager.OpenChannelException;
import org.eclipse.tcf.te.tcf.core.events.ChannelEvent;
import org.eclipse.tcf.te.tcf.core.interfaces.steps.ITcfStepAttributes;
import org.eclipse.tcf.te.tcf.core.steps.AbstractPeerStep;

/**
 * ChainPeerStep
 */
public class ChainPeerStep extends AbstractPeerStep {

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.interfaces.IStep#validateExecute(org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void validateExecute(IStepContext context, IPropertiesContainer data, IFullQualifiedId fullQualifiedId, IProgressMonitor monitor) throws CoreException {
		Assert.isNotNull(context);
		Assert.isNotNull(data);
		Assert.isNotNull(fullQualifiedId);
		Assert.isNotNull(monitor);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.interfaces.IStep#execute(org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId, org.eclipse.core.runtime.IProgressMonitor, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	public void execute(IStepContext context, final IPropertiesContainer data, final IFullQualifiedId fullQualifiedId, IProgressMonitor monitor, final ICallback callback) {
		Assert.isNotNull(context);
		Assert.isNotNull(data);
		Assert.isNotNull(fullQualifiedId);
		Assert.isNotNull(monitor);
		Assert.isNotNull(callback);

		final AtomicReference<IChannel> channel = new AtomicReference<IChannel>((IChannel)StepperAttributeUtil.getProperty(ITcfStepAttributes.ATTR_CHANNEL, fullQualifiedId, data));
		final IPeer peer = getActivePeerContext(context, data, fullQualifiedId);
		final String logname = StepperAttributeUtil.getStringProperty(ITcfStepAttributes.ATTR_LOG_NAME, fullQualifiedId, data);

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				IChannel c = channel.get();

				// If the channel is not yet opened, open it now.
				// Otherwise redirect the channel to the next peer.
				if (c == null) {
					c = peer.openChannel();
					channel.set(c);

					String message = "to " + peer.getID(); //$NON-NLS-1$

					IPropertiesContainer eventData = new PropertiesContainer();
					eventData.setProperty(ChannelEvent.PROP_MESSAGE, message);
					eventData.setProperty(ChannelEvent.PROP_LOG_NAME, logname);

					ChannelEvent event = new ChannelEvent(ChainPeerStep.this, c, ChannelEvent.TYPE_OPENING, eventData);
					EventManager.getInstance().fireEvent(event);
				} else {
					String id = peer.getAttributes().get(IPeer.ATTR_TRANSPORT_NAME) + ":" + peer.getAttributes().get(IPeer.ATTR_IP_HOST) + ":" + peer.getAttributes().get(IPeer.ATTR_IP_PORT); //$NON-NLS-1$ //$NON-NLS-2$
					id = id.equalsIgnoreCase(peer.getID()) ? peer.getID() : (id + " (" + peer.getID() + ")");  //$NON-NLS-1$ //$NON-NLS-2$
					String message = c.getRemotePeer().getID() + " --> " + id; //$NON-NLS-1$

					IPropertiesContainer eventData = new PropertiesContainer();
					eventData.setProperty(ChannelEvent.PROP_MESSAGE, message);
					eventData.setProperty(ChannelEvent.PROP_LOG_NAME, logname);

					ChannelEvent event = new ChannelEvent(ChainPeerStep.this, c, ChannelEvent.TYPE_REDIRECT, eventData);
					EventManager.getInstance().fireEvent(event);

					c.redirect(peer.getAttributes());
				}

				// At this point, channel must not be null and
				// channel.get and c must be the same
				Assert.isNotNull(c);
				Assert.isTrue(c.equals(channel.get()));

				c.addChannelListener(new IChannel.IChannelListener() {
					@Override
					public void onChannelOpened() {
						channel.get().removeChannelListener(this);
						StepperAttributeUtil.setProperty(ITcfStepAttributes.ATTR_CHANNEL, fullQualifiedId, data, channel.get(), true);
						callback(data, fullQualifiedId, callback, Status.OK_STATUS, null);
					}

					@Override
					public void onChannelClosed(Throwable error) {
						// Clean Logs when closed channels
						ChannelEvent event = new ChannelEvent(ChainPeerStep.this, channel.get(), ChannelEvent.TYPE_CLOSE, null);
						EventManager.getInstance().fireEvent(event);
                    
						// Remove ourself as listener from the channel
						channel.get().removeChannelListener(this);
						// The error is repackaged
						if (error != null) {
							error = new OpenChannelException(error);
						}
						// Invoke the callback
						callback(data, fullQualifiedId, callback, StatusHelper.getStatus(error), null);
					}

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

		if (Protocol.isDispatchThread()) runnable.run();
		else Protocol.invokeLater(getProxyDelay(), runnable);
	}
	
	private long getProxyDelay() {
		int tcfProxyDelay = 1000;
		String tcfRedirectDelayString = System.getProperty("tcfProxyDelay");
		if ( null != tcfRedirectDelayString ) {
			try {
				tcfProxyDelay = Integer.parseInt(tcfRedirectDelayString);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (tcfProxyDelay < 10) {
			tcfProxyDelay = 1000;
		}
				
		return tcfProxyDelay;
	}


	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.steps.AbstractStep#rollback(org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.core.runtime.IStatus, org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId, org.eclipse.core.runtime.IProgressMonitor, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	public void rollback(final IStepContext context, final IPropertiesContainer data, final IStatus status, final IFullQualifiedId fullQualifiedId, final IProgressMonitor monitor, final ICallback callback) {
		final IChannel channel = (IChannel)StepperAttributeUtil.getProperty(ITcfStepAttributes.ATTR_CHANNEL, fullQualifiedId, data);

		if (channel != null && channel.getState() != IChannel.STATE_CLOSED) {
			Runnable runnable = new Runnable() {
				@SuppressWarnings("synthetic-access")
                @Override
				public void run() {
					channel.close();
					ChainPeerStep.super.rollback(context, data, status, fullQualifiedId, monitor, callback);
				}
			};

			if (Protocol.isDispatchThread()) runnable.run();
			else Protocol.invokeLater(runnable);
		} else {
			super.rollback(context, data, status, fullQualifiedId, monitor, callback);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.steps.AbstractStep#getCancelTimeout()
	 */
	@Override
	public int getCancelTimeout() {
	    return 500;
	}
}

Back to the top