Skip to main content
summaryrefslogtreecommitdiffstats
blob: cf206e2ea6b60c08b89cefc916fa503e72a92cdb (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
/****************************************************************************
* Copyright (c) 2004 Composent, 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:
*    Composent, Inc. - initial API and implementation
*****************************************************************************/

package org.eclipse.ecf.tests.discovery;

import java.net.InetAddress;
import java.util.Properties;

import junit.framework.TestCase;

import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.discovery.IDiscoveryContainerAdapter;
import org.eclipse.ecf.discovery.IServiceEvent;
import org.eclipse.ecf.discovery.IServiceListener;
import org.eclipse.ecf.discovery.IServiceTypeListener;
import org.eclipse.ecf.discovery.ServiceInfo;
import org.eclipse.ecf.discovery.ServiceProperties;
import org.eclipse.ecf.discovery.identity.ServiceID;

public class DiscoveryTest extends TestCase {
	
	static IContainer container = null;
	static IDiscoveryContainerAdapter discoveryContainer = null;
	static final String TEST_SERVICE_TYPE = "_ecftcp._tcp.local.";
	static final String TEST_PROTOCOL = "ecftcp";
	static final String TEST_HOST = "localhost";
	static final int TEST_PORT = 3282;
	static final String TEST_SERVICE_NAME = System.getProperty("user.name") + "." + TEST_PROTOCOL;
	
	public void testContainerCreate() throws Exception {
		container = ContainerFactory.getDefault().createContainer(
		"ecf.discovery.jmdns");
		assertNotNull(container);
	}
	
	public void testContainerConnect() throws Exception {
		container.connect(null, null);		
	}
	public void testDiscoveryContainerAdapter() throws Exception {
		discoveryContainer = (IDiscoveryContainerAdapter) container
		.getAdapter(IDiscoveryContainerAdapter.class);		
		assertNotNull(discoveryContainer);
	}
	
	public void testAddServiceTypeListener() throws Exception {
		discoveryContainer
		.addServiceTypeListener(new CollabServiceTypeListener());
	}
	
	public void testRegisterServiceType() throws Exception {
		discoveryContainer.registerServiceType(TEST_SERVICE_TYPE);
		System.out.println("registered service type "+TEST_SERVICE_TYPE+" waiting 5s");
		Thread.sleep(5000);
	}
	
	public void testRegisterService() throws Exception {
		Properties props = new Properties();
		String protocol = TEST_PROTOCOL;
		InetAddress host = InetAddress.getByName(TEST_HOST);
		int port = TEST_PORT;
		String svcName = System.getProperty("user.name") + "."
				+ protocol;
		ServiceInfo svcInfo = new ServiceInfo(host, new ServiceID(
				TEST_SERVICE_TYPE, svcName), port,
				0, 0, new ServiceProperties(props));
		discoveryContainer.registerService(svcInfo);
	}
	public final void testDiscovery() throws Exception {
		
		System.out.println("Discovery started.  Waiting 10s for discovered services");
		Thread.sleep(10000);
	}

	class CollabServiceTypeListener implements IServiceTypeListener {
		public void serviceTypeAdded(IServiceEvent event) {
			System.out.println("serviceTypeAdded(" + event + ")");
			ServiceID svcID = event.getServiceInfo().getServiceID();
			discoveryContainer.addServiceListener(svcID.getServiceType(),
					new CollabServiceListener());
			discoveryContainer.registerServiceType(svcID.getServiceType());
		}
	}
	class CollabServiceListener implements IServiceListener {
		public void serviceAdded(IServiceEvent event) {
			System.out.println("serviceAdded(" + event + ")");
			discoveryContainer.requestServiceInfo(event.getServiceInfo()
					.getServiceID(), 3000);
		}
		public void serviceRemoved(IServiceEvent event) {
			System.out.println("serviceRemoved(" + event + ")");
		}
		public void serviceResolved(IServiceEvent event) {
			System.out.println("serviceResolved(" + event + ")");
		}
	}

}

Back to the top