Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4c6b9580168239a79f63b2d970b13b9d66e2600b (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
/*******************************************************************************
 * Copyright (c) 2011 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:
 * Uwe Stieber (Wind River) - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.te.tcf.locator.services;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.eclipse.tm.tcf.protocol.IPeer;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.ILocator;
import org.eclipse.tm.te.tcf.core.Tcf;
import org.eclipse.tm.te.tcf.locator.ScannerRunnable;
import org.eclipse.tm.te.tcf.locator.interfaces.nodes.ILocatorModel;
import org.eclipse.tm.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tm.te.tcf.locator.interfaces.services.ILocatorModelLookupService;
import org.eclipse.tm.te.tcf.locator.interfaces.services.ILocatorModelRefreshService;
import org.eclipse.tm.te.tcf.locator.interfaces.services.ILocatorModelUpdateService;
import org.eclipse.tm.te.tcf.locator.nodes.LocatorModel;
import org.eclipse.tm.te.tcf.locator.nodes.PeerModel;


/**
 * Default locator model refresh service implementation.
 */
public class LocatorModelRefreshService extends AbstractLocatorModelService implements ILocatorModelRefreshService {

	/**
	 * Constructor.
	 *
	 * @param parentModel The parent locator model instance. Must not be <code>null</code>.
	 */
	public LocatorModelRefreshService(ILocatorModel parentModel) {
		super(parentModel);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.tcf.locator.core.interfaces.services.ILocatorModelRefreshService#refresh()
	 */
	public void refresh() {
		assert Protocol.isDispatchThread();

		// Get the parent locator model
		ILocatorModel model = getLocatorModel();

		// If the parent model is already disposed, the service will drop out immediately
		if (model.isDisposed()) return;

		// If the TCF framework isn't initialized yet, the service will drop out immediately
		if (!Tcf.isRunning()) return;

		// Get the list of old children (update node instances where possible)
		final List<IPeerModel> oldChildren = new ArrayList<IPeerModel>(Arrays.asList(model.getPeers()));

		// Get the locator service
		ILocator locatorService = Protocol.getLocator();
		if (locatorService != null) {
			// Check for the locator listener to be created and registered
			if (model instanceof LocatorModel) ((LocatorModel)model).checkLocatorListener();
			// Get the map of peers known to the locator service.
			Map<String, IPeer> peers = locatorService.getPeers();
			for (String peerId : peers.keySet()) {
				// Get the peer instance for the current peer id
				IPeer peer = peers.get(peerId);
				// Try to find an existing peer node first
				IPeerModel peerNode = model.getService(ILocatorModelLookupService.class).lkupPeerModelById(peerId);
				// And create a new one if we cannot find it
				if (peerNode == null) peerNode = new PeerModel(model, peer);
				else oldChildren.remove(peerNode);
				// Validate the peer node before adding
				if (peerNode != null) peerNode = model.validatePeerNodeForAdd(peerNode);
				if (peerNode != null) {
					// Add the peer node to model
					model.getService(ILocatorModelUpdateService.class).add(peerNode);
					// And schedule for immediate status update
					Runnable runnable = new ScannerRunnable(model.getScanner(), peerNode);
					Protocol.invokeLater(runnable);
				}
			}
		}

		// If there are remaining old children, remove them from the model (non-recursive)
		for (IPeerModel oldChild : oldChildren) model.getService(ILocatorModelUpdateService.class).remove(oldChild);

		// Create and fire the notification event if non null
//		if (dirty) {
//			IWRNotificationEvent event = model.getFactory().newPropertyChangeEvent(model, IContainerModelNode.PROPERTY_CHANGED, null, null);
//			if (event != null) WRNotificationManager.getInstance().fireEvent(event);
//		}
	}

}

Back to the top