Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8d5d5d39c90528b0eb6c93c6f12676cb5019f6e8 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/****************************************************************************
 * Copyright (c) 2005, 2010 Jan S. Rellermeyer, Systems Group,
 * Department of Computer Science, ETH Zurich 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:
 *    Jan S. Rellermeyer - initial API and implementation
 *    Markus Alexander Kuppe - enhancements and bug fixes
 *
*****************************************************************************/
package ch.ethz.iks.slp.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Dictionary;
import java.util.List;
import java.util.Locale;
import ch.ethz.iks.slp.Advertiser;
import ch.ethz.iks.slp.ServiceLocationException;
import ch.ethz.iks.slp.ServiceURL;

/**
 * Implementation of the Advertiser that provides SLP SA functionality. If the
 * configuration does not have to support SA functionalities, this class does
 * not have to be included in the distribution.
 * 
 * @see ch.ethz.iks.slp.Advertiser
 * @author Jan S. Rellermeyer, Systems Group, ETH Zurich
 * @since 0.1
 */
public final class AdvertiserImpl implements Advertiser {

	/**
	 * the locale of this instance. Will be used for all messages created by
	 * this Advertiser instance.
	 */
	private Locale locale;

	/**
	 * Constructor for AdvertiserImpl.
	 */
	public AdvertiserImpl() {
		locale = SLPCore.DEFAULT_LOCALE;
	}
	
	/**
	 * Constructor for AdvertiserImpl.
	 * 
	 * @param theLocale
	 *            Locale.
	 */
	public AdvertiserImpl(final Locale locale) {
		this.locale = locale;
	}	

	/**
	 * Get the locale of this instance.
	 * 
	 * @return Locale.
	 * @see Advertiser#getLocale()
	 */
	public Locale getLocale() {
		return locale;
	}
	
	/**
	 * Set the locale of this instance.
	 * 
	 * @param locale
	 *            the Locale.
	 * @see Advertiser#setLocale()
	 */
	public void setLocale(final Locale locale) {
		this.locale = locale;
	}

	/**
	 * register a new service with the framework.
	 * 
	 * @param url
	 *            the ServiceURL of the service.
	 * @param attributes
	 *            a Dictionary of attributes.
	 * @throws ServiceLocationException
	 *             if the registration has failed for any reason.
	 * @see Advertiser#register(ServiceURL, Dictionary)
	 */
	public void register(final ServiceURL url, final Dictionary attributes)
			throws ServiceLocationException {
		register(url, null, attributes);
	}

	/**
	 * register a new service with the framework using scopes.
	 * 
	 * @param url
	 *            the ServiceURL of the service.
	 * @param scopes
	 *            a List of scopes.
	 * @param attributes
	 *            a Dictionary of attributes.
	 * @throws ServiceLocationException
	 *             if the registration has failed for any reason.
	 * @see Advertiser#register(ServiceURL, List, Dictionary)
	 */
	public void register(final ServiceURL url, final List scopes,
			final Dictionary attributes) throws ServiceLocationException {
		ServiceRegistration reg = new ServiceRegistration(url, url
				.getServiceType(), scopes, SLPUtils.dictToAttrList(attributes),
				locale);
		try {
			reg.address = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			reg.address = SLPCore.getMyIP();
		}
		reg.port = SLPCore.SLP_PORT;
		ServiceAcknowledgement ack = (ServiceAcknowledgement) SLPCore
				.sendMessage(reg, true);
		if (ack.errorCode != 0) {
			throw new ServiceLocationException((short) ack.errorCode,
					"Registration failed");
		}
	}

	/**
	 * deregister a service.
	 * 
	 * @param url
	 *            the ServiceURL of the service.
	 * @throws ServiceLocationException
	 *             if the deregistration has failed for any reason.
	 * @see Advertiser#deregister(ServiceURL)
	 */
	public void deregister(final ServiceURL url)
			throws ServiceLocationException {
		deregister(url, null);
	}

	/**
	 * deregister a service in some scopes.
	 * 
	 * @param url
	 *            the ServiceURL of the service.
	 * @param scopes
	 *            the scopes.
	 * @throws ServiceLocationException
	 *             if the deregistration has failed for any reason.
	 * @see Advertiser#deregister(ServiceURL, List)
	 * @since 0.7.1
	 */

	public void deregister(final ServiceURL url, final List scopes)
			throws ServiceLocationException {
		ServiceDeregistration dereg = new ServiceDeregistration(url, scopes,
				null, locale);
		try {
			dereg.address = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			dereg.address = SLPCore.getMyIP();
		}
		dereg.port = SLPCore.SLP_PORT;
		ServiceAcknowledgement ack = (ServiceAcknowledgement) SLPCore
				.sendMessage(dereg, true);
		if (ack.errorCode != 0) {
			throw new ServiceLocationException((short) ack.errorCode,
					"Deregistration failed");
		}
	}

	/**
	 * currently not supported.
	 * 
	 * @see Advertiser#addAttributes(ServiceURL, Dictionary)
	 * @param url
	 *            the serviceURL
	 * @param attributes
	 *            the attributes to add.
	 * @throws ServiceLocationException
	 *             always.
	 */
	public void addAttributes(final ServiceURL url, final Dictionary attributes)
			throws ServiceLocationException {
		throw new ServiceLocationException(
				ServiceLocationException.NOT_IMPLEMENTED,
				"incremental registration not supported");
	}

	/**
	 * currently not supported.
	 * 
	 * @see Advertiser#deleteAttributes(ServiceURL, Dictionary)
	 * @param url
	 *            the serviceURL.
	 * @param attributes
	 *            the attributes to delete.
	 * @throws ServiceLocationException
	 *             always.
	 */
	public void deleteAttributes(final ServiceURL url,
			final Dictionary attributes) throws ServiceLocationException {
		throw new ServiceLocationException(
				ServiceLocationException.NOT_IMPLEMENTED,
				"incremental registration not supported");
	}

	/**
	 * Get the IP address of this machine that is configured as primary jSLP
	 * address. Can be used to register Services that are located on this host.
	 * 
	 * @return the local InetAddress.
	 */
	public InetAddress getMyIP() {
		try {
			return SLPCore.getMyIP();
		} catch (Exception e) {
			return null;
		}
	}
}

Back to the top