Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 14f2887e0738e15d7d27b454f0c887adb34bda8c (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 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.processes.core.model.runtime.services;


import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IProcesses;
import org.eclipse.tcf.services.IRunControl;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager;
import org.eclipse.tcf.te.tcf.core.model.interfaces.services.IModelChannelService;
import org.eclipse.tcf.te.tcf.core.model.services.AbstractModelService;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.processes.core.model.interfaces.runtime.IRuntimeModel;
import org.eclipse.tcf.te.tcf.processes.core.model.runtime.listener.RuntimeModelProcessServiceListener;
import org.eclipse.tcf.te.tcf.processes.core.model.runtime.listener.RuntimeModelRunControlServiceListener;

/**
 * Runtime model channel service implementation.
 */
public class RuntimeModelChannelService extends AbstractModelService<IRuntimeModel> implements IModelChannelService {
	// Reference to the channel instance
	/* default */ IChannel channel;
	// Reference to the process service listener
	/* default */ IProcesses.ProcessesListener serviceListener;
	// Reference to the run control service listener
	/* default */ IRunControl.RunControlListener runControlListener;

	/**
	 * Constructor.
	 *
	 * @param model The parent model. Must not be <code>null</code>.
	 */
	public RuntimeModelChannelService(IRuntimeModel model) {
		super(model);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.core.model.interfaces.services.IModelChannelService#getChannel()
	 */
	@Override
	public IChannel getChannel() {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
		return channel;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.core.model.interfaces.services.IModelChannelService#openChannel(org.eclipse.tcf.te.tcf.core.model.interfaces.services.IModelChannelService.DoneOpenChannel)
	 */
	@Override
	public void openChannel(final DoneOpenChannel done) {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		// If a channel is associated which is in open state, return it
		if (channel != null && channel.getState() == IChannel.STATE_OPEN) {
			done.doneOpenChannel(null, channel);
			return;
		}

		// Get the peer model node
		IPeerNode node = getModel().getPeerModel();
		if (node != null) {
			// Open a new channel to the remote peer
			Tcf.getChannelManager().openChannel(node.getPeer(), null, new IChannelManager.DoneOpenChannel() {

				@Override
				public void doneOpenChannel(Throwable error, IChannel channel) {
					// Remember the channel instance
					RuntimeModelChannelService.this.channel = channel;
					// Attach the process service listener instance
					if (error == null && serviceListener == null) {
						IProcesses service = channel.getRemoteService(IProcesses.class);
						if (service != null) {
							serviceListener = new RuntimeModelProcessServiceListener(getModel());
							service.addListener(serviceListener);
						}
					}
					// Attach the run control service listener instance
					if (error == null && runControlListener == null) {
						IRunControl service = channel.getRemoteService(IRunControl.class);
						if (service != null) {
							runControlListener = new RuntimeModelRunControlServiceListener(getModel());
							service.addListener(runControlListener);
						}
					}

					done.doneOpenChannel(error, channel);
				}
			});
		}
		else {
			done.doneOpenChannel(new NullPointerException("Peer model node is null"), null); //$NON-NLS-1$
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.core.model.interfaces.services.IModelChannelService#closeChannel()
	 */
	@Override
	public void closeChannel() {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
		if (channel != null) {
			if (serviceListener != null) {
				IProcesses service = channel.getRemoteService(IProcesses.class);
				if (service != null) { service.removeListener(serviceListener); serviceListener = null; }
			}

			Tcf.getChannelManager().closeChannel(channel);
			channel = null;
		}
	}
}

Back to the top