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
|
/****************************************************************************
* 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.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
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.IServiceID;
import org.eclipse.ecf.discovery.identity.ServiceIDFactory;
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;
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
container = ContainerFactory.getDefault().createContainer("ecf.discovery.jmdns");
container.connect(null, null);
discoveryContainer = (IDiscoveryContainerAdapter) container.getAdapter(IDiscoveryContainerAdapter.class);
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
discoveryContainer = null;
container.disconnect();
container.dispose();
container = null;
}
public void testAddServiceTypeListener() throws Exception {
discoveryContainer.addServiceTypeListener(new CollabServiceTypeListener());
}
public void testRegisterServiceType() throws Exception {
final IServiceID serviceID = ServiceIDFactory.getDefault().createServiceID(discoveryContainer.getServicesNamespace(), TEST_SERVICE_TYPE);
discoveryContainer.registerServiceType(serviceID.getServiceTypeID());
System.out.println("registered service type " + TEST_SERVICE_TYPE + " waiting 5s");
Thread.sleep(5000);
}
public void testRegisterService() throws Exception {
final Properties props = new Properties();
final String protocol = TEST_PROTOCOL;
final InetAddress host = InetAddress.getByName(TEST_HOST);
final int port = TEST_PORT;
final String svcName = System.getProperty("user.name") + "." + protocol;
// XXX change this to create IServiceID in appropriate way
final Namespace ns = IDFactory.getDefault().getNamespaceByName("ecf.namespace.jmdns");
final IServiceID serviceID = (IServiceID) IDFactory.getDefault().createID(ns, new Object[] {TEST_SERVICE_TYPE, svcName});
final ServiceInfo svcInfo = new ServiceInfo(host, serviceID, 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 + ")");
final IServiceID svcID = event.getServiceInfo().getServiceID();
discoveryContainer.addServiceListener(svcID.getServiceTypeID(), new CollabServiceListener());
discoveryContainer.registerServiceType(svcID.getServiceTypeID());
}
}
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 + ")");
}
}
}
|